運用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)
2018-1-25 10:58 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|