I'm using PIC16F886 and there is one thing I don't quite understand in setting hardware PWM up. In datasheet there are detailed instructions on how to do this, but maybe somebody can explain why I first need to disable the PWM pin output driver. Then after everything is set up, it should be enabled after an interrupt
Here are the steps from the datasheet:
In my understanding, Timer2 even shouldn't overflow because it's value is constantly compared to PR2 register whose value in most cases will be smaller than maximum value of Timer2. And as soon as PR2 value is reached, TMR2 is cleared so no overflow.
Here are the steps from the datasheet:
- Disable the PWM pin (CCPx) output drivers as an input by setting the associated TRIS bit.
- Set the PWM period by loading the PR2 register.
- Configure the CCP module for the PWM mode by loading the CCPxCON register with the appropriate values.
- Set the PWM duty cycle by loading the CCPRxL register and DCxB<1:0> bits of the CCPxCON register.
- Configure and start Timer2:
- Clear the TMR2IF interrupt flag bit of the PIR1 register.
- Set the Timer2 prescale value by loading the T2CKPS bits of the T2CON register.
- Enable Timer2 by setting the TMR2ON bit of the T2CON register.
- Enable PWM output after a new PWM cycle has started:
- Wait until Timer2 overflows (TMR2IF bit of the PIR1 register is set).
- Enable the CCPx pin output driver by clearing the associated TRIS bit.
In my understanding, Timer2 even shouldn't overflow because it's value is constantly compared to PR2 register whose value in most cases will be smaller than maximum value of Timer2. And as soon as PR2 value is reached, TMR2 is cleared so no overflow.
Last edited: