#include"reg51.h" #include<intrins.h> #define uchar unsigned char #define uint
unsigned int #define left_infrare 0 #define right_infrare 1 #define dj_state1
0X5F// forward #define dj_state2 0X4F // Turn right #define dj_state3 0X1F // Turn left #define
dj_state4 0X0F// back off #define dj_state5 0XfF // parking #define light_off 0x0f // Turn off the turn signal #
define left_light 0X5F // Left turn signal lamp Two yes 5f #define right_light 0XaF // Right turn signal lamp 0xaf, Two yes 0xbf
#define back_light 0XcF // Brake lights are rear lights #define front_light 0x3f // Headlights #define light_on
0xff// Turn on all lights #define true 1 #define false 0 #define LCD_Data P0 #define Busy 0x80
// For detection LCD In the status word Busy identification sbit c=P1^2; // Turn signal enabling end 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 Chip selection signal of
//sbit left_led=P0^2; // Left infrared emitting tube //sbit right_led=P0^3; // Right infrared transmitting tube sbit LCD_RS = P1^
5; //LCD Define pin sbit LCD_RW = P1^6; // sbit LCD_E = P1^7 ; void Delay5Ms(void) {
uint TempCyc= 5552; while(TempCyc--); } //400ms delayed void Delay400Ms(void) {uchar
TempCycA= 5; uint TempCycB; while(TempCycA--) { TempCycB=7269; while(TempCycB--)
; } } //LCD Read status 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); // Detect busy signal
return(LCD_Data); } //LCD Write data void WriteDataLCD(unsigned char WDLCD ) {
ReadStatusLCD(); // Detection busy LCD_Data = WDLCD; LCD_RS=1; LCD_RW =0; LCD_E = 0;
// If the crystal oscillator speed is too high, a small delay can be added after this LCD_E = 0; // delayed , For safety LCD_E = 0; // delayed LCD_E = 1; } //LCD Write instruction
void WriteCommandLCD(unsigned char WCLCD,BuysC) { if (BuysC) ReadStatusLCD();
// Detect busy as needed ,BuysC by 0 Ignore busy detection when LCD_Data = WCLCD; LCD_RS= 0; LCD_RW= 0; LCD_E = 0; // delayed
, For safety LCD_E = 0; LCD_E = 0; // delayed LCD_E = 1; } void LCDInit(void) //LCD initialization {
Delay400Ms(); LCD_Data = 0; WriteCommandLCD(0x38,0); // Triple display mode setting , Do not detect busy signal Delay5Ms(
); WriteCommandLCD(0x38,0); Delay5Ms(); WriteCommandLCD(0x38,0); Delay5Ms();
WriteCommandLCD(0x38,1); // Display mode settings , Start to require each busy signal detection WriteCommandLCD(0x08,1); // Turn off display
WriteCommandLCD(0x01,1); // Display clear screen WriteCommandLCD(0x06,1); // Display cursor movement settings
WriteCommandLCD(0x0C,1); // Display on and cursor settings } // Display a character at the specified position void DisplayOneChar(uchar X
, uchar Y, uchar DData) { Y &= 0x1; X &= 0xF; // limit X Cannot be greater than 15,Y Cannot be greater than 1 if (Y) X |=
0x40; // Address code when the second line is to be displayed +0x40; X |= 0x80; // Calculate LCD Script for WriteCommandLCD(X, 0);
// Busy signal is not detected here , Sending address code WriteDataLCD(DData); } // Display a string of characters at a specified position ( Only one line can be written ); void
DisplayListChar(uchar X, uchar Y,uchar ListLength, uchar *DData,uchar n) {
uchar i; Y &= 0x01; X &= 0x0F; // limit X Cannot be greater than 15,Y Cannot be greater than 1 for(i=0;i<ListLength;i++) { if
(X <= 0x0F) //X Coordinates should be less than 0xF { DisplayOneChar(X, Y, DData[i]); // Show single character if(n==true)
Delay400Ms(); X++; } } } /**************************** Infrared receiving subroutine , Using the falling edge trigger mode of interrupt
****************************/ void infrared_ray()interrupt 0 using 3 { uchar i=
90; flage=0x01; // Accept flag bit while(i--); // Decrease sensitivity EX0=0; // Turn off interrupt , Wait until square wave is emitted , Stay still } //
Delay subroutine void delay(uint n) { while(--n); } // Interrupt initialization void Init0(void) { EA=1; IT0=1;
} /*************************************** /* The principle is to open the interrupt and Emission square wave ,
When there is an interrupt, turn to the interrupt generation interrupt position to prepare for the next turn , When no is off interrupt ****************************************/
void seng_wave(uchar timer,bit n)//timer Time of transmitting signal through carrier ,n-> Selection of left and right transmitting tubes { uchar i; P1 |=
0X04; //ledcs=1 by 74ls573 by 11 Direct output of data when the pin is at high level , Lock data when it is low , I.e. keep IE |= 0X01; P0 |=0x0c; //04
for(i=timer;i>0;i--) { if(n)P0^=0x08; // Right transmitting tube transmits signal through carrier wave //00 else P0^=0x04; //
The left transmitting tube transmits a signal through a carrier wave //0c delay(100); // Sensitivity is controlled here ( control 38khz What is the square wave of ) And distance }
//timer*delay(x) Is the average current obtained by the transmitting tube P1 &= 0Xfb; IE &= 0Xfe; } //led Turn signal indicator subroutine void
light_control(uchar deng) { ledcs=1; P0 =deng; ledcs=0; //11111011 }
// Control part of motor and light void control(uchar n,uchar dj_state,uchar light) { uchar i; //
P1|=0x04; light_control(light); //led Direction indicator delay(100); P2 =dj_state; // Direction control of motor
WriteCommandLCD(0x01,1); //LCD Display clear screen 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; // parking
light_control(light_off); //led close WriteCommandLCD(0x01,1); //LCD Display clear screen P2=
dj_state1; // forward if(dj_state1) { P1|=0X04; //ledcs=1; P0=0x0f; P1&=0XFB; delay(
100); DisplayListChar(0,0,7,talk2,false); } }
/**************************************** Main control parts of obstacle avoidance
*****************************************/ void move_car(void) { uchar temp =
0x00; // Left infrared tube emission seng_wave(1,left_infrare); // Down is the interrupt on and off , Statement to execute if(flage==0x01){
temp|=0x01;flage=0x00;} // Right infrared tube emission delay(30); seng_wave(1,right_infrare);
// Down is the interrupt on and off , Statement to execute if(flage==0x01){temp|=0x02;flage=0x00;} // Obstacle on the left , Turn right if(temp
==0x01){control(2,dj_state2,left_light); temp =0x00;} // Obstacle on the right , Turn left else if(temp==
0x02) {control(2,dj_state3,right_light ); temp =0x00;} // Obstacles in both directions , back off , Turn right else if
(temp==0x03) {control(10,dj_state4,back_light ); control(5,dj_state2,right_light
); temp =0x00;} } void main(void) { Init0(); // Interrupt initialization P1 |= 0X04; // Control bit of unlocking latch P0 =
0xFf; // Clearing of data port P1&=0XFB; // Close the control bit of the latch LCDInit(); //LCD initialization WriteCommandLCD(0x01,1);
// Display clear screen delay(100); P2=dj_state1; DisplayListChar(0,0,8,talk2,false); while(1) {
move_car(); // Main control part delay(200000);// delayed } }
Technology