專注電子技術(shù)學習與研究
當前位置:單片機教程網(wǎng) >> MCU設計實例 >> 瀏覽文章

單總線協(xié)議(ds18b20)讀寫詳解

作者:佚名   來源:本站原創(chuàng)   點擊數(shù):  更新時間:2013年12月13日   【字體:

1、時序圖
2、51c代碼
3、總結(jié)

                         1、時序圖
    1、初始化

 

    2、  寫

 

    3、  讀

 

 2、代碼

#include <reg52.h>
sbit DQ = P2^0; //定義總線的I/O管腳
void SendByte(unsigned char dat);
void Delay4us() //延時4us
{
;
}
void Delay(unsigned char j) //一個循環(huán)15us
{
unsigned char i;
while(j--)
{
i = 5;
while (--i);
}
}
bit d18b20_qs() //18b20 起始
{
bit dat;
DQ = 1; //DQ復位
Delay4us();
DQ = 0; //拉低總線
Delay(35); //這里延時大概 525us
DQ = 1; //拉高總線
Delay(2); //這里延時大概 30us
dat = DQ; //讀取返回值(0:有18b20存在 1:是沒有)
Delay(2);
return dat; //返回數(shù)值
}

void d18b20_x(unsigned char dat) //寫 8 位 數(shù) 據(jù)
{
unsigned char i;
for(i=0;i<8;i++) //8位計數(shù)器
{
DQ = 0; //拉低總線
DQ = dat & 0x01; //取最低位賦值給總線
Delay(3); //延時45us
DQ = 1; //拉過總線準備寫下一個數(shù)據(jù)(或者總線復位)
dat >>= 1; //數(shù)據(jù)右移一位
}
}
unsigned char d18b20_d() //讀 8 位 數(shù) 據(jù)
{
unsigned char i,dat=0;
for(i=0;i<8;i++) //8位計數(shù)器
{
DQ = 0; //拉低總線
dat >>= 1; //數(shù)據(jù)右移一位
DQ = 1; //拉過總線(準備讀取數(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; //無符號16整形用來存儲讀回來的 16位溫度數(shù)據(jù)(j和i組合后的數(shù)據(jù))

d18b20_qs(); //初始化
d18b20_x(0xCC); //跳過序列號的操作(因為18b20在總線上可以掛很多個,這個序列號和網(wǎng)卡MAC地址類似)
d18b20_x(0x44); //開啟溫度轉(zhuǎn)換
Delay(200); //開啟溫度轉(zhuǎn)換需要時間這里延時一下
d18b20_qs(); //初始化
d18b20_x(0xCC); //跳過序列號的操作(因為18b20在總線上可以掛很多個,這個序列號和網(wǎng)卡MAC地址類似)
d18b20_x(0xBE); //讀取溫度寄存器等(共可讀9個寄存器) 前兩個就是溫度
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 打開                         
    EA    = 1;                  //打開總中斷
    //ES    = 1;                  //打開串口中斷
}
void SendByte(unsigned char dat) //發(fā)送一個字符
{
 SBUF = dat; //SBUF 串行數(shù)據(jù)緩沖器
 while(!TI);  //TI發(fā)送中斷標志位 (當數(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); //通過串口把高8位數(shù)據(jù)返回給上位機
SendByte(i); //通過串口把低8位數(shù)據(jù)返回給上位機
P1 = j; //使用8個LED 輸出高8位數(shù)據(jù)
Delay(200); //延時3毫秒
Delay(200); //延時3毫秒
Delay(200); //延時3毫秒
Delay(200); //延時3毫秒
Delay(200); //延時3毫秒
Delay(200); //延時3毫秒
Delay(200); //延時3毫秒
Delay(200); //延時3毫秒
Delay(200); //延時3毫秒
Delay(200); //延時3毫秒


P1 = i; //使用8個LED輸出低8位數(shù)據(jù)

Delay(200); //延時3毫秒
Delay(200); //延時3毫秒
Delay(200); //延時3毫秒
Delay(200); //延時3毫秒
Delay(200); //延時3毫秒
Delay(200); //延時3毫秒
Delay(200); //延時3毫秒
Delay(200); //延時3毫秒
Delay(200); //延時3毫秒
Delay(200); //延時3毫秒

}
}
3、總結(jié)

 

1)使用的是11.0592的晶振
2)使用下面的公式可以計算出攝氏度的溫度
            wd :讀取到的16位數(shù)據(jù)
            攝氏度 = wd x 0.0625

關(guān)閉窗口

相關(guān)文章