#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit YENA=P0^0;
sbit YIN1=P0^1;
sbit YIN2=P0^2;
sbit YENB=P0^5;
sbit YIN3=P0^3;
sbit YIN4=P0^4;
sbit ZENA=P2^0;
sbit ZIN1=P2^2;
sbit ZIN2=P2^1;
sbit ZENB=P2^5;
sbit ZIN3=P2^4;
sbit ZIN4=P2^3;
//******定义寻迹********
sbit Z1= P1^3;
sbit Z2= P1^1;
sbit Y1= P1^0;
sbit Y2= P1^2;
uchar count_data,count_flag,temp_data,biaozhi;
uchar shuju[3]=0;
uchar nZQSD,nZHSD,nYQSD,nYHSD;
uchar ZQSD=0,ZHSD=0,YQSD=0,YHSD=0,PWM=100,xunjibiaozhi=0;
//*********初始化函数**********
void chushihua()
{
P0=0xff;P1=0xff;
P2=0xff;P3=0xff;
TMOD=0x21; //设置定时器的工作方式
TH0=(65536-200)/256;
TL0=(65536-200)%256;
TL1 = 0xFd; //设定定时初值
TH1 = 0xfd; //设定定时初值
SCON=0x50;
PCON=0x00;
TR0=1;
TR1=1;
ET0=1;
ES=1;
EA=1;
}
void fun_back()
{
ZQSD = 60;ZHSD = 60;
YQSD = 80;YHSD = 80;
ZIN1=0;ZIN2=1;
ZIN3=0;ZIN4=1;
YIN1=0;YIN2=1;
YIN3=0;YIN4=1;
}
void fun_ahead()
{
ZQSD = 100;ZHSD = 100;
YQSD = 120;YHSD = 120;
ZIN1=1;ZIN2=0;
ZIN3=1;ZIN4=0;
YIN1=1;YIN2=0;
YIN3=1;YIN4=0;
}
void fun_ZZ(int zs,int ys)
{
ZQSD = zs;ZHSD = zs;
YQSD = ys;YHSD = ys;
ZIN1=0;ZIN2=1;
ZIN3=0;ZIN4=1;
YIN1=1;YIN2=0;
YIN3=1;YIN4=0;
}
void fun_ZXZ(int zs,int ys) //0100
{
ZQSD = zs;ZHSD = zs;
YQSD = ys;YHSD = ys;
ZIN1=1;ZIN2=1;
ZIN3=1;ZIN4=1;
YIN1=1;YIN2=0;
YIN3=1;YIN4=0;
}
void fun_ZZZ(int zs,int ys) //1100
{
ZQSD = zs;ZHSD = zs;
YQSD = ys;YHSD = ys;
ZIN1=0;ZIN2=1;
ZIN3=0;ZIN4=1;
YIN1=1;YIN2=0;
YIN3=1;YIN4=0;
}
void fun_ZDZ(int zs,int ys) //1000
{
ZQSD = zs;ZHSD = zs;
YQSD = ys;YHSD = ys;
ZIN1=0;ZIN2=1;
ZIN3=0;ZIN4=1;
YIN1=1;YIN2=0;
YIN3=1;YIN4=0;
}
void fun_ZCZ(int zs,int ys) //1110
{
ZQSD = zs;ZHSD = zs;
YQSD = ys;YHSD = ys;
ZIN1=0;ZIN2=1;
ZIN3=0;ZIN4=1;
YIN1=1;YIN2=0;
YIN3=1;YIN4=0;
}
void fun_YZ(int zs,int ys)
{
ZQSD = zs;ZHSD = zs;
YQSD = ys;YHSD = ys;
ZIN1=1;ZIN2=0;
ZIN3=1;ZIN4=0;
YIN1=0;YIN2=1;
YIN3=0;YIN4=1;
}
void fun_YXZ(int zs,int ys) //0010
{
ZQSD = zs;ZHSD = zs;
YQSD = ys;YHSD = ys;
ZIN1=1;ZIN2=0;
ZIN3=1;ZIN4=0;
YIN1=1;YIN2=1;
YIN3=1;YIN4=1;
}
void fun_YZZ(int zs,int ys) //0011
{
ZQSD = zs;ZHSD = zs;
YQSD = ys;YHSD = ys;
ZIN1=1;ZIN2=0;
ZIN3=1;ZIN4=0;
YIN1=1;YIN2=1;
YIN3=1;YIN4=1;
}
void fun_YDZ(int zs,int ys) //0001
{
ZQSD = zs;ZHSD = zs;
YQSD = ys;YHSD = ys;
ZIN1=1;ZIN2=0;
ZIN3=1;ZIN4=0;
YIN1=0;YIN2=1;
YIN3=0;YIN4=1;
}
void fun_YCZ(int zs,int ys) //0111
{
ZQSD = zs;ZHSD = zs;
YQSD = ys;YHSD = ys;
ZIN1=1;ZIN2=0;
ZIN3=1;ZIN4=0;
YIN1=0;YIN2=1;
YIN3=0;YIN4=1;
}
void fun_stop()
{
ZQSD = 100;ZHSD = 100;
YQSD = 100;YHSD = 100;
ZIN1=1;ZIN2=1;
ZIN3=1;ZIN4=1;
YIN1=1;YIN2=1;
YIN3=1;YIN4=1;
}
void fun_xunji()
{
if (Z1 == 1 && Z2 == 1 && Y2 == 1 && Y1 == 1)
{
fun_ahead();
if (Z1 == 0 && Z2 == 0 && Y2 == 0 && Y1 == 0)
{
fun_stop();
biaozhi=0;
}
}
if (Z1 == 0 && Z2 == 1 && Y2 == 1 && Y1 == 0)
{
fun_ahead();
}
if (Z1 == 0 && Z2 == 1 && Y2 == 0 && Y1 == 0)
{
fun_ZXZ(55,60);
}
if (Z1 == 1 && Z2 == 1 && Y2 == 0 && Y1 == 0)
{
fun_ZZZ(75,80);
}
if (Z1 == 1 && Z2 == 0 && Y2 == 0 && Y1 == 0)
{
fun_ZDZ(80,80);
}
if (Z1 == 1 && Z2 == 1 && Y2 == 1 && Y1 == 0)
{
fun_ZCZ(90,100);
}
if (Z1 == 0 && Z2 == 0 && Y2 == 1 && Y1 == 0)
{
fun_YXZ(60,55);
}
if (Z1 == 0 && Z2 == 0 && Y2 == 1 && Y1 == 1)
{
fun_YZZ(80,75);
}
if (Z1 == 0 && Z2 == 0 && Y2 == 0 && Y1 == 1)
{
fun_YDZ(80,80);
}
if (Z1 == 0 && Z2 == 1 && Y2 == 1 && Y1 == 1)
{
fun_YCZ(100,100);
}
}
//************蓝牙***********
void fun_lanya(void)
{
switch (shuju[2])
{
case 'A': fun_ahead(); break;
case 'B': fun_back(); break;
case 'C': fun_ZZ(80,80); break;
case 'D': fun_YZ(80,80); break;
case 'F': fun_stop(); break;
case '9': xunjibiaozhi=1;
break;
case '7': xunjibiaozhi=0;
biaozhi=1;
fun_stop();
break;
}
}
//***************主函数*************
void main(void)
{
chushihua();
while(1)
{
if(biaozhi==1)
{
biaozhi=0;
fun_lanya();
}
if(xunjibiaozhi==1)
{
fun_xunji();
}
}
}
//***********串口中断************
void chuankou(void) interrupt 4
{
uchar temp_data;
if(RI)
{
RI=0;
temp_data=SBUF;
if(temp_data=='O'&&count_data==0)
{
count_flag=1;
}
else
{
if(count_flag==1)
{
count_data++;
shuju[count_data]=temp_data;
if(count_data>=2)
{
count_data=0;
count_flag=0;
biaozhi=1 ;
}
}
}
}
}
//********定时器,PWM*******
void dingshiqi() interrupt 1
{
TH0=(65536-200)/256;
TL0=(65536-200)%256;
nZQSD++;
nZHSD++;
nYQSD++;
nYHSD++;
if(nZQSD<=ZQSD)
{
ZENA=1;
}
else
{
ZENA=0;
}
if(nZQSD>=PWM)
{
nZQSD=0;
}
nZHSD++;
if(nZHSD<=ZHSD)
{
ZENB=1;
}
else
{
ZENB=0;
}
if(nZHSD>=PWM)
{
nZHSD=0;
}
nYQSD++;
if(nYQSD<=YQSD)
{
YENA=1;
}
else
{
YENA=0;
}
if(nYQSD>=PWM)
{
nYQSD=0;
}
nYHSD++;
if(nYHSD<=YHSD)
{
YENB=1;
}
else
{
YENB=0;
}
if(nYHSD>=PWM)
{
nYHSD=0;
}
nYHSD++;
}
这是代码,求帮助
|