#include <REGX52.H>
#include <intrins.h>
sbit ENA=P1^2;
sbit EN1=P1^3;
sbit EN2=P1^4;
sbit ENB=P1^7;
sbit EN3=P1^5;
sbit EN4=P1^6;
sbit check2=P3^3;
sbit check3=P3^4;
sbit check4=P3^5;
void Timer0_Init(void)
{
TMOD &= 0xF0;
TMOD |= 0x01;
TL0 = 0xF6;
TH0 = 0xFF;
TF0 = 0;
TR0 = 1;
ET0 = 1;
EA = 1;
PT0 = 0;
}
void Delay130us(void) //@11.0592MHz
{
unsigned char data i, j;
i = 2;
j = 98;
do
{
while (--j);
} while (--i);
}
void Delay200us(void) //@11.0592MHz
{
unsigned char data i, j;
i = 3;
j = 35;
do
{
while (--j);
} while (--i);
}
void Delay3500us(void) //@11.0592MHz
{
unsigned char data i, j;
i = 38;
j = 163;
do
{
while (--j);
} while (--i);
}
unsigned int recive=0;
unsigned int count,a,e;//on是占空比
unsigned int on=36,M=0;
void zhizou()
{
ENA=1;ENB=1;
EN1=1;EN2=0;
EN3=1;EN4=0;
Delay130us();
}
void daoche()
{
ENA=1;ENB=1;
EN1=0;EN2=1;
EN3=0;EN4=1;
}
void turnleft()
{
ENA=1;ENB=1;
EN1=0;EN2=1;
EN3=1;EN4=0;
Delay3500us();
Delay200us();
}
void turnright()
{
ENA=1;ENB=1;
EN1=1;EN2=0;
EN3=0;EN4=1;
Delay3500us();
Delay200us();
}
void stop()
{
ENA=0;ENB=0;
}
void Uart_Init(void)
{
SCON = 0x50; //方式1 8位数据 接收地址甄别禁止 接收使能 0101 0000
PCON = 0x00; //波特率不增倍
TMOD = 0x20; //T1,8位自动重装
TH1 = 0xfd; //波特率9600
TL1 = 0xfd;
EA = 1;
ES = 1;
TR1 = 1;
}
void Uart_Routine(void) interrupt 4
{
if(RI==1)
{
RI=0;
recive= SBUF; //接收数据
}
}
void main()
{
Timer0_Init();
Uart_Init();
while(1)
{
if(recive==6)
{
while(1)
{
switch(recive)
{
case 1:
on=120;zhizou();break;
case 2:
on=120;daoche();break;
case 3:
on=60;turnleft();break;
case 4:
on=60;turnright();break;
case 5:
stop();break;
}
if(recive==0)
{
break;
}
}
}
else if(recive==0)
{
while(1)
{
if(recive==6)
{
break;
}
ENA=0;
ENB=0;
if((check2==1&&check3==1&&check4==1)||(check2==0&&check3==0&&check4==0)||(check2==0&&check3==1&&check4==0))//直走
{
on=31;zhizou();Delay200us();
}
if(check2==1&&check4==0)//左转
{
turnleft();
}
if((check2==0&&check4==1)||(check2==1&&check3==0&&check4==1))//右转
{
on=60;
turnright();
}
// if(check2=1&&check3==1&&check4==1)
// {
// e++;
// if(e==20)
// while(1)
// {
// {
// ENA=0;ENB=0;
// }
// }
// }
}
}
}
}
void Timer0_Routine() interrupt 1
{
TL0 = (65535-900)%256; //900
TH0 = (65535-900)/256;
if(count<on)
{
ENA=1;
ENB=1;
}
else
{
if(count<on+3)
{
ENA=1;
}
else
{
ENA=0;
}
ENB=0;
}
count++;
count%=200;
}
【无标题】五路红外循迹的c51小车
于 2024-07-15 11:25:07 首次发布