<dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><small id="yhprb"></small><dfn id="yhprb"></dfn><small id="yhprb"><delect id="yhprb"></delect></small><small id="yhprb"></small><small id="yhprb"></small> <delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"></dfn><dfn id="yhprb"></dfn><s id="yhprb"><noframes id="yhprb"><small id="yhprb"><dfn id="yhprb"></dfn></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><small id="yhprb"></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn> <small id="yhprb"></small><delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn>

新聞中心

EEPW首頁(yè) > 嵌入式系統 > 設計應用 > 35. DS18B20數字溫度計使用

35. DS18B20數字溫度計使用

作者: 時(shí)間:2016-11-18 來(lái)源:網(wǎng)絡(luò ) 收藏
1.DS18B20基本知識
DS18B20數字溫度計是DALLAS公司生產(chǎn)的1-Wire,即單總線(xiàn)器件,具有線(xiàn)路簡(jiǎn)單,體積小的特點(diǎn)。因此用它來(lái)組成一個(gè)測溫系統,具有線(xiàn)路簡(jiǎn)單,在一根通信線(xiàn),可以?huà)旌芏噙@樣的數字溫度計,十分方便。
1、DS18B20產(chǎn)品的特點(diǎn)
(1)、只要求一個(gè)端口即可實(shí)現通信。
(2)、在DS18B20中的每個(gè)器件上都有獨一無(wú)二的序列號。
(3)、實(shí)際應用中不需要外部任何元器件即可實(shí)現測溫。
(4)、測量溫度范圍在-55。C到+125。C之間。
(5)、數字溫度計的分辨率用戶(hù)可以從9位到12位選擇。
(6)、內部有溫度上、下限告警設置。
2、DS18B20的引腳介紹
TO-92封裝的DS18B20的引腳排列見(jiàn)圖1,其引腳功能描述見(jiàn)表1。
(底視圖)
圖1

表1DS18B20詳細引腳功能描述
序號
名稱(chēng)
引腳功能描述
1
GND
地信號
2
DQ
數據輸入/輸出引腳。開(kāi)漏單總線(xiàn)接口引腳。當被用著(zhù)在寄生電源下,也可以向器件提供電源。
3
VDD
可選擇的VDD引腳。當工作于寄生電源時(shí),此引腳必須接地。
3.DS18B20的使用方法
由于DS18B20采用的是1-Wire總線(xiàn)協(xié)議方式,即在一根數據線(xiàn)實(shí)現數據的雙向傳輸,而對AT89S51單片機來(lái)說(shuō),硬件上并不支持單總線(xiàn)協(xié)議,因此,我們必須采用軟件的方法來(lái)模擬單總線(xiàn)的協(xié)議時(shí)序來(lái)完成對DS18B20芯片的訪(fǎng)問(wèn)。
由于DS18B20是在一根I/O線(xiàn)上讀寫(xiě)數據,因此,對讀寫(xiě)的數據位有著(zhù)嚴格的時(shí)序要求。DS18B20有嚴格的通信協(xié)議來(lái)保證各位數據傳輸的正確性和完整性。該協(xié)議定義了幾種信號的時(shí)序:初始化時(shí)序、讀時(shí)序、寫(xiě)時(shí)序。所有時(shí)序都是將主機作為主設備,單總線(xiàn)器件作為從設備。而每一次命令和數據的傳輸都是從主機主動(dòng)啟動(dòng)寫(xiě)時(shí)序開(kāi)始,如果要求單總線(xiàn)器件回送數據,在進(jìn)行寫(xiě)命令后,主機需啟動(dòng)讀時(shí)序完成數據接收。數據和命令的傳輸都是低位在先。
DS18B20的復位時(shí)序

DS18B20的讀時(shí)序
對于DS18B20的讀時(shí)序分為讀0時(shí)序和讀1時(shí)序兩個(gè)過(guò)程。
對于DS18B20的讀時(shí)隙是從主機把單總線(xiàn)拉低之后,在15秒之內就得釋放單總線(xiàn),以讓DS18B20把數據傳輸到單總線(xiàn)上。DS18B20在完成一個(gè)讀時(shí)序過(guò)程,至少需要60us才能完成。

DS18B20的寫(xiě)時(shí)序
對于DS18B20的寫(xiě)時(shí)序仍然分為寫(xiě)0時(shí)序和寫(xiě)1時(shí)序兩個(gè)過(guò)程。
對于DS18B20寫(xiě)0時(shí)序和寫(xiě)1時(shí)序的要求不同,當要寫(xiě)0時(shí)序時(shí),單總線(xiàn)要被拉低至少60us,保證DS18B20能夠在15us到45us之間能夠正確地采樣IO總線(xiàn)上的“0”電平,當要寫(xiě)1時(shí)序時(shí),單總線(xiàn)被拉低之后,在15us之內就得釋放單總線(xiàn)。



4.實(shí)驗任務(wù)
用一片DS18B20構成測溫系統,測量的溫度精度達到0.1度,測量的溫度的范圍在-20度到+100度之間,用8位數碼管顯示出來(lái)。
5.電路原理

6.系統板上硬件連線(xiàn)
(1).把“單片機系統”區域中的P0.0-P0.7用8芯排線(xiàn)連接到“動(dòng)態(tài)數碼顯示”區域中的ABCDEFGH端子上。
(2).把“單片機系統”區域中的P2.0-P2.7用8芯排線(xiàn)連接到“動(dòng)態(tài)數碼顯示”區域中的S1S2S3S4S5S6S7S8端子上。
(3).把DS18B20芯片插入“四路單總線(xiàn)”區域中的任一個(gè)插座中,注意電源與地信號不要接反。
(4).把“四路單總線(xiàn)”區域中的對應的DQ端子連接到“單片機系統”區域中的P3.7/RD端子上。
7.C語(yǔ)言源程序
#include
#include
unsignedcharcodedisplaybit[]={0xfe,0xfd,0xfb,0xf7,
0xef,0xdf,0xbf,0x7f};
unsignedcharcodedisplaycode[]={0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71,0x00,0x40};
unsignedcharcodedotcode[32]={0,3,6,9,12,16,19,22,
25,28,31,34,38,41,44,48,
50,53,56,59,63,66,69,72,
75,78,81,84,88,91,94,97};
unsignedchardisplaycount;
unsignedchardisplaybuf[8]={16,16,16,16,16,16,16,16};
unsignedchartimecount;
unsignedcharreaddata[8];
sbitDQ=P3^7;
bitsflag;
bitresetpulse(void)
{
unsignedchari;
DQ=0;
for(i=255;i>0;i--);
DQ=1;
for(i=60;i>0;i--);
return(DQ);
for(i=200;i>0;i--);
}
voidwritecommandtods18b20(unsignedcharcommand)
{
unsignedchari;
unsignedcharj;
for(i=0;i<8;i++)
{
if((command&0x01)==0)
{
DQ=0;
for(j=35;j>0;j--);
DQ=1;
}
else
{
DQ=0;
for(j=2;j>0;j--);
DQ=1;
for(j=33;j>0;j--);
}
command=_cror_(command,1);
}
}
unsignedcharreaddatafromds18b20(void)
{
unsignedchari;
unsignedcharj;
unsignedchartemp;
temp=0;
for(i=0;i<8;i++)
{
temp=_cror_(temp,1);
DQ=0;
_nop_();
_nop_();
DQ=1;
for(j=10;j>0;j--);
if(DQ==1)
{
temp=temp|0x80;
}
else
{
temp=temp|0x00;
}
for(j=200;j>0;j--);
}
return(temp);
}
voidmain(void)
{
TMOD=0x01;
TH0=(65536-4000)/256;
TL0=(65536-4000)%256;
ET0=1;
EA=1;
while(resetpulse());
writecommandtods18b20(0xcc);
writecommandtods18b20(0x44);
TR0=1;
while(1)
{
;
}
}
voidt0(void)interrupt1using0
{
unsignedcharx;
unsignedintresult;
TH0=(65536-4000)/256;
TL0=(65536-4000)%256;
if(displaycount==2)
{
P0=displaycode[displaybuf[displaycount]]|0x80;
}
else
{
P0=displaycode[displaybuf[displaycount]];
}
P2=displaybit[displaycount];
displaycount++;
if(displaycount==8)
{
displaycount=0;
}
timecount++;
if(timecount==150)
{
timecount=0;
while(resetpulse());
writecommandtods18b20(0xcc);
writecommandtods18b20(0xbe);
readdata[0]=readdatafromds18b20();
readdata[1]=readdatafromds18b20();
for(x=0;x<8;x++)
{
displaybuf[x]=16;
}
sflag=0;
if((readdata[1]&0xf8)!=0x00)
{
sflag=1;
readdata[1]=~readdata[1];
readdata[0]=~readdata[0];
result=readdata[0]+1;
readdata[0]=result;
if(result>255)
{
readdata[1]++;
}
}
readdata[1]=readdata[1]<<4;
readdata[1]=readdata[1]&0x70;
x=readdata[0];
x=x>>4;
x=x&0x0f;
readdata[1]=readdata[1]|x;
x=2;
result=readdata[1];
while(result/10)
{
displaybuf[x]=result%10;
result=result/10;
x++;
}
displaybuf[x]=result;
if(sflag==1)
{
displaybuf[x+1]=17;
}
x=readdata[0]&0x0f;
x=x<<1;
displaybuf[0]=(dotcode[x])%10;
displaybuf[1]=(dotcode[x])/10;
while(resetpulse());
writecommandtods18b20(0xcc);
writecommandtods18b20(0x44);
}
}


關(guān)鍵詞: DS18B20數字溫度

評論


技術(shù)專(zhuān)區

關(guān)閉
国产精品自在自线亚洲|国产精品无圣光一区二区|国产日产欧洲无码视频|久久久一本精品99久久K精品66|欧美人与动牲交片免费播放
<dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><small id="yhprb"></small><dfn id="yhprb"></dfn><small id="yhprb"><delect id="yhprb"></delect></small><small id="yhprb"></small><small id="yhprb"></small> <delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"></dfn><dfn id="yhprb"></dfn><s id="yhprb"><noframes id="yhprb"><small id="yhprb"><dfn id="yhprb"></dfn></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><small id="yhprb"></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn> <small id="yhprb"></small><delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn>