|
0.png (17.53 KB, 下載次數(shù): 61)
下載附件
2016-10-9 22:40 上傳
0.png (53.1 KB, 下載次數(shù): 44)
下載附件
2016-10-9 22:41 上傳
仿真工程文件及所有完整程序等資料下載地址:
http://www.zg4o1577.cn/bbs/dpj-56304-1.html
單片機(jī)源程序:
- #include <at89x51.h>//用AT89C51時(shí)就用這個(gè)頭文件
- //#include <reg52.h>//用華邦W78E58B時(shí)必須用這個(gè)頭文件
- #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; //用來(lái)存放溫度值,測(cè)溫程序就是通過(guò)這個(gè)數(shù)組與主函數(shù)通信的
- unsigned char TempBuffer1[9]={0x2b,0x31,0x32,0x32,0x2e,0x30,0x30,0x43,'\0'};
- //顯示實(shí)時(shí)溫度,上電時(shí)顯示+125.00C
- unsigned char TempBuffer0[17]={0x54,0x48,0x3a,0x2b,0x31,0x32,0x35,0x20,
- 0x54,0x4c,0x3a,0x2b,0x31,0x32,0x34,0x43,'\0'};
- //顯示溫度上下限,上電時(shí)顯示TH:+125 TL:+124C
- unsigned char code dotcode[4]={0,25,50,75};
- /***因顯示分辨率為0.25,但小數(shù)運(yùn)算比較麻煩,故采用查表的方法*******
- 再將表值分離出十位和個(gè)位后送到十分位和百分位********************/
- void covert0( unsigned char TH, unsigned char TL) //將溫度上下限轉(zhuǎn)換為L(zhǎng)CD顯示的數(shù)據(jù)
- {
- if(TH>0x7F) //判斷正負(fù),如果為負(fù)溫,將其轉(zhuǎn)化為其絕對(duì)值
- {
- 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的百十個(gè)位
- if( TempBuffer0[4]==0x30) TempBuffer0[4]=0xfe; //百位數(shù)消隱
- TempBuffer0[5]=(TH%100)/10+0x30; //分離出十位
- TempBuffer0[6]=(TH%100)%10+0x30; //分離出個(gè)位
- TempBuffer0[12]=TL/100+0x30; //分離出TL的百十個(gè)位
- if( TempBuffer0[12]==0x30) TempBuffer0[12]=0xfe; //百位數(shù)消隱
- TempBuffer0[13]=(TL%100)/10+0x30; //分離出十位
- TempBuffer0[14]=(TL%100)%10+0x30; //分離出個(gè)位
- }
- void covert1(void) //將溫度轉(zhuǎn)換為L(zhǎng)CD顯示的數(shù)據(jù)
- {
- unsigned char x=0x00,y=0x00;
- t[0]=*pt;
- pt++;
- t[1]=*pt;
- if(t[1]>0x07) //判斷正負(fù)溫度
- {
- TempBuffer1[0]=0x2d; //0x2d為"-"的ASCII碼
- t[1]=~t[1]; /*下面幾句把負(fù)數(shù)的補(bǔ)碼*/
- t[0]=~t[0]; /* 換算成絕對(duì)值*********/
- x=t[0]+1; /***********************/
- t[0]=x; /***********************/
- if(x>255) /**********************/
- t[1]++; /*********************/
- }
- else TempBuffer1[0]=0x2b; //0xfe為變"+"的ASCII碼
- t[1]<<=4; //將高字節(jié)左移4位
- t[1]=t[1]&0x70; //取出高字節(jié)的3個(gè)有效數(shù)字位
- x=t[0]; //將t[0]暫存到X,因?yàn)槿⌒?shù)部分還要用到它
- x>>=4; //右移4位
- x=x&0x0f; //和前面兩句就是取出t[0]的高四位
- t[1]=t[1]|x; //將高低字節(jié)的有效值的整數(shù)部分拼成一個(gè)字節(jié)
- TempBuffer1[1]=t[1]/100+0x30; //+0x30 為變 0~9 ASCII碼
- if( TempBuffer1[1]==0x30) TempBuffer1[1]=0xfe; //百位數(shù)消隱
- TempBuffer1[2]=(t[1]%100)/10+0x30; //分離出十位
- TempBuffer1[3]=(t[1]%100)%10+0x30; //分離出個(gè)位
- t[0]=t[0]&0x0c; //取有效的兩位小數(shù)
- t[0]>>=2; //左移兩位,以便查表
- x=t[0];
- y=dotcode[x]; //查表?yè)Q算成實(shí)際的小數(shù)
- TempBuffer1[5]=y/10+0x30; //分離出十分位
- TempBuffer1[6]=y%10+0x30; //分離出百分位
- }
- void delay(unsigned char i)
- {
- while(i--);
- }
- main()
- {
- unsigned char TH=110,TL=-20; //下一步擴(kuò)展時(shí)可能通過(guò)這兩個(gè)變量,調(diào)節(jié)上下限
- //測(cè)溫函數(shù)返回這個(gè)數(shù)組的頭地址
- while(1)
- {
- pt=ReadTemperature(TH,TL,0x3f); //上限溫度-22,下限-24,分辨率10位,也就是0.25C
- //讀取溫度,溫度值存放在一個(gè)兩個(gè)字節(jié)的數(shù)組中,
- delay(100);
- covert1();
- covert0(TH,TL);
- LCD_Initial(); //第一個(gè)參數(shù)列號(hào),第二個(gè)為行號(hào),為0表示第一行
- //為1表示第二行,第三個(gè)參數(shù)為顯示數(shù)據(jù)的首地址
- LCD_Print(0,0,TempBuffer0);
- LCD_Print(0,1,TempBuffer1);
- }
- }
-
復(fù)制代碼 |
|