一个一线接口的温度传感器
DS18B20 发送所有的命令和数据都是字节的低位在前
每个器件都有自己的地址序列号
可以设置测量精度有四种,9---12位(0.5℃,0.25℃,0.125℃和 0.0625℃。),出场默认12位最高精度

高5位是0-->温度大于0, 高5位是1-->温度小于0
12位精度时,测量温度大于0,温度 = 测量数值 x 0.0625;
测量温度小于0, 温度 = (测量数值取反+1) x 0.0625
初始化时序
复位 : 引脚配置为输出模式。主机输出低电平 时间 480us ~ 960us,以产生复位脉冲后输出高电平线延时 15~60 us。
检查 :引脚配置为接收模式。接着 DS18B20 拉低总线 60~240 us,以产生低电平应答脉冲,若为低 电平,还要做延时,其延时的时间从外部上拉电阻将单总线拉高算起最少要 480 us
写1 :主机输出低电平延时2us,然后输出高电平延时60us
写0 :主机输出高电平延时60us,然后输出低电平延时2us
读时序:主机输出低电平延时 2us,然后主机转入输入模式延 时 12us,然后读取单总线当前的电平,然后延时 50us。
DS18B20 的典型温度读取过程为:复位→发 SKIP ROM 命令( 0XCC)→发开始 转换命令( 0X44)→延时→复位→发送 SKIP ROM 命令( 0XCC)→发读存储器 命令( 0XBE)→连续读出两个字节数据(即温度)→结束。
ds18b20.c
#include "ds18b20.h"
#include "SysTick.h"
/*******************************************************************************
* 函 数 名 : DS18B20_IO_IN
* 函数功能 : DS18B20_IO输入配置
* 输 入 : 无
* 输 出 : 无
*******************************************************************************/
void DS18B20_IO_IN(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin=DS18B20_PIN;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;//上拉输入
GPIO_Init(DS18B20_PORT,&GPIO_InitStructure);
}
/*******************************************************************************
* 函 数 名 : DS18B20_IO_OUT
* 函数功能 : DS18B20_IO输出配置
* 输 入 : 无
* 输 出 : 无
*******************************************************************************/
void DS18B20_IO_OUT(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin=DS18B20_PIN;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;//推挽输出
GPIO_Init(DS18B20_PORT,&GPIO_InitStructure);
}
/*******************************************************************************
* 函 数 名 : DS18B20_Reset
* 函数功能 : 复位DS18B20
* 输 入 : 无
* 输 出 : 无
*******************************************************************************/
void DS18B20_Reset(void)
{
DS18B20_IO_OUT(); //SET PG11 OUTPUT
DS18B20_DQ_OUT=0; //拉低DQ
delay_us(750); //拉低750us
DS18B20_DQ_OUT=1; //DQ=1
delay_us(15); //15US
}
/*******************************************************************************
* 函 数 名 : DS18B20_Check
* 函数功能 : 检测DS18B20是否存在
* 输 入 : 无
* 输 出 : 1:未检测到DS18B20的存在,0:存在
*******************************************************************************/
u8 DS18B20_Check(void)
{
u8 retry=0;
DS18B20_IO_IN();//SET PG11 INPUT
while (DS18B20_DQ_IN&&retry<200)
{
retry++;
delay_us(1);
};
if(retry>=200)return 1;
else retry=0;
while (!DS18B20_DQ_IN&&retry<240)
{
retry++;
delay_us(1);
};
if(retry>=240)return 1;
return 0;
}
/*******************************************************************************
* 函 数 名 : DS18B20_Read_Bit
* 函数功能 : 从DS18B20读取一个位
* 输 入 : 无
* 输 出 : 1/0
*******************************************************************************/
u8 DS18B20_Read_Bit(void) // read one bit
{
u8 data;
DS18B20_IO_OUT();//SET PG11 OUTPUT
DS18B20_DQ_OUT=0;
delay_us(2);
DS18B20_DQ_OUT=1;
DS18B20_IO_IN();//SET PG11 INPUT
delay_us(12);
if(DS18B20_DQ_IN)data=1;
else data=0;
delay_us(50);
return data;
}
/*******************************************************************************
* 函 数 名 : DS18B20_Read_Byte
* 函数功能 : 从DS18B20读取一个字节
* 输 入 : 无
* 输 出 : 一个字节数据
*******************************************************************************/
u8 DS18B20_Read_Byte(void) // read one byte
{
u8 i,j,dat;
dat=0;
for (i=1;i<=8;i++)
{
j=DS18B20_Read_Bit();
dat=(j<<7)|(dat>>1);
}
return dat;
}
/*******************************************************************************
* 函 数 名 : DS18B20_Write_Byte
* 函数功能 : 写一个字节到DS18B20
* 输 入 : dat:要写入的字节
* 输 出 : 无
*******************************************************************************/
void DS18B20_Write_Byte(u8 dat)
{
u8 j;
u8 testb;
DS18B20_IO_OUT();//SET PG11 OUTPUT;
for (j=1;j<=8;j++)
{
testb=dat&0x01;
dat=dat>>1;
if (testb)
{
DS18B20_DQ_OUT=0;// Write 1
delay_us(2);
DS18B20_DQ_OUT=1;
delay_us(60);
}
else
{
DS18B20_DQ_OUT=0;// Write 0
delay_us(60);
DS18B20_DQ_OUT=1;
delay_us(2);
}
}
}
/*******************************************************************************
* 函 数 名 : DS18B20_Start
* 函数功能 : 开始温度转换
* 输 入 : 无
* 输 出 : 无
*******************************************************************************/
void DS18B20_Start(void)// ds1820 start convert
{
DS18B20_Reset();
DS18B20_Check();
DS18B20_Write_Byte(0xcc);// skip rom
DS18B20_Write_Byte(0x44);// convert
}
/*******************************************************************************
* 函 数 名 : DS18B20_Init
* 函数功能 : 初始化DS18B20的IO口 DQ 同时检测DS的存在
* 输 入 : 无
* 输 出 : 1:不存在,0:存在
*******************************************************************************/
u8 DS18B20_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(DS18B20_PORT_RCC,ENABLE);
GPIO_InitStructure.GPIO_Pin=DS18B20_PIN;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_Init(DS18B20_PORT,&GPIO_InitStructure);
DS18B20_Reset();
return DS18B20_Check();
}
/*******************************************************************************
* 函 数 名 : DS18B20_GetTemperture
* 函数功能 : 从ds18b20得到温度值
* 输 入 : 无
* 输 出 : 温度数据
*******************************************************************************/
float DS18B20_GetTemperture(void)
{
u16 temp;
u8 a,b;
float value;
DS18B20_Start(); // ds1820 start convert
DS18B20_Reset();
DS18B20_Check();
DS18B20_Write_Byte(0xcc);// skip rom
DS18B20_Write_Byte(0xbe);// convert
a=DS18B20_Read_Byte(); // LSB
b=DS18B20_Read_Byte(); // MSB
temp=b;
temp=(temp<<8)+a;
if((temp&0xf800)==0xf800)
{
temp=(~temp)+1;
value=temp*(-0.0625);
}
else
{
value=temp*0.0625;
}
return value;
}
ds18b20.h
#ifndef _ds18b20_H
#define _ds18b20_H
#include "system.h"
/* DS18B20时钟端口、引脚定义 */
#define DS18B20_PORT GPIOG
#define DS18B20_PIN (GPIO_Pin_11)
#define DS18B20_PORT_RCC RCC_APB2Periph_GPIOG
////IO操作函数
#define DS18B20_DQ_OUT PGout(11) //数据端口 PG11
#define DS18B20_DQ_IN PGin(11) //数据端口 PG11
u8 DS18B20_Init(void); //初始化DS18B20
float DS18B20_GetTemperture(void); //获取温度
void DS18B20_Start(void); //开始温度转换
void DS18B20_Write_Byte(u8 dat);//写入一个字节
u8 DS18B20_Read_Byte(void); //读出一个字节
u8 DS18B20_Read_Bit(void); //读出一个位
u8 DS18B20_Check(void); //检测是否存在DS18B20
void DS18B20_Reset(void); //复位DS18B20
#endif
main.c
#include "system.h"
#include "SysTick.h"
#include "led.h"
#include "usart.h"
#include "ds18b20.h"
int main()
{
u8 i=0;
float temper;
SysTick_Init(72);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //中断优先级分组 分2组
LED_Init();
USART1_Init(9600);
while(DS18B20_Init())
{
printf("DS18B20检测失败,请插好!\r\n");
delay_ms(500);
}
printf("DS18B20检测成功!\r\n");
while(1)
{
i++;
if(i%20==0)
{
led1=!led1;
}
if(i%50==0)
{
temper=DS18B20_GetTemperture();
if(temper<0)
{
printf("检测的温度为:-");
}
else
{
printf("检测的温度为: ");
}
printf("%.2f°C\r\n",temper);
}
delay_ms(10);
}
}