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 support@ccsinfo.com

problem with pwm

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



Joined: 09 Dec 2012
Posts: 3

View user's profile Send private message

problem with pwm
PostPosted: Sun Dec 09, 2012 5:16 pm     Reply with quote

I have a simple code that I expect to generate a pwm with 11khz and 50% duty but isn't, it only works when I change the timer setup for:
Code:
setup_timer_2(T2_DIV_BY_16,244,1);

It generates a a 2.77khz wave but when I change for:
Code:
 setup_timer_2(T2_DIV_BY_16,68,1);

nothing happens at the ccp1 output?
Code:

#include <18F4550.h>
#device adc=10
#fuses HSPLL,WDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN, PUT
#use delay(clock=48000000)

#include <usb_bootloader.h>
#USE FAST_IO (ALL)
#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7)

int i=0,data;

#int_timer2
void timer2_isr(void)
{
set_pwm1_duty(data);
}

void main(){
SET_TRIS_C( 0X00 );
setup_ccp1(CCP_PWM);
setup_timer_2(T2_DIV_BY_16,68,1); 
clear_interrupt(INT_TIMER2);
enable_interrupts(INT_TIMER2);
enable_interrupts(GLOBAL);
set_pwm1_duty(data);
while(1);
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Dec 09, 2012 5:24 pm     Reply with quote

Read this thread about pwm with the 18F4550. It might help you:
http://www.ccsinfo.com/forum/viewtopic.php?t=48575
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Sun Dec 09, 2012 5:49 pm     Reply with quote

Is your code supposed to be complete and compilable?
How are you calculating the divisor to get the frequency you expect?
Where does the 'data' value come from to set the duty ratio?

Mike
kind2011



Joined: 09 Dec 2012
Posts: 3

View user's profile Send private message

PostPosted: Sun Dec 09, 2012 9:54 pm     Reply with quote

I forgot to set the duty,
this is the complete code and compilable:
Code:

#include <18F4550.h>
#device adc=10
#fuses HSPLL,WDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN, PUT
#use delay(clock=48000000)
#include <usb_bootloader.h>
#USE FAST_IO (ALL)
#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7)

unsigned int16 data;

#int_timer2
void timer2_isr(void)
{
set_pwm1_duty(data);
}

void main(){
SET_TRIS_C( 0X00 );
setup_ccp1(CCP_PWM);
setup_timer_2(T2_DIV_BY_16,244,1); 
clear_interrupt(INT_TIMER2);
enable_interrupts(INT_TIMER2);
enable_interrupts(GLOBAL);

data=511;
set_pwm1_duty(data);
while(1);
}

With this formula:
The cycle time will be (1/clock)*4*t2div*(period+1)
with a clock of 48Mhz for a cycle time of 92.5us , then period =68
then changing to:
Code:
setup_timer_2(T2_DIV_BY_16,68,1);
 

only see a line, not a pwm signal.
Then I noticed that changing period to 200 the duty=60 and period 180 duty=70 then period of 160 and duty=79. From that I think with a period of 68 the duty is 100 but why, if I am not changing the duty?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Dec 09, 2012 10:10 pm     Reply with quote

Read all the pwm links in this post. They will tell you everything you
need to know:
http://www.ccsinfo.com/forum/viewtopic.php?t=45968&start=1
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Mon Dec 10, 2012 1:12 pm     Reply with quote

Quote:
Then I noticed that changing period to 200 the duty=60 and period 180 duty=70 then period of 160 and duty=79. From that I think with a period of 68 the duty is 100 but why, if I am not changing the duty


You're setting the duty to a fixed 10bit 511 value.

When you set the period with:-

1) An 8bit 200 value; the duty ratio is 511/(4*200) = 0,63875 approx 60%
2) An 8bit 180 value; the duty ratio is 511/(4*180) = 0,70972 approx 70%
3) An 8bit 160 value; the duty ratio is 511/(4*160) = 0,79844 approx 80%

You're changing the PWM total period with a fixed ON period, so the DUTY RATIO changes.

Mike
kind2011



Joined: 09 Dec 2012
Posts: 3

View user's profile Send private message

PostPosted: Tue Dec 11, 2012 4:16 pm     Reply with quote

Thanks! there's a way to change the period of pwm without changing the
duty ?
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Tue Dec 11, 2012 5:31 pm     Reply with quote

Kindly rephrase this question
Quote:
there's a way to change the period of pwm without changing the duty ?
As it stands it makes no sense.

You need to clearly understand the basic terms.

The PWM_period is set by
Code:
setup_timer_2(T2_DIV_BY_16,244,1);
where the 244 is the (PWM_period-1).

The duty_period is set by
Code:
set_pwm1_duty(data);
where 'data' is the duty_period.

Assuming that 'data' is 16bit, the duty ratio is given by:-

duty_ratio = duty_period / (4 * PWM_period)

So now, what is your question?

Mike
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