View previous topic :: View next topic |
Author |
Message |
Harry Mueller
Joined: 17 Oct 2005 Posts: 116
|
Question about hardware CCP/PWM value to set duty |
Posted: Wed Apr 19, 2006 11:02 am |
|
|
I've got version 3.245 and am using a 16F877.
When I write the following code it will work if I declare torque as an 8 bit variable but not if I declare it as a 16 bit variable: Code: | set_pwm1_duty(torque); // calculated duty cycle on pin B2 |
Any ideas why that would be?
Thanks, Harry |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Apr 19, 2006 11:45 am |
|
|
The PWM module has 10 register bits to control the duty cycle. The
upper 8 bits are in the PR2 register and the lower 2 bits are in the
CCPxCON register.
If you use an 8-bit parameter for the set_pwmx_duty() function,
CCS puts that value into the PR2 register, which is the upper 8 bits
of the 10-bit duty cycle value. The bottom 2 bits are left unchanged,
but they are set to zero upon power-on reset.
If you use a 16-bit variable as the parameter, the compiler will
use this value to set all 10 bits of the PWM duty cycle. The lower
2 bits are put into the CCPxCON register, and the upper 8 bits are
put into the PR2 register.
To summarize, the difference between the two modes is entirely
in how the compiler interprets the data. In "8-bit mode", the
compiler leaves the bottom 2 bits of the 10-bit PWM duty cycle
value set to 0, and just puts your 8-bit parameter into the upper
8-bits of the PWM duty cycle register bits. These two modes
of compiler operation are controlled entirely by the size of the
variable that you pass to the set_pwmx_duty() cycle function.
An int8 variable will cause "8-bit mode" to be used and an int16
(or a constant with an "L" on the end) will cause 16-bit mode to
be used.
So if you use the same parameter value in both 8-bit and 10-bit mode,
then the perceived effect will be that the duty cycle is divided by 4
when you use a 16-bit parameter. If that's controlling a motor, the
maximum speed will be only 1/4 of what it was. (Assuming a 1:1
relationship between duty cycle and speed).
To get back to the same performance as in 8-bit mode, you must
multiply your parameter by 4.
Last edited by PCM programmer on Wed Apr 19, 2006 11:50 am; edited 1 time in total |
|
|
Harry Mueller
Joined: 17 Oct 2005 Posts: 116
|
|
Posted: Wed Apr 19, 2006 11:50 am |
|
|
Thanks for the great explanation!
Harry |
|
|
pacman91
Joined: 17 Jun 2011 Posts: 28 Location: Malaysia
|
|
Posted: Sat Jun 18, 2011 1:33 pm |
|
|
good, thx for the explanation |
|
|
|