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

Strange set_pwm_duty(value) function

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



Joined: 29 Sep 2006
Posts: 114

View user's profile Send private message

Strange set_pwm_duty(value) function
PostPosted: Fri Nov 03, 2006 4:40 am     Reply with quote

Hi,

I don't get it. Shocked You would expect the value to be put into the 10 bits. But no, we just skip the 2 lsbits. If you don't want that, then you have add the undocumented postfix 'L' to the value... I wonder if this works also on a variable.

Who created this stupid function...

Regards,
Edwin.
Torello



Joined: 29 Sep 2006
Posts: 114

View user's profile Send private message

PostPosted: Fri Nov 03, 2006 5:00 am     Reply with quote

Okee, I found out:

set_pwm1_duty((long)0x00FF); Compiler does not recognize 0x00FF as a int16 and the function does not expect int16 by default, so type cast is needed.

set_pwm1_duty(0x01FF); Compiler sees a value higher than 8 bits so handles it as int16, typecast is not needed.

Still wondering about the L postfix...

regards,
Ttelmah
Guest







PostPosted: Fri Nov 03, 2006 5:10 am     Reply with quote

'L', is not 'undocumented'. It is standard C, to say 'this value is a _long_'.
You do not need it for a variable, if the variable is defined as a long:

long pwm_val;

or

int16 pwm_val;

Then the compiler will handle the function to use 10bits. If the value given is only 8 bits:

int pwm_val;

Then the compiler says "the value is not ten bits long, so the user must want me to only use the main 8bit PWM register". This saves significant work in the routines involved.
You can get the same effect on the constant, either by adding the 'L', or by explicitly casting:

set_pwm1_duty((long)128);

You have to remember that the pwm hardware, only has an 8bit register for the main part of the duty cycle, and the extra two bits need to be trimmed off the incoming value, moved up, and then put into two bits in another register. Quite messy. Hence a lot of applications requiring the fastest possible update of the pwm, would actually prefer not to do this.

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