Hello,
I aim to produce PWM with 20ms period and 2ms duty cycle. I use P12F683 @4MHz, MpLab xc8. I calculated the PR2 value given by data sheet;
PWM Period = [(PR2) + 1] 4 Tosc TMR2 Prescale Value
However when I do the simulation on Proteus, PWM's period comes out shorter than I expect it to be. It's 3.7ms. I also tried some random values for PR2 in order to approach 20ms, none of them worked. I can't really understand what causes the problem. I'd be grateful if you could share your opinions.
Here's the function I use for initializing PWM
Thank you
I aim to produce PWM with 20ms period and 2ms duty cycle. I use P12F683 @4MHz, MpLab xc8. I calculated the PR2 value given by data sheet;
PWM Period = [(PR2) + 1] 4 Tosc TMR2 Prescale Value
However when I do the simulation on Proteus, PWM's period comes out shorter than I expect it to be. It's 3.7ms. I also tried some random values for PR2 in order to approach 20ms, none of them worked. I can't really understand what causes the problem. I'd be grateful if you could share your opinions.
Here's the function I use for initializing PWM
Rich (BB code):
void InitPWM()
{
OSCCONbits.SCS=1; //Internal oscillator is used for system clock
OSCCONbits.IRCF=0b110; // Internal Oscillator Frequency 4 MHz
CCP1CON=0x2C; // bit3-0 PWM Mode
TRISIO2=0; // CCP1 output
T2CON=0x06; // Timer2 is on , Prescale 1:16
PR2=1249; // 20 ms = (PR +1) *4 * 250ns * TMR2 Prescale value
CCPR1L=0b01; //
}