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 Modulator Duty Cycle Problem

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







PWM Modulator Duty Cycle Problem
PostPosted: Fri Aug 10, 2007 2:01 am     Reply with quote

Hi,

I cannot seem to set the dutty cycle for my PWM to less than 25%. Here is my code:-

Code:
 setup_ccp2(CCP_PWM);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   setup_timer_2(T2_DIV_BY_1, 0xff, 1);
   set_pwm2_duty(256);


This gives me an on time (duty cycle) of 12.8us and a period of 51.2us, but if I reduce the duty cycle setup value to 250 I get an on time of 50us (the period remains 51.2us).

Any ideas?

Regards, Ali
Ttelmah
Guest







PostPosted: Fri Aug 10, 2007 2:41 am     Reply with quote

The problem is that the pwmx_duty function, will accept long values, or a normal short int, and _switches how it works_, for the different number types. When you send '256', since it is over 255, it is automatically a 'long', and the function treats it as such, giving you the full range of control. As soon as you switch to sending a value under of 255 or under, the function changes mode, and treats the value as an 'int8', effectively multiplying it by 4, before setting the duty cycle.
The ways to deal with this are:
1) Using a constant:
set_pwm2_duty(50L);

Note the 'L'. This means this number is to be treated as a 'long'.
2) Using a variable:
int16 pwm_duty;
pwm_duty=50;

set_pwm2_duty(pwm_duty);

Since the number is put into a long variable, _before_it is given to the function, the function receives it as a 'long'.
3) Using either a constant or a variable, that is an int8.

set_pwm2_duty((int16)50);

This is similar to '1', but functionally slightly different. In '1', the number is explicitly 'made' into an int16. In '3', whatever is given (constant or variable), is _converted_ to an int16, before being given to the function. This is useful, if you want to use an 'int8' to hold a duty cycle, but access only the bottom part of the output range, with this.

Best Wishes
Ali P
Guest







Your answer
PostPosted: Fri Aug 10, 2007 3:06 am     Reply with quote

Hi there,

Thanks for your quick reply, I tried it straight away and it worked no problem. I knew it would be something silly Embarassed , just couldn't see what!

Very Happy Thanks again, have a great weekend, Ali
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