CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Exact PWM frequency

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
peter101



Joined: 14 Apr 2008
Posts: 2

View user's profile Send private message

Exact PWM frequency
PostPosted: Fri Apr 25, 2008 4:43 am     Reply with quote

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.
RLScott



Joined: 10 Jul 2007
Posts: 465

View user's profile Send private message

Re: Exact PWM frequency
PostPosted: Fri Apr 25, 2008 5:17 am     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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