If you're using HPWM you don't need to mess with Timer2. It's automatically handled
for you.
If you plan to setup hardware PWM manually, these calcs should help.
Search here for HPWM and you'll find a boat-load more examples.Code:PR2=(Fosc/(4*PS*Freq))-1 TMR2 Prescaler = 1 PR2 = (8MHz/(4*1*12,000)) = (8MHz/48,000)-1 PR2 = 165.666 TMR2 Prescaler = 4 PR2 = (8MHz/(4*4*12,000)) = (8MHz/192,000)-1 PR2 = 40.666 TMR2 Prescaler = 16 PR2 = (8MHz/(4*16*12,000))-1 = (8MHz/768,000))-1 PR2 = 9.416 CCPR1L:CCP1CON<5:4>=(PR2+1)*4*Duty% (165+1)*4*0.5 = 332 (40+1)*4*0.5 = 82 (9+1)*4*0.5 = 20 To load the duty cycle registers just place the value calculated in a word, and do something like this; Duty = 332 ' Roughly 50% duty cycle CCP1CON.4 = Duty.0 ' Setup 10-bit duty cycle as CCP1CON.5 = Duty.1 ' a 10-bit word CCPR1L = Duty >> 2




Bookmarks