资源描述:
《自动避障红外电动小车C51程序复习进程.doc》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、自动避障红外电动小车C51程序自动避障红外电动小车C51程序#include"reg51.h"#include#defineucharunsignedchar#defineuintunsignedint#defineleft_infrare0#defineright_infrare1#definedj_state10X5F//前进#definedj_state20X4F//右转#definedj_state30X1F//左转#definedj_state40X0F//后退#definedj_state50XfF//停车#definelight_off0x0f
2、//关转向灯#defineleft_light0X5F//左转向灯两个是5f#defineright_light0XaF//右转向灯0xaf,两个是0xbf#defineback_light0XcF//刹车灯即后灯#definefront_light0x3f//前灯#definelight_on0xff//开所有灯#definetrue1#definefalse0#defineLCD_DataP0#defineBusy0x80//用于检测LCD状态字中的Busy标识sbitc=P1^2;//转向灯使能端ucharcodetalk1[]={"backward"};ucharcode
3、talk2[]={"forward"};ucharcodetalk3[]={"Turnleft"};ucharcodetalk4[]={"Turnright"};ucharflage=0x00;sbitledcs=P1^2;//74H573的片选信号//sbitleft_led=P0^2;//左红外发射管//sbitright_led=P0^3;//右红外发射管sbitLCD_RS=P1^5;//LCD定义引脚sbitLCD_RW=P1^6;//sbitLCD_E=P1^7;voidDelay5Ms(void){uintTempCyc=5552;while(TempCyc--);
4、}//400ms延时voidDelay400Ms(void){ucharTempCycA=5;uintTempCycB;while(TempCycA--){TempCycB=7269;while(TempCycB--);}}//LCD读状态unsignedcharReadStatusLCD(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写数据voidWriteDataLCD(unsignedcharWD
5、LCD){ReadStatusLCD();//检测忙LCD_Data=WDLCD;LCD_RS=1;LCD_RW=0;LCD_E=0;//若晶振速度太高可以在这后加小的延时LCD_E=0;//延时,为了安全LCD_E=0;//延时LCD_E=1;}//LCD写指令voidWriteCommandLCD(unsignedcharWCLCD,BuysC){if(BuysC)ReadStatusLCD();//根据需要检测忙,BuysC为0时忽略忙检测LCD_Data=WCLCD;LCD_RS=0;LCD_RW=0;LCD_E=0;//延时,为了安全LCD_E=0;LCD_E=0;//
6、延时LCD_E=1;}voidLCDInit(void)//LCD初始化{Delay400Ms();LCD_Data=0;WriteCommandLCD(0x38,0);//三次显示模式设置,不检测忙信号Delay5Ms();WriteCommandLCD(0x38,0);Delay5Ms();WriteCommandLCD(0x38,0);Delay5Ms();WriteCommandLCD(0x38,1);//显示模式设置,开始要求每次检测忙信号WriteCommandLCD(0x08,1);//关闭显示WriteCommandLCD(0x01,1);//显示清屏WriteC
7、ommandLCD(0x06,1);//显示光标移动设置WriteCommandLCD(0x0C,1);//显示开及光标设置}//按指定位置显示一个字符voidDisplayOneChar(ucharX,ucharY,ucharDData){Y&=0x1;X&=0xF;//限制X不能大于15,Y不能大于1if(Y)X
8、=0x40;//当要显示第二行时地址码+0x40;X
9、=0x80;//算出LCD的指令码WriteCommandLCD(X,0);//这里不检测忙信号,发送地址码W