資源描述:
《智能小車_走黑線_避障_物體跟隨_紅外遙控_C程序》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在教育資源-天天文庫(kù)。
1、#include"reg52.h"#include/*接線定義*/sbitIN1=P1^5;sbitIN2=P1^6;sbitIN3=P1^1;sbitIN4=P1^0;sbitEN1=P1^3;sbitEN2=P1^4;/*傳感器接線定義*/sbitLeft_InSen=P2^4;sbitRight_InSen=P2^0;sbitbleft=P2^3;sbitbright=P2^1;sbitBUZZ=P1^7;/*遙控器接線定義*/sbitb=P0^7;//對(duì)應(yīng)D0Bsbitd=P0^6;//對(duì)應(yīng)D1Dsbita=P0^5;/
2、/對(duì)應(yīng)D2Asbitc=P0^4;//對(duì)應(yīng)D3C/*按鍵功能定義*/sbitk2=P3^0;//走黑線sbitk3=P3^1;//避障sbitk4=P3^2;//物體跟隨sbitk5=P3^3;//遙控器/*標(biāo)志位定義*/bitflag_heixian=0;bitflag_bizhang=0;bitflag_gensui=0;bitflag_yaokong=0;voiddelay(unsignedintn){unsignedchari,j,k;for(k=0;k<=n;k++){_nop_();_nop_();i=5;j=6;do{while(--j
3、);}while(--i);}}voidbeep(void){unsignedchari;for(i=0;i<3;i++){BUZZ=~BUZZ;delay(10);}BUZZ=1;}voidgogogo(void){IN1=1;IN2=0;IN3=1;IN4=0;}voidbackbackback(void){IN1=0;IN2=1;IN3=0;IN4=1;}voidstop(void){IN1=0;IN2=0;IN3=0;IN4=0;}voidturnleft(void){IN1=0;IN2=1;IN3=1;IN4=0;}voidturnrigh
4、t(void){IN1=1;IN2=0;IN3=0;IN4=1;}voidkeyscan(){/*走黑線按鍵設(shè)置*/if(k2==0){while(!k2);flag_heixian=1;flag_bizhang=0;flag_gensui=0;flag_yaokong=0;}/*避障按鍵設(shè)置*/if(k3==0){while(!k3);flag_heixian=0;flag_bizhang=1;flag_gensui=0;flag_yaokong=0;}/*物體跟隨按鍵設(shè)置*/if(k4==0){while(!k4);flag_heixian=0;
5、flag_bizhang=0;flag_gensui=1;flag_yaokong=0;}/*遙控器按鍵設(shè)置*/if(k5==0){while(!k5);flag_heixian=0;flag_bizhang=0;flag_gensui=0;flag_yaokong=1;}}voidmain(void){flag_heixian=0;flag_bizhang=0;flag_gensui=0;flag_yaokong=0;while(1){keyscan();//按鍵掃描/*走黑線程序設(shè)計(jì)*/if(flag_heixian){delay(1);if(L
6、eft_InSen==1&&Right_InSen==0){turnright();delay(10-4);stop();delay(4);}elseif(Left_InSen==0&&Right_InSen==1){turnleft();delay(10-4);stop();delay(4);}else{gogogo();delay(10-7);stop();delay(7);}}/*避障程序設(shè)計(jì)*/if(flag_bizhang){delay(1);if(bleft==0&&bright==0)//1(滅)沒有檢測(cè)到有障礙物0(亮)檢測(cè)到?jīng)]有障礙
7、物{gogogo();delay(4);stop();delay(6);}if(bleft==1&&bright==1){BUZZ=0;backbackback();delay(4);stop();BUZZ=1;delay(6);}if(bleft==1&&bright==0){turnright();delay(2);stop();delay(8);}if(bleft==0&&bright==1){turnleft();delay(2);stop();delay(8);}}/*物體跟隨程序設(shè)計(jì)*/if(flag_gensui){delay(1);i
8、f(bleft==0&&bright==0)//1(滅)沒有檢測(cè)到有障礙物0(亮)檢測(cè)到?jīng)]有障礙物{gogo