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
rantt4
Guest







PROBLEM WITH PWM..
PostPosted: Fri Jun 20, 2008 2:15 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Jun 20, 2008 3:01 pm     Reply with quote

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);
}
}
}
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