dima2882
Joined: 13 Jul 2005 Posts: 25 Location: Maryland
|
PWM period incorrect, pulse width correct |
Posted: Tue Mar 23, 2010 1:32 am |
|
|
Hi all,
I am using a PIC24F04KA200 with 4.104. I successfully generate a PWM with pulse width (duty cycle) and period as programmed. However this only works until a period of ~16.4ms. Faster than that, and the pulse width is correct, but the period is way off... I have an FRC PLLed, and I'm doing this:
Code: |
#fuses FRC_PLL, NOWDT, NOPROTECT
#use delay(internal=8M)
#byte PR2 = 0x010C // This is the timer period out of the datasheet
...
// Set up PWM
PR2 = 306;
setup_timer2(TMR_INTERNAL | TMR_DIV_BY_1);
setup_compare(1, COMPARE_PWM | COMPARE_TIMER2);
// Enter main software infinite loop
while(TRUE)
{
set_pwm_duty(1, 150);
}
|
This is the same example as in the datasheet for this chip, ex. 14-1, pg. 115. Quote datasheet:
Quote: |
Find the Timer Period register value for a desired PWM frequency of 52.08 kHz, where FOSC = 8 MHz with PLL
(32 MHz device clock rate) and a Timer2 prescaler setting of 1:1.
TCY = 2 * TOSC = 62.5 ns
PWM Period = 1/PWM Frequency = 1/52.08 kHz = 19.2 μs
PWM Period = (PR2 + 1) • TCY • (Timer 2 Prescale Value)
19.2 μs = (PR2 + 1) • 62.5 ns • 1
PR2 = 306
|
Doesn't this mean that I should expect to see PWM pulses stay up for ~9.6μs every 19.2μs? Or am I mistaken somewhere? |
|