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

Question about hardware CCP/PWM value to set duty

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



Joined: 17 Oct 2005
Posts: 116

View user's profile Send private message

Question about hardware CCP/PWM value to set duty
PostPosted: Wed Apr 19, 2006 11:02 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Apr 19, 2006 11:45 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Apr 19, 2006 11:50 am     Reply with quote

Thanks for the great explanation!

Harry
pacman91



Joined: 17 Jun 2011
Posts: 28
Location: Malaysia

View user's profile Send private message Send e-mail

PostPosted: Sat Jun 18, 2011 1:33 pm     Reply with quote

good, thx for the explanation Very Happy
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