仅供参考哈
家庭温控使用的资料 源码,谢谢支持
完整代码下载:
UltrasonicR Success!.zip
(63.69 KB, 下载次数: 11)
源程序:
- #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; //算出来是CM
- if((S>=700)||flag==1) //超出测量范围显示“-”
- {
- flag=0;
-
- DisplayOneChar(0, 1, ASCII[11]);
- DisplayOneChar(1, 1, ASCII[10]); //显示点
- 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]); //显示点
- DisplayOneChar(2, 1, ASCII[disbuff[1]]);
- DisplayOneChar(3, 1, ASCII[disbuff[2]]);
- DisplayOneChar(4, 1, 'M'); //显示M
-
- }
- }
- void zd0() interrupt 1 //T0中断用来计数器溢出,超过测距范围
- {
- flag=1; //中断溢出标志
- }
- void StartModule() //启动模块
- {
-
- Trig=1; //启动一次模块
- _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为节拍常数变量
- unsigned char code music_tab[] ={
- 0x18, 0x30, 0x1C , 0x10, //格式为: 频率常数, 节拍常数, 频率常数, 节拍常数,
- 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 控制节拍
- { TH0=0xd8;
- TL0=0xef;
- n--;
- }
-
- void delay (unsigned char m) //控制频率延时
- {
- unsigned i=3*m;
- while(--i);
- }
-
- void delayms1(unsigned char a) //豪秒延时子程序
- {
- while(--a); //采用while(--a) 不要采用while(a--); 各位可编译一下看看汇编结果就知道了!
- }
-
- 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为频率常数变量
- 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;} //如果碰到结束符,延时1秒,回到开始再来一遍
- else if(p==0xff) { i=i+1;delayms1(100),TR0=0; goto a;} //若碰到休止符,延时100ms,继续取下一音符
- else {m=music_tab[i++], n=music_tab[i++];} //取频率常数 和 节拍常数
- TR0=1; //开定时器1
- while(n!=0) Beep=~Beep,delay(m); //等待节拍完成, 通过P1口输出音频(可多声道哦!)
- TR0=0; //关定时器1
-
- keyscan();
- }
- }
-
- void led()
- {
- unsigned char n;
- GPIO_LED=0xfe; //1111_1110
- while(menu==0)
- {
- for(n=0;n<7;n++) //左移7次,这样子就会到达最左边
- {
- GPIO_LED=_crol_(GPIO_LED,1); //将GPIO_LED左移一位
- Delay50ms(10); //延时
- }
- for(n=0;n<7;n++) //右移7次,这样子就会到达最右边
- {
- GPIO_LED=_cror_(GPIO_LED,1); //将GPIO_LED右移一位
- Delay50ms(10);
- }
-
- keyscan();
- }
- }
- void main()
- {
- TMOD=0x01; //设T0为方式1,GATE=1;
- TH0=0;
- TL0=0;
- ET0=1; //允许T0中断
- EA=1; //开启总中断
- while(1)
- {
- StartModule();
- while(!Echo); //当RX为零时等待
- TR0=1; //开启计数
- while(Echo); //当RX为1计数并等待
- TR0=0; //关闭计数
-
- 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); //当RX为零时等待
- TR0=1; //开启计数
- while(Echo); //当RX为1计数并等待
- TR0=0; //关闭计数
- Conut(); //计算
- keyscan();
- delayms(70);
- ///////////////////////////////////////////////////////////////////////////////////
- }
- }
- }
- if(S>40)
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
|