/* USER CODE BEGIN Header /
/*
@file : main.c
@brief : Main program body
@attention
Copyright © 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 “usart.h”
#include “gpio.h”
#include “fsmc.h”
/* Private includes ----------------------------------------------------------/
/ USER CODE BEGIN Includes */
#include “stdio.h”
#include “stdint.h”
#include “string.h”
#include “math.h” // 添加数学库用于浮点运算
#include “bsp_ili9341_lcd.h”
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------/
/ USER CODE BEGIN PTD */
void Printf_Charater(void) ;
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------/
/ USER CODE BEGIN PD */
extern UART_HandleTypeDef huart1;
extern UART_HandleTypeDef huart3;
#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
uint8_t rx3_byte;
uint8_t rx3_buffer[FRAME_SIZE] = {0};
uint8_t rx3_index = 0;
uint8_t frame3_received = 0;
uint8_t start3_received = 0;
// 用于显示的变量
float display_value = 0.0f;
char display_str[50] = “Waiting for data…”;
/* 串口重定向 */
int fgetc(FILE *f) {
uint8_t ch = 0;
HAL_UART_Receive(&huart1,&ch,1,0xffff);
return ch;
}
int fputc(int ch, FILE *f) {
HAL_UART_Transmit(&huart1,(uint8_t *)&ch,1,0xffff);
return ch;
}
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------/
/ USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* 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 HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
HAL_UART_Receive_IT(huart, &rx_byte, 1); // 重新开启接收中断
if (huart == &huart1) { if (!start_received) { if (rx_byte == 0xFF) { start_received = 1; // 帧头开始 rx_index = 1; // 第一个字节是帧头 rx_buffer[0] = rx_byte; } } else { rx_buffer[rx_index++] = rx_byte; if (rx_index >= FRAME_SIZE) { uint8_t calculated_checksum = 0; // 计算从第1字节到倒数第2字节的和 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]) { frame_received = 1; // 校验通过 } else { // 校验失败,重置接收状态 start_received = 0; rx_index = 0; } } } } else if (huart == &huart3) { HAL_UART_Receive_IT(&huart3, &rx3_byte, 1); // 重新开启接收中断 if (!start3_received) { if (rx3_byte == 0xFF) { start3_received = 1; // 帧头开始 rx3_index = 1; // 第一个字节是帧头 rx3_buffer[0] = rx3_byte; } } else { rx3_buffer[rx3_index++] = rx3_byte; if (rx3_index >= FRAME_SIZE) { uint8_t calculated_checksum = 0; for (int i = 1; i < FRAME_SIZE - 1; i++) { calculated_checksum += rx3_buffer[i]; } calculated_checksum = ~calculated_checksum + 1;
// if (calculated_checksum == rx3_buffer[FRAME_SIZE - 1])
// {
frame3_received = 1; // 校验通过
// }
// else
// {
// start3_received = 0;
// rx3_index = 0;
// }
}
}
}
}
/* 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_FSMC_Init();
/ USER CODE BEGIN 2 */
// 开始接收单字节中断
HAL_UART_Receive_IT(&huart1, &rx_byte, 1);
HAL_UART_Receive_IT(&huart3, &rx3_byte, 1); // 启动 USART3 接收中断
ILI9341_Init(); //LCD 初始化 ILI9341_GramScan(6); // 清屏 ILI9341_Clear(0, 0, LCD_X_LENGTH, LCD_Y_LENGTH); // 设置字体和颜色 LCD_SetFont(&Font8x16); // 改用更小的字体 LCD_SetColors(WHITE, BLACK); // 显示标题 ILI9341_DispString_EN(10, 10, "Sensor Data:");
/* USER CODE END 2 */
/* Infinite loop /
/ USER CODE BEGIN WHILE /
while (1)
{
/ USER CODE END WHILE */
/* USER CODE BEGIN 3 */
// if (frame_received)
//{
// // 示例:解析第3字节为分辨率代码
// uint8_t resolution_code = rx_buffer[3];
// float resolution;
// switch (resolution_code)
// {
// case 0: resolution = 1.0f; break;
// case 1: resolution = 0.1f; break;
// case 2: resolution = 0.01f; break;
// default: resolution = 1.0f; break;
// }
// // 提取第4、5字节组成16位整数
// uint16_t raw_value = (rx_buffer[4] << 8) | rx_buffer[5];
// float final_value = raw_value * resolution;
// // 打印结果
// char msg[50];
// sprintf(msg, “计算结果: %f\r\n”, final_value);
// HAL_UART_Transmit(&huart1, (uint8_t*)msg, strlen(msg), HAL_MAX_DELAY);
// // 重置帧接收状态
// frame_received = 0;
// start_received = 0;
// rx_index = 0;
//}
if (frame_received)
{
// 在LCD上显示数据
ILI9341_Clear(0, 50, LCD_X_LENGTH, 40); // 清除旧数据显示区域
// 显示当前值 char buffer[50]; snprintf(buffer, sizeof(buffer), "Raw: %04X ", (rx_buffer[4] << 8) | rx_buffer[5]); ILI9341_DispString_EN(10, 50, buffer); ILI9341_DispString_EN(10, 90, display_str); // 显示分辨率信息 char res_str[30]; switch (rx_buffer[3]) { case 0: snprintf(res_str, sizeof(res_str), "Res: 1.0"); break; case 1: snprintf(res_str, sizeof(res_str), "Res: 0.1"); break; case 2: snprintf(res_str, sizeof(res_str), "Res: 0.01"); break; default: snprintf(res_str, sizeof(res_str), "Res: N/A"); } ILI9341_Clear(0, 130, LCD_X_LENGTH, 30); // 清除旧数据显示区域 ILI9341_DispString_EN(10, 130, res_str); // 重置帧接收状态 frame_received = 0; }
// if (frame3_received)
// {
// // 解析传感器数据:第三位 * 256 + 第四位
// uint16_t sensor_data = (rx3_buffer[2] << 8) | rx3_buffer[3];
// // 显示在LCD上
// char buffer[50];
// snprintf(buffer, sizeof(buffer), “Sensor: %u”, sensor_data);
// ILI9341_Clear(0, 170, LCD_X_LENGTH, 30); // 清除旧数据显示区域
// ILI9341_DispString_EN(10, 170, buffer);
// // 重置接收状态
// frame3_received = 0;
// start3_received = 0;
// rx3_index = 0;
// }
if (frame3_received)
{
// 显示原始数据帧(十六进制)
char hex_str[50] = "Raw: ";
for (int i = 0; i < FRAME_SIZE; i++)
{
int len = strlen(hex_str);
sprintf(hex_str + len, "%02X ", rx3_buffer[i]);
}
// 清除旧数据显示区域并更新 ILI9341_Clear(0, 170, LCD_X_LENGTH, 30); ILI9341_DispString_EN(10, 170, hex_str); // 同时保留解析后的传感器数据显示 uint16_t sensor_data = (rx3_buffer[2] << 8) | rx3_buffer[3]; char sensor_str[50]; snprintf(sensor_str, sizeof(sensor_str), "Sensor: %u", sensor_data); ILI9341_Clear(0, 210, LCD_X_LENGTH, 30); ILI9341_DispString_EN(10, 210, sensor_str); // 重置接收状态 frame3_received = 0; start3_received = 0; rx3_index = 0;
}
}
/* 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 */
/* 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 */
那么让代码中加入一开始向串口发送指令FF 91 78 04 00 00 00 00 83,并且每秒发送一次FF 01 86 00 00 00 00 00 79
最新发布