|
基于STC89C52單片機的小車實現(xiàn)
責(zé)任描述:
一、完成小車的原理設(shè)計和焊接。
二、用Keil4編寫單片機實現(xiàn)了五個功能:1.紅外線遙控前進(jìn)后退轉(zhuǎn)彎功能。2.小車地面尋跡功能。3.超聲波感應(yīng)避障功能。4.光敏感應(yīng)行走功能。 ps:最后有文檔源代碼。
下載:
源代碼.docx
(14.22 KB, 下載次數(shù): 21)
2018-2-12 09:40 上傳
點擊文件名下載附件
單片機源程序:
- #include <REGX52.H>
- #include <intrins.h>
- sbit LeftLed=P2^0;
- sbit RightLed=P0^7;
- sbit FontLled=P1^7;
- sbit LeftIR=P3^5;
- sbit RightIR=P3^6;
- sbit FontIR=P3^7;
- sbit M1A=P0^0;
- sbit M1B=P0^1;
- sbit M2A=P0^2;
- sbit M2B=P0^3;
- sbit B1=P0^4;
- sbit SB1=P0^6;
- sbit IRIN=P3^3;
- unsigned char code LedShowData[]={0x03,0x9F,0x25,0x0D,0x99,
- 0x49,0x41,0x1F,0x01,0x19};//0,1,2,3,4,5,6,7,8,9
- unsigned char code RecvData[]={0x19,0x46,0x15,0x43,0x44,0x40,0x0D,0x0E,0x00,0x0F};
- unsigned char IRCOM[7];
- static unsigned int LedFlash;
- bit EnableLight=0;
- #define ShowPort P2
- unsigned char temp = 1;
- void tingzhi()
- {
- M1A=0;
- M1B=0;
- M2A=0;
- M2B=0;
- }
- void qianjin()
- {
- M1A=1;
- M1B=0;
- M2A=1;
- M2B=0;
- }
- void houtui()
- {
- M1A=0;
- M1B=1;
- M2A=0;
- M2B=1;
- }
- void zuozhuan()
- {
- M1A=0;
- M1B=1;
- M2A=1;
- M2B=0;
- }
- void youzhuan()
- {
- M1A=1;
- M1B=0;
- M2A=0;
- M2B=1;
- }
- void Delay1ms(unsigned int i)
- {
- unsigned char j,k;
- do{
- j = 10;
- do{
- k = 50;
- do{
- _nop_();
- }while(--k);
- }while(--j);
- }while(--i);
- }
- void delay_nus(unsigned int i)
- {
- i=i/10;
- while(--i);
- }
- void delay_nms(unsigned int n)
- {
- n=n+1;
- while(--n)
- delay_nus(900);
-
- }
- void delayms(unsigned char x)
- {
- unsigned char i;
- while(x--)
- {
- for (i = 0; i<13; i++) {}
- }
- }
- void Delay()
- {
- unsigned int DelayTime=30000;
- while(DelayTime--);
- return;
- }
- void ControlCar(unsigned char ConType)
- {
-
- tingzhi();
- switch(ConType)
- {
- case 1: //前進(jìn)
- {
- //tingzhi();
- //Delay1ms(50);
- LeftLed = 0 ;
- qianjin();
- break;
- }
- case 2: //后退
- {
- // tingzhi();
- // Delay1ms(50);
- LeftLed = 1 ;
- houtui();
- break;
- }
- case 3: //左轉(zhuǎn)
- {
- //tingzhi();
- // Delay1ms(50);
- zuozhuan();
- break;
- }
- case 4: //右轉(zhuǎn)
- {
- // tingzhi();
- // Delay1ms(50);
- youzhuan();
-
- break;
- }
- case 8: //停止
- {
- tingzhi();
- break;
- }
- }
- }
- void ControlCar_yaokong(unsigned char ConType)
- {
-
- tingzhi();
- switch(ConType)
- {
- case 1: //前進(jìn)
- {
- tingzhi();
- Delay1ms(150);
- LeftLed = 0 ;
- qianjin();
- break;
- }
- case 2: //后退
- {
- tingzhi();
- Delay1ms(150);
- LeftLed = 1 ;
- houtui();
- break;
- }
- case 3: //左轉(zhuǎn)
- {
- tingzhi();
- Delay1ms(150);
- zuozhuan();
- break;
- }
- case 4: //右轉(zhuǎn)
- {
- tingzhi();
- Delay1ms(150);
- youzhuan();
- break;
- }
- case 8: //停止
- {
- tingzhi();
- break;
- }
- }
- }
- void Robot_Avoidance()
- {
- LeftLed=LeftIR;
- RightLed=RightIR;
- FontLled= FontIR;
- SB1=FontIR;
- if(FontIR == 0)
- {
- ControlCar(8);
- delay_nms (300);
- ControlCar(2);
- delay_nms (1000);
- ControlCar(3);
- delay_nms (1800);
- }
- else
- {
- ControlCar(1);
- delay_nms (10);
- }
- ControlCar(1);
- }
- //機器人循跡子程序
- void Robot_Traction()
- {
- LeftLed=LeftIR;
- RightLed=RightIR;
- FontLled= FontIR;
- SB1=LeftIR;
- if(LeftIR == 0 && RightIR == 0)
- {
- ControlCar(1);
- delay_nms (10);
- }
-
- else if(LeftIR == 0 && RightIR == 1)
- {
- ControlCar(3);
- delay_nms (10);
- }
- else if(LeftIR == 1 && RightIR == 0)
- {
- ControlCar(4);
- delay_nms (10);
- }
- ControlCar(8);
- }
- void main()
- {
-
- bit RunFlag=0;
- LedFlash=3000;
- //EX1=1;
- IT1=1;
- EA=1;
- ControlCar(8);
- while(1)
- {
- if(P3_2 == 0)
- {
- delay_nms(10);
- if(P3_2 == 0)
- {
- temp++;
- while(!P3_2);
- }
- }
- if(temp > 3)
- {
- temp = 1;
- }
- switch(temp)
- {
- case 1: ShowPort = LedShowData[1];Robot_Traction();EX1 = 0;break;
- case 2: ShowPort = LedShowData[2];Robot_Avoidance();EX1 = 0;break;
- case 3: ShowPort = LedShowData[3];EX1 = 1;break;
- }
- }
- }
- //----------紅外遙控-------------------------------------------------------------
- void IR_IN() interrupt 2 using 0
- {
- unsigned char j,k,N=0;
-
- EX1 = 0;
- delayms(15);
- if (IRIN==1)
- {
- EX1 =1;
- return;
- }
-
- while (!IRIN)
- {
- delayms(1);
- }
- for (j=0;j<4;j++)
- {
- for (k=0;k<8;k++)
- {
- while (IRIN)
- {
- delayms(1);
- }
-
- while (!IRIN)
- {
- delayms(1);
- }
-
- while (IRIN)
- {
- delayms(1);
- N++;
- if (N>=30)
- {
- EX1=1;
- return;
- }
- }
-
- IRCOM[j]=IRCOM[j] >> 1;
-
- if (N>=8)
- {
- IRCOM[j] = IRCOM[j] | 0x80;
- }
- N=0;
- }
- }
-
- if (IRCOM[2]!=~IRCOM[3])
- {
- EX1=1;
- return;
- }
- for(j=0;j<10;j++)
- {
- if(IRCOM[2]==RecvData[j])
- {
- // ControlCar(j);
- ControlCar_yaokong(j);
- }
- }
- EX1 = 1;
- }
- //-------------------------------------------------------------------------------------------------------
復(fù)制代碼
|
|