运用18b20进行温度测量,达到设定阈值由蜂鸣器进行报警。
单片机源程序如下:
- #include<reg51.h>
- #include"temp.h"
- int m,t;
- unsigned char DisplayData[5];//数据缓存
- unsigned char intr;
- unsigned char z;
- unsigned char h,s;
- unsigned char g,n,a,b,c;
- unsigned char p,q,d,e,f;
- bit temper_flag = 0;//温度读取标志
- bit Flag_EN=1;//阈值标志位
- bit Flag_ENM=1;//模式标志位
- sbit dian=P0^7;//设置小数点
- sbit fmq=P0^6;//蜂鸣器位
- code unsigned char tab[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,
- 0xff};//0-9段码显示
- unsigned char duan[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,
- 0xff,0xbf,0xc7,0x89};//0-9,-、L、H显示段码
-
- //声明全局函数
- void display(int);
- void Display();
- unsigned char key_band();
- void Delayms();
- void LDisplay();
- void HDisplay();
- void LSet();
- void HSet();
- //1ms延时函数
- void Delay1ms(unsigned int y)
- {
- unsigned int x;
- for( ; y>0; y--)
- {
- for(x=110; x>0; x--);
- }
- }
- void main(void)
- {
- TMOD |= 0x01; //配置定时器工作模式
- TH0 = (65536-50000)/256;
- TL0 = (65536-50000)%256;
- EA = 1;
- ET0 = 1; //打开定时器中断
- TR0 = 1; //启动定时器
- P0=0x00;
- P2=0xa0;
- P2=0x00;//关闭蜂鸣器
- h=0;s=0;z=0;a=11;b=11;c=11;d=11;e=11;f=11;
- m=rd_temperature();
- while(1)
- {
- if(key_band()==15)
- {
- Delay1ms(30);
- if(key_band()==15)
- {
- if(Flag_ENM)
- {
- z++;
- Flag_ENM=0;
- }
- if(z==3)
- {z=0;}
- }
- }
- if(key_band()!=15)
- Flag_ENM=1;
- if(z==0)
- {
- LSet();
- }
- if(z==1)
- {
- HSet();
- }
- if(z==2)
- {
- if(temper_flag==1)
- {
- temper_flag = 0;
- m=rd_temperature();
- }
- display(m);
- if(t<=(a*1000+b*100+c*10)||t>=(d*1000+e*100+f*10))
- {
- fmq=1;
- P2=0xa0;
- P2=0x00;
- }
- else
- {
- fmq=0;
- P2=0xa0;
- P2=0x00;
- }
- }
-
- }
- }
-
-
- //定时器中断服务函数
- void isr_timer_0(void) interrupt 1 //默认中断优先级 1
- {
- TH0 = (65536-50000)/256;
- TL0 = (65536-50000)%256; //定时器重载
- if(++intr == 200) //10ms执行一次
- {
- intr = 0;
- temper_flag = 1; //10s温度读取标志位置1
- }
- }
- //赋值缓存函数
- void display(int temp)
- {
- float tp;
- DisplayData[0] = 0x00;
- tp=temp;
- temp=tp*0.0625*100+0.5;
- t=temp;
- DisplayData[1] = tab[temp % 10000 / 1000];
- DisplayData[2] = tab[temp % 1000 / 100] | 0x80;
- DisplayData[3] = tab[temp % 100 / 10];
- DisplayData[4] = tab[temp % 10];
- Display(); //扫描显示
- }
- //温度显示函数
- void Display()
- {
- P0=0x10;
- P2=0xc0;
- P2=0x00;
- P0=DisplayData[1];
- P2=0xe0;
- P2=0x00;
- Delay1ms(1);
-
- P0=0x20;
- P2=0xc0;
- P2=0x00;
- P0=DisplayData[2];
- dian=0;
- P2=0xe0;
- P2=0x00;
- Delay1ms(1);
-
- P0=0x40;
- P2=0xc0;
- P2=0x00;
- P0=DisplayData[3];
- P2=0xe0;
- P2=0x00;
- Delay1ms(1);
-
- P0=0x80;
- P2=0xc0;
- P2=0x00;
- P0=DisplayData[4];
- P2=0xe0;
- P2=0x00;
- Delay1ms(1);
- }
- //按键扫描函数
- unsigned char key_band()
- {
- P3=0xfe;
- if(P3!=0xfe)
- {
- if(P3==0xee) return 3;
- if(P3==0xde) return 2;
- if(P3==0xbe) return 1;
- if(P3==0x7e) return 0;
- }
- P3=0xfd;
- if(P3!=0xfd)
- {
- if(P3==0xed) return 7;
- if(P3==0xdd) return 6;
- if(P3==0xbd) return 5;
- if(P3==0x7d) return 4;
- }
- P3=0xfb;
- if(P3!=0xfb)
- {
- if(P3==0xeb) return 11;
- if(P3==0xdb) return 10;
- if(P3==0xbb) return 9;
- if(P3==0x7b) return 8;
- }
- P3=0xf7;
- if(P3!=0xf7)
- {
- if(P3==0xe7) return 15;
- if(P3==0xd7) return 14;
- if(P3==0xb7) return 13;
- if(P3==0x77) return 12;
- }
- return 16;
- }
- //低阈值显示
- void LDisplay()
- {
- P0=0x10;
- P2=0xc0;
- P2=0x00;
- P0=duan[12];
- P2=0xe0;
- P2=0x00;
- Delay1ms(1);
-
- P0=0x20;
- P2=0xc0;
- P2=0x00;
- P0=duan[a];
- P2=0xe0;
- P2=0x00;
- Delay1ms(1);
-
- P0=0x40;
- P2=0xc0;
- P2=0x00;
- P0=duan[b];
- dian=0;
- P2=0xe0;
- P2=0x00;
- Delay1ms(1);
-
- P0=0x80;
- P2=0xc0;
- P2=0x00;
- P0=duan[c];
- P2=0xe0;
- P2=0x00;
- Delay1ms(1);
- }
- //高阈值显示
- void HDisplay()
- {
- P0=0x10;
- P2=0xc0;
- P2=0x00;
- P0=duan[13];
- P2=0xe0;
- P2=0x00;
- Delay1ms(1);
-
- P0=0x20;
- P2=0xc0;
- P2=0x00;
- P0=duan[d];
- P2=0xe0;
- P2=0x00;
- Delay1ms(1);
-
- P0=0x40;
- P2=0xc0;
- P2=0x00;
- P0=duan[e];
- dian=0;
- P2=0xe0;
- P2=0x00;
- Delay1ms(1);
- P0=0x80;
- P2=0xc0;
- P2=0x00;
- P0=duan[f];
- P2=0xe0;
- P2=0x00;
- Delay1ms(1);
- }
- //低阈值设置
- void LSet()
- {
- LDisplay();
- g=key_band();
- if(g!=15)
- {
- if(g!=16)
- {
- Delay1ms(30);
- if(g!=16)
- {
- n=g;
- if(Flag_EN)
- {
- Flag_EN=0;
- h++;
- }
- if(h==4)
- {h=0;}
- }
- }
- if(g==16)
- Flag_EN=1;
-
- }
- if(h==0);
- if(h==1)
- {
- a=n;
- }
- if(h==2)
- {
- b=n;
- }
- if(h==3)
- {
- c=n;
- }
- }
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
温度显示报警程序.zip
(50.84 KB, 下载次数: 20)
|