|
|
View previous topic :: View next topic |
Author |
Message |
rantt4 Guest
|
PROBLEM WITH PWM.. |
Posted: Fri Jun 20, 2008 2:15 pm |
|
|
hi
i have to blink a led in a pwm mode but my code somehow turn the led on on a pwm but then it remains ON. i need to blink it while pwm. on and off while pwm.
i add my code.
thanx!
Code: | #include <16F690.h>
#fuses XT,NOWDT,NOPROTECT,put
#use delay(clock=8000000)
int j; // variable to hold 8-bit PWM value
void main() {
setup_ccp1(CCP_PWM);
setup_timer_2(T2_DIV_BY_1, 124, 1);
set_pwm1_duty(0);
while(1)
{
for (j=0; j<255; j++){
set_pwm1_duty(j);
delay_ms(100);
}
for (j=255; j>0; j--){
set_pwm1_duty(j);
delay_ms(100);
}
}
} |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Jun 20, 2008 3:01 pm |
|
|
Quote: | setup_ccp1(CCP_PWM);
setup_timer_2(T2_DIV_BY_1, 124, 1);
set_pwm1_duty(0);
while(1)
{
for (j=0; j<255; j++){
set_pwm1_duty(j);
delay_ms(100);
}
for (j=255; j>0; j--){
set_pwm1_duty(j);
delay_ms(100);
}
}
} |
You are running the PWM functions in 8-bit mode. In that mode,
if the duty cycle value is equal to, or greater than the Timer period,
then the PWM output will be a constant high level.
To fix your code, change it as shown in bold below:
Quote: | setup_ccp1(CCP_PWM);
setup_timer_2(T2_DIV_BY_1, 124, 1);
set_pwm1_duty(0);
while(1)
{
for (j=0; j<125; j++){
set_pwm1_duty(j);
delay_ms(100);
}
for (j=124; j>0; j--){
set_pwm1_duty(j);
delay_ms(100);
}
}
} |
|
|
|
|
|
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
|