STM32CubeMX+Proteus仿真DS18B20

本教程介绍了如何使用STM32F103R6通过STM32CubeMX和Keil5配置开发环境,结合DS18B20采集温度,并通过USART1发送至串口。详细步骤包括DS18B20的引脚说明、配置、延时函数编写、主函数实现以及Proteus仿真。在Proteus仿真中,DS18B20外接电源,通过5K上拉电阻连接,成功读取并显示温度值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文说明

开发工具:STM32CubeMX+Keil5
开发芯片:STM32F103R6
采温模块:DS18B20
仿真软件:Proteus 8.12

工程目标:用DS18B20采集温度并用发送温度信息至串口

资源包

DS18B20中文手册和(HAL库)驱动文件代码,需要的自行下载
链接:https://pan.baidu.com/s/1zxeTfS-LpogKTkBUwJfC3w
提取码:tq3i

DS18B20简介

测温特征

测温范围:-55 ~ +125℃
在-10 ~ +85℃范围内精确度为±5℃
最多可以在750ms内将温度转换为12位数字

看一下Proteus里的DS18B20长啥样

在这里插入图片描述

引脚说明

一共三个引脚,引脚介绍如下表

序号符号引脚说明
1GND电源地
2DQ数据I/O
3VCC可选电源

其中VCC可选电源可以接外部电源,也可以不接
区别在于接外部电源时,总线控制器在转换指令后发起“读时序”
而没有外部电源供电时,DQ与一上拉电阻连接通过单总线对器件供电,这种供电方式称为寄生电源,该模式下进行温度转换时总线要被强上拉拉高,否则没有返回值

本项目采用接外部电源的方法进行温度转换,此时DQ需要连接一个弱上拉电阻

其他详细介绍请自行下载并查看资源包里的DS18B20中文手册

STM32CubeMX配置DS18B20

配置GPIO,选择PA5作为DQ数据接口,设置推挽,上拉,并改名叫DQ,如图

在这里插入图片描述

接着配置RCC,设置时钟72MHz
由于需要把信息发送到串口,所以配置USART1,如图

在这里插入图片描述
由于DS18B20的相关时序要涉及微秒级级别的延迟,所以我们这里设置一个定时器TIM1,相关参数如图

在这里插入图片描述
最后配置Project,生成代码即可完成配置

Keil移植DS18B20驱动

首先下载资源包里的ds18b20.c和ds18b20.h文件
驱动文件移植方法之前写过,贴个传送门: https://blog.csdn.net/qq_41873311/article/details/119425197.
最后项目结构如图所示

在这里插入图片描述

编写微秒级延迟函数

在tim.c文件里写入代码

/* USER CODE BEGIN 1 */
/******************************************

***功能:实现us级别延时

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

void delay_us(uint16_t us)
{
uint16_t differ=0xffff-us-5; //设定定时器计数器起始值
__HAL_TIM_SET_COUNTER(&htim1,differ);
HAL_TIM_Base_Start(&htim1); //启动定时器
while(differ<0xffff-6) //补偿,判断
  {
    differ=__HAL_TIM_GET_COUNTER(&htim1); //查询计数器的计数值
  }
  HAL_TIM_Base_Stop(&htim1);
}
/* USER CODE END 1 */

然后在tim.h中调用delay_us函数

/* USER CODE BEGIN Prototypes */
void delay_us(uint16_t us);
/* USER CODE END Prototypes */

做完以上工作,编译运行,不报错即可

编写主函数

引入ds18b20.h和stdio.h

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "ds18b20.h"
#include "stdio.h"
/* USER CODE END Includes */

printf重定向

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* printf重定向 */
int fputc(int ch ,FILE *f){
	uint8_t temp[1] = {ch};
	HAL_UART_Transmit(&huart1, temp, 1, 2);
	return ch;
}
/* USER CODE END 0 */

调用init函数

/* USER CODE BEGIN PFP */
uint8_t DS18B20_Init(void);
/* USER CODE END PFP */

定义温度变量

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */
 float temperature;
/* USER CODE END PV */

然后在主函数内写入代码

  /* USER CODE BEGIN 2 */
  while(DS18B20_Init())
	{
		printf("DS18B20 init failed!!!\r\n");
		HAL_Delay(500);
	}
	printf("DS18B20 init success!!!\r\n");
	
  /* USER CODE END 2 */
  
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {	
		temperature = DS18B20_GetTemp_SkipRom();
		printf("当前温度: %.2f\r\n", temperature);	
		HAL_Delay(500);		/* 500ms 读取一次温度值 */
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
	
  }
  /* USER CODE END 3 */

编译运行,生成hex文件就可以开始仿真了

Proteus仿真DS18B20

原理图如下,DS18B20外接电源,所以DQ连一个5K的上拉电阻

在这里插入图片描述
仿真结果如图所示,初始化成功,打印 init success
通过DS18B20器件上的两个按钮随意调节一下温度,可以看到温度正常显示

在这里插入图片描述

写在最后

测温初始值是85℃如何解决

在这里插入图片描述
手册里有这么一个提示,上电复位时温度寄存器默认值为+85℃

最后查看手册并结合论坛上老哥的解答解决了这一问题

上电状态下默认的精度为12位,12位需要750ms转换温度才完成,如图

在这里插入图片描述
简单来说,就是寄存器里的默认值时85℃,在发起温度命令转换后750ms才完成温度转换
也就是说如果你在750ms之内就进行读取温度的指令,那么你读取到的温度自然是默认值85℃
那要显示正确的温度值,只需要在发起温度命令转换后添加750ms的延时即可

详见ds18b20.c文件中读取温度函数DS18B20_GetTemp_SkipRom()

	DS18B20_SkipRom();
	DS18B20_WriteByte(0X44);				/* 开始转换 */
	HAL_Delay(750);                 /*延迟750ms,过滤初始值85°,十分重要*/
	DS18B20_SkipRom();
    DS18B20_WriteByte(0XBE);				/* 读温度值 */

这篇就写到这儿吧

我是爱学习的诸葛铁锤,觉得有用的话点个赞哈,啾咪

### DS18B20 Sensor Simulation on STM32 Tutorial For simulating the DS18B20 temperature sensor with an STM32 microcontroller, such as the STM32F103C8T6, one can follow a structured approach to understand and implement this functionality. The process involves configuring the hardware connections between the STM32 and the DS18B20 sensor along with writing software code that interfaces these components effectively. #### Hardware Configuration The DS18B20 is a digital thermometer providing 9-bit to 12-bit Celsius temperature measurements. Connecting it to the STM32 requires wiring three pins: VCC (power), GND (ground), and DQ (data). A pull-up resistor should be connected between the data line and power supply because the communication protocol used by DS18B20 relies on open-drain outputs which require external pull-ups for proper operation[^1]. ```c // Example of initializing GPIO pin for DS18B20 Data Line in HAL library. GPIO_InitTypeDef GPIO_InitStruct = {0}; __HAL_RCC_GPIOA_CLK_ENABLE(); // Enable clock control for Port A GPIO_InitStruct.Pin = GPIO_PIN_4; // Selecting Pin PA4 for example purposes GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); ``` #### Software Implementation To simulate or emulate the behavior of the DS18B20 without having physical access to the device during development phases, developers often use predefined datasets representing typical output values from actual sensors under various conditions. This method allows testing algorithms responsible for processing raw readings into meaningful information like displaying temperatures on LCD screens. In terms of coding practices when working specifically within embedded systems environments using C/C++ languages alongside libraries provided by STMicroelectronics: - Utilize functions offered through OneWire API available either natively supported inside some versions of CubeMX-generated projects targeting specific families including F1 series where applicable or via third-party implementations compatible across different platforms. Below demonstrates how to initialize the One-Wire bus interface necessary before any read/write operations occur over the single-wire connection established earlier between MCU and peripheral devices sharing same medium. ```c #include "onewire.h" OneWire ds(PA_4); // Initialize OneWire object associated with previously configured port/pin combination void setup(void){ ... } ``` After setting up communications properly according to manufacturer guidelines found elsewhere documented online resources related directly towards interfacing particular models together successfully then proceed onto crafting routines capable enough at fetching current ambient air surrounding environment's thermal state periodically while ensuring accuracy remains consistent throughout entire application lifecycle span. --related questions-- 1. What are common challenges encountered when integrating DS18B20 sensors with STM32 controllers? 2. How does changing the resolution affect measurement precision in DS18B20 sensors? 3. Can you provide examples of applications benefiting most significantly from utilizing DS18B20 alongside STM32 boards? 4. Are there alternative methods besides direct hardware attachment for connecting DS18B20s to STM32 units remotely?
评论 66
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值