使用stm32读取两个传感器的数据。传感器1:首先通过UART1发送FF 01 03 02 00 00 00 00 FB关闭主动上报,发送:FF 01 03 03 02 00 00 00 F8,向传感器问询,传感器返回:XXX+%(ASCII码,数值+单位%),把返回的数值以float类型,保留四位小数保存。传感器2:通过UART2发送:11 01 01 ED读取O2 测量结果,返回结果为:16 09 01 DF1 DF2 DF3 DF4 DF5 DF6 DF7 DF8 [CS],数据处理方法为:O2 浓度值= (DF1*256 + DF2)/10 (Vol %),O2 流量值= (DF3*256 + DF4)/10 (L/min),O2 温度值= (DF5*256 + DF6)/10 (℃)。帮我写—段使用标准库函数读取这2个传感器数值的代码,请使用标准库函数
时间: 2023-11-27 18:52:44 浏览: 64
STM32使用ADC获取内部温度传感器数据输出(直接读取方式实现)
5星 · 资源好评率100%
以下是使用标准库函数读取两个传感器数据的代码:
```c
#include "stm32f1xx_hal.h"
#include <stdio.h>
UART_HandleTypeDef huart1;
UART_HandleTypeDef huart2;
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void);
static void MX_USART2_UART_Init(void);
int main(void)
{
float sensor1_value = 0.0;
float sensor2_o2_concentration = 0.0;
float sensor2_o2_flow_rate = 0.0;
float sensor2_o2_temperature = 0.0;
uint8_t sensor1_rx_buffer[20]; // 接收缓冲区
uint8_t sensor2_rx_buffer[20]; // 接收缓冲区
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART1_UART_Init();
MX_USART2_UART_Init();
while (1)
{
// 读取传感器1的数据
// 发送关闭主动上报指令
uint8_t sensor1_cmd1[] = {0xFF, 0x01, 0x03, 0x02, 0x00, 0x00, 0x00, 0x00, 0xFB};
HAL_UART_Transmit(&huart1, sensor1_cmd1, sizeof(sensor1_cmd1), 100);
HAL_Delay(10);
// 发送询问指令
uint8_t sensor1_cmd2[] = {0xFF, 0x01, 0x03, 0x03, 0x02, 0x00, 0x00, 0x00, 0xF8};
HAL_UART_Transmit(&huart1, sensor1_cmd2, sizeof(sensor1_cmd2), 100);
// 接收传感器1的返回值
HAL_UART_Receive(&huart1, sensor1_rx_buffer, sizeof(sensor1_rx_buffer), 1000);
// 解析传感器1的返回值
char* sensor1_value_str = strstr((const char*)sensor1_rx_buffer, "%");
if (sensor1_value_str != NULL)
{
sscanf(sensor1_value_str - 7, "%f", &sensor1_value);
}
// 读取传感器2的数据
uint8_t sensor2_cmd[] = {0x11, 0x01, 0x01, 0xED};
HAL_UART_Transmit(&huart2, sensor2_cmd, sizeof(sensor2_cmd), 100);
// 接收传感器2的返回值
HAL_UART_Receive(&huart2, sensor2_rx_buffer, sizeof(sensor2_rx_buffer), 1000);
// 解析传感器2的返回值
if (sensor2_rx_buffer[0] == 0x16 && sensor2_rx_buffer[1] == 0x09 && sensor2_rx_buffer[2] == 0x01)
{
uint16_t o2_concentration_raw = (sensor2_rx_buffer[3] << 8) | sensor2_rx_buffer[4];
uint16_t o2_flow_rate_raw = (sensor2_rx_buffer[5] << 8) | sensor2_rx_buffer[6];
uint16_t o2_temperature_raw = (sensor2_rx_buffer[7] << 8) | sensor2_rx_buffer[8];
sensor2_o2_concentration = o2_concentration_raw / 10.0f;
sensor2_o2_flow_rate = o2_flow_rate_raw / 10.0f;
sensor2_o2_temperature = o2_temperature_raw / 10.0f;
}
// 打印传感器数据
printf("Sensor1 value: %f\n", sensor1_value);
printf("Sensor2 O2 concentration: %f Vol %%\n", sensor2_o2_concentration);
printf("Sensor2 O2 flow rate: %f L/min\n", sensor2_o2_flow_rate);
printf("Sensor2 O2 temperature: %f C\n", sensor2_o2_temperature);
HAL_Delay(1000);
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
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.PLLMUL = RCC_PLL_MUL9;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses 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_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
}
static void MX_USART1_UART_Init(void)
{
huart1.Instance = USART1;
huart1.Init.BaudRate = 9600;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
Error_Handler();
}
}
static void MX_USART2_UART_Init(void)
{
huart2.Instance = USART2;
huart2.Init.BaudRate = 9600;
huart2.Init.WordLength = UART_WORDLENGTH_8B;
huart2.Init.StopBits = UART_STOPBITS_1;
huart2.Init.Parity = UART_PARITY_NONE;
huart2.Init.Mode = UART_MODE_TX_RX;
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart2) != HAL_OK)
{
Error_Handler();
}
}
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET);
/*Configure GPIO pin : PA1 */
GPIO_InitStruct.Pin = GPIO_PIN_1;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
```
需要注意的是,这段代码中的串口配置和引脚配置是针对 STM32F103C8T6 开发板的,如果你使用的是其他型号的 STM32 开发板,需要进行相应的修改。
阅读全文