1.原理图
2.CUBEMX配置
DS18B20不需要去专门配置引脚
3.在Keil修改代码
(1)修改ds18b20_hal.c和ds18b20_hal.h代码
在ds18b20_hal.c的最后手动添加读取温度的代码:
//需要手动添加读的操作
double read_temperature(void)
{
unsigned char h,l;
double result;
ow_reset();
ow_byte_wr(0xcc);//跳过ROM
ow_byte_wr(0x44);//开始转换温度
ow_reset();
ow_byte_wr(0xcc);//跳过ROM
ow_byte_wr(0xbe);//读暂存器
l=ow_byte_rd();
h=ow_byte_rd();
result=((h<<8)+l)*0.0625;
return result;
}
在ds18b20_hal.h中声明
(2)修改main.c
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2024 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include