//用于控制小车,检测六路红外
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
uint ma,mb,mc;
sbit out1 = P0^1 ; //ENA
sbit out2 = P0^2 ; //IN1
sbit out3 = P0^3 ; //IN2
sbit out4 = P0^4 ; //IN3
sbit out5 = P0^5 ; //IN4
sbit out6 = P0^6 ; //ENB
//4路红外检测黑线
sbit in0 = P1^0;
sbit in1 = P1^1;
sbit in2 = P1^2;
sbit in3 = P1^3;
sbit in4 = P1^4;
sbit in5 = P1^5;
sbit in6 = P1^6;
sbit in7 = P1^7;
//----------------------------------------
void delay(uint t) //延时程序1
{
uchar j;
while(t--) for(j=1;j>0;j--);
}
//----------------------------------------
void init()
{
TMOD = 0x20;//M1=1,M0=0 定时器1工作方式2(定时常数重装,8 位)
TH1 = 0xfd; //晶振11.0592时,波特率9600
TL1 = 0xfd; //TL1计数,溢出后,TH1值送入TL1
SCON = 0x50;//SM0=0,SM1=1,REN=1 串口工作方式1,允许接收
TR1 = 1; //启动定时器1
EA = 1; //开总中断
ES = 1; //允许串口中断
}
//----------------------------------------
void you () //右转
{
out1 = 1; out2 = 0; out3 = 0; out4 = 1; out5 = 0; out6 = 1; delay(100);
}
void zuo() //左转
{
out1 = 1; out2 = 1; out3 = 0; out4 = 0; out5 = 0; out6 = 1; delay(100);
}
void qian() //前进
{
out1 = 1; out2 = 1; out3 = 0; out4 = 1; out5 = 0; out6 = 1; delay(200);
}
void hou() //后退
{
out1 = 1; out2 = 0; out3 = 1; out4 = 0; out5 = 1; out6 = 1; delay(200);
}
void ting() //停止
{
out1 = 0; out2 = 0; out3 = 0; out4 = 0; out5 = 0; out6 = 0;
}
//----------------------------------------
void main()
{
char P1_in,P1_0;
init();
//循环
//--------------------------------------------------
while(1)
{
P1_in=P1;
P1_0=P1_in&0xff;
delay(10);
//P1_in=P1;
//P1_1=P1_in&0x0f;
//delay(10);
//if (P1_0 == P1_1) P1_2 = P1_1;
switch(P1_0)
{
case 0xf1: zuo();break;
case 0xf3: zuo();break;
case 0xf7: ting();break;
case 0xff: ting();break;
case 0xf2: zuo();break;
case 0xf6: qian();break;
case 0xfe: ting();break;
case 0xf4: you();;break;
case 0xfc: you();break;
case 0xf8: you();break;
case 0xf0: ting();break;
}
}
//--------------------------------------------------
}
void ser() interrupt 4
{
}
|