|
這個(gè)溫度計(jì)使用的單片機(jī)是AT89C2051,測(cè)量范圍-55到+125度,顯示精度為0.1度,DS18B20數(shù)據(jù)腳P3.4 ,采用器件默認(rèn)的12位轉(zhuǎn)化,最大轉(zhuǎn)化時(shí)間750微秒,顯示采用4位LED共陽(yáng)顯示測(cè)溫值,P1口為段碼輸入,P0~P3為位選。下面是制作好實(shí)物圖,電路原理比較簡(jiǎn)單,這里就不給出。資料是從網(wǎng)絡(luò)上收集整理的。
源代碼下載:
DS18B20數(shù)字溫度計(jì)C程序.zip
(22.71 KB, 下載次數(shù): 283)
2015-4-11 20:03 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5



程序源代碼:
- //***********DS18B20數(shù)字溫度計(jì)C程序****************//
- //*MCU: AT892051 //
- //*MCU-crystal: 12M //
- //DS18B20的讀寫(xiě)程序,數(shù)據(jù)腳P3.4 //
- //溫度傳感器18B20匯編程序,采用器件默認(rèn)的12位轉(zhuǎn)化 //
- //最大轉(zhuǎn)化時(shí)間750微秒,顯示溫度-55到+125度,顯示精度 //
- //為0.1度,顯示采用4位LED共陽(yáng)顯示測(cè)溫值 //
- //P1口為段碼輸入,P0~P3為位選 //
- /***************************************************/
- #include "reg51.h"
- #include "intrins.h" //_nop_();延時(shí)函數(shù)用
- #define Disdata P1 //段碼輸出口
- #define discan P3 //掃描口
- #define uchar unsigned char
- #define uint unsigned int
- sbit DQ=P3^4; //溫度輸入口
- sbit DIN=P1^7; //LED小數(shù)點(diǎn)控制
- uint h;
- uint temp;
- //
- //
- //**************溫度小數(shù)部分用查表法***********//
- uchar code ditab[16]=
- {0x00,0x01,0x01,0x02,0x03,0x03,0x04,0x04,0x05,0x06,0x06,0x07,0x08,0x08,0x09,0x09};
- //
- uchar code dis_7[12]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0xbf};
- //共陽(yáng)LED段碼表 "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "不亮" "-"
- uchar code scan_con[4]={0x08,0x04,0x02,0x01}; //列掃描控制字
- uchar data temp_data[2]={0x00,0x00}; //讀出溫度暫放
- uchar data display[5]={0x00,0x00,0x00,0x00,0x00}; //顯示單元數(shù)據(jù),共4個(gè)數(shù)據(jù)和一個(gè)運(yùn)算暫用
- //
- //
- //
- /*****************11us延時(shí)函數(shù)*************************/
- //
- void delay(uint t)
- {
- for (;t>0;t--);
- }
- //
- /****************顯示掃描函數(shù)***************************/
- scan()
- {
- char k;
- for(k=0;k<4;k++) //4位LED掃描控制
- {
- Disdata=dis_7[display[k]]; //數(shù)據(jù)顯示
- if (k==1){DIN=0;} //小數(shù)點(diǎn)顯示
- discan=scan_con[k]; //位選
- delay(300);
- }
- return 0;
- }
- //
- //
- /****************DS18B20復(fù)位函數(shù)************************/
- ow_reset(void)
- {
- char presence=1;
- while(presence)
- {
- while(presence)
- {
- DQ=1;_nop_();_nop_();//從高拉倒低
- DQ=0;
- delay(50); //550 us
- DQ=1;
- delay(6); //66 us
- presence=DQ; //presence=0 復(fù)位成功,繼續(xù)下一步
- }
- delay(45); //延時(shí)500 us
- presence=~DQ;
- }
- DQ=1; //拉高電平
- return 0;
- }
- //
- //
- /****************DS18B20寫(xiě)命令函數(shù)************************/
- //向1-WIRE 總線上寫(xiě)1個(gè)字節(jié)
- void write_byte(uchar val)
- {
- uchar i;
- for(i=8;i>0;i--)
- {
- DQ=1;_nop_();_nop_(); //從高拉倒低
- DQ=0;_nop_();_nop_();_nop_();_nop_(); //5 us
- DQ=val&0x01; //最低位移出
- delay(6); //66 us
- val=val/2; //右移1位
- }
- DQ=1;
- delay(1);
- }
- //
- /****************DS18B20讀1字節(jié)函數(shù)************************/
- //從總線上取1個(gè)字節(jié)
- uchar read_byte(void)
- {
- uchar i;
- uchar value=0;
- for(i=8;i>0;i--)
- {
- DQ=1;_nop_();_nop_();
- value>>=1;
- DQ=0;_nop_();_nop_();_nop_();_nop_(); //4 us
- DQ=1;_nop_();_nop_();_nop_();_nop_(); //4 us
- if(DQ)value|=0x80;
- delay(6); //66 us
- }
- DQ=1;
- return(value);
- }
- //
- /****************讀出溫度函數(shù)************************/
- //
- read_temp()
- {
- ow_reset(); //總線復(fù)位
- delay(200);
- write_byte(0xcc); //發(fā)命令
- write_byte(0x44); //發(fā)轉(zhuǎn)換命令
- ow_reset();
- delay(1);
- write_byte(0xcc); //發(fā)命令
- write_byte(0xbe);
- temp_data[0]=read_byte(); //讀溫度值的第字節(jié)
- temp_data[1]=read_byte(); //讀溫度值的高字節(jié)
- temp=temp_data[1];
- temp<<=8;
- temp=temp|temp_data[0]; // 兩字節(jié)合成一個(gè)整型變量。
- return temp; //返回溫度值
- }
- //
- /****************溫度數(shù)據(jù)處理函數(shù)************************/
- //二進(jìn)制高字節(jié)的低半字節(jié)和低字節(jié)的高半字節(jié)組成一字節(jié),這個(gè)
- //字節(jié)的二進(jìn)制轉(zhuǎn)換為十進(jìn)制后,就是溫度值的百、十、個(gè)位值,而剩
- //下的低字節(jié)的低半字節(jié)轉(zhuǎn)化成十進(jìn)制后,就是溫度值的小數(shù)部分
- /********************************************************/
- work_temp(uint tem)
- {
- uchar n=0;
- if(tem>6348) // 溫度值正負(fù)判斷
- {tem=65536-tem;n=1;} // 負(fù)溫度求補(bǔ)碼,標(biāo)志位置1
- display[4]=tem&0x0f; // 取小數(shù)部分的值
- display[0]=ditab[display[4]]; // 存入小數(shù)部分顯示值
- display[4]=tem>>4; // 取中間八位,即整數(shù)部分的值
- display[3]=display[4]/100; // 取百位數(shù)據(jù)暫存
- display[1]=display[4]%100; // 取后兩位數(shù)據(jù)暫存
- display[2]=display[1]/10; // 取十位數(shù)據(jù)暫存
- display[1]=display[1]%10;
- /******************符號(hào)位顯示判斷**************************/
- if(!display[3])
- {
- display[3]=0x0a; //最高位為0時(shí)不顯示
- if(!display[2])
- {
- display[2]=0x0a; //次高位為0時(shí)不顯示
- }
- }
- if(n){display[3]=0x0b;} //負(fù)溫度時(shí)最高位顯示"-"
- return 0;
- }
- //
- //
- /****************主函數(shù)************************/
- main()
- {
- Disdata=0xff; //初始化端口
- discan=0xff;
- for(h=0;h<4;h++) //開(kāi)機(jī)顯示"8888"
- {display[h]=8;}
- ow_reset(); //開(kāi)機(jī)先轉(zhuǎn)換一次
- write_byte(0xcc); //Skip ROM
- write_byte(0x44); //發(fā)轉(zhuǎn)換命令
- for(h=0;h<100;h++) //開(kāi)機(jī)顯示"8888"
- {scan();}
- while(1)
- {
- work_temp(read_temp()); //處理溫度數(shù)據(jù)
- scan(); //顯示溫度值
- }
- }
- //
- //***********************結(jié)束**************************//
復(fù)制代碼
|
評(píng)分
-
查看全部評(píng)分
|