完整DS18B20溫度測控程序_第1頁
完整DS18B20溫度測控程序_第2頁
完整DS18B20溫度測控程序_第3頁
完整DS18B20溫度測控程序_第4頁
完整DS18B20溫度測控程序_第5頁
已閱讀5頁,還剩2頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、#include<reg52.h>sbit s2=P00;sbit led=P13;unsigned char wendushangxian=0;sbit DQ=P16;sbit wei1=P30;sbit wei2=P31;sbit wei3=P32;sbit wei4=P33;sbit key_Max_jia=P00;sbit key_Max_jian=P01;sbit key_Min_jia=P02;sbit key_Min_jian=P03;sbit dianji_jian=P12;sbit dianji_jia=P17;sbit Led_tempreture_Max=P1

2、3;sbit Led_tempreture_Min=P14;sbit Led_normal=P15;unsigned char num11=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf;int tempreture_Max=40;int tempreture_Min=-10;void delay_mylself(int temp)unsigned int i,j;for(i=0;i<temp;i+)for(j=0;j<1140;j+);void delay(unsigned int i) /如果i是unsigend c

3、har類型,則會出現(xiàn)錯誤結(jié)果while(i-);void DS18B20_Init(void)/初始化unsigned char flag=0;DQ = 1; /DQ復(fù)位delay(1); DQ = 0; /單片機將DQ拉低 delay(80); /精確延時 大于 480us小于960us DQ = 1; /拉高總線 delay(6);while(DQ);while(!DQ); /flag=DQ; /稍做延時后 如果flag=0則初始化成功 flag=1則初始化失敗/delay(30);unsigned char Read(void)/讀字節(jié)unsigned char i=0; unsigne

4、d char dat = 0; for (i=8;i>0;i-) DQ = 0; / 給脈沖信號 dat>>=1; DQ = 1; / 給脈沖信號 if(DQ) dat|=0x80; delay(5); return(dat);void Write(unsigned char dat)/寫字節(jié)unsigned char i=0; for (i=8; i>0; i-) DQ = 0; DQ = dat&0x01; delay(5); DQ = 1; dat>>=1; void init()wei1=0;wei2=0;wei3=0;wei4=0;void

5、 Display(unsigned int temp)unsigned char one,two,three,four;int wendu=0;if(temp<=0xf000)temp>>=4; /右移4位,相當(dāng)于乘0.0625,將溫度化為十進制 /temp*=10; /擴大10倍,顯示一位小數(shù)one=temp/1000; /千位two=temp%1000/100; /百位three=temp%1000%100/10; /十位four=temp%1000%100%10; /個位wendu=temp;elsetemp=temp;temp+=1;temp>>=4;on

6、e=10; /負數(shù) two=temp%1000/100; /百位three=temp%1000%100/10; /十位four=temp%1000%100%10; /個位wendu=-temp;if(wendu>tempreture_Max)Led_tempreture_Max=0;Led_tempreture_Min=1;Led_normal=1;dianji_jian=1;dianji_jia=0;else if(wendu<tempreture_Min)Led_tempreture_Max=1;Led_tempreture_Min=0;Led_normal=1; dianji

7、_jia=1;dianji_jian=0;elseLed_tempreture_Max=1;Led_tempreture_Min=1;Led_normal=0; dianji_jian=0; dianji_jia=0;/第1位wei1=1;wei2=0;wei3=0;wei4=0;P2=numone;delay_mylself(1);/第2位wei1=0;wei2=1;wei3=0;wei4=0;P2=numtwo;delay_mylself(1);/第3位wei1=0;wei2=0;wei3=1;wei4=0;P2=numthree;delay_mylself(1);/第4位wei1=0;w

8、ei2=0;wei3=0;wei4=1;P2=numfour;delay_mylself(10);void common_display(int temp)unsigned char one,two,three,four;one=temp/1000; /千位two=temp%1000/100; /百位three=temp%1000%100/10; /十位four=temp%1000%100%10; /個位if(temp<0)temp=-temp;two=temp%1000/100; /百位three=temp%1000%100/10; /十位four=temp%1000%100%10;

9、/個位/第1位wei1=1;wei2=0;wei3=0;wei4=0;P2=num10;delay_mylself(50);else/第1位wei1=1;wei2=0;wei3=0;wei4=0;P2=numone;delay_mylself(50);/第2位wei1=0;wei2=1;wei3=0;wei4=0;P2=numtwo;delay_mylself(50);/第3位wei1=0;wei2=0;wei3=1;wei4=0;P2=numthree;delay_mylself(50);/第4位wei1=0;wei2=0;wei3=0;wei4=1;P2=numfour;delay_myl

10、self(60);void key()unsigned char flag=0;if(key_Max_jia=0)flag=1;tempreture_Max+=1;if(tempreture_Max>125) tempreture_Max=125;while(!key_Max_jia); /按鍵松手檢測else if(key_Max_jian=0)flag=2;tempreture_Max-=1;if(tempreture_Max<tempreture_Min) tempreture_Max=tempreture_Min;while(!key_Max_jian); /按鍵松手檢測e

11、lse if(key_Min_jia=0)flag=3;tempreture_Min+=1;if(tempreture_Min>tempreture_Max) tempreture_Min=tempreture_Max;while(!key_Min_jia);/按鍵松手檢測else if(key_Min_jian=0)flag=4;tempreture_Min-=1;if(tempreture_Min<-55) tempreture_Min=-55;while(!key_Min_jian);/按鍵松手檢測if(flag=1|flag=2)common_display(tempreture_Max);delay_mylself(200);flag=0;else if(flag=4|flag=3)common_display(tempreture_Min);delay_mylself(200);flag=0;void main() unsigned int temp;unsigned char tl=0,th=0; while(1) DS18B20_Init(); Write(0xCC); / 跳過讀序號列號的操作 Write(0x44);

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論