📄 ds18b20_driver.h
字号:
/************************************************************************
//
// Author : 蓝韵-生化部
//
// Date : 5 December 2008
//
// File : DS18B20_Test.c
//
// Hardware : MN 51 Singlechip develop Board, DB18B20
//
// Version : V1.0
//
// Description : 程序完成对DS18B20温度传感器的初始化并实时采集环境温度
//
*************************************************************************/
#ifndef __DS18B20_Driver_H__
#define __DS18B20_Driver_H__
#include <reg52.h>
unsigned char Temp_Data; // 测量到的温度值
sbit DQ = P3^2; // DS18B20的数据输入/输出脚DQ,根据情况设定
/***********************************************************
** Function name: Delay_ms( )
** Descriptions: 延时毫秒函数
** Input: unsigned int t
** Output: 无
** Created by: 王 斌
** Created Date: 2008-11-20
**----------------------------------------------------------
** Modified by:
** Modified Date:
**----------------------------------------------------------
*************************************************************/
void Delay_ms(unsigned int t)
{
unsigned int i;
while(t--)
{
for (i=0;i<125;i++)
{ ;}
}
}
/***********************************************************
** Function name: DS18B20_Reset( )
** Descriptions: DS18B20复位函数
** Input: 无
** Output: 无
** Created by: 王 斌
** Created Date: 2008-11-20
**----------------------------------------------------------
** Modified by:
** Modified Date:
**----------------------------------------------------------
*************************************************************/
void DS18B20_Reset(void)
{
unsigned int i;
DQ = 0;
i = 100;
while (i>0) i--;
DQ = 1;
i = 4;
while (i>0) i--;
}
/***********************************************************
** Function name: RdBit( )
** Descriptions: 接收DS18B20温度转换值,因为其是每1bit传输的
** Input: 无
** Output: Dat
** Created by: 王 斌
** Created Date: 2008-11-20
**----------------------------------------------------------
** Modified by:
** Modified Date:
**----------------------------------------------------------
*************************************************************/
bit RdBit(void)
{
unsigned int i;
bit Dat;
DQ = 0;
i++;
DQ = 1;
i++;i++;
Dat = DQ;
i = 8;
while(i>0) i--;
return (Dat);
}
/***********************************************************
** Function name: RdByte( )
** Descriptions: 接收DS18B20温度转换值,收集8bit数据
** Input: 无
** Output: Dat
** Created by: 王 斌
** Created Date: 2008-11-20
**----------------------------------------------------------
** Modified by:
** Modified Date:
**----------------------------------------------------------
*************************************************************/
unsigned char RdByte(void)
{
unsigned char i;
unsigned char j;
unsigned char Dat = 0;
for (i=1; i<=8; i++)
{
j = RdBit();
Dat = (j<<7)|(Dat>>1);
}
return(Dat);
}
/***********************************************************
** Function name: WrByte( )
** Descriptions: 向18b20写入数据
** Input: Dat
** Output: 无
** Created by: 王 斌
** Created Date: 2008-11-20
**----------------------------------------------------------
** Modified by:
** Modified Date:
**----------------------------------------------------------
*************************************************************/
void WrByte(unsigned char Dat)
{
unsigned int i;
unsigned char j;
bit btmp;
for(j=1; j<=8; j++)
{
btmp = Dat&0x01;
Dat = Dat>>1;
if (btmp)
{
DQ = 0;
i++;i++;
DQ = 1;
i = 8;
while(i>0) i--;
}
else
{
DQ = 0;
i = 8;
while(i>0) i--;
DQ = 1;
i++;
i++;
}
}
}
/***********************************************************
** Function name: Convert( )
** Descriptions: 启动DS18B20的转换
** Input: 无
** Output: Dat
** Created by: 王 斌
** Created Date: 2008-11-20
**----------------------------------------------------------
** Modified by:
** Modified Date:
**----------------------------------------------------------
*************************************************************/
void Convert(void)
{
DS18B20_Reset();
Delay_ms(1);
WrByte(0xcc);
WrByte(0x44);
}
/***********************************************************
** Function name: RdTemp( )
** Descriptions: 完整的读出采集一次温度值,并转换成16进制
** Input: 无
** Output: 无
** Created by: 王 斌
** Created Date: 2008-11-20
**----------------------------------------------------------
** Modified by:
** Modified Date:
**----------------------------------------------------------
*************************************************************/
void RdTemp(void)
{
unsigned char Tmp_L,Tmp_H;
DS18B20_Reset();
Delay_ms(1);
WrByte(0xcc);
WrByte(0xbe);
Tmp_L = RdByte();
Tmp_H = RdByte();
Temp_Data=Tmp_L/16+Tmp_H*16;
}
/***********************************************************
** Function name: UART_Init( )
** Descriptions: 串口初始化,9600BPS
** Input: 无
** Output: 无
** Created by: 王 斌
** Created Date: 2008-11-20
**----------------------------------------------------------
** Modified by:
** Modified Date:
**----------------------------------------------------------
*************************************************************/
void UART_Init(void)
{
EA = 0;
TMOD =(TMOD & 0X0f) | 0X20;
TH1 = -36864000L/12/32/9600;
TL1 = -36864000L/12/32/9600;
TR1 = 1;
SCON = 0X50;
PCON |= 0X80;
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -