#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
#define right_light 0XaF
#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
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;
sbit LCD_RS = P1^5;
sbit LCD_RW = P1^6;
sbit LCD_E = P1^7 ;
void Delay5Ms(void)
{
uint TempCyc = 5552;
while(TempCyc--);
}
void Delay400Ms(void)
{
uchar TempCycA = 5;
uint TempCycB;
while(TempCycA--)
{
TempCycB=7269;
while(TempCycB--);
}
}
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);
}
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;
}
void WriteCommandLCD(unsigned char WCLCD,BuysC)
{
if (BuysC) ReadStatusLCD();
LCD_Data = WCLCD;
LCD_RS= 0;
LCD_RW= 0;
LCD_E = 0;
LCD_E = 0;
LCD_E = 0;
LCD_E = 1;
}
void LCDInit(void)
{
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);
WriteCommandLCD(0x06,1);
WriteCommandLCD(0x0C,1);
}
void DisplayOneChar(uchar X, uchar Y, uchar DData)
{
Y &= 0x1;
X &= 0xF;
if (Y)
X |= 0x40;
X |= 0x80;
WriteCommandLCD(X, 0);
WriteDataLCD(DData);
}
void DisplayListChar(uchar X, uchar Y,uchar ListLength, uchar *DData,uchar n)
{
uchar i;
Y &= 0x01;
X &= 0x0F;
for(i=0;i<ListLength;i++)
{
if (X <= 0x0F)
{
DisplayOneChar(X, Y, DData[i]);
if(n==true)Delay400Ms();
X++;
}
}
}
void infrared_ray()interrupt 0 using 3
{
uchar i=90;
flage=0x01;
while(i--);
EX0=0;
}
void delay(uint n)
{
while(--n);
}
void Init0(void)
{
EA=1;
IT0=1;
}
void seng_wave(uchar timer,bit n)
{
uchar i;
P1 |= 0X04;
IE |= 0X01;
P0 |=0x0c;
for(i=timer;i>0;i--)
{
if(n)P0^=0x08;
else P0^=0x04;
delay(100);
}
P1 &= 0Xfb;
IE &= 0Xfe;
}
void light_control(uchar deng)
{
ledcs=1;
P0 =deng;
ledcs=0;
}
void control(uchar n,uchar dj_state,uchar light)
{
uchar i;
light_control(light);
delay(100);
P2 =dj_state;
WriteCommandLCD(0x01,1);
switch(dj_state)
{
case dj_state2 :{
DisplayListChar(3,1,10,talk4,false);}break;
case dj_state3: {
DisplayListChar(3,1,8,talk3,false);}break;
case dj_state4: {
DisplayListChar(3,1,7,talk1,false); }break;
default :break;
}
for(i=n;i>0;i--)
{
delay(2000);}
P2=dj_state5;
light_control(light_off);
WriteCommandLCD(0x01,1);
P2=dj_state1;
if(dj_state1)
{
P1|=0X04;
P0=0x0f;
P1&=0XFB;
delay(100);
DisplayListChar(0,0,7,talk2,false);
}
}
void move_car(void)
{
uchar temp =0x00;
seng_wave(1,left_infrare);
if(flage==0x01){
temp|=0x01;flage=0x00;}
delay(30);
seng_wave(1,right_infrare);
if(flage==0x01){
temp|=0x02;flage=0x00;}
if(temp==0x01){
control(2,dj_state2,left_light); temp =0x00;}
else if(temp==0x02) {
control(2,dj_state3,right_light ); temp =0x00;}
else if(temp==0x03) {
control(10,dj_state4,back_light );
control(5,dj_state2,right_light ); temp =0x00;}
}
void main(void)
{
Init0();
P1 |= 0X04;
P0 = 0xFf;
P1&=0XFB;
LCDInit();
WriteCommandLCD(0x01,1);
delay(100);
P2=dj_state1;
DisplayListChar(0,0,8,talk2,false);
while(1)
{
move_car();
delay(200000);
}
}