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

51單片機的ds18b20驅(qū)動程序

作者:劉溫電   來源:本站原創(chuàng)   點擊數(shù):  更新時間:2013年11月26日   【字體:

 

//////////////////////////////////////////////////////頭文件///////////////////////////////////////////////////////
/*******************************************************************************
*  標(biāo)題:  18b20溫度芯片驅(qū)動程序                                                            *
*  文件:  18b20.h 18b20.c                                                                         *
*  日期:  2010-3-11                                                                                     *
*  版本:  1.12(試驗通過版)可用51hei單片機開發(fā)板               *
*  郵箱: 
acsdf139@163.com       qq:526083029                                 *
*  網(wǎng)站: 無                                                                                                  *
********************************************************************************
*  描述:                                                                                                          *
*         用法:首先在主程序內(nèi)添加頭文件#include"18b20.c"                 *
*      在程序中使用ReadTemp();獲取溫度芯片內(nèi)數(shù)據(jù)                             *
*      使用 temp_to_str();函數(shù)將時間數(shù)據(jù)轉(zhuǎn)換液晶字符                         *         
*                                                                                                                     *
********************************************************************************
*【版權(quán)】自己參照網(wǎng)上,朋友,以及書本資料上寫的,所以沒有版權(quán)       *
*【聲明】此程序,可用于學(xué)習(xí),參考及實際設(shè)計中,歡迎轉(zhuǎn)載復(fù)制以及建議批評     *
*【技術(shù)支持】qq:526083029  小張                                                      *
********************************************************************************/
 
#ifndef _18B20_H_
#define _18B20_H_
#define uchar unsigned char
#define rerom1820            0x33      // 讀ROM指令   
#define matchrom1820         0x55      // 匹配ROM指令   
#define skiprom1820          0xCC      // 跳過ROM指令   
#define searchrom1820        0xF0      // 搜索ROM指令   
#define alarmsearch1820      0xEC      // 報警搜索指令   
#define wrscratchpad1820     0x4E      // 寫暫存寄存器指令   
#define rescratchpad1820     0xBE      // 讀暫存寄存器指令   
#define copyscratchpad1820   0x48      // 復(fù)制暫存寄存器指令   
#define convert1820          0x44      // 啟動溫度轉(zhuǎn)換指令   
#define recalle21820         0xB8      // 重新調(diào)出E2PROM的數(shù)據(jù) 
/***********ds18b20延遲子函數(shù)*******/
extern void delaydq(uchar i);
/**********ds18b20初始化函數(shù)**********************/
extern void dqinit(void);
/***********ds18b20讀一個字節(jié)**************/ 
extern uchar re1820b(void);
/*************ds18b20寫一個字節(jié)****************/ 
extern void wr1820b(uchar dat);
extern void ReadTemp(void);
extern void temp_to_str();
#endif
///////////////////////////////////////////////////////////c程序//////////////////////////////////////////////
#include <AT89X52.H>
#include "18b20.h"
#include <intrins.h>
//管腳定義
sbit DQ=P3^0;
 
uchar TempBuffer[8];
uchar temp_value,temp2,xs,fflag;
             
 
/*************************************
函數(shù)名:delaydq
函數(shù)功能:延時
參數(shù):i(秒數(shù))
返回:無
備注:無
*************************************/
void delaydq(uchar i)
{
       while(i--);
}
/*************************************
函數(shù)名:dqinit
函數(shù)功能:初始化
參數(shù):無
返回:無
備注:無
*************************************/
void dqinit(void)
{
        uchar x=0;
        DQ = 1;          //DQ復(fù)位
        delaydq(8); //稍做延時
        DQ = 0;          //單片機將DQ拉低
        delaydq(80); //精確延時 大于 480us
        DQ = 1;          //拉高總線
        delaydq(14);
        x=DQ;            //稍做延時后 如果x=0則初始化成功 x=1則初始化失敗
        delaydq(20);
}
/*************************************
函數(shù)名:re1820b
函數(shù)功能:讀數(shù)據(jù)
參數(shù):無
返回:dat(數(shù)據(jù))
備注:無
*************************************/
uchar re1820b(void)
{
       uchar i=0;
       uchar dat = 0;
       for (i=8;i>0;i--)
        {
               DQ = 0; // 給脈沖信號
               dat>>=1;
               DQ = 1; // 給脈沖信號
               if(DQ)
               dat|=0x80;
               delaydq(4);
        }
      return(dat);
}
/*************************************
函數(shù)名:wr1820b
函數(shù)功能:寫數(shù)據(jù)
參數(shù):dat(數(shù)據(jù))
返回:無
備注:無
*************************************/
void wr1820b(uchar dat)
{
      unsigned char i=0;
      for (i=8; i>0; i--)
      {
            DQ = 0;
             DQ = dat&0x01;
           delaydq(5);
             DQ = 1;
           dat>>=1;
      }
}
/*************************************
函數(shù)名:ReadTemp
函數(shù)功能:溫度轉(zhuǎn)換
參數(shù):無
返回:無
備注:無
*************************************/
void ReadTemp(void)
{
       unsigned char a=0;
       unsigned char b=0;
       unsigned char t=0;
 
       dqinit();
       wr1820b(0xCC);        // 跳過讀序號列號的操作
       wr1820b(0x44);    // 啟動溫度轉(zhuǎn)換
 
       delaydq(100);      
       dqinit();
       wr1820b(0xCC);   //跳過讀序號列號的操作
       wr1820b(0xBE);  //讀取溫度寄存器等(共可讀9個寄存器) 前兩個就是溫度
 
       delaydq(100);
 
       a=re1820b();       //讀取溫度值低位
       b=re1820b();              //讀取溫度值高位
    if(b>127)
       {
       b=(256-b);a=(256-a);fflag=1; //負(fù)溫度求補碼
       }
       temp_value=b<<4;
       temp_value+=(a&0xf0)>>4;  
    temp2=a&0x0f;             //小數(shù)的值
      
 
   xs=temp2*0.0625*10; //小數(shù)位,若為0.5則算為5來顯示   xs小數(shù)
 
        
}
 
/*************************************
函數(shù)名:temp_to_str
函數(shù)功能:溫度顯示
參數(shù):無
返回:無
備注:無
*************************************/
void temp_to_str()   //溫度數(shù)據(jù)轉(zhuǎn)換成液晶字符顯示
{
 if(fflag==1)
 { TempBuffer[0]='-';
            TempBuffer[1]=temp_value/10+'0'; //十位
            TempBuffer[2]=temp_value%10+'0';  //個位
            TempBuffer[3]='.';
            TempBuffer[4]=xs+'0';
            TempBuffer[5]=0xdf;   //溫度符號
            TempBuffer[6]='C';
            TempBuffer[7]='\0';
 }
 else
 {
            TempBuffer[0]='+';
            TempBuffer[1]=temp_value/10+'0';  //十位
            TempBuffer[2]=temp_value%10+'0'; //個位
            TempBuffer[3]='.';
            TempBuffer[4]=xs+'0';
            TempBuffer[5]=0xdf;   //溫度符號
            TempBuffer[6]='C';
            TempBuffer[7]='\0';
 
 }
 
}
 
關(guān)閉窗口

相關(guān)文章