自动避障红外电动小车C51程序 #include"reg51.h" #include #define uchar unsigned char #define uint unsigned int #define left_infrare 0 #define right_infrare 1 #define dj_state1 0X5F //前进 #define dj_state2 0X4F //右转 #define dj_state3 0X1F //左转 #define dj_state4 0X0F //后退 #define dj_state5 0XfF //停车 #define light_off 0x0f //关转向灯 #define left_light 0X5F //左转向灯 两个是5f #define right_light 0XaF //右转向灯0xaf,两个是0xbf #define back_light 0XcF //刹车灯即后灯 #define front_light 0x3f //前灯 #define light_on 0xff //开所有灯 #define true 1 #define false 0 #define LCD_Data P0 #define Busy 0x80 //用于检测LCD状态字中的Busy标识 sbit c=P1^2; //转向灯使能端 uchar code talk1[]={"backward"}; uchar code talk2[]={"forward"}; uchar code talk3[]={"Turnleft"}; uchar code talk4[]={"Turn right"}; uchar flage =0x00; sbit ledcs=P1^2; //74H573的片选信号 //sbit left_led=P0^2; //左红外发射管 //sbit right_led=P0^3; //右红外发射管 sbit LCD_RS = P1^5; //LCD定义引脚 sbit LCD_RW = P1^6; // sbit LCD_E = P1^7 ; void Delay5Ms(void) { uint TempCyc = 5552; while(TempCyc--); } //400ms延时 void Delay400Ms(void) {uchar TempCycA = 5; uint TempCycB; while(TempCycA--) { TempCycB=7269; while(TempCycB--); } } //LCD读状态 unsigned char ReadStatusLCD(void) { LCD_Data = 0xFF; LCD_RS = 0; LCD_RW = 1; LCD_E = 0; LCD_E = 0; LCD_E = 1; while (LCD_Data & Busy); //检测忙信号 return(LCD_Data); } //LCD写数据 void WriteDataLCD(unsigned char WDLCD ) { ReadStatusLCD(); //检测忙 LCD_Data = WDLCD; LCD_RS=1; LCD_RW =0; LCD_E = 0; //若晶振速度太高可以在这后加小的延时 LCD_E = 0; //延时 ,为了安全 LCD_E = 0; //延时 LCD_E = 1; }