單片機(jī)遙控音樂燈閃爍等功能程序(共17頁)_第1頁
單片機(jī)遙控音樂燈閃爍等功能程序(共17頁)_第2頁
單片機(jī)遙控音樂燈閃爍等功能程序(共17頁)_第3頁
單片機(jī)遙控音樂燈閃爍等功能程序(共17頁)_第4頁
單片機(jī)遙控音樂燈閃爍等功能程序(共17頁)_第5頁
已閱讀5頁,還剩13頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、#include /包含(bohn)51單片機(jī)相關(guān)的頭文件#define uint unsigned int /重定義無符號(hào)(fho)整數(shù)類型#define uchar unsigned char /重定義無符號(hào)(fho)字符類型uchar code LedShowData=0 x03,0 x9F,0 x25,0 x0D,0 x99, /定義數(shù)碼管顯示數(shù)據(jù) 0 x49,0 x41,0 x1F,0 x01,0 x19;/0,1,2,3,4,5,6,7,8,9uchar code RecvData=0 x07,0 x0A,0 x1B,0 x1F,0 x0C,0 x0D,0 x0E,0 x00,0

2、x0F,0 x19;uchar IRCOM7; /接收數(shù)據(jù)緩沖區(qū)static unsigned int LedFlash; /定義閃動(dòng)頻率計(jì)數(shù)變量unsigned char RunFlag=0; /定義運(yùn)行標(biāo)志位/*完成基本數(shù)據(jù)變量定義*/ sbit LeftLed=P20; /定義前方左側(cè)指示燈端口sbit RightLed=P07;sbit Led=P10; /定義前方右側(cè)指示燈端口 sbit M1A=P00; /定義電機(jī)1正向端口sbit M1B=P01; /定義電機(jī)1反向端口sbit M2A=P02; /定義電機(jī)2正向端口sbit M2B=P03;sbit MUSIC=P06;sbit

3、 IRIN=P33;sbit LED1=P11;sbit LED2=P12;sbit LED3=P13;sbit LED4=P14;sbit LED5=P15;sbit LED6=P16;sbit LED7=P17;sbit LeftIR1=P31; /定義前方左側(cè)紅外探頭端口sbit RightIR1=P34; /定義前方右側(cè)紅外探頭端口sbit FontIR1=P30;#define LED P1 /定義電機(jī)2反向端口/*完成電機(jī)端口定義*/ typedef structunsigned char h;unsigned char l;FREQ_T;typedef structFREQ_T

4、* freq;unsigned char time;NOTE_T;#defineTIME_1_321#defineTIME_1_162#defineTIME_1_84#defineTIME_1_48#defineTIME_1_216#defineTIME_1_132extern void initialize_music(void);extern FREQ_T * s; void initialize_music(void); code FREQ_T LDO = 0 xf2,0 x4b; code FREQ_T LDOR= 0 xf3,0 x09; code FREQ_T LRE = 0 xf

5、3,0 xca; code FREQ_T LRER= 0 xf4,0 x75; code FREQ_T LMI = 0 xf5,0 x20; code FREQ_T LFA = 0 xf5,0 xb8; code FREQ_T LFAR= 0 xf6,0 x4e; code FREQ_T LSO = 0 xf6,0 xd9; code FREQ_T LSOR= 0 xf7,0 x5c; code FREQ_T LLA = 0 xf7,0 xda; code FREQ_T LLAR= 0 xf8,0 x4f; code FREQ_T LSI = 0 xf8,0 xbf; code FREQ_T

6、DO = 0 xf9,0 x26; code FREQ_T DOR= 0 xf9,0 x89; code FREQ_T RE = 0 xf9,0 xce; code FREQ_T RER= 0 xfa,0 x3f; code FREQ_T MI = 0 xfa,0 x92; code FREQ_T FA = 0 xfa,0 xe0; code FREQ_T FAR= 0 xfb,0 x2b; code FREQ_T SO = 0 xfb,0 x71; code FREQ_T SOR= 0 xfb,0 xb3; code FREQ_T LA = 0 xfb,0 xf1; code FREQ_T

7、LAR= 0 xfc,0 x2c; code FREQ_T SI = 0 xfc,0 x64; code FREQ_T HDO = 0 xfc,0 x97; code FREQ_T HDOR= 0 xfc,0 xc9; code FREQ_T HRE = 0 xfc,0 xf8; code FREQ_T HRER= 0 xfd,0 x24; code FREQ_T HMI = 0 xfd,0 x4d; code FREQ_T HFA = 0 xfd,0 x75; code FREQ_T HFAR= 0 xfd,0 x9a; code FREQ_T HSO = 0 xfd,0 xbd; code

8、 FREQ_T HSOR= 0 xfd,0 xde; code FREQ_T HLA = 0 xfd,0 xfd; code FREQ_T HLAR= 0 xfe,0 x1a; code FREQ_T HSI = 0 xfe,0 x36; code FREQ_T PAUSE=0,0; FREQ_T freq; code NOTE_T alice= &HMI,TIME_1_16, &HRER,TIME_1_16, &HMI,TIME_1_16, &HRER,TIME_1_16, &HMI,TIME_1_16, &SI,TIME_1_16, &HRE,TIME_1_16, &HDO,TIME_1_

9、16, &LA,TIME_1_8, &PAUSE,TIME_1_16, &DO,TIME_1_16, &MI,TIME_1_16, &LA,TIME_1_16, &SI,TIME_1_8, &PAUSE,TIME_1_16, &MI,TIME_1_16, &SOR,TIME_1_16, &SI,TIME_1_16, &HDO,TIME_1_8, &PAUSE,TIME_1_16, &MI,TIME_1_16, &HMI,TIME_1_16, &HRER,TIME_1_16, &HMI,TIME_1_16, &HRER,TIME_1_16, &HMI,TIME_1_16, &SI,TIME_1_

10、16, &HRE,TIME_1_16, &HDO,TIME_1_16, &LA,TIME_1_8, &PAUSE,TIME_1_16, &DO,TIME_1_16, &MI,TIME_1_16, &LA,TIME_1_16, &SI,TIME_1_8, &PAUSE,TIME_1_16, &MI,TIME_1_16, &HDO,TIME_1_16, &SI,TIME_1_16, &LA,TIME_1_8, &PAUSE,TIME_1_16, &SI,TIME_1_16, &HDO,TIME_1_16, &HRE,TIME_1_16, &HMI,TIME_1_16, &HMI,TIME_1_8,

11、 &SO,TIME_1_16, &HFA,TIME_1_16, &HMI,TIME_1_16, &HRE,TIME_1_16, &HRE,TIME_1_8, &FA,TIME_1_16, &HMI,TIME_1_16, &HRE,TIME_1_16, &HDO,TIME_1_16, &HDO,TIME_1_8, &MI,TIME_1_16, &HRE,TIME_1_16, &HDO,TIME_1_16, &SI,TIME_1_16, &SI,TIME_1_8, &MI,TIME_1_16, &HMI,TIME_1_16, &MI,TIME_1_16, &HMI,TIME_1_16, &MI,T

12、IME_1_16, &HMI,TIME_1_16, &HRER,TIME_1_16, &HMI,TIME_1_16, &HRER,TIME_1_16, &HMI,TIME_1_16, &HRER,TIME_1_16, &HMI,TIME_1_16, &HRER,TIME_1_16,; /定義(dngy)紅外接收端口/*完成(wn chng)紅外接收端口的定義*/#define ShowPort P2 /定義(dngy)數(shù)碼管顯示端口extern void ControlCar(uchar CarType); /聲明小車控制子程序void interrupt_timer1(void) inter

13、rupt 3 static unsigned char count=0; static unsigned char countb=0; static unsigned char i=0; unsigned char k; TH1=0X70;/20msTL1=0X00;count+;if(count=5) count=0;countb+;if(countb=alicei.time)countb=0;k=i+1;if(k = sizeof(alice)/sizeof(NOTE_T)k=0;freq.h=alicek.freq-h;freq.l=alicek.freq-l;i+;if(i=(size

14、of(alice)/sizeof(NOTE_T)i=0; void interrupt_timer0(void) interrupt 1TH0=freq.h;/60HzTL0=freq.l;if(freq.h | freq.l)MUSIC=!MUSIC; void initialize_music(void) TMOD|=0X01; /T0 16bit MODE; TMOD|=0X10; freq.h=alice0.freq-h; freq.l=alice0.freq-l; TH0=freq.h; TL0=freq.l; TF0=0; TR0=1; /start timer0 ET0=1; /

15、Timer0 interrupt enable TF1=0; TR1=1; ET1=1; EA=1; /interrupt enable void delay(unsigned char x) /0.14mS延時(shí)程序(chngx) unsigned char i; /定義(dngy)臨時(shí)變量 while(x-) /延時(shí)時(shí)間(shjin)循環(huán) for (i = 0; i13; i+) /14mS延時(shí) void Delay() /定義(dngy)延時(shí)子程序 uint DelayTime=30000; /定義(dngy)延時(shí)時(shí)間變量 while(DelayTime-); /開始進(jìn)行延時(shí)循環(huán) retu

16、rn; /子程序返回void Delay1()unsigned int DelayTime=50000; /定義可變延時(shí)函數(shù)while(DelayTime-); /定義前方左側(cè)指示燈端口 void Delay6(unsigned int DelayTime) while(DelayTime-); void Delay2()unsigned int DelayTime=2000; /定義可變延時(shí)函數(shù) while(DelayTime-);Led= Led; if(ShowPort=LedShowData3) RightLed=1;LeftLed=LeftLed; if(ShowPort=LedSh

17、owData4) LeftLed=1;RightLed=RightLed; /定義前方左側(cè)指示燈端口 return ; /延時(shí)函數(shù)進(jìn)入倒計(jì)時(shí)void Delay5() /定義機(jī)器人調(diào)轉(zhuǎn)子時(shí)間子程序 unsigned int DelayTime=20000; /定義機(jī)器人轉(zhuǎn)彎時(shí)間變量 while(DelayTime-); /機(jī)器人轉(zhuǎn)彎循環(huán) MUSIC=!MUSIC; /蜂鳴器閃響 return;void Delay7()unsigned int DelayTime=2000; /定義可變延時(shí)函數(shù) while(DelayTime-);LED1= LED1 ; if(ShowPort=LedShow

18、Data3) RightLed=1;LeftLed=LeftLed; if(ShowPort=LedShowData4) LeftLed=1; RightLed=RightLed; /定義前方(qinfng)左側(cè)指示燈端口 return ; /延時(shí)函數(shù)(hnsh)進(jìn)入倒計(jì)時(shí)void Delay8()unsigned int DelayTime=2000; /定義(dngy)可變延時(shí)函數(shù) while(DelayTime-);LED2= LED2; if(ShowPort=LedShowData3) RightLed=1;LeftLed=LeftLed; if(ShowPort=LedShowDa

19、ta4) LeftLed=1; RightLed=RightLed; /定義前方左側(cè)指示燈端口 return ; /延時(shí)函數(shù)進(jìn)入倒計(jì)時(shí)void Delay9()unsigned int DelayTime=2000; /定義可變延時(shí)函數(shù) while(DelayTime-);LED3= LED3; if(ShowPort=LedShowData3) RightLed=1;LeftLed=LeftLed; if(ShowPort=LedShowData4) LeftLed=1; RightLed=RightLed; /定義前方左側(cè)指示燈端口 return ; /延時(shí)函數(shù)進(jìn)入倒計(jì)時(shí)void Dela

20、y10()unsigned int DelayTime=2000; /定義可變延時(shí)函數(shù) while(DelayTime-);LED4= LED4 ; if(ShowPort=LedShowData3) RightLed=1;LeftLed=LeftLed; if(ShowPort=LedShowData4) LeftLed=1; RightLed=RightLed; /定義前方左側(cè)指示燈端口 return ; /延時(shí)函數(shù)進(jìn)入倒計(jì)時(shí) void Delay11()unsigned int DelayTime=2000; /定義可變延時(shí)函數(shù) while(DelayTime-);LED5= LED5

21、; if(ShowPort=LedShowData3) RightLed=1;LeftLed=LeftLed; if(ShowPort=LedShowData4) LeftLed=1; RightLed=RightLed; /定義前方(qinfng)左側(cè)指示燈端口 return ; /延時(shí)函數(shù)(hnsh)進(jìn)入倒計(jì)時(shí)void Delay12()unsigned int DelayTime=2000; /定義(dngy)可變延時(shí)函數(shù) while(DelayTime-);LED6= LED6 ; if(ShowPort=LedShowData3) RightLed=1;LeftLed=LeftLed

22、; if(ShowPort=LedShowData4) LeftLed=1;RightLed=RightLed; /定義前方左側(cè)指示燈端口 return ; /延時(shí)函數(shù)進(jìn)入倒計(jì)時(shí)void Delay13()unsigned int DelayTime=2000; /定義可變延時(shí)函數(shù) while(DelayTime-);LED7= LED7 ; if(ShowPort=LedShowData3)RightLed=1;LeftLed=LeftLed; if(ShowPort=LedShowData4) LeftLed=1;RightLed=RightLed; /定義前方左側(cè)指示燈端口 return

23、 ; /延時(shí)函數(shù)進(jìn)入倒計(jì)時(shí)void pao() Delay2(); Delay2(); Led=1; Delay7(); Delay7(); LED1=1; Delay8(); Delay8(); LED2=1; Delay9(); Delay9(); LED3=1; Delay10(); Delay10(); LED4=1; Delay11(); Delay11(); LED5=1; Delay12(); Delay12(); LED6=1; Delay13(); Delay13(); LED7=1;void Flash1() unsigned int CountData=0; while(

24、1) if(CountData90) Delay6(5000); else if(CountData100) CountData=0; CountData=CountData+1; LeftLed=1; Led=Led; LED1= LED1 ; LED2= LED2 ; LED3= LED3 ; LED4= LED4 ; LED5= LED5 ; LED6= LED6 ; LED7= LED7 ; RightLed=RightLed; if(IRIN=0) break; void Flash2() unsigned int CountData=0; while(1) if(CountData

25、90) Delay6(5000); else if(CountData100) CountData=0; CountData=CountData+1; Led=Led; LED1= LED1 ; LED2= LED2 ; LED3= LED3 ; LED4= LED4 ; LED5= LED5 ; LED6= LED6 ; LED7= LED7 ; LeftLed=LeftLed; RightLed=1; if(IRIN=0) break; void bzang() LeftLed=LeftIR1; /前方左側(cè)指示(zhsh)燈指示出前方左側(cè)紅外探頭 RightLed=RightIR1; M1

26、A=1; /避障子(zhng zi)程序 while(1) if(FontIR1=1&LeftIR1=0& RightIR1=1) ControlCar(4); Delay5(); Delay5(); ControlCar(1); MUSIC=1; else if(FontIR1=1&LeftIR1=1& RightIR1=0) ControlCar(3); Delay5();Delay5(); ControlCar(1); MUSIC=1; else if(FontIR1=1&LeftIR1=1& RightIR1=1) M1A=1; /將電機(jī)(dinj)1正向端口置高 M2A=1; Del

27、ay5(); MUSIC=1; else if(FontIR1=1&LeftIR1=0& RightIR1=0) ControlCar(1); Delay5(); MUSIC=1; else if(FontIR1=0& LeftIR1=0 & RightIR1=1) ControlCar(2); Delay5(); ControlCar(4); Delay5(); ControlCar(1); MUSIC=1; else if(FontIR1=0&LeftIR1=1& RightIR1=0) ControlCar(2); Delay5(); ControlCar(3); Delay5(); C

28、ontrolCar(1); MUSIC=1; else if(FontIR1=0&LeftIR1=0& RightIR1=0) ControlCar(2); Delay5(); Delay5(); ControlCar(5); MUSIC=1; else if(FontIR1=0 & LeftIR1=1& RightIR1=1) ControlCar(2); Delay5(); Delay5(); Delay5(); ControlCar(3); Delay5();Delay5();Delay5();Delay5();Delay5(); ControlCar(1);MUSIC=1;if(IRI

29、N=0) break; void ControlCar(uchar CarType) /定義(dngy)小車控制子程序 M1A=0; /將電機(jī)(dinj)1正向電平置低 M1B=0; /將電機(jī)(dinj)1反向電平置低 M2A=0; /將電機(jī)2正向電平置低 M2B=0; Delay(); /將電機(jī)2反向電平置低 switch(CarType) /判斷小車控制指令類型 case 1: /前進(jìn) /判斷是否是前進(jìn) M1A=1; /將電機(jī)1正向端口置高 M2A=1; LeftLed=0; /定義前方左側(cè)指示燈端口 RightLed=0; ShowPort=LedShowData1; /數(shù)碼管顯示前進(jìn)狀

30、態(tài) break; /退出判斷 case 2: /后退(hutu) /判斷是否是后退 M1B=1; /將電機(jī)(dinj)1反向端口置高 M2B=1; LeftLed=1; /定義前方(qinfng)左側(cè)指示燈端口 RightLed=1; /將電機(jī)2反向端口置高 ShowPort=LedShowData2; /數(shù)碼管顯示后退狀態(tài) /將前方左側(cè)指示燈置低(亮) break; /退出判斷 case 3: /左轉(zhuǎn) /判斷是否是左轉(zhuǎn) M1B=1; /將電機(jī)1反向端口置高 M2A=1; /將電機(jī)2正向端口置高 ShowPort=LedShowData3; /數(shù)碼管顯示左轉(zhuǎn)狀態(tài) /將前方左側(cè)指示燈置低(亮)

31、break; /退出判斷 case 4: /右轉(zhuǎn) /判斷是否是右轉(zhuǎn) M1A=1; /將電機(jī)1正向端口置高 M2B=1; /將電機(jī)2反向端口置高 ShowPort=LedShowData4; /數(shù)碼管顯示右轉(zhuǎn)狀態(tài) /將前方右側(cè)指示燈置低(亮) break; /退出判斷 case 5: /停止 /判斷是否是右轉(zhuǎn) M1A=0; /將電機(jī)1正向端口置高 M2B=0; M1B=0; /將電機(jī)1反向端口置高 M2A=0; /將電機(jī)2反向端口置高 ShowPort=LedShowData5; /數(shù)碼管顯示右轉(zhuǎn)狀態(tài) /將前方右側(cè)指示燈置低(亮) break; /退出判斷 default: /默認(rèn)情況下的判斷

32、break; /直接退出判斷 void IR_IN() interrupt 2 using 0 /定義INT2外部中斷函數(shù) unsigned char j,k,N=0; EX1 = 0; /關(guān)閉外部中斷,防止再有信號(hào)到達(dá) delay(15); /延時(shí)時(shí)間,進(jìn)行紅外消抖 if (IRIN=1) /判斷紅外信號(hào)是否消失 EX1 =1; /外部(wib)中斷開return; /返回(fnhu) while (!IRIN) /等IR變?yōu)楦唠娖?,跳過9ms的前導(dǎo)(qindo)低電平信號(hào)。 delay(1); /延時(shí)等待 for (j=0;j4;j+) /采集紅外遙控器數(shù)據(jù) for (k=0;k=30)

33、/判斷計(jì)數(shù)器累加值 EX1=1; /打開外部中斷功能 return; /返回 IRCOMj=IRCOMj 1; /進(jìn)行數(shù)據(jù)位移操作并自動(dòng)補(bǔ)零 if (N=8) /判斷數(shù)據(jù)長(zhǎng)度 IRCOMj = IRCOMj | 0 x80; /數(shù)據(jù)最高位補(bǔ)1 N=0; /清零位數(shù)計(jì)錄器 if (IRCOM2!=IRCOM3) /判斷地址碼是否相同 EX1=1; /打開外部中斷 return; /返回 for(j=0;j10;j+) /循環(huán)進(jìn)行(jnxng)鍵碼解析 if(IRCOM2=RecvDataj) /進(jìn)行(jnxng)鍵位對(duì)應(yīng) P2=LedShowDataj; /數(shù)碼管顯示相應(yīng)(xingyng)數(shù)碼

34、switch(IRCOM2) case 0 x1A: /全力前進(jìn) ControlCar(1); LeftLed=0; /定義前方左側(cè)指示燈端口 RightLed=0; LED=0; /定義前方左側(cè)指示燈端口 break; case 0 x05: /全力后退 ControlCar(2);LED=LED; break; case 0 x01: /全力左轉(zhuǎn) ControlCar(3);Flash2(); break; case 0 x03: /全力右轉(zhuǎn) ControlCar(4); Flash1() ; break; case 0 x12: /電源 ControlCar(5); LeftLed=1; /定義前方左側(cè)指示燈端口 RightLed=1; /停止 break; case 0

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論