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 duty cycle with PIC18F4550

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



Joined: 23 May 2012
Posts: 8

View user's profile Send private message

PWM duty cycle with PIC18F4550
PostPosted: Mon Jul 08, 2013 9:51 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Jul 08, 2013 11:10 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Jul 08, 2013 11:34 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Jul 09, 2013 1:03 am     Reply with quote

thank you for your answers!
Have a nice day Smile
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