No need for assembly code.
You want a PWM period of 1/38000 = 26.315us. Assuming 20MHz operating frequency your instruction cycle time is 200ns so you want a PWM period of 131-132 instruction cycles. Then you simply follow the instructions in the datasheet:
8.4.3 SETUP FOR PWM OPERATION
The following steps should be taken when configuring
the CCP module for PWM operation:
1. Set the PWM period by writing to the PR2 register.
2. Set the PWM duty cycle by writing to the CCPR1L register and CCP1CON<5:4> bits.
3. Make the CCP1 pin an output by clearing the TRISC<2> bit.
4. Set the TMR2 prescale value and enable Timer2 by writing to T2CON.
5. Configure the CCP1 module for PWM operation.
Code:
PR2 = 130 ' 1) PWM period of 131 instruction cycles assuming 1:1 prescaler
CCPR1L = 65 ' 2) Dutycycle of ~50%, no need to write 2 LSB for this.
TRISC.2 = 0 ' 3) Pin is output
T2CON = %00000100 ' 4) 1:1 Prescaler, TMR2 ON
CCP1CON = %00001100 ' 5) PWM mode enabled
This sets up the carrier and enables it. To disable the carrier:Enable it again:I'm not 100% sure what the note in the datasheet says
Note: Clearing the CCP1CON register will force
the CCP1 PWM output latch to the default
low level. This is not the PORTC I/O data
latch.
It either says that as long as you set PortC.2 to 0 the output pin will revert to 0 as soon as the CCP module is disabled (which is good in this case) OR it says that actual output pin is left in the state it was at the time the CCP module was disabled which means that it can be high or low and that you need to force it back down by doing PortC.2 = 0 after turning off the CCP module.
(Untested, please verify against datasheet)
/Henrik.
Bookmarks