stm32 hal库 串口显示ds18b20 Pa3为ds18b20的gpio引脚 KEY4为pb4 使用串口3 115200波特率 使用定时器3 。编写程序,实现以下功能。 使用终端A上的扩展IO口PA3连接DS18B20温度传感器。 每当按键ASW4按下,通过串口上报温度数据一次,温度发送格式: "C:25.26\r\n" 串口发送格式(字符串) 备注: C为大写字母,温度值结果保留小数点后2位有效数字。 串口发送的字符串应包含换行、回车符号:"\r\n"。
时间: 2024-03-16 17:47:24 浏览: 65
STM32温度传感器DS18B20加串口通信
5星 · 资源好评率100%
以下是基于STM32 HAL库的代码实现,实现了按下按键ASW4后,通过串口3上报温度数据一次,温度发送格式为"C:xx.xx\r\n",其中xx.xx为浮点数格式的温度值,保留小数点后两位有效数字,字符串末尾包含"\r\n"。
```
#include "main.h"
#include "stdio.h"
/* Private variables ---------------------------------------------------------*/
UART_HandleTypeDef huart3;
TIM_HandleTypeDef htim3;
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART3_UART_Init(void);
static void MX_TIM3_Init(void);
/* Private function prototypes -----------------------------------------------*/
void delay_us(uint32_t us);
void DS18B20_Start(void);
void DS18B20_Write(uint8_t data);
uint8_t DS18B20_Read(void);
float DS18B20_Get_Temp(void);
void send_temp(float temp);
/* Private variables ---------------------------------------------------------*/
uint8_t ds18b20_present = 0;
/* Main function -------------------------------------------------------------*/
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART3_UART_Init();
MX_TIM3_Init();
/* Start timer */
HAL_TIM_Base_Start_IT(&htim3);
/* Infinite loop */
while (1)
{
/* Wait for button press */
while (HAL_GPIO_ReadPin(KEY4_GPIO_Port, KEY4_Pin) == GPIO_PIN_SET);
/* Get temperature */
float temp = DS18B20_Get_Temp();
/* Send temperature via UART */
send_temp(temp);
/* Wait for button release */
while (HAL_GPIO_ReadPin(KEY4_GPIO_Port, KEY4_Pin) == GPIO_PIN_RESET);
}
}
/* Timer3 callback function */
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if (htim->Instance == TIM3)
{
/* DS18B20 presence detection */
DS18B20_Start();
DS18B20_Write(0xCC);
DS18B20_Write(0x44);
delay_us(750000);
DS18B20_Start();
DS18B20_Write(0xCC);
DS18B20_Write(0xBE);
uint8_t byte1 = DS18B20_Read();
uint8_t byte2 = DS18B20_Read();
if ((byte1 == 0x28) && (byte2 == 0xFF))
{
ds18b20_present = 1;
}
else
{
ds18b20_present = 0;
}
}
}
/* Send temperature via UART */
void send_temp(float temp)
{
char buffer[16];
sprintf(buffer, "C:%.2f\r\n", temp);
HAL_UART_Transmit(&huart3, (uint8_t *)buffer, strlen(buffer), 1000);
}
/* Get temperature from DS18B20 */
float DS18B20_Get_Temp(void)
{
float temp = 0;
if (ds18b20_present)
{
DS18B20_Start();
DS18B20_Write(0xCC);
DS18B20_Write(0x44);
delay_us(750000);
DS18B20_Start();
DS18B20_Write(0xCC);
DS18B20_Write(0xBE);
uint8_t byte1 = DS18B20_Read();
uint8_t byte2 = DS18B20_Read();
int16_t raw = (byte2 << 8) | byte1;
if (raw & 0x8000)
{
raw = ~raw + 1;
temp = -(float)raw / 16.0;
}
else
{
temp = (float)raw / 16.0;
}
}
return temp;
}
/* Read one byte from DS18B20 */
uint8_t DS18B20_Read(void)
{
uint8_t data = 0;
for (int i = 0; i < 8; i++)
{
HAL_GPIO_WritePin(PA3_GPIO_Port, PA3_Pin, GPIO_PIN_SET);
delay_us(2);
HAL_GPIO_WritePin(PA3_GPIO_Port, PA3_Pin, GPIO_PIN_RESET);
delay_us(2);
data |= HAL_GPIO_ReadPin(PA3_GPIO_Port, PA3_Pin) << i;
delay_us(60);
}
return data;
}
/* Write one byte to DS18B20 */
void DS18B20_Write(uint8_t data)
{
for (int i = 0; i < 8; i++)
{
HAL_GPIO_WritePin(PA3_GPIO_Port, PA3_Pin, GPIO_PIN_RESET);
delay_us(2);
HAL_GPIO_WritePin(PA3_GPIO_Port, PA3_Pin, (data & (1 << i)) ? GPIO_PIN_SET : GPIO_PIN_RESET);
delay_us(60);
HAL_GPIO_WritePin(PA3_GPIO_Port, PA3_Pin, GPIO_PIN_SET);
}
}
/* Start DS18B20 */
void DS18B20_Start(void)
{
HAL_GPIO_WritePin(PA3_GPIO_Port, PA3_Pin, GPIO_PIN_RESET);
delay_us(480);
HAL_GPIO_WritePin(PA3_GPIO_Port, PA3_Pin, GPIO_PIN_SET);
delay_us(80);
HAL_GPIO_WritePin(PA3_GPIO_Port, PA3_Pin, GPIO_PIN_RESET);
delay_us(480);
}
/* Delay for us microseconds */
void delay_us(uint32_t us)
{
__HAL_TIM_SET_COUNTER(&htim3, 0);
while ((__HAL_TIM_GET_COUNTER(&htim3)) < us);
}
/* GPIO and UART initialization functions */
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/* Configure GPIO pin : KEY4_Pin */
GPIO_InitStruct.Pin = KEY4_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(KEY4_GPIO_Port, &GPIO_InitStruct);
/* Configure GPIO pin : PA3_Pin */
GPIO_InitStruct.Pin = PA3_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(PA3_GPIO_Port, &GPIO_InitStruct);
HAL_GPIO_WritePin(PA3_GPIO_Port, PA3_Pin, GPIO_PIN_SET);
}
static void MX_USART3_UART_Init(void)
{
huart3.Instance = USART3;
huart3.Init.BaudRate = 115200;
huart3.Init.WordLength = UART_WORDLENGTH_8B;
huart3.Init.StopBits = UART_STOPBITS_1;
huart3.Init.Parity = UART_PARITY_NONE;
huart3.Init.Mode = UART_MODE_TX_RX;
huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart3.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart3) != HAL_OK)
{
Error_Handler();
}
}
static void MX_TIM3_Init(void)
{
htim3.Instance = TIM3;
htim3.Init.Prescaler = ((SystemCoreClock / 1000000) * 1) - 1;
htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
htim3.Init.Period = 0xFFFF;
if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
{
Error_Handler();
}
}
void Error_Handler(void)
{
while (1);
}
#ifdef USE_FULL_ASSERT
void assert_failed(uint8_t *file, uint32_t line)
{
}
#endif /* USE_FULL_ASSERT */
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;
/**Initializes the CPU, AHB and APB busses clocks
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 25;
RCC_OscInitStruct.PLL.PLLN = 336;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 7;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/**Initializes the CPU, AHB and APB busses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
| RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
{
Error_Handler();
}
}
```
阅读全文