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

How to change PWM resolution from 10bits to 8bits ?

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



Joined: 19 Nov 2004
Posts: 11

View user's profile Send private message MSN Messenger ICQ Number

How to change PWM resolution from 10bits to 8bits ?
PostPosted: Thu Sep 15, 2005 12:39 am     Reply with quote

Hi everyone,

I using PIC18f2331. I wish to increase the frequency of the PWM, therefore, i wish to lower the PWM resolution to 8bits.

Can someone guide me on how to change PWM resolution from 10bits to 8bits ?


Regards,
Daviz
MikeValencia



Joined: 04 Aug 2004
Posts: 238
Location: Chicago

View user's profile Send private message Send e-mail Yahoo Messenger

PostPosted: Thu Sep 15, 2005 7:22 am     Reply with quote

In general, the CCPR1L register contains the duty cycle value, which is 8 bits. These are the most significant 8 bits in a 10 bit duty cycle. The least significant two bits are in the control register (CCP1CON?). If you keep the least significant two bits as '00', then you have 8-bit resolution.

You raise your frequency by decreasing the value in PR2, because now your period will be smaller, and thus a higher frequency.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Sep 15, 2005 8:16 am     Reply with quote

This thread has code examples for PWM in 8 or 10 bit mode, and links to more examples:
http://www.ccsinfo.com/forum/viewtopic.php?t=24055
Ttelmah
Guest







PostPosted: Thu Sep 15, 2005 8:32 am     Reply with quote

Mike has described what is involved, but it may not be obvious from this what has to be done in CCS.
The Timer2 value, controls the total 'period' length. Then the PWM value, can be adjusted up to this value*4. The '*4', comes because the counter accesses an extra two bits for the comparison, which correspond to the prescaler feeding the counter.
There are then two ways of getting '8 bit resolution'. The first is simply not to access these extra bits. The 'set_pwmx_duty' commands do this automatically, if given an integer value, instead of a long. So, if you have:

setup_timer_2(T2_DIV_BY_16,249,10);

and then use:

set_pwm1_duty(125);

You will get 50:50 duty, because the compiler automatically puts the 'short' value it is given into the most significant 8 bits. However if you use:

set_pwm1_duty(125L);

You will now only get a 12.5% 'on' period, as the full 10 bits are accessed.

Note also, that because timer_2 is only counting to 249, the 50:50 point is at 125, not the 128 for the full count. This is the 'key' to using higher frequencies with lower resolution.

So if you now change the setup to:

setup_timer_2(T2_DIV_BY_16,63,10);

The frequency used will increase to 3.9* the previous rate (250/64), and a duty setting of:

set_pwm1_duty(128L);

will now give 50:50 duty cycle, corresponding to an 8 bit resolution.

The highest frequency PWM, with an 8bit duty cycle control, will be:

setup_timer_2(T2_DIV_BY_1,63,10);

Note that in all these, the last byte (10), does not affect the pwm width or the duty, but how many cycles occur between interrupts. If you are not using the timer2 interrupt, the value here does not matter.

Using a 20MHz clock for example, this will give a 78.125KHz output.

So your available resolution, is the value used as the second parameter in the setup_timer_2 function+1, *4.

Best Wishes
sseidman



Joined: 14 Mar 2005
Posts: 159

View user's profile Send private message

PostPosted: Thu Sep 15, 2005 11:27 am     Reply with quote

Is there anything special that needs knowing with respect to setup_power_pwm on the 18fx331?

Scott
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