View previous topic :: View next topic |
Author |
Message |
Bilal
Joined: 24 Apr 2010 Posts: 75 Location: Islamabad
|
pwm dutycycle |
Posted: Sun Jan 30, 2011 4:06 am |
|
|
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
|
|
Posted: Sun Jan 30, 2011 4:51 am |
|
|
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
|
|
Posted: Sun Jan 30, 2011 5:09 am |
|
|
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
|
|
Posted: Sun Jan 30, 2011 9:50 am |
|
|
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 |
|
|
|