|
為了方便使用,自己改進(jìn)了一下遙控器控制智能小車程序,現(xiàn)在分享給大家。
(不同遙控器可能稍有不同,本程序使用的配套遙控器如下圖,如果你的和我不同,可以向賣家索要遙控器資料)
本程序使用的配套遙控器
單片機(jī)源程序如下:
- #include<AT89x51.H>
- sbit lnfrared_led=P3^2; //定義紅外線接收端
- #define Imax 14000 //此處為晶振為11.0592時(shí)的取值,
- #define Imin 8000 //如用其它頻率的晶振時(shí),
- #define Inum1 1450 //要改變相應(yīng)的取值。
- #define Inum2 700
- #define Inum3 3000
- /**********************電機(jī)驅(qū)動(dòng)模塊設(shè)置********************/
- #define Left_moto_go {P1_0=1,P1_1=0,P1_2=1,P1_3=0;} //左邊兩個(gè)電機(jī)向前
- #define Right_moto_go {P1_4=1,P1_5=0,P1_6=1,P1_7=0;} //右邊兩個(gè)電機(jī)向前
- #define Right_moto_back {P1_4=0,P1_5=1,P1_6=0,P1_7=1;} //右邊兩個(gè)電機(jī)向后
- #define Left_moto_back {P1_0=0,P1_1=1,P1_2=0,P1_3=1;} //左邊兩個(gè)電機(jī)向后
- #define Right_moto_Stop {P1_4=0,P1_5=0,P1_6=0,P1_7=0;} //右邊兩個(gè)電機(jī)停轉(zhuǎn)
- #define Left_moto_Stop {P1_0=0,P1_1=0,P1_2=0,P1_3=0;} //左邊兩個(gè)電機(jī)停轉(zhuǎn)
- unsigned char f=0;
- unsigned char Im[4]={0x00,0x00,0x00,0x00};
- unsigned char show[2]={0,0};
- unsigned long m,Tc;
- unsigned char IrOK;
- /***************************延時(shí)函數(shù)*********************************************/
- void delay(unsigned int k)
- {
- unsigned int x,y;
- for(x=0;x<k;x++)
- for(y=0;y<2000;y++);
- }
- /************************外部中斷解碼程序*****************************************/
- void intersvr0(void) interrupt 1 using 1
- {
- Tc=TH0*256+TL0; //提取中斷時(shí)間間隔時(shí)長
- TH0=0;
- TL0=0; //定時(shí)中斷重新置零
- if((Tc>Imin)&&(Tc<Imax))
- {
- m=0;
- f=1;
- return;
- } //找到啟始碼
- if(f==1)
- {
- if(Tc>Inum1&&Tc<Inum3)
- {
- Im[m/8]=Im[m/8]>>1|0x80; m++;
- }
- if(Tc>Inum2&&Tc<Inum1)
- {
- Im[m/8]=Im[m/8]>>1; m++; //取碼
- }
- if(m==32)
- {
- m=0;
- f=0;
- if(Im[2]==~Im[3])
- {
- IrOK=1;
- }
- else IrOK=0; //取碼完成后判斷讀碼是否正確
- }
- //準(zhǔn)備讀下一碼
- }
-
- }
- /**********************電機(jī)運(yùn)行模式設(shè)置*****************************************/
- //前速前進(jìn)
- void run(void)
- {
- Left_moto_go ; //左電機(jī)往前走
- Right_moto_go ; //右電機(jī)往前走
- }
- //前速后退
- void back(void)
- {
-
- Left_moto_back ; //左電機(jī)往前走
- Right_moto_back ; //右電機(jī)往前走
- }
- //左轉(zhuǎn)
- void leftrun(void)
- {
- Left_moto_back ; //左電機(jī)往前走
- Right_moto_go ; //右電機(jī)往前走
- }
- //右轉(zhuǎn)
- void rightrun(void)
- {
- Left_moto_go ; //左電機(jī)往前走
- Right_moto_back ; //右電機(jī)往前走
- }
- //停
- void Stop(void)
- {
- Left_moto_Stop ; //左電機(jī)往前走
- Right_moto_Stop ; //右電機(jī)往前走
- }
- /***********************主函數(shù)**********************************************/
- void main(void)
- {
-
- m=0;
- f=0;
- IT0=1;
- EX0=1;
-
- TMOD=0x11;
- TH0=0;
- TL0=0;
- TR0=1;
- EA=1;
-
- delay(100);
- while(1) /*無限循環(huán)*/
- {
- if(IrOK==1) //如果處理好后進(jìn)行紅外處理
- {
- switch(Im[2])
- {
- case 0x18: run(); //前進(jìn) 2
- ……………………
- …………限于本文篇幅 余下代碼請(qǐng)從51黑下載附件…………
復(fù)制代碼
所有資料51hei提供下載:
紅外遙控智能小車.rar
(656.55 KB, 下載次數(shù): 71)
2017-7-5 15:09 上傳
點(diǎn)擊文件名下載附件
|
評(píng)分
-
查看全部評(píng)分
|