|
|
View previous topic :: View next topic |
Author |
Message |
Torello
Joined: 29 Sep 2006 Posts: 120
|
Strange set_pwm_duty(value) function |
Posted: Fri Nov 03, 2006 4:40 am |
|
|
Hi,
I don't get it. 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: 120
|
|
Posted: Fri Nov 03, 2006 5:00 am |
|
|
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
|
|
Posted: Fri Nov 03, 2006 5:10 am |
|
|
'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 |
|
|
|
|
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
|