#include <regx52.h>
sbit IN1 = P1^0;
sbit IN2 = P1^1;
sbit IN3 = P1^2;
sbit IN4 = P1^3;
sbit OUT1 = P2^1;
sbit OUT2 = P2^2;
sbit OUT3 = P2^3;
sbit OUT4 = P2^4;
sbit ENA_1 = P3^0;
sbit ENA_2 = P3^1;
sbit ENB_1 = P3^2;
sbit ENB_2 = P3^3;
void Delay(unsigned int xms)
{
unsigned char i, j;
while(xms--)
{
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
}
}
void Timer0_Init(void) //100微秒@11.0592MHz
{
TMOD &= 0xF0; //设置定时器模式
TMOD |= 0x01; //设置定时器模式
TL0 = (65536-100)%256; //设置定时初始值
TH0 = (65536-100)/256; //设置定时初始值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
ET0 = 1;
EA = 1;
PT0 = 0;
}
/*void Timer0_Routine() interrupt 1
{
static unsigned int T0Count;
TL0 = 0x91; //设置定时初始值
TH0 = 0xFF; //设置定时初始值
T0Count++;
if(T0Count>=1000)
{
T0Count=0;
}
}
*/
unsigned int pwml,pwmr,t;//左右占空比,比较值
void Timer0_Routine() interrupt 1
{
TL0 = (65536-100)%256; //设置定时初始值
TH0 = (65536-100)/256; //设置定时初始值
t++;
if(t<pwml)
{
ENA_1 = 1;
ENA_2 = 1;
}
else
{
ENA_1 = 0;
ENA_2 = 0;
}
if(t<pwmr)
{
ENB_1 = 1;
ENB_2 = 1;
}
else
{
ENB_1 = 0;
ENB_2 = 0;
}
if(t>=100){t = 0;}
}
void Left_Motor_Forward()
{
IN1 = 1;
IN2 = 0;
}
void Right_Motor_Forward()
{
IN3 = 1;
IN4 = 0;
}
void Left_Motor_Back()
{
IN1 = 0;
IN2 = 1;
}
void Right_Motor_Back()
{
IN3 = 0;
IN4 = 1;
}
void Left_Motor_Stop()
{
IN1 = 0;
IN2 = 0;
}
void Right_Motor_Stop()
{
IN3 = 0;
IN4 = 0;
}
void Car_Go()
{Left_Motor_Forward();Right_Motor_Forward();}
void Car_Left()
{Left_Motor_Stop();Right_Motor_Forward();}
void Car_Right()
{Right_Motor_Stop();Left_Motor_Forward();}
void Car_Stop()
{Left_Motor_Stop();Right_Motor_Stop();}
void Car_Back()
{Left_Motor_Back();Right_Motor_Back();}
void Chack()
{
if(OUT1 == 0 && OUT2 == 0 && OUT3 == 0 && OUT4 == 0)
{
pwml = 36;
pwmr = 36;
Car_Go();
}
if(OUT1 == 1 && OUT2 == 1 && OUT3 == 1 && OUT4 == 1)
{
pwml = 0;
pwmr = 0;
Car_Stop();
}
if(OUT1 == 0 && OUT2 == 0 && OUT3 == 1 && OUT4 == 0)
{
pwml = 40;
pwmr = 32;
Car_Go();
}
if(OUT1 == 0 && OUT2 == 1 && OUT3 == 0 && OUT4 == 0)
{
pwml = 32;
pwmr = 40;
Car_Go();
}
if(OUT1 == 1 && OUT2 == 1 && OUT3 == 0 && OUT4 == 0)
{
Car_Stop();
Delay(20);
pwml = 30;
pwmr = 15;
Delay(20);
Car_Go();
while(1)
{
IN1 = 0;
IN2 = 1;
if(OUT3 == 1) {
IN1 = 1;
IN2 = 0;
break;}
pwml = 18;
pwmr = 18;
Car_Go();
}
}
if(OUT1 == 0 && OUT2 == 0 && OUT3 == 1 && OUT4 == 1)//you转
{
Car_Stop();
Delay(20);
pwml = 15;
pwmr = 30;
Delay(20);
Car_Go();
while(1)
{
IN3 = 0;
IN4 = 1;
if(OUT2 == 1) {
IN3 = 1;
IN4 = 0;
break;}
pwml = 25;
pwmr = 25;
Car_Go();
}
}
if(OUT1 == 0 && OUT2 == 1 && OUT3 == 1 && OUT4 == 0)
{
pwml = 16;
pwmr = 16;
Car_Go();
}
}
void main()
{
Timer0_Init();
while(1)
{
Chack();
}
}
#include <regx52.h>
sbit IN1 = P1^0;
sbit IN2 = P1^1;
sbit IN3 = P1^2;
sbit IN4 = P1^3;
sbit OUT1 = P2^1;
sbit OUT2 = P2^2;
sbit OUT3 = P2^3;
sbit OUT4 = P2^4;
sbit ENA_1 = P3^0;
sbit ENA_2 = P3^1;
sbit ENB_1 = P3^2;
sbit ENB_2 = P3^3;
void Delay(unsigned int xms)
{
unsigned char i, j;
while(xms--)
{
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
}
}
void Timer0_Init(void) //100微秒@11.0592MHz
{
TMOD &= 0xF0; //设置定时器模式
TMOD |= 0x01; //设置定时器模式
TL0 = (65536-100)%256; //设置定时初始值
TH0 = (65536-100)/256; //设置定时初始值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
ET0 = 1;
EA = 1;
PT0 = 0;
}
/*void Timer0_Routine() interrupt 1
{
static unsigned int T0Count;
TL0 = 0x91; //设置定时初始值
TH0 = 0xFF; //设置定时初始值
T0Count++;
if(T0Count>=1000)
{
T0Count=0;
}
}
*/
unsigned int pwml,pwmr,t;//左右占空比,比较值
void Timer0_Routine() interrupt 1
{
TL0 = (65536-100)%256; //设置定时初始值
TH0 = (65536-100)/256; //设置定时初始值
t++;
if(t<pwml)
{
ENA_1 = 1;
ENA_2 = 1;
}
else
{
ENA_1 = 0;
ENA_2 = 0;
}
if(t<pwmr)
{
ENB_1 = 1;
ENB_2 = 1;
}
else
{
ENB_1 = 0;
ENB_2 = 0;
}
if(t>=100){t = 0;}
}
void Left_Motor_Forward()
{
IN1 = 1;
IN2 = 0;
}
void Right_Motor_Forward()
{
IN3 = 1;
IN4 = 0;
}
void Left_Motor_Back()
{
IN1 = 0;
IN2 = 1;
}
void Right_Motor_Back()
{
IN3 = 0;
IN4 = 1;
}
void Left_Motor_Stop()
{
IN1 = 0;
IN2 = 0;
}
void Right_Motor_Stop()
{
IN3 = 0;
IN4 = 0;
}
void Car_Go()
{Left_Motor_Forward();Right_Motor_Forward();}
void Car_Left()
{Left_Motor_Stop();Right_Motor_Forward();}
void Car_Right()
{Right_Motor_Stop();Left_Motor_Forward();}
void Car_Stop()
{Left_Motor_Stop();Right_Motor_Stop();}
void Car_Back()
{Left_Motor_Back();Right_Motor_Back();}
void Chack()
{
if(OUT1 == 0 && OUT2 == 0 && OUT3 == 0 && OUT4 == 0)
{
pwml = 36;
pwmr = 36;
Car_Go();
}
if(OUT1 == 1 && OUT2 == 1 && OUT3 == 1 && OUT4 == 1)
{
pwml = 0;
pwmr = 0;
Car_Stop();
}
if(OUT1 == 0 && OUT2 == 0 && OUT3 == 1 && OUT4 == 0)
{
pwml = 40;
pwmr = 32;
Car_Go();
}
if(OUT1 == 0 && OUT2 == 1 && OUT3 == 0 && OUT4 == 0)
{
pwml = 32;
pwmr = 40;
Car_Go();
}
if(OUT1 == 1 && OUT2 == 1 && OUT3 == 0 && OUT4 == 0)
{
Car_Stop();
Delay(20);
pwml = 30;
pwmr = 15;
Delay(20);
Car_Go();
while(1)
{
IN1 = 0;
IN2 = 1;
if(OUT3 == 1) {
IN1 = 1;
IN2 = 0;
break;}
pwml = 18;
pwmr = 18;
Car_Go();
}
}
if(OUT1 == 0 && OUT2 == 0 && OUT3 == 1 && OUT4 == 1)//you转
{
Car_Stop();
Delay(20);
pwml = 15;
pwmr = 30;
Delay(20);
Car_Go();
while(1)
{
IN3 = 0;
IN4 = 1;
if(OUT2 == 1) {
IN3 = 1;
IN4 = 0;
break;}
pwml = 25;
pwmr = 25;
Car_Go();
}
}
if(OUT1 == 0 && OUT2 == 1 && OUT3 == 1 && OUT4 == 0)
{
pwml = 16;
pwmr = 16;
Car_Go();
}
}
void main()
{
Timer0_Init();
while(1)
{
Chack();
}
}