![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
peter101
Joined: 14 Apr 2008 Posts: 2
|
Exact PWM frequency |
Posted: Fri Apr 25, 2008 4:43 am |
|
|
Hi,
is there any way of acheiving an exact PWM frequency of 12kHz.
setup_ccp1(CCP_PWM);
setup_timer_2(T2_DIV_BY_1, 82, 1);
set_pwm1_duty(25);
(1/4000000)*4*1*val=(1/12000)
val = 83.33-1 rounded is 82 which equals 12.195khz 195Hz out?
Thanks. |
|
![](templates/subSilver/images/spacer.gif) |
RLScott
Joined: 10 Jul 2007 Posts: 465
|
Re: Exact PWM frequency |
Posted: Fri Apr 25, 2008 5:17 am |
|
|
peter101 wrote: |
val = 83.33-1 rounded is 82 which equals 12.195khz 195Hz out?
|
Actually, when you enter 82 as the divide parameter, Timer 2 divides by 82+1=83 (which is why you subtracted 1 in the first place), so the actual frequency is 1000/83 = 12.048 kHz. Still not 12 kHz, but closer.
There is no way to make any timer based on a 4 MHz clock produce exactly 12 kHz. In fact, dividing a 1 usec clock by 83 as you are doing is as close as you can get. If you really need exactly 12 kHz, there are some things you can do:
1. Change the crystal from 4 MHz to 3.984 Mhz and continue to use 82.
...or...
2. Use a more software-intensive pulse generation method that continually dithers the divide ratio in order to make the average frequency come out to exactly 12 kHz. If you changed the divide parameter on each cycle so that you used 82, 82, 83, 82, 82, 83,...etc., then that would be equivalent to using an average of 82.33333, which would mean a divide ratio of 83.33333, which would give you 12 kHz exactly. But as I said this method is very software-intensive. You have to closely monitor the progress of Timer 2 and change the divide parameter just after each re-load. You could not use the CCS library function to do this because setup_timer_2() takes too much time and possibly does other things besides just reload the divide ratio. But it can be done by talking to the SFRs directly. I have used this method for exact frequency generation. But it would certainly be easier to use option 1. of just changing the crystal.
Robert Scott
Real-Time Specialties |
|
![](templates/subSilver/images/spacer.gif) |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|