⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ds18b20.h

📁 温度传感器
💻 H
字号:

#ifndef __ds18b20_h__
#define __ds18b20_h__

#include<reg51.h>
#include<intrins.h>
#include<absacc.h>

unsigned char *p;
unsigned int Temperature;
unsigned char temp_buff[9]; //存储读取的字节,read scratchpad为9字节,read rom ID为8字节
unsigned char temh_d;
unsigned char temm_d;
unsigned char teml_d;
unsigned char count;
unsigned int wend4=0;

sbit dq=P3^5;

/************************************************************
*Function:延时处理
*parameter:
*Return:
*Modify:
*************************************************************/
void TempDelay (unsigned char us)
{
    while(us--)
    {
       ; 
    }
}
/************************************************************
*Function:18B20初始化
*parameter:
*Return:
*Modify:
*************************************************************/
void Init18b20(void)
{
    unsigned char i;
    bit presence;
    presence=1;
    while(presence)
    {
        _nop_();
        dq=0;
        TempDelay(86); //delay 530 uS//80
        _nop_();
        dq=1;
        TempDelay(5); //delay 100 uS//14
        _nop_();
        _nop_();
        _nop_();
        i=1;
        while(i<=4)
        {
            presence=dq;//读18b20的应答信号
            i++;
        }
        //led=0;//detect 1820 fail!
    }
    //led=1;//detect 1820 success!
    TempDelay(20); //20
    _nop_();
    _nop_();
    dq = 1;
}
/************************************************************
*Function:向18B20写入一个字节
*parameter:
*Return:
*Modify:
*************************************************************/
void Write_Byte(unsigned char wr) //单字节写入
{
    unsigned char i;
    for (i=0;i<8;i++)
    {
        dq = 0;
        _nop_();
        dq=wr&0x01;
        TempDelay(5); //delay 45 uS //5
        _nop_();
        _nop_();
        dq=1;
        wr >>= 1;
     }
}
/************************************************************
*Function:读18B20的一个字节
*parameter:
*Return:
*Modify:
*************************************************************/
unsigned char Read_Byte(void) //读取单字节
{
    unsigned char i,u=0;
    for(i=0;i<8;i++)
    {
        dq = 0;
        u >>= 1;
        dq = 1;
        if(dq==1)
        {
            u |= 0x80;
        }
        TempDelay (4);
        _nop_();
    }
    return(u);
}
/************************************************************
*Function:读18B20
*parameter:
*Return:
*Modify:
*************************************************************/
void read_bytes (unsigned char j)
{
    unsigned char i;
    for(i=0;i<j;i++)
    {
        temp_buff[i]= Read_Byte();
       
    }
     
    
}

/**********************************************
        测温子程序
***********************************************/
void wendu()
{
    unsigned char teml,temh,temm;
    Init18b20();
    Write_Byte(0xcc);//跳过读序列号操作
    Write_Byte(0x44);//启动温度转换
    Init18b20();
    TempDelay(0xff);
    TempDelay(0xff);
    TempDelay(0xff);
    Write_Byte(0xcc);//跳过读序列号操作
    Write_Byte(0xbe);//读暂存器指令
    read_bytes (2);
    teml=temp_buff[0];
    temh=temp_buff[1];
    temm=teml;
    temm&=0x0f;
    teml>>=4;
    temh<<=4;
    count=teml|temh;
    temh_d=count/10;
    teml_d=count%10;
    temm_d=(temm*6)/10;
	wend4=temh_d*100+teml_d*10+temm_d;
}

/*********************************************************/

#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -