STM32F103C8T6驱动DS18B20温度传感器程序
本项目基于 STM32F103C8T6 微控制器,实现了对 DS18B20 单总线数字温度传感器的高精度、稳定驱动与实时温度采集。采用经典的 1-Wire 通信协议,通过 GPIO 精确模拟复位时序、读写时序及 ROM/暂存器操作指令,支持 Skip ROM 方式快速通信(适用于单传感器场景)。主程序以约 900ms 周期持续采集,实现稳定、可靠的环境温度感知,最终将原始数据转换为带符号的浮点温度值(℃)。
一、完整代码
DS18B20温度模块驱动程序头文件(DS18B20.h)
c
#ifndef __DS18B20_H__
#define __DS18B20_H__
#include"stm32f10x.h"
#define SkipROM 0xCC //跳过ROM
#define SearchROM 0xF0 //搜索ROM
#define ReadROM 0x33 //读ROM
#define MatchROM 0x55 //匹配ROM
#define AlarmROM 0xEC //告警ROM
#define StartConvert 0x44 // 开始温度转换
#define ReadScratchpad 0xBE // 读暂存器的9个字节
#define WriteScratchpad 0x4E // 写暂存器的温度告警TH和TL
#define CopyScratchpad 0x48 // 将暂存器复制到 EEPROM
#define RecallEEPROM 0xB8 // 将 EEPROM 数据恢复到暂存器
#define ReadPower 0xB4 // 读电源的供电方式
void ds18b20_init(void);
unsigned short ds18b20_read(void);
#endif
DS18B20 温度传感器驱动程序(DS18B20.c)
c
#include "DS18B20.h"
#include "Delay.h"
#define EnableINT()
#define DisableINT()
#define DS_PORT GPIOA
#define DS_DQIO GPIO_Pin_1
#define DS_RCC_PORT RCC_APB2Periph_GPIOA
#define DS_PRECISION 0x7f // 12位精度
#define DS_AlarmTH 0x64 // 高温告警
#define DS_AlarmTL 0x8a // 低温告警
#define DS_CONVERT_TICK 1000
#define ResetDQ() GPIO_ResetBits(DS_PORT,DS_DQIO)
#define SetDQ() GPIO_SetBits(DS_PORT,DS_DQIO)
#define GetDQ() GPIO_ReadInputDataBit(DS_PORT,DS_DQIO)
static unsigned char TempX_TAB[16]={0x00,0x01,0x01,0x02,0x03,0x03,0x04,0x04,0x05,0x06,0x06,0x07,0x08,0x08,0x09,0x09};
// DS18B20 复位
unsigned char ResetDS18B20(void)
{
unsigned char resport;
SetDQ();
Delay_us(50);
ResetDQ();
Delay_us(500); //500us (该时间的时间范围可以从480到960微秒)
SetDQ();
Delay_us(40); //40us
//resport = GetDQ();
while(GetDQ());
Delay_us(500); //500us
SetDQ();
return resport;
}
void DS18B20WriteByte(unsigned char Dat)
{
unsigned char i;
for(i=8;i>0;i--)
{
ResetDQ(); //在15u内送数到数据线上,DS18B20在15-60u读数
Delay_us(5); //5us
if(Dat & 0x01)
SetDQ();
else
ResetDQ();
Delay_us(65); //65us
SetDQ();
Delay_us(2); //连续两位间应大于1us
Dat >>= 1;
}
}
unsigned char DS18B20ReadByte(void)
{
unsigned char i,Dat;
SetDQ();
Delay_us(5);
for(i=8;i>0;i--)
{
Dat >>= 1;
ResetDQ(); //从读时序开始到采样信号线必须在15u内,且采样尽量安排在15u的最后
Delay_us(5); //5us
SetDQ();
Delay_us(5); //5us
if(GetDQ())
Dat|=0x80;
else
Dat&=0x7f;
Delay_us(65); //65us
SetDQ();
}
return Dat;
}
void ReadRom(unsigned char *Read_Addr)
{
unsigned char i;
DS18B20WriteByte(ReadROM);
for(i=8;i>0;i--)
{
*Read_Addr=DS18B20ReadByte();
Read_Addr++;
}
}
void DS18B20Init(unsigned char Precision,unsigned char AlarmTH,unsigned char AlarmTL)
{
DisableINT();
ResetDS18B20();
DS18B20WriteByte(SkipROM);
DS18B20WriteByte(WriteScratchpad);
DS18B20WriteByte(AlarmTL);
DS18B20WriteByte(AlarmTH);
DS18B20WriteByte(Precision);
ResetDS18B20();
DS18B20WriteByte(SkipROM);
DS18B20WriteByte(CopyScratchpad);
EnableINT();
while(!GetDQ()); //等待复制完成
}
void DS18B20StartConvert(void)
{
DisableINT();
ResetDS18B20();
DS18B20WriteByte(SkipROM);
DS18B20WriteByte(StartConvert);
EnableINT();
}
void DS18B20_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(DS_RCC_PORT, ENABLE);
GPIO_InitStructure.GPIO_Pin = DS_DQIO;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD; //开漏输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //2M时钟速度
GPIO_Init(DS_PORT, &GPIO_InitStructure);
}
void ds18b20_init(void)
{
DS18B20_Configuration();
DS18B20Init(DS_PRECISION, DS_AlarmTH, DS_AlarmTL);
DS18B20StartConvert();
}
unsigned short ds18b20_read(void)
{
unsigned char TemperatureL,TemperatureH;
unsigned int Temperature;
DisableINT();
ResetDS18B20();
DS18B20WriteByte(SkipROM);
DS18B20WriteByte(ReadScratchpad);
TemperatureL=DS18B20ReadByte();
TemperatureH=DS18B20ReadByte();
ResetDS18B20();
EnableINT();
if(TemperatureH & 0x80)
{
TemperatureH=(~TemperatureH) | 0x08;
TemperatureL=~TemperatureL+1;
if(TemperatureL==0)
TemperatureH+=1;
}
TemperatureH=(TemperatureH<<4)+((TemperatureL&0xf0)>>4);
TemperatureL=TempX_TAB[TemperatureL&0x0f];
//bit0-bit7为小数位,bit8-bit14为整数位,bit15为正负位
Temperature=TemperatureH;
Temperature=(Temperature<<8) | TemperatureL;
DS18B20StartConvert();
return Temperature;
//返回16位数据 bit0-bit7为小数位,bit8-bit14为整数位,bit15为正负位
}
主程序文件(main.c)
c
#include "stm32f10x.h"
#include "DS18B20.h"
#include "Delay.h"
volatile float temperature;
int main(void)
{
/*用户变量定义*/
/*用户函数初始化*/
ds18b20_init();
while (1)
{
unsigned short buffer = ds18b20_read();
temperature = ((buffer >> 8) & 0x7F) + (buffer & 0xFF) * 0.1f;
if (buffer & 0x8000)
temperature = -temperature;
if (temperature >= 0)
temperature = (int)(temperature * 10 + 0.5f) / 10.0f;
else
temperature = (int)(temperature * 10 - 0.5f) / 10.0f;
Delay_ms(900);
}
}
二、硬件说明
采用TO-92封装型DS18B20数字温度计,提供9位至12位摄氏度温度测量,并具有非易失性用户可编程上下触发点报警功能。DS18B20通过1线总线通信,根据定义,只需要一条数据线(和地线)与中央微处理器通信。它的工作温度范围为-55°C至+125°C,在-10°C至+85°C的范围内精确到0.5°C。此外,DS18B20可以直接从数据线(寄生电源)获得功率,供电:3.0V - 5.5V,无需外部电源。
引脚功能定义图
硬件实物图
硬件模块原理图
三、实物操作演示
硬件接线
- V D D V_{DD} VDD:连接3.3V
- GND:连接GND
- DQ:连接PA1
实物连接图
编译调试
编译并下载程序至单片机中
打开调试,在Watch 1窗口可以看到实时温度数据。
调试数据输出
结束~
例程资源: https://download.csdn.net/download/hsdujdjrjrj/92588307
参考博客:https://zhuanlan.zhihu.com/p/665316098