Datasheet

© 2008 Microchip Technology Inc. DS41249E-page 99
PIC16F785/HV785
13.8 PWM Configuration
When configuring the Two-Phase PWM, care must be
taken to avoid active output levels from the PH1 and
PH2 pins before the PWM is fully configured. The
following sequence is suggested before the TRISC
register or any of the Two-Phase PWM control registers
are first configured:
Output inactive (OFF) levels to the PORTC RC1/
AN5/C12IN1-/PH1 and RC4/C2OUT/PH2 pins.
Clear TRISC bits 1 and 4 to configure the PH1
and PH2 pins as outputs.
Configure the PWMCLK, PWMPH1, PWMPH2,
and PWMCON1 registers.
Configure the PWMCON0 register.
EXAMPLE 13-1: PWM SETUP EXAMPLE
;Example to configure PH1 as a free running PWM output using the SYNC output as the duty cycle
;termination feedback.
;This requires an external connection between the SYNC output and the comparator input.
;SYNC out = RB7 on pin 10
;C1 inverting input = RC2/AN6 on pin 14
;Configure PH1, PH2 and SYNC pins as outputs
;First, ensure output latches are low
BCF PORTC,1 ;PH1 low
BCF PORTC,4 ;PH2 low
BCF PORTB,7 ;SYNC low
;Configure the I/Os as outputs
BANKSEL TRISB
BCF TRISC,1 ;PH1 output
BCF TRISC,4 ;PH2 output
BCF TRISB,7 ;SYNC output
;PH1 shares its function with AN5
;Configure AN5 as digital I/O
BCF ANSEL0,5 ;AN5 is digital, all others default as analog
;Configure the PWM but don't enable PH1 or PH2 yet
BANKSEL PWMCLK
;PWM control setup
MOVLW B'00001100' ;auto shutdown off, no blanking, SYNC on, PH1 and PH2 off
MOVWF PWMCON0 ;see data sheet page 93
;PWM clock setup
MOVLW B'00111101' ;pwm_clk = Fosc, 30 clocks in PWM period
MOVWF PWMCLK ;see data sheet page 94
;PH1 setup
MOVLW B'00101111' ;non-inverted, terminate on C1, Start on clock 15
MOVWF PWMPH1 ;see data sheet page 95
;PH2 setup
MOVLW B'00110101' ;non-inverted, terminate on C1, Start on clock 21
MOVWF PWMPH2 ;see data sheet page 96
;Configure Comparator 1
MOVLW B'10011110' ;C1 on, internal, inverted, normal speed, +:C1V
REF, -:AN6
MOVWF CM1CON0 ;see data sheet page 68
;Configure comparator voltage reference
BANKSEL VRCON
MOVLW B'10101100' ;C1V
REN on, low range, CVREF= VDD/2
MOVWF VRCON ;see data sheet page 72
;Everything is setup at this point so now it is time to enable PH1
BANKSEL PWMCON0
BSF PWMCON0,PH1EN ;enable PH1
;Module is running autonomously at this point