|
需要DS1820的C51源程序看看這里,這邊需要雙DS1820.正在研究中。。。。
- 網(wǎng)上有很多這樣的例程啊。給你一個STC的,STC的下載程序方便,上手也比較快
- CPU:STC12C5A48S2
- #include "18B20.h"
- #include<intrins.h>
- #include <math.h> //要用到取絕對值函數(shù)abs()
- /**************************************
- 延時X微秒(STC12C5A60S2@12M)
- 不同的工作環(huán)境,需要調(diào)整此函數(shù)
- 此延時函數(shù)是使用1T的指令周期進(jìn)行計算,與傳統(tǒng)的12T的MCU不同
- **************************************/
- sbit DQ=P1^0; //定義18B20數(shù)據(jù)腳為P1.0端口
- void DelayXus(uint n)
- {
- while (n--)
- {
- _nop_();
- _nop_();
- }
- }
- void getTmp_Update()
- {
- uint TPL,TPH,tmpvalue,value;
- float t;
-
- Room_tmep=0;
- DS18B20_Reset(); //設(shè)備復(fù)位
- DS18B20_WriteByte(0xCC); //跳過ROM命令
- DS18B20_WriteByte(0x44); //開始轉(zhuǎn)換命令
-
- while (!DQ){}; //等待轉(zhuǎn)換完成
- DS18B20_Reset(); //設(shè)備復(fù)位
- DS18B20_WriteByte(0xCC); //跳過ROM命令
- DS18B20_WriteByte(0xBE); //讀暫存存儲器命令
- TPL = DS18B20_ReadByte(); //讀溫度低字節(jié)
- TPH = DS18B20_ReadByte(); //讀溫度高字節(jié)
- tmpvalue = TPH;
- tmpvalue <<= 8;
- tmpvalue |= TPL;
- value = tmpvalue;
- t = value * 0.0625; //使用DS18B20的默認(rèn)分辨率12位, 精確度為0.0625度, 即讀回數(shù)據(jù)的最低位代表0.0625度
- /* 如果將它放大100倍, 使顯示時可顯示小數(shù)點(diǎn)后兩位, 并對小數(shù)點(diǎn)后第三進(jìn)行4舍5入, 如t=11.0625, 進(jìn)行計數(shù)后, 得到value = 1106, 即11.06 度,
- 如t=-11.0625, 進(jìn)行計數(shù)后, 得到value = -1106, 即-11.06 度 */
- Room_tmep= t; //得到最終溫度
- }
- /**************************************
- 復(fù)位DS18B20,并檢測設(shè)備是否存在
- **************************************/
- void DS18B20_Reset()
- {
- uchar i=0;
- CY = 1;
- while (CY)
- {
-
- DQ = 0; //送出低電平復(fù)位信號
- DelayXus(240); //延時至少480us
- DelayXus(240);
- DQ = 1; //釋放數(shù)據(jù)線
- DelayXus(60); //等待60us
- CY = DQ; //檢測存在脈沖
- DelayXus(240); //等待設(shè)備釋放數(shù)據(jù)線
- DelayXus(180);
- i++;
- if (i>50){AD_Error_code=1;return;}else{if (AD_Error_code==1) {AD_Error_code=0;}}
- }
- }
- /**************************************
- 從DS18B20讀1字節(jié)數(shù)據(jù)
- **************************************/
- uint DS18B20_ReadByte()
- {
- uchar i;
- uchar dat = 0;
- for (i=0; i<8; i++) //8位計數(shù)器
- {
- dat >>= 1;
- DQ = 0; //開始時間片
- DelayXus(1); //延時等待
- DQ = 1; //準(zhǔn)備接收
- DelayXus(1); //接收延時
- if (DQ) dat |= 0x80; //讀取數(shù)據(jù)
- DelayXus(60); //等待時間片結(jié)束
- }
- return dat;
- }
- /**************************************
- 向DS18B20寫1字節(jié)數(shù)據(jù)
- **************************************/
- void DS18B20_WriteByte(uint dat)
- {
- uchar i;
- for (i=0; i<8; i++) //8位計數(shù)器
- {
- DQ = 0; //開始時間片
- DelayXus(1); //延時等待
- dat >>= 1; //送出數(shù)據(jù)
- DQ = CY;
- DelayXus(60); //等待時間片結(jié)束
- DQ = 1; //恢復(fù)數(shù)據(jù)線
- DelayXus(1); //恢復(fù)延時
- }
- }
復(fù)制代碼
|
-
-
Ds1820 源程序.zip
2018-5-6 20:23 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
42.39 KB, 下載次數(shù): 10, 下載積分: 黑幣 -5
C51 讀DS1820
|