#include<reg52.h>
#define uchar unsigned char
#define uint usigned int
//???????????
#define zuo_qian IN1 = 1; IN2 = 0
#define zuo_hou IN1 = 0; IN2 = 1
#define you_qian IN3 = 0; IN4 = 1
#define you_hou IN3 = 1; IN4 = 0
//????????λ??
sbit zuo = P0^1;
sbit zhong = P0^2;
sbit you = P0^3;
//????in1~4,????io??
sbit IN1 = P1^1;
sbit IN2 = P1^2;
sbit IN3 = P1^3;
sbit IN4 = P1^4;
sbit ENA = P1^5;
sbit ENB = P1^6;
sbit led = P1^0;
//????????
uchar zkb_zuo = 0;
uchar zkb_you = 0;
uchar t = 0;
void timer()
{
TMOD = 0x01; //????????λ????????
TH0 = (65535-100)/256;
TH0 = (65535-100)%256;
ET0 = 1;
TR0 = 1;
}
//??pwm???ε??????100???ж??????ж?100us?????pwm??????????????б???zkb_????
void timer0() interrupt 1
{
TH0 = (65535-100)/256;//?????趨???
TH0 = (65535-100)%256;
if(t<zkb_zuo)//?????????t????????????????????
ENA = 1;
else
ENA = 0;//?????1/100
if(t<zkb_you)//?????????t????????????????????
ENB = 1;
else
ENB = 0;//?????1/100
t++;
if(t>=100)
t = 0;
}
/*//???????????????????????????????????????
??1 ??1 ??1
0 1 1 ????? ?????
0 0 1 ????? С????
1 0 1 ???
1 0 0 ????? С????
1 1 0 ????? ?????
*/
void xunji()
{
if((zuo==1)&&(zhong==0))
{
zkb_zuo = 100;
zkb_you = 100;
zuo_hou;
you_qian;
led = 0;
}
else if((zuo==1)&&(zhong==1))
{
zkb_zuo = 40;
zkb_you = 100;
zuo_qian;
you_qian;
led = 1;
}
/* if((zuo==0)&&(zhong==1))
{
led = 0;
}
else if((zuo==0)&&(zhong==0))
{
led = 1;
}
else if((zuo==0)&&(zhong==1)&&(you==1))
{
zkb_zuo = 20;
zkb_you = 100;
zuo_hou;
you_qian;
}
else if((zuo==1)&&(zhong==0)&&(you==1))
{
zkb_zuo = 80;
zkb_you = 80;
zuo_qian;
you_qian;
}
else if((zuo==1)&&(zhong==0)&&(you==0))
{
zkb_zuo = 20;
zkb_you = 100;
zuo_qian;
you_hou;
}
else if((zuo==1)&&(zhong==1)&&(you==0))
{
zkb_zuo = 100;
zkb_you = 100;
zuo_qian;
you_hou;
} */
}
void main()
{
timer();//??????????
EA=1;//?????ж?
while(1)
{
xunji();
}
}
|