#include
#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; 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; TR0=(65536-1000)%6; 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; } 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=0; //右电机 port_4=0; } }
void infraredscan() { if(!infrared1 && !infrared2 && !infrared3) { motor(4); } if(!infrared1 && !infrared2 && infrared3) { // if(num <= 60) motor(1); // else // motor(4); } if(!infrared1 && infrared2 && !infrared3) { // if(num <= 60) 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) { infraredscan(); }
}
void T0_time() interrupt 1 { TH0=(65536-10000)/256; TR0=(65536-10000)%6; num++; if(num == 100) { num = 0; } }