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 CCS Technical Support

Problem with PWM Duty Cycle

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



Joined: 14 Nov 2007
Posts: 7

View user's profile Send private message

Problem with PWM Duty Cycle
PostPosted: Wed Dec 12, 2007 3:17 pm     Reply with quote

Hi,

I cannot get a 10 bit pwm duty cycle. When I put a number that is higher than 8 bit into the duty cycle I get full pwm duty.

This is my code:

setup_ccp2(CCP_PWM);
setup_timer_2(T2_DIV_BY_1, 63, 1);
set_pwm2_duty((int16)255);

I can only vary pwm in the 8 bit range. Is there something else I need to change. Im using a 16F917

Thanks
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Dec 12, 2007 4:02 pm     Reply with quote

I compiled the following test program with vs. 4.064 and it worked in
10-bit pwm mode. Your test value for duty cycle is very close to 100%,
so all you will see on the oscilloscope is a very thin negative pulse.

I put in the following duty cycle values, and measured the pulse width
on an oscilloscope. Since I'm using a 20 MHz crystal, the clock period
is 50 ns, and the pulse width does change in 50 ns increments, which
is correct for 10-bit PWM operation.
Code:

Duty cycle   Negative pulse
value          width
255              50 ns
254             100 ns
253             150 ns
252             200 ns


Code:

#include <16F877.H>
#fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 20000000)
//#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

//======================================
void main()
{
setup_ccp2(CCP_PWM);
setup_timer_2(T2_DIV_BY_1, 63, 1);
set_pwm2_duty((int16)255);

while(1);
}
Ttelmah
Guest







PostPosted: Wed Dec 12, 2007 4:25 pm     Reply with quote

The key is the setup timer statement.
The PWM range, is a maximum of 10bits, also limited to 4* the timer maximum count+1. With a timer setup of 63, the PWM limit, is 4*64. To get a full 10bit PWM, the timer setup has to increase to 255. With the example given, and 63 in the timer, you will get 255:256 on, with just a single 50nSec low pulse.

Best Wishes
pklein



Joined: 14 Nov 2007
Posts: 7

View user's profile Send private message

PostPosted: Wed Dec 12, 2007 11:37 pm     Reply with quote

Thanks. That makes perfect sense. I didnt know about it being limited to 4 times the timer count.
Ttelmah
Guest







PostPosted: Thu Dec 13, 2007 3:47 am     Reply with quote

On most PICs, there is to be a table in the data sheets, giving the 'effective' PWM resolution, with different timing setups. This seems to have disappeared from a few of the latest PIC sheets, but if you pull one of the older ones (for example, the one for the 16F684), then table 11-4, gives "example pwm frequencies and resolution", with the output frequency, and the 'effective resolution' in bits on the bottom line.

Best Wishes
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