/* 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 */
char current_raw_str[64] = "Raw: -- -- -- -- -- -- --"; // 显示原始7字节数据
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;
// 电压相关
float voltage_value = 0.0f;
char voltage_str[32] = "Voltage: --.-- V";
char voltage_raw_str[64] = "VRaw: -- -- -- -- -- -- --";
uint8_t voltage_read_cmd[8] = {0x01, 0x03, 0x00, 0x56, 0x00, 0x01, 0x64, 0x1A}; // 地址0x01,CRC已计算
#define VOLTAGE_READ_CMD_LEN 8
static uint8_t voltage_rx_index = 0;
static uint8_t voltage_temp_buffer[7];
#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 */
// CRC16 函数(Modbus标准,多项式:0x8005,初始值:0xFFFF)
static uint16_t CalculateCRC16(uint8_t *data, uint8_t length)
{
uint16_t crc = 0xFFFF;
for (int i = 0; i < length; i++)
{
crc ^= data[i];
for (int j = 0; j < 8; j++)
{
if (crc & 0x0001)
{
crc = (crc >> 1) ^ 0xA001;
}
else
{
crc >>= 1;
}
}
}
return crc;
}
/* 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); // 第四行:电流值
OLED_ShowString(0, 4, (uint8_t*)voltage_str); // 新增:电压值
OLED_ShowString(0, 5, (uint8_t*)voltage_raw_str); // 第五行:原始7字节
}
if (htim->Instance == TIM6) {
// 每秒发送一次电流读取命令
static int toggle = 0;
if (toggle == 0)
{
// 发送电流请求(设备0x02)
HAL_UART_Transmit(&huart1, current_read_cmd, CURRENT_READ_CMD_LEN, 100);
}
else
{
// 发送电压请求(设备0x01)
HAL_UART_Transmit(&huart1, voltage_read_cmd, VOLTAGE_READ_CMD_LEN, 100);
}
toggle = 1 - toggle; // 切换下次发送
}
}
/* 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); // 启动 TIM6 中断
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)
{
static uint8_t current_rx_index = 0;
static uint8_t current_temp_buffer[7]; // 临时存储接收到的7字节
if (huart->Instance == USART1)
{
uint8_t byte = rx_byte;
// 先判断属于哪一个设备(根据地址字段)
if (byte == 0x02 || (voltage_rx_index == 0 && byte == 0x01))
{
// 开始新帧:记录起始地址
if (byte == 0x02)
{
// 属于电流传感器帧
current_temp_buffer[current_rx_index++] = byte;
}
else if (byte == 0x01)
{
// 属于电压传感器帧
voltage_temp_buffer[voltage_rx_index++] = byte;
}
}
else
{
// 不是以合法地址开头?可能是错位或噪声
current_rx_index = 0;
voltage_rx_index = 0;
HAL_UART_Receive_IT(&huart1, &rx_byte, 1);
return;
}
// 继续接收直到满7字节
if (current_rx_index > 0 && current_rx_index < 7)
{
current_temp_buffer[current_rx_index++] = byte;
if (current_rx_index < 7)
{
HAL_UART_Receive_IT(&huart1, &rx_byte, 1);
return;
}
}
else if (voltage_rx_index > 0 && voltage_rx_index < 7)
{
voltage_temp_buffer[voltage_rx_index++] = byte;
if (voltage_rx_index < 7)
{
HAL_UART_Receive_IT(&huart1, &rx_byte, 1);
return;
}
}
// === 当前帧完整接收完成,进行分类处理 ===
if (current_rx_index == 7)
{
uint8_t *buf = current_temp_buffer;
// 检查是否为合法电流响应帧
if (buf[0] == 0x02 && buf[1] == 0x03 && buf[2] == 0x02)
{
uint16_t received_crc = (buf[6] << 8) | buf[5];
uint16_t calculated_crc = CalculateCRC16(buf, 5);
if (received_crc == calculated_crc)
{
uint16_t raw_current = (buf[3] << 8) | buf[4];
current_value = raw_current * 250.0f / 10000.0f; // 假设满量程10000对应250A
sprintf(current_str, "Current: %.2f A", current_value);
current_receive_complete = 1;
sprintf(current_raw_str, "CRaw:%02X%02X%02X%02X%02X%02X%02X",
buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6]);
}
}
current_rx_index = 0;
}
if (voltage_rx_index == 7)
{
uint8_t *buf = voltage_temp_buffer;
// 检查是否为合法电压响应帧
if (buf[0] == 0x01 && buf[1] == 0x03 && buf[2] == 0x02)
{
uint16_t received_crc = (buf[6] << 8) | buf[5];
uint16_t calculated_crc = CalculateCRC16(buf, 5);
// if (received_crc == calculated_crc)
// {
uint16_t raw_voltage = (buf[3] << 8) | buf[4];
// 假设电压量程 0~1000V,对应 0~10000 数值
float V0 = 0.0f;
float Vf = 1000.0f;
voltage_value = raw_voltage * Vf / 10000.0f;
sprintf(voltage_str, "Voltage:%.2f V", voltage_value);
// 注意字符串长度,OLED_ShowString 要能显示
sprintf(voltage_raw_str, "VRaw:%02X%02X%02X%02X%02X%02X%02X",
buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6]);
// }
}
voltage_rx_index = 0;
}
// 重启接收下一个字节
HAL_UART_Receive_IT(&huart1, &rx_byte, 1);
}
// 其他 UART 处理...
if (huart->Instance == 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);
}
}
/* 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 */
现在电压传感器已经能正常接收单片机的数据,接收到指令后会返回七位数据,但是单片机oled上却没有数据显示,我一开始以为是校验的问题所以注释掉了CRC校验,但是依然不显示数据,帮我找找问题所在