PROBLEM WITH PWM GENERATION IN 89C2051.

Status
Not open for further replies.

usmani

Newbie level 4
Joined
Oct 1, 2009
Messages
5
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
PaKistan
Activity points
1,331
please check and correct the code.
i am using 89c2051 microcontroller. normally at pin p3.2 and p3.3 signal is high (4.8v) i what to generate pwm signal when signal goes low on p3.2, and when signal goes low on p3.3 decreasing pwm should be generated at p1.6.







#include <reg51.h>
#include <intrins.h>

sbit PWM_OUT=P1^6;
sbit GREEN_SIG=P3^2;
sbit RED_SIG=P3^3;

unsigned char duty_cycle=1;
#define PULSES_LIMIT 1000
#define ON 1
#define OFF 0

//Delay Function

void Delay5Us(unsigned char delay)
{
while(delay)
{
delay--;
_nop_();
}
}//DelayUs

void RunCar()
{

unsigned int pulse_count=0;
duty_cycle=20;
while(GREEN_SIG=0)
// while(0)
{
PWM_OUT=MOTOR_ON;
if(duty_cycle<60)
{
if(pulse_count==PULSES_LIMIT)
{
duty_cycle++;
pulse_count=0;
}
pulse_count++;
Delay5Us(duty_cycle*10);
PWM_OUT=MOTOR_OFF;
Delay5Us((20-duty_cycle)*10);
}
}
}//RunCar

void StopCar()
{

PWM_OUT=0;

}//StopCar

void main()
{
PWM_OUT=OFF;
while(1)
{
if (Green_Sig==0)

{
RunCar();
}//iff
else if(RED_SIG==0)
{
StopCar();
}//else if

}//while

}//main
 

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…