#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit ENA=P0^5; //右側電機使能端
sbit ENB=P0^0; //左側電機使能端
sbit IN2=P0^4; //右側電機正端
sbit IN1=P0^3; //右側電機負端
sbit IN4=P0^2; //左側電機正端
sbit IN3=P0^1; //左側電機負端
sbit you1=P1^3; //循跡模塊定義
sbit you2=P1^2;
sbit zuo2=P1^1;
sbit zuo1=P1^0;
uchar speed;
uchar t;
unsigned int k=50;
unsigned int i;
void T0_init()
{
TMOD|=0x01; //設置16位定時器方式
TH0=0xD8; // 65535-55535=10000 0XD8EF=55535
TL0=0xEF;
ET0=1;
EA=1;
TR0=1;
}
void qianjin() //前進程序
{
IN1=1;
IN2=0;
IN3=1;
IN4=0;
{
t++;
if(t>=10)
{
t=0;
}
if(t<k)
{
ENA=ENB=1;
}
else if(t>=k)
{
ENA=ENB=0;
}
}
}
void houtui() //后退程序
{
IN1=0;
IN2=1;
IN3=0;
IN4=1;
}
void tingzhi() //停止程序
{
IN1=0;
IN2=0;
IN3=0;
IN4=0;
}
void zuozhuan() //da左轉程序
{
IN1=0;
IN2=1;
IN3=1;
IN4=0;
{
t++;
if(t>=10)
{
t=0;
}
if(t<k)
{
ENA=ENB=1;
}
else if(t>=k)
{
ENA=ENB=0;
}
}
}
void xiaozuozhuan() //xiao左轉程序
{
IN1=0;
IN2=0;
IN3=1;
IN4=0;
}
void youzhuan() //da右轉程序
{
IN1=1;
IN2=0;
IN3=0;
IN4=1;
{
t++;
if(t>=10)
{
t=0;
}
if(t<k)
{
ENA=ENB=1;
}
else if(t>=k)
{
ENA=ENB=0;
}
}
}
void xiaoyouzhuan() //xiao右轉程序
{
IN1=1;
IN2=0;
IN3=0;
IN4=0;
}
void delay_nus(unsigned int i) //延時:i>=12 ,i的最小延時單12 us
{
i=i/14;
while(--i);
}
void delay_nms(unsigned int n) //延時n ms
{
n=n+1;
while(--n)
delay_nus(10); //延時 1ms,同時進行補償
}
void ControlCar(unsigned char ConType) //定義電機控制子程序
{
tingzhi();
switch(ConType) //判斷用戶設定電機形式
{
case 1: //判斷用戶是否選擇形式1
{
qianjin(); //前進
break;
}
case 2: //判斷用戶是否選擇形式2
{
houtui(); //后退
break;
}
case 3: //判斷用戶是否選擇形式3
{
zuozhuan(); //da左轉
break;
}
case 4:
{
xiaozuozhuan(); //xiao左轉
break;
}
case 5: //判斷用戶是否選擇形式4
{
youzhuan(); //da右轉
break;
}
case 6: //判斷用戶是否選擇形式4
{
xiaoyouzhuan(); //xiao右轉
break;
}
case 8: //判斷用戶是否選擇形式8
{
tingzhi(); //停止
break; //退出當前選擇
}
}
}
void main() //主程序入口
{
bit RunFlag=1; //定義小車運行標志位
//Ru nShow=0; //初始化顯示狀態
ControlCar(8); //初始化小車運行狀態
T0_init();
// speed=8;
while(1) //程序主循環
{
Start:
if(zuo1==0&&zuo2==0&&you1==0&&you2==0) //四個循跡都沒有檢測到
{
ControlCar(1);
delay_nms (5);
goto NextRun;
}
if(zuo1==1&&zuo2==1&&you1==1&&you2==1) //四個循跡都檢測到
{
ControlCar(1);
delay_nms (5);
goto NextRun;
}
if(zuo1==1&&zuo2==1&&you2==1&&you1==0) //da zuo
{
ControlCar(3);
delay_nms (5);
goto NextRun;
}
if(you1==1&&you2==1&&zuo2==1&&zuo1==0) //da you
{
ControlCar(5);
delay_nms (5);
goto NextRun;
}
if(you1==1&&you2==1&&zuo2==0&&zuo1==1)
{
ControlCar(8);
delay_nms (5);
goto NextRun;
}
if(you1==1&&you2==0&&zuo2==1&&zuo1==1)
{
ControlCar(8);
delay_nms (5);
goto NextRun;
}
if(you1==1&&zuo1==1&&you2==0&&zuo2==0)//liang bian jian ce
{
ControlCar(8);
delay_nms (5);
goto NextRun;
}
if(zuo2==1&&you2==1&&you1==0&&zuo1==0) //中間兩個循跡都檢測到
{
ControlCar(4); //xiao zuo
delay_nms (5);
goto NextRun;
}
if(you1==1&&you2==1&&zuo2==0&&zuo1==1) //you bian liang ge jian ce
{
ControlCar(5);
delay_nms (5);
goto NextRun;
}
if(zuo1==1&&zuo2==1&&you1==0&&you2==0) //zuo bian liang ge jian ce
{
ControlCar(3);
delay_nms (5);
goto NextRun;
}
if(zuo1==1&&zuo2==0&&you1==0&&you2==0) //zuo 1 jian ce
{
ControlCar(4);
delay_nms (5);
goto NextRun;
}
if(zuo1==0&&zuo2==0&&you1==1&&you2==0) //you1 jian ce
{
ControlCar(6);
delay_nms (5);
goto NextRun;
}
goto Start;
NextRun:
ControlCar(1);
}
}
void Timer0() interrupt 1
{
t++;
if(t>=10)
{
t=0;
}
if(t<speed)
{
ENA=ENB=1;
}
else if(t>=speed)
{
ENA=ENB=0;
}
}
|