|
|
View previous topic :: View next topic |
Author |
Message |
kind2011
Joined: 09 Dec 2012 Posts: 3
|
problem with pwm |
Posted: Sun Dec 09, 2012 5:16 pm |
|
|
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
|
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Sun Dec 09, 2012 5:49 pm |
|
|
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
|
|
Posted: Sun Dec 09, 2012 9:54 pm |
|
|
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
|
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Mon Dec 10, 2012 1:12 pm |
|
|
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
|
|
Posted: Tue Dec 11, 2012 4:16 pm |
|
|
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
|
|
Posted: Tue Dec 11, 2012 5:31 pm |
|
|
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 |
|
|
|
|
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
|