单片机的循迹小车程序
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
TR0=(65536-1000)%256;
EA=1;
ET0=1;
TR0=1;
en_1 = 1;
en_2 = 1;
}
void motor(uchar pesition)
{
if(pesition == 0)
{
port_1=0; //左电机
port_2=1;
port_3=0; //右电机
port_4=1;
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit port_1=P3^5;
sbit port_2=P3^4;
sbit port_3=P3^1;
sbit port_4=P3^0;
sbit en_1=P3^2;
motor(0);
//else
//motor(4);
}
if(!infrared1 && infrared2 && infrared3)
{
//if(num <= 60)
motor(1);
//else
//motor(4);
}
if(infrared1 && !infrared2 && !infrared3)
{
//if(num <= 60)
motor(2);
//else
//motor(4);
}
if(infrared1 && !infrared2 && infrared3)
{
//if(num <= 60)
motor(4);
//else
//motor(4);
}
if(infrared1 && infrared2 && !infrared3)
{
//if(num <= 60)
motor(2);
//else
//motor(4);
}
if(infrared1 && infrared2 && infrared3)
{
//if(num <= 60)
motor(0);
//else
//motor(4);
}
}
void main()
{
init();
while(1)
sbit en_2=P3^3;
sbit infrared1 = P0^2;
sbit infrared2 = P0^3;
sbit infrared3 = P0^4;
uchar infrared,num,left,right;
void init()
{
TMOD=0x01;
TH0=(65536-1000)/256;
{
infraredscan();
}
}
void T0_time() interrupt 1
{
TH0=(65536-10000)/256;
TR0=(65536-10000)%256;
num++;
if(num == 100)
{
num = 0;
}
}
{
motor(4);
}
if(!infrared1 && !infrared2 && infrared3)
{
//if(num <= 60)
motor(1);
//else
//motor(4);
}
if(!infrared1 && infrared2 && !infrared3)
{
//if(num <= 60)
}
if(pesition == 1)
{
port_1=0; //左电机
port_2=1;
port_3=1; //右电机
port_4=0;
}
if(pesition == 2)
{
port_1=1; //左电机
port_2=0;
port_3=0; //右电机
port_4=1;
}
if(pesition == 3)
{
port_1=1; //左电机
port_2=0;
port_3=1; //右电机
port_4=0;
}
if(pesition == 4)
{
port_1=0; //左电机
port_2=0;
port_3=0Biblioteka Baidu //右电机
port_4=0;
}
}
void infraredscan()
{
if(!infrared1 && !infrared2 && !infrared3)
EA=1;
ET0=1;
TR0=1;
en_1 = 1;
en_2 = 1;
}
void motor(uchar pesition)
{
if(pesition == 0)
{
port_1=0; //左电机
port_2=1;
port_3=0; //右电机
port_4=1;
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit port_1=P3^5;
sbit port_2=P3^4;
sbit port_3=P3^1;
sbit port_4=P3^0;
sbit en_1=P3^2;
motor(0);
//else
//motor(4);
}
if(!infrared1 && infrared2 && infrared3)
{
//if(num <= 60)
motor(1);
//else
//motor(4);
}
if(infrared1 && !infrared2 && !infrared3)
{
//if(num <= 60)
motor(2);
//else
//motor(4);
}
if(infrared1 && !infrared2 && infrared3)
{
//if(num <= 60)
motor(4);
//else
//motor(4);
}
if(infrared1 && infrared2 && !infrared3)
{
//if(num <= 60)
motor(2);
//else
//motor(4);
}
if(infrared1 && infrared2 && infrared3)
{
//if(num <= 60)
motor(0);
//else
//motor(4);
}
}
void main()
{
init();
while(1)
sbit en_2=P3^3;
sbit infrared1 = P0^2;
sbit infrared2 = P0^3;
sbit infrared3 = P0^4;
uchar infrared,num,left,right;
void init()
{
TMOD=0x01;
TH0=(65536-1000)/256;
{
infraredscan();
}
}
void T0_time() interrupt 1
{
TH0=(65536-10000)/256;
TR0=(65536-10000)%256;
num++;
if(num == 100)
{
num = 0;
}
}
{
motor(4);
}
if(!infrared1 && !infrared2 && infrared3)
{
//if(num <= 60)
motor(1);
//else
//motor(4);
}
if(!infrared1 && infrared2 && !infrared3)
{
//if(num <= 60)
}
if(pesition == 1)
{
port_1=0; //左电机
port_2=1;
port_3=1; //右电机
port_4=0;
}
if(pesition == 2)
{
port_1=1; //左电机
port_2=0;
port_3=0; //右电机
port_4=1;
}
if(pesition == 3)
{
port_1=1; //左电机
port_2=0;
port_3=1; //右电机
port_4=0;
}
if(pesition == 4)
{
port_1=0; //左电机
port_2=0;
port_3=0Biblioteka Baidu //右电机
port_4=0;
}
}
void infraredscan()
{
if(!infrared1 && !infrared2 && !infrared3)