help in PWM change Code

Status
Not open for further replies.

ahmelsayed

Newbie level 5
Joined
Feb 25, 2011
Messages
9
Helped
6
Reputation
12
Reaction score
6
Trophy points
1,283
Activity points
1,343
hi every one
=============
I wrote this program sothat i can change pulse-width but i think there is an error as it doesn't work as it should.
i'm using Microc pro Compiler .
so can anyone help me.
===================================
here is the code:
Code:
sbit speed_I at RB0_bit;
sbit speed_D at RB1_bit;
sbit speed_C at RB2_bit;

unsigned short A, const_duty;

#define delay delay_ms(50)

void Pic_Init(){
                trisb=255;
                trisd=0;
                portd=0;
                trisc=0;
                portc=0;
                pwm1_init(10000);
                }

void main() {
              Pic_Init();
              const_duty=127;
              PWM1_Start();
              current_duty=0;
              pwm1_set_duty(A);
              while(1){
                        if(speed_I==1){
                                       delay;
                                       A++;
                                       pwm1_set_duty(A);
                                       if(current_duty==240){
                                                             A=240;
                                                             pwm1_set_duty(A);
                                                             }
                                       }
                        if(speed_D==1){
                                       delay;
                                       A--;
                                       pwm1_set_duty(A);
                                       if(current_duty==16){
                                                             A=16;
                                                             pwm1_set_duty(A);
                                                             }


                                       }
                        if(speed_C==1){
                                       delay;
                                       pwm1_set_duty(const_duty);
                                       }
                        delay_ms(5);
                        }
 

hi!

try this:


sbit speed_I at RB0_bit;
sbit speed_D at RB1_bit;
sbit speed_C at RB2_bit;

unsigned short A, const_duty;

#define delay delay_ms(50)

void Pic_Init(){
trisb=255;
trisd=0;
portd=0;
trisc=0;
portc=0;
pwm1_init(10000);
}

void main() {
Pic_Init();
const_duty=127;
PWM1_Start();
current_duty=0;
pwm1_set_duty(current_duty);
while(1){
if(speed_I==1){
delay;
current_duty++;
pwm1_set_duty(current_duty);
if(current_duty==241){
current_duty=240;
pwm1_set_duty(current_duty);
}
}
if(speed_D==1){
delay;
current_duty--;
pwm1_set_duty(current_duty);
if(current_duty==15){
current_duty=16;
pwm1_set_duty(current_duty);
}


}
if(speed_C==1){
delay;
pwm1_set_duty(const_duty);
}
delay_ms(5);
}
 
thanks pmar_kpj for help

the program now run very well.

but if i want to increase the period at which the pulse-width change since i press the increase button, i.e. when i press pushbutton the pulse width become full after short time so if i want to increase this time i have to increase delay time of ( delay; ) . Is that right?
 

hi!

if change the "#define delay delay_ms(50)" with "#define delay delay_ms(500)" you increase the period with 2 units per seconds! now with delay_ms(50) you increase with 20 units!
 
Last edited:
Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…