程序注釋解釋清楚明了
單片機源程序如下:
- //??????????C51??
- #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 //??
- #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;
- }
- //LCD???
- void WriteCommandLCD(unsigned char WCLCD,BuysC)
- {
- if (BuysC) ReadStatusLCD(); //???????,BuysC?0??????
- LCD_Data = WCLCD;
- LCD_RS= 0;
- LCD_RW= 0;
- LCD_E = 0; //?? ,????
- LCD_E = 0;
- LCD_E = 0; //??
- LCD_E = 1;
- }
- void LCDInit(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); //????
- WriteCommandLCD(0x06,1); // ????????
- WriteCommandLCD(0x0C,1); // ????????
- }
- //???????????
- void DisplayOneChar(uchar X, uchar Y, uchar DData)
- {
- Y &= 0x1;
- X &= 0xF; //??X????15,Y????1
- if (Y)
- X |= 0x40; //???????????+0x40;
- X |= 0x80; // ??LCD????
- WriteCommandLCD(X, 0); //????????,?????
- WriteDataLCD(DData);
- }
- //???????????(?????);
- void DisplayListChar(uchar X, uchar Y,uchar ListLength, uchar *DData,uchar n)
- { uchar i;
- Y &= 0x01;
- X &= 0x0F; //??X????15,Y????1
- for(i=0;i<ListLength;i++)
- { if (X <= 0x0F) //X?????0xF
- { 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)//timer???????????,n->????????
- { uchar i;
- P1 |= 0X04; //ledcs=1?74ls573?11????????????,?????????,???
- IE |= 0X01;
- P0 |=0x0c; //04
- for(i=timer;i>0;i--)
- { if(n)P0^=0x08; // ????????????//00
- else P0^=0x04; // ????????????//0c
- delay(100); //????????(??38khz??????)???
- } //timer*delay(x)????????????
- P1 &= 0Xfb;
- IE &= 0Xfe;
- }
- //led????????
- void light_control(uchar deng)
- { ledcs=1;
- P0 =deng;
- ledcs=0; //11111011
- }
- //??????????
- void control(uchar n,uchar dj_state,uchar light)
- { uchar i;
- // P1|=0x04;
- light_control(light); //led?????
- delay(100);
- P2 =dj_state; //???????
- WriteCommandLCD(0x01,1); //LCD????
- 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); //led??
- WriteCommandLCD(0x01,1); //LCD????
- P2=dj_state1; //??
- if(dj_state1)
- { P1|=0X04; //ledcs=1;
- 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(); //LCD???
- WriteCommandLCD(0x01,1); //????
- delay(100);
- P2=dj_state1;
- DisplayListChar(0,0,8,talk2,false);
- while(1)
- { move_car(); //??????
- delay(200000);//??
- }
- }
復制代碼
所有資料51hei提供下載:
自動避障紅外電動小車C51程序.rar
(2.81 KB, 下載次數: 7)
2018-11-9 15:40 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|