DS18B20+1602溫度顯示
DS18B20.jpg (158.81 KB, 下載次數: 21)
下載附件
仿真電路
2022-10-24 08:01 上傳
用示實時顯示DS18B20溫度值
單片機源程序如下:
- #include <at89x51.h>//用AT89C51時就用這個頭文件
- //#include <reg52.h>//用華邦W78E58B時必須用這個頭文件
- #include <absacc.h>
- #include <ctype.h>
- #include <math.h>
- #include <stdio.h>
- #include <string.h>
- #include <DS18B20.h>
- #include "LCD1602.h" ////液晶顯示頭文件
- //sbit DQ = P3^4; //定義DQ引腳為P3.4
- unsigned char t[2],*pt; //用來存放溫度值,測溫程序就是通過這個數組與主函數通信的
- unsigned char TempBuffer1[9]={0x2b,0x31,0x32,0x32,0x2e,0x30,0x30,0x43,'\0'};
- //顯示實時溫度,上電時顯示+125.00C
- unsigned char TempBuffer0[17]={0x54,0x48,0x3a,0x2b,0x31,0x32,0x35,0x20,
- 0x54,0x4c,0x3a,0x2b,0x31,0x32,0x34,0x43,'\0'};
- //顯示溫度上下限,上電時顯示TH:+125 TL:+124C
- unsigned char code dotcode[4]={0,25,50,75};
- /***因顯示分辨率為0.25,但小數運算比較麻煩,故采用查表的方法*******
- 再將表值分離出十位和個位后送到十分位和百分位********************/
- void covert0( unsigned char TH, unsigned char TL) //將溫度上下限轉換為LCD顯示的數據
- {
- if(TH>0x7F) //判斷正負,如果為負溫,將其轉化為其絕對值
- {
- TempBuffer0[3]=0x2d; //0x2d為"-"的ASCII碼
- TH=~TH;
- TH++;
- }
- else TempBuffer0[3]=0x2b; //0x2B為"+"的ASCII碼
- if(TL>0x7f)
- {
- TempBuffer0[11]=0x2d; //0x2d為"-"的ASCII碼
- TL=~TL+1;
- }
- else TempBuffer0[11]=0x2b; //0x2B為"+"的ASCII碼
- TempBuffer0[4]=TH/100+0x30; //分離出TH的百十個位
- if( TempBuffer0[4]==0x30) TempBuffer0[4]=0xfe; //百位數消隱
- TempBuffer0[5]=(TH%100)/10+0x30; //分離出十位
- TempBuffer0[6]=(TH%100)%10+0x30; //分離出個位
- TempBuffer0[12]=TL/100+0x30; //分離出TL的百十個位
- if( TempBuffer0[12]==0x30) TempBuffer0[12]=0xfe; //百位數消隱
- TempBuffer0[13]=(TL%100)/10+0x30; //分離出十位
- TempBuffer0[14]=(TL%100)%10+0x30; //分離出個位
- }
- void covert1(void) //將溫度轉換為LCD顯示的數據
- {
- unsigned char x=0x00,y=0x00;
- t[0]=*pt;
- pt++;
- t[1]=*pt;
- if(t[1]>0x07) //判斷正負溫度
- {
- TempBuffer1[0]=0x2d; //0x2d為"-"的ASCII碼
- t[1]=~t[1]; /*下面幾句把負數的補碼*/
- t[0]=~t[0]; /* 換算成絕對值*********/
- x=t[0]+1; /***********************/
- t[0]=x; /***********************/
- if(x>255) /**********************/
- t[1]++; /*********************/
- }
- else TempBuffer1[0]=0x2b; //0xfe為變"+"的ASCII碼
- t[1]<<=4; //將高字節左移4位
- t[1]=t[1]&0x70; //取出高字節的3個有效數字位
- x=t[0]; //將t[0]暫存到X,因為取小數部分還要用到它
- x>>=4; //右移4位
- x=x&0x0f; //和前面兩句就是取出t[0]的高四位
- t[1]=t[1]|x; //將高低字節的有效值的整數部分拼成一個字節
- TempBuffer1[1]=t[1]/100+0x30; //+0x30 為變 0~9 ASCII碼
- if( TempBuffer1[1]==0x30) TempBuffer1[1]=0xfe; //百位數消隱
- TempBuffer1[2]=(t[1]%100)/10+0x30; //分離出十位
- TempBuffer1[3]=(t[1]%100)%10+0x30; //分離出個位
- t[0]=t[0]&0x0c; //取有效的兩位小數
- t[0]>>=2; //左移兩位,以便查表
- x=t[0];
- y=dotcode[x]; //查表換算成實際的小數
- TempBuffer1[5]=y/10+0x30; //分離出十分位
- TempBuffer1[6]=y%10+0x30; //分離出百分位
- }
- void delay(unsigned char i)
- {
- while(i--);
- }
- main()
- {
- unsigned char TH=110,TL=-20; //下一步擴展時可能通過這兩個變量,調節上下限
- //測溫函數返回這個數組的頭地址
- while(1)
- {
- pt=ReadTemperature(TH,TL,0x3f); //上限溫度-22,下限-24,分辨率10位,也就是0.25C
- //讀取溫度,溫度值存放在一個兩個字節的數組中,
- delay(100);
- covert1();
- covert0(TH,TL);
- LCD_Initial(); //第一個參數列號,第二個為行號,為0表示第一行
- //為1表示第二行,第三個參數為顯示數據的首地址
- LCD_Print(0,0,TempBuffer0);
- LCD_Print(0,1,TempBuffer1);
- }
- }
-
復制代碼
缺少文件 無法編譯,求幫助:
DS18B20(已通過).rar
(72.02 KB, 下載次數: 7)
2022-10-24 08:02 上傳
點擊文件名下載附件
附件
|