benoitstjean
Joined: 30 Oct 2007 Posts: 566 Location: Ottawa, Ontario, Canada
|
Two different PWM frequency ranges |
Posted: Fri Aug 17, 2012 9:49 am |
|
|
PIC: 18F4620
Crystal: 16,384,000 MHz
The following code generates a PWM of exactly 128kHz 50% duty on PWM 1:
setup_timer_2( T2_DIV_BY_1, 31, 1 );
set_pwm1_duty( 16 );
PWM 1 = 16384000
-----------
1 x (31+1) x 4
PWM 1 = 16384000
-----------
128
PWM 1 = 128 000
How would I go about if I want my PWM 2 to be a totally different PWM frequency that requires a different calculation?
For instance, above, the function setup_timer_2( T2_DIV_BY_1, 31, 1 ); values 31 and T2_DIV_BY_1 gives me 128kHz.
But if I want PWM 2 to be 64kHz? I'd have to change 31 to 63.... Do I use a different timer?
I've used two PWM's before but both were the same frequency since they were for servos and the only thing I had to change were their individual duty cycles.
Now it's two different frequencies. I will check the manual but if someone know, please post it!
Thanks!
Ben |
|