ds18b20 溫度液晶顯示
單片機(jī)源代碼:
- //------------------------------------------------------
- //用數(shù)碼管與DS18B20設(shè)計(jì)溫度報(bào)警器
- //------------------------------------------------------
- //本例將報(bào)警溫度設(shè)為高:40度 低:-3度,當(dāng)DS18B20感知到溫度達(dá)到此臨界值時
- //系統(tǒng)發(fā)出報(bào)警聲
- #include <REG52.H>
- #include <intrins.h>
- #include "delay.h"
- #include "DS18B20.h"
- uchar display_digit[4]={0,0,0,0}; //待顯示的各溫度數(shù)位
- uchar LCD_display[16]=" "; //LCD顯示的溫度值
- uchar LCD_alarm_display[16]="H: L: "; //顯示報(bào)警溫度
- bit HI_Alarm=0,LO_Alarm=0; //高低溫報(bào)警標(biāo)志
- bit DS18B20_IS_OK; //傳感器正常標(biāo)志
- //溫度小數(shù)位對照表
- uchar code df_table[]={0,1,1,2,3,3,4,4,5,6,6,7,8,8,9,9};
- //報(bào)警溫度上下限,為進(jìn)行正負(fù)數(shù)比較,此處注意設(shè)為char類型
- //取值范圍為-128~+127,DS18B20支持范圍為-50~+125
- char Alarm_temp_HL[4]={30,-3,0,0};//報(bào)警溫度預(yù)設(shè)及存儲
- uchar currentT=0; //當(dāng)前讀取的溫度整數(shù)部分
- uchar temp_value[]={0x00,0x00}; //從DS18B20讀取的溫度值
- //初始化DS18B20
- uchar init_DS18B20()
- {
- uchar status;
- DQ=1; delay(8);
- DQ=0; delay(90);
- DQ=1; delay(8);
- status=DQ;
- delay(100);
- DQ=1;
- return status; //初始化成功時返回0
- }
- //讀一字節(jié)
- uchar readonebyte()
- {
- uchar i,dat=0;
- DQ=1; _nop_();
- for(i=0;i<8;i++)
- {
- DQ=0;dat>>=1;
- DQ=1;_nop_();_nop_();//拉高,延時進(jìn)行采樣 單總線是進(jìn)行與的
- if(DQ) dat|=0x80; //如果DQ是1,把1存到dat里
- delay(30);DQ=1;
- }
- return dat;
- }
- //寫一字節(jié)
- void writeonebyte(uchar dat)
- {
- uchar i;
- for(i=0;i<8;i++)
- {
- DQ=0;DQ=dat&0x01; //先取最低位,先讀的也是最低位
- delay(5);DQ=1;dat>>=1;
- }
- }
- //讀取溫度值
- void read_temp()
- {
- if(init_DS18B20()==1) //DS18B20故障
- DS18B20_IS_OK=0;
- else
- {
- writeonebyte(0xcc); //跳過序列號
- writeonebyte(0x44); //啟動溫度轉(zhuǎn)換
- init_DS18B20();
- writeonebyte(0xcc); //跳過序列號
- writeonebyte(0xbe); //讀取溫度寄存器
- temp_value[0]=readonebyte(); // 溫度低8位
- temp_value[1]=readonebyte(); // 溫度高8位
- Alarm_temp_HL[2]=readonebyte(); //報(bào)警溫度TH
- Alarm_temp_HL[3]=readonebyte(); //報(bào)警溫度TL
- DS18B20_IS_OK=1;
- }
- }
- //設(shè)置DS18B20溫度報(bào)警值
- void set_Alarm_temp()
- {
- init_DS18B20();
- writeonebyte(0xcc); //跳過序列號
- writeonebyte(0x4e); //將設(shè)定的溫度報(bào)警值寫入DS18B20
- writeonebyte(Alarm_temp_HL[0]); //寫TH
- writeonebyte(Alarm_temp_HL[1]); //寫TL
- writeonebyte(0x7f); //12位精度
- init_DS18B20();
- writeonebyte(0xcc); //跳過序列號
- writeonebyte(0x48); //溫度報(bào)警值存入DS18B20
- }
- //在液晶上顯示溫度處理
- void chuli_temp()
- {
- //------------------顯示溫度處理-------------------------
- uchar ng=0; //負(fù)數(shù)標(biāo)識
- uchar t=150; //延時值
- char signed_current_temp; //注意類型為char
- //如果為負(fù)數(shù)則取反加1,并設(shè)置負(fù)號標(biāo)識及負(fù)號顯示位置
- if((temp_value[1]&0xf8)==0xf8)
- {
- temp_value[1]=~temp_value[1];
- temp_value[0]=~temp_value[0]+1;
- if(temp_value[0]==0x00) temp_value[1]++;
- ng=1;
-
- }
- //查表得到溫度小數(shù)部分
- display_digit[0]=df_table[temp_value[0]&0x0f];
- //獲取溫度整數(shù)部分(無符號)
- currentT=((temp_value[0]&0xf0)>>4)|((temp_value[1]&0x07)<<4);
- //有符號的當(dāng)前溫度值,注意定義為char,其值可為-128~+127
- signed_current_temp=ng?-currentT:currentT;
- //高低溫報(bào)警標(biāo)志設(shè)置(與定義為char類型的Alarm_temp_HL比較,這樣可區(qū)分正負(fù)比較)
- HI_Alarm=signed_current_temp>=Alarm_temp_HL[2]? 1 : 0;
- LO_Alarm=signed_current_temp<=Alarm_temp_HL[3]? 1 : 0;
- //將整數(shù)部分分解為三位待顯示數(shù)字
- display_digit[3]=currentT/100;
- display_digit[2]=currentT%100/10;
- display_digit[1]=currentT%10;
- //LCD顯示數(shù)據(jù)
- LCD_display[2]=display_digit[3]+'0'; //bai
- LCD_display[3]=display_digit[2]+'0'; //shi
- LCD_display[4]=display_digit[1]+'0'; //ge
- LCD_display[5]='.';
- LCD_display[6]=display_digit[0]+'0'; //xiaoshu
- LCD_display[7]=0xdf; //。
- LCD_display[8]=0x43; // C
- if(display_digit[3]==0)
- {
- LCD_display[2]=' '; //高位為0則不顯示
- if(display_digit[2]==0)
- {
- LCD_display[3]=' ';
- }
- }
- //負(fù)號顯示在恰當(dāng)位置
- if(ng)
- {
- if(LCD_display[3]==' ')
- LCD_display[3]='-';
- else if(LCD_display[2]==' ')
- LCD_display[2]='-';
- else LCD_display[1]='-';
- }
- //------------------------------------------------------
- }
- void chuli_alarm_temp()
- {
- //---------------------報(bào)警溫度處理---------------------
- uchar ng;
- //由于Alarm_temp_HL類型為char 故可以直接進(jìn)行正負(fù)比較
- //高溫報(bào)警值
- ng=0;
- if(Alarm_temp_HL[2]<0)
- {
- Alarm_temp_HL[2]=~Alarm_temp_HL[2]+1;
- ng=1;
- }
- //分解高溫各數(shù)位到待顯示數(shù)組中
- LCD_alarm_display[3]=Alarm_temp_HL[2]/100+'0';
- LCD_alarm_display[4]=Alarm_temp_HL[2]%100/10+'0';
- LCD_alarm_display[5]=Alarm_temp_HL[2]%10+'0';
- //屏蔽高位不為0的
- if(LCD_alarm_display[3]=='0') LCD_alarm_display[3]=' ';
- if(LCD_alarm_display[3]==' ' && LCD_alarm_display[4]=='0')
- LCD_alarm_display[4]=' ';
- //'-' 顯示
- if(ng)
- {
- if(LCD_alarm_display[4]==' ') LCD_alarm_display[4]='-';
- else
- if(LCD_alarm_display[3]==' ') LCD_alarm_display[3]='-';
- else LCD_alarm_display[2]='-';
- }
- //低溫報(bào)警值
-
-
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復(fù)制代碼
完整代碼下載:
ds18b20液晶顯示.zip
(63.44 KB, 下載次數(shù): 177)
2017-4-12 10:54 上傳
點(diǎn)擊文件名下載附件
|