|
|
View previous topic :: View next topic |
Author |
Message |
deniska_gus
Joined: 11 Jul 2006 Posts: 42 Location: Minden, Germany
|
PWM on/off |
Posted: Fri Oct 05, 2007 2:53 am |
|
|
Hi,
i have some question about PWM. I have to toggle the PWM Pin with 18Hz. It means, 28 miliseconds on and 28 miliseconds off. How can i do it right? A LED have to flash with 18 Hz, but the voltage have to be set by PWM (1KHz) |
|
|
Ttelmah Guest
|
|
Posted: Fri Oct 05, 2007 3:09 am |
|
|
Ok.
You need some other form of timer (either a simple 'delay' statement, or (better for accuracy), a hardware timer 'tick'at the required 28mSec interval.
Then, beyond this, it depends a little on what PWM implementation your chip has. On ones with an 'enhanced' PWM, you can use the 'output override' ability, to turn the PWM pin(s) on/off. On ones with a 'standard' pwm, use the 'setup_ccpx' function, to switch the CCP, from being programmed as a PWM, to the function being 'off'. So:
Code: |
setup_timer_2(T2_DIV_BY_1,49,1); //choose to suit your required freq
while (true) {
setup_ccp1(CCP_PWM);
set_pwm1_duty(100L);
delay_ms(28);
setup_CCP1(CCP_OFF);
output_low(defined_pwm_pin);
delay_ms(28);
}
|
Obviously, this assumes you want the pin 'low' when not driven, and that you substitute the same of the PWM pin where needed.
Alternatively, just set the 'duty' beyond the limit. However 'beware', that some chips have errata on this, and in some cases, don't go quite completely 'off', or fully 'on'. Check before using this.
Obviously, if you use an interrupt driven 'tick' to control the on/off time, this allows other code to run in the 'main', without interfering with the timing.
Best Wishes |
|
|
|
|
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
|