|
|
View previous topic :: View next topic |
Author |
Message |
nilsener
Joined: 06 Dec 2005 Posts: 59
|
PWM Duty Cycle [DONE] |
Posted: Thu Aug 28, 2008 4:40 am |
|
|
18F2620
PCWH 4.014
MPLAB 7.62
Hello,
I have to generate a 20 kHz output signal with 50% duty cycle using the PWM module. With setup_timer_2 (T2_DIV_BY_1, 99, 1) I get the 20 kHz on my scope. By setting the duty cycle with set_pwm1_duty(256); I get approx. 66% duty cycle. If I adjust the value below 256 e.g. set_pwm1_duty(255); then the 20 kHz signal is gone. The output is always high. I think I did'nt understand the set_pwm1_duty(256); command!?
Code: |
setup_oscillator(OSC_8MHZ);
setup_timer_2(T2_DIV_BY_1, 99, 1); // set timer 2 to 20 kHz
setup_ccp1(CCP_PWM); // set to hardware PWM using timer 2
set_pwm1_duty(256); // set PWM duty cycle
|
Are there any ideas how to adjust the duty cycle to 50% ?
What is the valid range for x of set_pwm1_duty(x); ?
Thanks for any help
Best Regards
nilsener
Last edited by nilsener on Tue Sep 02, 2008 9:14 am; edited 1 time in total |
|
|
Ttelmah Guest
|
|
Posted: Thu Aug 28, 2008 7:04 am |
|
|
Add 'L' to the value you use in the set_pwm_duty command.
If you read the manual, you will find that the command has two different 'modes' of operation. It uses '10bit' mode, if the value being given to it is a _long_ integer. It switches to 8bit mode, if the value being given is a normal '8bit' integer.
Now, if you used a variable, so:
Code: |
int16 duty;
setup_oscillator(OSC_8MHZ);
setup_timer_2(T2_DIV_BY_1, 99, 1); // set timer 2 to 20 kHz
setup_ccp1(CCP_PWM); // set to hardware PWM using timer 2
duty=200;
set_pwm1_duty(duty); // set PWM duty cycle
|
The operation would work with the lower value as shown.
The problem is that when your value goes below '256', it becomes storable in a 8bit integer, so the compiler switches to doing so. This changes the operation of the command to it's 8bit mode. If you instead use '200L', this tells the compiler, to keep treating the value as a 'long', and avoids the problem.
In the 8bit mode, the vaue used is effectively multiplied by four, so you could simply use:
set_pwm1_duty(50);
A search here, would find this explained a lot of times before....
Best Wishes |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Thu Aug 28, 2008 7:11 am |
|
|
Quote: | If value is an 8 bit item, it is shifted up with two zero bits in the lsb positions to get 10 bits. |
You have to assure that the < 256 value is understood as 16 bit constant or use value/4 |
|
|
nilsener
Joined: 06 Dec 2005 Posts: 59
|
|
Posted: Tue Sep 02, 2008 9:13 am |
|
|
Thank you very much guys,
this was exactly the problem. I changed the value to 16 bit and it works. C is so strictly
Best Regards
nilsener |
|
|
|
|
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
|