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

pwm frequency library

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



Joined: 28 Jan 2009
Posts: 2

View user's profile Send private message

pwm frequency library
PostPosted: Wed Jan 28, 2009 12:04 pm     Reply with quote

hi, does anybody have a library that can change PWM frequency on the fly?

like
Code:
 setup_pwm_frequency(5000);
Ttelmah
Guest







PostPosted: Thu Jan 29, 2009 5:06 am     Reply with quote

Realistically, this is both simple, and hard.
Simple. The existing timer setup function, is quite happy to wok 'on the fly'. Hard, because you need to apply your own decisions as to acceptable margins. Also, if you are using the PWM, to generate modulated widths, every piece of code using the PWM, will have to change, to adjust for the different range of controls allowed at different frequencies...
So, if your master clock is 20MHz, and you input a value like '5000' (presuming this is in 'Hz'), you would start by taking the clock/4, divided by the frequency required:

(CLOCK/4)/5000 = 1000

Then you test the required divisor, against 256, 1024, and 4096. If the value is >4096, it is impossible to do. If <=256 then set the prescaler to 1. If 256 to 1024, divide by four, and set the PWM prescaler to 4. If 1025 to 4096, then divide by 16, and set the prescaler to 16.
Then take the calculated divisor, subtract one, and use this value to setup the timer.
Now, you have the 'error' decision'. Your resulting frequency, will be master clock/4, over the prescaler, over the final divisor number+1. You should look at this, and compare to with the requested frequency, and set an 'error', if the difference is more than some margin of your choice. Then you have the 'control limits'. The divisor, plus one, times four, is now the maximum value that can be used to control the pulse width. If you just want a 50% duty cycle, then you would simply calculate this over two, and use this to set the PWM duty, but if you are using the PWM, then this value, will need to be fed to all routines using the PWM, so that they can limit their output accordingly.

Best Wishes
vysakh



Joined: 28 Jan 2009
Posts: 2

View user's profile Send private message

PostPosted: Sun Feb 01, 2009 12:53 am     Reply with quote

Hi,

Thanks a lot. The function is more than enough for the task at hand. I'll try to tweak it a bit if possible [;-)]
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