|
僅供參考哈
家庭溫控使用的資料 源碼,謝謝支持
完整代碼下載:
UltrasonicR Success!.zip
(63.69 KB, 下載次數(shù): 11)
2017-5-3 23:19 上傳
點(diǎn)擊文件名下載附件
超聲波試驗(yàn)成功!
源程序:
- #define uint unsigned int
- #define uchar unsigned char
- #define GPIO_LED P2 //將P2口另外取名為GPIO_LED
- #include"lcd.h"
- #include<reg51.h>
- #include<intrins.h>
- #include<stdio.h>
- sbit io=P0^3;
- sbit rs=P2^6;
- sbit rw=P2^5;
- sbit lcden=P2^7;
- sbit key1=P1^4; //位操作
- sbit key2=P1^5;
- sbit key3=P1^6;
- sbit key4=P1^7;
- sbit key5=P3^5;
- sbit Trig=P2^1;
- sbit Beep=P2^1;
- sbit Echo = P2^0; //////////////////////******************************************//////////////////////
- int menu=0;
- unsigned long TN=26;
- unsigned long TE=26;
- unsigned char menu1[]="#1 Warning Tem";
- unsigned char menu2[]="#2 WarningTeEdit";
- unsigned char menu3[]="#3 Music";
- unsigned char menu4[]="#4 UltrasonicR";
- unsigned char menu5[]="#5 More funtions";
- unsigned char Wt[]="HereisWarningTem";
- unsigned char Et[]="Original:";
- unsigned char Et1[]="Edit:";
- unsigned char open1[]="Hello,everyone";
- unsigned char open2[]="Welcom to here";
- unsigned char table[]="Hello,everyone";
- unsigned char table1[]="Welcom to here";
- unsigned char music[]="Laurel Blossom";
- unsigned char code ASCII[15] = {'0','1','2','3','4','5','6','7','8','9','.','-','C'};
- static unsigned char DisNum = 0; //顯示用指針
- unsigned int time=0;
- unsigned long S=0;
- unsigned long Wtnow=26;
- unsigned long wedit=26;
- bit flag =0;
- void DisplayOneChar(uchar, uchar, uchar);
- unsigned char disbuff[4] ={ 0,0,0,0,};
- unsigned char DST[]="The Distance";
- void Delay50ms(unsigned int c) //誤差 0us
- {
- unsigned char a,b;
- for(;c>0;c--)
- for(b=38;b>0;b--)
- for(a=130;a>0;a--);
- }
- void Delay10ms(void) //誤差 0us
- {
- unsigned char a,b,c;
- for(c=1;c>0;c--)
- for(b=38;b>0;b--)
- for(a=130;a>0;a--);
- } ///////////////////////////////////////////////////////
- ///////////////////////////////////////////////////////////////////////////////////////////////
- void Conut(void)
- { //time=100*S/1.7
- time=TH0*256+TL0;
- TH0=0;
- TL0=0;
-
- S=(time*1.7)/100; //算出來(lái)是CM
- if((S>=700)||flag==1) //超出測(cè)量范圍顯示“-”
- {
- flag=0;
-
- DisplayOneChar(0, 1, ASCII[11]);
- DisplayOneChar(1, 1, ASCII[10]); //顯示點(diǎn)
- DisplayOneChar(2, 1, ASCII[11]);
- DisplayOneChar(3, 1, ASCII[11]);
- DisplayOneChar(4, 1, ASCII[12]); //顯示M
- }
- else
- {
- disbuff[0]=S%1000/100;
- disbuff[1]=S%1000%100/10;
- disbuff[2]=S%1000%10 %10;
- DisplayOneChar(0, 1, ASCII[disbuff[0]]);
- DisplayOneChar(1, 1, ASCII[10]); //顯示點(diǎn)
- DisplayOneChar(2, 1, ASCII[disbuff[1]]);
- DisplayOneChar(3, 1, ASCII[disbuff[2]]);
- DisplayOneChar(4, 1, 'M'); //顯示M
-
- }
- }
- void zd0() interrupt 1 //T0中斷用來(lái)計(jì)數(shù)器溢出,超過(guò)測(cè)距范圍
- {
- flag=1; //中斷溢出標(biāo)志
- }
- void StartModule() //啟動(dòng)模塊
- {
-
- Trig=1; //啟動(dòng)一次模塊
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- Trig=0;
- }
- /////////////////////////////////////////////////////////// ////
- ///////////////////////////////////////////////////////////////////
- void delayms(unsigned int ms)
- {
- unsigned char i=100,j;
- for(;ms;ms--)
- {
- while(--i)
- {
- j=10;
- while(--j);
- }
- }
- }
- unsigned char n=0; //n為節(jié)拍常數(shù)變量
- unsigned char code music_tab[] ={
- 0x18, 0x30, 0x1C , 0x10, //格式為: 頻率常數(shù), 節(jié)拍常數(shù), 頻率常數(shù), 節(jié)拍常數(shù),
- 0x20, 0x40, 0x1C , 0x10,
- 0x18, 0x10, 0x20 , 0x10,
- 0x1C, 0x10, 0x18 , 0x40,
- 0x1C, 0x20, 0x20 , 0x20,
- 0x1C, 0x20, 0x18 , 0x20,
- 0x20, 0x80, 0xFF , 0x20,
- 0x30, 0x1C, 0x10 , 0x18,
- 0x20, 0x15, 0x20 , 0x1C,
- 0x20, 0x20, 0x20 , 0x26,
- 0x40, 0x20, 0x20 , 0x2B,
- 0x20, 0x26, 0x20 , 0x20,
- 0x20, 0x30, 0x80 , 0xFF,
- 0x20, 0x20, 0x1C , 0x10,
- 0x18, 0x10, 0x20 , 0x20,
- 0x26, 0x20, 0x2B , 0x20,
- 0x30, 0x20, 0x2B , 0x40,
- 0x20, 0x20, 0x1C , 0x10,
- 0x18, 0x10, 0x20 , 0x20,
- 0x26, 0x20, 0x2B , 0x20,
- 0x30, 0x20, 0x2B , 0x40,
- 0x20, 0x30, 0x1C , 0x10,
- 0x18, 0x20, 0x15 , 0x20,
- 0x1C, 0x20, 0x20 , 0x20,
- 0x26, 0x40, 0x20 , 0x20,
- 0x2B, 0x20, 0x26 , 0x20,
- 0x20, 0x20, 0x30 , 0x80,
- 0x20, 0x30, 0x1C , 0x10,
- 0x20, 0x10, 0x1C , 0x10,
- 0x20, 0x20, 0x26 , 0x20,
- 0x2B, 0x20, 0x30 , 0x20,
- 0x2B, 0x40, 0x20 , 0x15,
- 0x1F, 0x05, 0x20 , 0x10,
- 0x1C, 0x10, 0x20 , 0x20,
- 0x26, 0x20, 0x2B , 0x20,
- 0x30, 0x20, 0x2B , 0x40,
- 0x20, 0x30, 0x1C , 0x10,
- 0x18, 0x20, 0x15 , 0x20,
- 0x1C, 0x20, 0x20 , 0x20,
- 0x26, 0x40, 0x20 , 0x20,
- 0x2B, 0x20, 0x26 , 0x20,
- 0x20, 0x20, 0x30 , 0x30,
- 0x20, 0x30, 0x1C , 0x10,
- 0x18, 0x40, 0x1C , 0x20,
- 0x20, 0x20, 0x26 , 0x40,
- 0x13, 0x60, 0x18 , 0x20,
- 0x15, 0x40, 0x13 , 0x40,
- 0x18, 0x80, 0x00
- };
-
- void int0() interrupt 1 //采用中斷0 控制節(jié)拍
- { TH0=0xd8;
- TL0=0xef;
- n--;
- }
-
- void delay (unsigned char m) //控制頻率延時(shí)
- {
- unsigned i=3*m;
- while(--i);
- }
-
- void delayms1(unsigned char a) //豪秒延時(shí)子程序
- {
- while(--a); //采用while(--a) 不要采用while(a--); 各位可編譯一下看看匯編結(jié)果就知道了!
- }
-
- void keyscan() //按鍵掃描
- {
- if(key1==0)
- {
- Delay10ms();//防抖
- if(key1==0)
- {
- menu=1;
- while(!key1); //等待按鍵釋放
- }
- }
- if(key2==0)
- {
- Delay10ms();//防抖
- if(key2==0)
- {
- switch(menu)
- {
- case 1:menu=2;break;
- case 2:menu=3;break;
- case 3:menu=4;break;
- case 4:menu=5;break;
- case 5:menu=1;break;
- case 11:TE--;break;
- }
- while(!key2); //等待按鍵釋放
- }
- }
- if(key3==0)
- {
-
- Delay10ms();//防抖
- if(key3==0)
- {
- switch(menu)
- {
- case 1:menu=5;break;
- case 2:menu=1;break;
- case 3:menu=2;break;
- case 4:menu=3;break;
- case 5:menu=4;break;
- case 11:TE++;break;
- }
- while(!key3); //等待按鍵釋放
- }
- }
- if(key4==0)
- {
- Delay10ms();//防抖
- if(key4==0)
- {
- switch(menu)
- {
- case 1:menu=10;break;
- case 2:menu=11;break;
- case 3:menu=12;break;
- case 4:menu=13;break;
- case 11:TN=TE;break;
- }
- while(!key4); //等待按鍵釋放
- }
- }
- if(key5==0)
- {
- Delay10ms();//防抖
- if(key5==0)
- {
- switch(menu)
- {
- case 10:menu=1;break;
- case 11:menu=2;break;
- case 12:menu=3;break;
- case 13:menu=4;break;
- default:menu=0;
- }
- while(!key5); //等待按鍵釋放
- }
- }
- }
-
- void music_p()
- { unsigned char p,m; //m為頻率常數(shù)變量
- unsigned char i=0;
- TMOD&=0x0f;
- TMOD|=0x01;
- TH0=0xd8;TL0=0xef;
- IE=0x82;
- play:
- while(menu==12)
- {
- a: p=music_tab[i];
- if(p==0x00) { i=0, delayms1(1000); goto play;} //如果碰到結(jié)束符,延時(shí)1秒,回到開(kāi)始再來(lái)一遍
- else if(p==0xff) { i=i+1;delayms1(100),TR0=0; goto a;} //若碰到休止符,延時(shí)100ms,繼續(xù)取下一音符
- else {m=music_tab[i++], n=music_tab[i++];} //取頻率常數(shù) 和 節(jié)拍常數(shù)
- TR0=1; //開(kāi)定時(shí)器1
- while(n!=0) Beep=~Beep,delay(m); //等待節(jié)拍完成, 通過(guò)P1口輸出音頻(可多聲道哦!)
- TR0=0; //關(guān)定時(shí)器1
-
- keyscan();
- }
- }
-
- void led()
- {
- unsigned char n;
- GPIO_LED=0xfe; //1111_1110
- while(menu==0)
- {
- for(n=0;n<7;n++) //左移7次,這樣子就會(huì)到達(dá)最左邊
- {
- GPIO_LED=_crol_(GPIO_LED,1); //將GPIO_LED左移一位
- Delay50ms(10); //延時(shí)
- }
- for(n=0;n<7;n++) //右移7次,這樣子就會(huì)到達(dá)最右邊
- {
- GPIO_LED=_cror_(GPIO_LED,1); //將GPIO_LED右移一位
- Delay50ms(10);
- }
-
- keyscan();
- }
- }
- void main()
- {
- TMOD=0x01; //設(shè)T0為方式1,GATE=1;
- TH0=0;
- TL0=0;
- ET0=1; //允許T0中斷
- EA=1; //開(kāi)啟總中斷
- while(1)
- {
- StartModule();
- while(!Echo); //當(dāng)RX為零時(shí)等待
- TR0=1; //開(kāi)啟計(jì)數(shù)
- while(Echo); //當(dāng)RX為1計(jì)數(shù)并等待
- TR0=0; //關(guān)閉計(jì)數(shù)
-
- S=((TH0*256+TL0)*1.7)/100;
- TH0=0;
- TL0=0;
- delayms(10);
- if(S<=40)
- {
-
-
- keyscan();
- if(menu==0)
- { InitLcd1602();
- LcdShowStr(0,0,table);
- LcdShowStr(0,1,table1);
- keyscan();
- keyscan();
- }
- if(menu==1)
- {
- LcdShowStr(0,0,menu1);
- LcdShowStr(0,1,menu2);
- //顯示菜單1 2欄
- }
- if(menu==2)
- {
- LcdShowStr(0,0,menu2);
- LcdShowStr(0,1,menu3);
-
- //顯示菜單2 3欄
- }
- if(menu==3)
- {
- LcdShowStr(0,0,menu3);
- LcdShowStr(0,1,menu4);
- //顯示菜單3 4欄
- }
- if(menu==4)
- {
- LcdShowStr(0,0,menu4);
- LcdShowStr(0,1,menu5);
- }
- if(menu==5)
- { InitLcd1602();
- LcdShowStr(0,0,menu5);
- LcdShowStr(0,1,menu1);
- }
- if(menu==10)
- { InitLcd1602();
- LcdShowStr(0,0,Wt);
- disbuff[0]=TN%1000/100;
- disbuff[1]=TN%1000%100/10;
- disbuff[2]=TN%1000%10 %10;
- DisplayOneChar(8, 1, ASCII[disbuff[0]]);
- DisplayOneChar(9, 1, ASCII[disbuff[1]]);
- DisplayOneChar(10, 1, ASCII[disbuff[2]]);
- DisplayOneChar(11, 1, 0xdf); //??M
- DisplayOneChar(12, 1, ASCII[12]); //顯示C
- }
- if(menu==11)
- { InitLcd1602();
- LcdShowStr(0,0,Et);
- LcdShowStr(0,1,Et1);
- disbuff[0]=TN%1000/100;
- disbuff[1]=TN%1000%100/10;
- disbuff[2]=TN%1000%10 %10;
- DisplayOneChar(10, 0, ASCII[disbuff[0]]);
- DisplayOneChar(11, 0, ASCII[disbuff[1]]);
- DisplayOneChar(12, 0, ASCII[disbuff[2]]);
- DisplayOneChar(13, 0, 0xdf); //??M
- DisplayOneChar(14, 0, ASCII[12]); //顯示C
- disbuff[0]=TE%1000/100;
- disbuff[1]=TE%1000%100/10;
- disbuff[2]=TE%1000%10 %10;
- DisplayOneChar(10, 1, ASCII[disbuff[0]]);
- DisplayOneChar(11, 1, ASCII[disbuff[1]]);
- DisplayOneChar(12, 1, ASCII[disbuff[2]]);
- DisplayOneChar(13, 1, 0xdf); //??M
- DisplayOneChar(14, 1, ASCII[12]); //顯示C
- }
- if(menu==12)
- { InitLcd1602();
- LcdShowStr(0,0,music);
- music_p();
- }
- if(menu==13) //////////////////////////////////////////////////////////////////////////////////////
- {
- InitLcd1602();
- LcdShowStr(0,0,DST);
- while(menu==13)
- {
- LcdShowStr(0,0,DST);
- StartModule();
- while(!Echo); //當(dāng)RX為零時(shí)等待
- TR0=1; //開(kāi)啟計(jì)數(shù)
- while(Echo); //當(dāng)RX為1計(jì)數(shù)并等待
- TR0=0; //關(guān)閉計(jì)數(shù)
- Conut(); //計(jì)算
- keyscan();
- delayms(70);
- ///////////////////////////////////////////////////////////////////////////////////
- }
- }
- }
- if(S>40)
- …………限于本文篇幅 余下代碼請(qǐng)從51黑下載附件…………
復(fù)制代碼
|
|