單總線協(xié)議(ds18b20)讀寫(xiě)詳解
作者:佚名 來(lái)源:本站原創(chuàng) 點(diǎn)擊數(shù):
… 更新時(shí)間:2013年12月13日 【字體:
大 中 小】
1、時(shí)序圖
2、51c代碼
3、總結(jié)
1、時(shí)序圖
1、初始化
.jpeg)
2、 寫(xiě)
.jpeg)
3、 讀
.jpeg)
2、代碼
#include <reg52.h>
sbit DQ = P2^0; //定義總線的I/O管腳
void SendByte(unsigned char dat);
void Delay4us() //延時(shí)4us
{
;
}
void Delay(unsigned char j) //一個(gè)循環(huán)15us
{
unsigned char i;
while(j--)
{
i = 5;
while (--i);
}
}
bit d18b20_qs() //18b20 起始
{
bit dat;
DQ = 1; //DQ復(fù)位
Delay4us();
DQ = 0; //拉低總線
Delay(35); //這里延時(shí)大概 525us
DQ = 1; //拉高總線
Delay(2); //這里延時(shí)大概 30us
dat = DQ; //讀取返回值(0:有18b20存在 1:是沒(méi)有)
Delay(2);
return dat; //返回?cái)?shù)值
}
void d18b20_x(unsigned char dat) //寫(xiě) 8 位 數(shù) 據(jù)
{
unsigned char i;
for(i=0;i<8;i++) //8位計(jì)數(shù)器
{
DQ = 0; //拉低總線
DQ = dat & 0x01; //取最低位賦值給總線
Delay(3); //延時(shí)45us
DQ = 1; //拉過(guò)總線準(zhǔn)備寫(xiě)下一個(gè)數(shù)據(jù)(或者總線復(fù)位)
dat >>= 1; //數(shù)據(jù)右移一位
}
}
unsigned char d18b20_d() //讀 8 位 數(shù) 據(jù)
{
unsigned char i,dat=0;
for(i=0;i<8;i++) //8位計(jì)數(shù)器
{
DQ = 0; //拉低總線
dat >>= 1; //數(shù)據(jù)右移一位
DQ = 1; //拉過(guò)總線(準(zhǔn)備讀取數(shù)據(jù))
if(DQ) //判斷是否是 1 如果是就把數(shù)據(jù)賦值給變量的高位
dat |= 0x80;
Delay(4);
}
return dat; //返回讀取到數(shù)據(jù)數(shù)據(jù)
}
unsigned int wd() //讀取溫度函數(shù)
{
unsigned char i = 0; //低8位數(shù)據(jù)
unsigned char j = 0; //高8位數(shù)據(jù)
unsigned int k = 0; //無(wú)符號(hào)16整形用來(lái)存儲(chǔ)讀回來(lái)的 16位溫度數(shù)據(jù)(j和i組合后的數(shù)據(jù))
d18b20_qs(); //初始化
d18b20_x(0xCC); //跳過(guò)序列號(hào)的操作(因?yàn)?8b20在總線上可以掛很多個(gè),這個(gè)序列號(hào)和網(wǎng)卡MAC地址類似)
d18b20_x(0x44); //開(kāi)啟溫度轉(zhuǎn)換
Delay(200); //開(kāi)啟溫度轉(zhuǎn)換需要時(shí)間這里延時(shí)一下
d18b20_qs(); //初始化
d18b20_x(0xCC); //跳過(guò)序列號(hào)的操作(因?yàn)?8b20在總線上可以掛很多個(gè),這個(gè)序列號(hào)和網(wǎng)卡MAC地址類似)
d18b20_x(0xBE); //讀取溫度寄存器等(共可讀9個(gè)寄存器) 前兩個(gè)就是溫度
i = d18b20_d(); //讀取低8位
j = d18b20_d(); //讀取高8位
k = j;
k <<= 8;
k = k + i;
return k; //返回讀取到的16位數(shù)據(jù)
}
void CSH (void) //初始化串口
{
SCON = 0x50; // SCON: 模式 1, 8-bit UART, 使能接收
TMOD |= 0x20; // TMOD: timer 1, mode 2, 8-bit 重裝
TH1 = 0xFD; // TH1: 重裝值 9600 波特率 晶振 11.0592MHz
TR1 = 1; // TR1: timer 1 打開(kāi)
EA = 1; //打開(kāi)總中斷
//ES = 1; //打開(kāi)串口中斷
}
void SendByte(unsigned char dat) //發(fā)送一個(gè)字符
{
SBUF = dat; //SBUF 串行數(shù)據(jù)緩沖器
while(!TI); //TI發(fā)送中斷標(biāo)志位 (當(dāng)數(shù)據(jù)發(fā)送完畢后由硬件置 1 否則等待硬件置 1)
TI = 0;
}
void main()
{
unsigned char i,j;
unsigned int w;
CSH();
while(1)
{
w = wd();
i= w & 0xff; //取低8位
j= (w >> 8)&0xff; //取高8位
SendByte(j); //通過(guò)串口把高8位數(shù)據(jù)返回給上位機(jī)
SendByte(i); //通過(guò)串口把低8位數(shù)據(jù)返回給上位機(jī)
P1 = j; //使用8個(gè)LED 輸出高8位數(shù)據(jù)
Delay(200); //延時(shí)3毫秒
Delay(200); //延時(shí)3毫秒
Delay(200); //延時(shí)3毫秒
Delay(200); //延時(shí)3毫秒
Delay(200); //延時(shí)3毫秒
Delay(200); //延時(shí)3毫秒
Delay(200); //延時(shí)3毫秒
Delay(200); //延時(shí)3毫秒
Delay(200); //延時(shí)3毫秒
Delay(200); //延時(shí)3毫秒
P1 = i; //使用8個(gè)LED輸出低8位數(shù)據(jù)
Delay(200); //延時(shí)3毫秒
Delay(200); //延時(shí)3毫秒
Delay(200); //延時(shí)3毫秒
Delay(200); //延時(shí)3毫秒
Delay(200); //延時(shí)3毫秒
Delay(200); //延時(shí)3毫秒
Delay(200); //延時(shí)3毫秒
Delay(200); //延時(shí)3毫秒
Delay(200); //延時(shí)3毫秒
Delay(200); //延時(shí)3毫秒
}
}
3、總結(jié)
1)使用的是11.0592的晶振
2)使用下面的公式可以計(jì)算出攝氏度的溫度
wd :讀取到的16位數(shù)據(jù)
攝氏度 = wd x 0.0625