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

pwm dutycycle

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



Joined: 24 Apr 2010
Posts: 75
Location: Islamabad

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

pwm dutycycle
PostPosted: Sun Jan 30, 2011 4:06 am     Reply with quote

hi.. in the data sheet of PIC16F877A.. i have find that PWM is of 10 bit..
what does it mean.. can i change my duty cycle at 0--1024???
_________________
...
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Sun Jan 30, 2011 4:51 am     Reply with quote

Potentially, nearly yes.

'Nearly', because a 10bit value can generate 0 to 1023 (not 1024)....

Potentially, because if you read on, you will find that 10 bit, is the _maximum_ duty cycle resolution, and only applies when the timer used to run the PWM, counting 0-255.
So if timer2, is setup with

setup_timer_2(T2_DIV_BY_1,255,1);

Then the pwm duty value, can be 0 to 1023.

However if the timer is setup with:

setup_timer_2(T2_DIV_BY_1,99,1);

Then the duty cycle, can only be 0 to 399.

The formula is (((PR2value)+1)*4)-1 for the maximum supported value.

PR2 is the second value in the timer2 setup line.

There is a table in the data sheet (8-3), giving 'example PWM frequencies and resolutions', showing how this affects the maximum resolution.

You also need to ensure that the number fed into the set pwm duty function, is coded as an int16.

Best Wishes
Bilal



Joined: 24 Apr 2010
Posts: 75
Location: Islamabad

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

PostPosted: Sun Jan 30, 2011 5:09 am     Reply with quote

It gives a lot of confusion.
I use PIC16F877A with 4MHz external crystal.
and generate a 1KHz PWM freq

setup_timer_2(T2_DIV_BY_4,249,1);

But I find that with duty cycle of 125 it gives 50% duty cycle!!!!
_________________
...
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Sun Jan 30, 2011 9:50 am     Reply with quote

It would....
Remember what I said about _you must use a 16bit value_. 125, is an 8 bit value.
125L, makes the compiler represent the value using 16bit.

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