/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2025 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 "main.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "stdint.h"
#include "string.h"
#include "math.h" // 添加数学库用于浮点运算
#include "oled.h"
#include "ds18b20.h"
#include "delay.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
#define CURRENT_SENSOR_ADDR 0x02
#define CURRENT_READ_CMD_LEN 8
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
uint8_t uart3_rx_buffer[9]; // 存储9个字节
uint8_t uart3_rx_index = 0; // 当前接收索引
uint8_t uart3_receive_complete = 0; // 接收完成标志
uint8_t uart3_rx_byte; // 单字节临时变量
//电流传感器相关变量
uint8_t current_read_cmd[CURRENT_READ_CMD_LEN] = {0x02, 0x03, 0x00, 0x56, 0x00, 0x01, 0x64, 0x29};
uint8_t current_rx_buffer[7] = {0}; // 电流传感器返回数据缓冲区
uint8_t current_rx_index = 0;
uint8_t current_receive_complete = 0;
float current_value = 0.0f;
char current_str[32] = "Current: --.- A";
uint32_t last_current_read_time = 0;
#define FRAME_SIZE 9 // 帧长度 = 帧头(1) + 数据(7) + 校验(1) = 9
uint8_t rx_byte;
uint8_t rx_buffer[FRAME_SIZE] = {0};
uint8_t rx_index = 0;
uint8_t frame_received = 0;
uint8_t start_received = 0; // 是否接收到帧头FF
float display_value = 0.0f;
char display_str[50] = "Waiting for data...";
float current_temperature = 0.0f;
uint8_t temp_update_flag = 0; // 标志位,表示需要刷新显示
float gas_concentration_uart3 = 0.0f;
float gas_concentration_uart1 = 0.0f;
uint8_t gas_update_flag = 0;
// 分辨率映射表
const float res_table[3] = {1.0f, 0.1f, 0.01f};
char gas_str_uart3[32] = "VOC: --.- ppm";
char gas_str_uart1[32] = "CO : --.- ppm";
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void Display_Temperature(uint8_t x, uint8_t y)
{
static float last_temp = -999.0f;
static uint32_t last_update = 0;
float current_temp;
uint32_t now = HAL_GetTick();
// if (now - last_update < 500) {
// return;
// }
if (DS18B20_Init() == 0) {
current_temp = DS18B20_GetTemperture();
} else {
current_temp = 999.9f;
}
// if (fabs(current_temp - last_temp) < 0.01f) {
// last_update = now;
// return;
// }
char temp_str[20];
if (current_temp == 999.9f) {
strcpy(temp_str, "Err");
} else {
sprintf(temp_str, "%.2f'C", current_temp);
}
// 标题 + 温度
OLED_ShowString(x, y, (uint8_t*)temp_str);
last_temp = current_temp;
last_update = now;
}
uint8_t update_display_flag = 0; // 显示刷新标志
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if (htim->Instance == TIM2) {
OLED_Clear();
// OLED_ShowCHinese(0, 0, 0); // 标题汉字“温”
// OLED_ShowCHinese(17, 0, 1); // 汉字“度”
OLED_ShowString(0,0,(uint8_t*)"temp:");
Display_Temperature(38, 0); // 第一行:温度
OLED_ShowString(0, 1, (uint8_t*)gas_str_uart3); // 第二行:CO 浓度
OLED_ShowString(0, 2, (uint8_t*)gas_str_uart1); // 第三行:VOC 浓度
OLED_ShowString(0, 3, (uint8_t*)current_str); // 第四行:电流值
}
if (htim->Instance == TIM6) {
// 每秒发送一次电流读取命令
HAL_UART_Transmit(&huart1, current_read_cmd, CURRENT_READ_CMD_LEN, 100);
}
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART1_UART_Init();
// MX_USART3_UART_Init();
MX_TIM3_Init();
MX_TIM2_Init();
MX_TIM6_Init();
/* USER CODE BEGIN 2 */
delay_init();
OLED_Init();
HAL_TIM_Base_Start_IT(&htim2); // 启动 TIM2 中断
HAL_TIM_Base_Start_IT(&htim6); // 启动 TIM6c 中断
HAL_UART_Receive_IT(&huart1, &rx_byte, 1);
HAL_UART_Receive_IT(&huart3, &uart3_rx_byte, 1);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
HAL_UART_Transmit(&huart1, current_read_cmd, CURRENT_READ_CMD_LEN, 10);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
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();
}
}
/* USER CODE BEGIN 4 */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if (huart->Instance == USART3)
{
// 原有的USART3气体传感器代码保持不变
uart3_rx_buffer[uart3_rx_index++] = uart3_rx_byte;
if (uart3_rx_index >= 9)
{
uint16_t raw_value = (uart3_rx_buffer[2] << 8) | uart3_rx_buffer[3];
gas_concentration_uart3 = raw_value * 0.1f;
sprintf(gas_str_uart3, "VOC:%.1f ppm", gas_concentration_uart3);
uart3_receive_complete = 1;
uart3_rx_index = 0;
gas_update_flag = 1;
}
HAL_UART_Receive_IT(&huart3, &uart3_rx_byte, 1);
}
if (huart->Instance == USART1)
{
// 检查是否是电流传感器数据(地址为0x02)
static uint8_t expecting_current_data = 0;
if (!expecting_current_data && !start_received)
{
// 检查是否是电流传感器响应帧头(地址0x02,功能码0x03)
if (rx_byte == 0x02)
{
expecting_current_data = 1;
current_rx_index = 0;
current_rx_buffer[current_rx_index++] = rx_byte;
}
else if (rx_byte == 0xFF) // 气体传感器帧头
{
start_received = 1;
rx_index = 1;
rx_buffer[0] = rx_byte;
}
}
else if (expecting_current_data)
{
// 接收电流传感器数据
current_rx_buffer[current_rx_index++] = rx_byte;
if (current_rx_index >= 7) // 电流传感器返回7个字节
{
// 验证CRC(简化处理,实际应该计算CRC)
// 提取电流数据(第3、4字节)
uint16_t raw_current = (current_rx_buffer[3] << 8) | current_rx_buffer[4];
// 根据说明书公式计算实际电流值
// 这里需要根据您的电流传感器量程修改A0和Af值
float A0 = 0.0f; // 电流下限,根据您的传感器修改
float Af = 250.0f; // 电流上限,根据您的传感器修改
current_value = raw_current * (Af - A0) / 10000.0f - fabs(A0);
sprintf(current_str, "Current: %.2f A", current_value);
current_receive_complete = 1;
expecting_current_data = 0;
current_rx_index = 0;
}
}
else if (start_received)
{
// 原有的气体传感器数据处理代码
rx_buffer[rx_index++] = rx_byte;
if (rx_index >= FRAME_SIZE)
{
uint8_t calculated_checksum = 0;
for (int i = 1; i < FRAME_SIZE - 1; i++)
{
calculated_checksum += rx_buffer[i];
}
calculated_checksum = ~calculated_checksum + 1;
if (calculated_checksum == rx_buffer[FRAME_SIZE - 1])
{
uint8_t res_index = rx_buffer[2];
float resolution = (res_index <= 2) ? res_table[res_index] : 0.01f;
uint16_t raw_value = (rx_buffer[3] << 8) | rx_buffer[4];
gas_concentration_uart1 = raw_value * resolution;
sprintf(gas_str_uart1, "CO :%.2f ppm", gas_concentration_uart1);
frame_received = 1;
gas_update_flag = 1;
}
start_received = 0;
rx_index = 0;
}
}
HAL_UART_Receive_IT(&huart1, &rx_byte, 1);
}
}
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
将电流传感器部分的校验机制弄得严格一些,确保收到的前四帧是02 03 00 56才接收,然后带上最后两位的CRC校验(先低后高位)
最新发布