View previous topic :: View next topic |
Author |
Message |
itsbasix
Joined: 23 May 2012 Posts: 8
|
PWM duty cycle with PIC18F4550 |
Posted: Mon Jul 08, 2013 9:51 am |
|
|
Hi guys,
i know that this topic has been already discussed in many post, but i still have some doubts about it to be cleared.
I'm using a PIC18F4550 in USB CDC mode, with a 20MHz crystal and PLL to get a 48MHz frequency.
In order to get a 12KHz PWM freq with CCP module I set Timer2 like this:
Code: | setup_timer_2(T2_DIV_BY_4,249,1); |
I know that T_high in PWM is calculated as:
Code: | T_high=value*(1/clock)*t2div |
( where value is chosen when calling set_pwmX_duty(value) function ).
Then, if i need to set the duty cycle generated from CCP module i calculate value as it follows and call set_pwmX_duty(value):
Code: | value= T_high*clock/t2div;
set_pwmX_duty(value); |
With my current configuration, for a 100% duty cycle, i should set:
Code: |
T_high=(1/12000)=8.333333*10^(-5)
1 * 48*10^(6)
value = --------------------- = 1000
12*10^(3) * 4
|
what i don't understand is this: wasn't 1023 supposed to be the value to get 100% duty in any case? Am i making any mistake?
Thank you and sorry for my bad english. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Jul 08, 2013 11:10 am |
|
|
Quote: | wasn't 1023 supposed to be the value to get 100% duty |
No. The PIC data sheet doesn't say anywhere that the duty cycle is
based on some power-of-2 value. It's based on the Timer2 reload value. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19546
|
|
Posted: Mon Jul 08, 2013 11:34 am |
|
|
The maximum value for the duty cycle, is always ((PR2+1)*4-1).
So with your timer setup, and PR2=249, 999.
The point about 1023, is with the maximum PR2 value (255), this gives 1023.
So there is no magic about 1023, except it is the maximum duty cycle value, _when the maximum PR2 value is selected_....
Best Wishes |
|
|
itsbasix
Joined: 23 May 2012 Posts: 8
|
|
Posted: Tue Jul 09, 2013 1:03 am |
|
|
thank you for your answers!
Have a nice day |
|
|
|