/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file : main.h * @brief : Header for main.c file. * This file contains the common defines of the application. ****************************************************************************** * @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 */ /* Define to prevent recursive inclusion -------------------------------------*/ #ifndef __MAIN_H #define __MAIN_H #ifdef __cplusplus extern "C" { #endif /* Includes ------------------------------------------------------------------*/ #include "stm32f1xx_hal.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ /* USER CODE END Includes */ /* Exported types ------------------------------------------------------------*/ /* USER CODE BEGIN ET */ void my_delay_us(uint16_t time); void my_delay_ms(uint16_t time); /* USER CODE END ET */ /* Exported constants --------------------------------------------------------*/ /* USER CODE BEGIN EC */ /* USER CODE END EC */ /* Exported macro ------------------------------------------------------------*/ /* USER CODE BEGIN EM */ /* USER CODE END EM */ /* Exported functions prototypes ---------------------------------------------*/ void Error_Handler(void); /* USER CODE BEGIN EFP */ /* USER CODE END EFP */ /* Private defines -----------------------------------------------------------*/ #define MOTOR_Pin GPIO_PIN_0 #define MOTOR_GPIO_Port GPIOC #define MOTORC3_Pin GPIO_PIN_3 #define MOTORC3_GPIO_Port GPIOC #define MOTORA4_Pin GPIO_PIN_4 #define MOTORA4_GPIO_Port GPIOA #define MOTORA5_Pin GPIO_PIN_5 #define MOTORA5_GPIO_Port GPIOA #define HS_O4_Pin GPIO_PIN_7 #define HS_O4_GPIO_Port GPIOA #define MOTORC4_Pin GPIO_PIN_4 #define MOTORC4_GPIO_Port GPIOC #define XUN_Pin GPIO_PIN_5 #define XUN_GPIO_Port GPIOC #define XUNB13_Pin GPIO_PIN_13 #define XUNB13_GPIO_Port GPIOB #define ANGLE_Pin GPIO_PIN_6 #define ANGLE_GPIO_Port GPIOC #define ANGLEC7_Pin GPIO_PIN_7 #define ANGLEC7_GPIO_Port GPIOC #define MOTORA8_Pin GPIO_PIN_8 #define MOTORA8_GPIO_Port GPIOA #define MOTORA11_Pin GPIO_PIN_11 #define MOTORA11_GPIO_Port GPIOA #define KEY_Pin GPIO_PIN_12 #define KEY_GPIO_Port GPIOA #define ANGLEA15_Pin GPIO_PIN_15 #define ANGLEA15_GPIO_Port GPIOA #define ANGLEB3_Pin GPIO_PIN_3 #define ANGLEB3_GPIO_Port GPIOB #define PHOTO_Pin GPIO_PIN_7 #define PHOTO_GPIO_Port GPIOB #define OLED_Pin GPIO_PIN_8 #define OLED_GPIO_Port GPIOB #define OLEDB9_Pin GPIO_PIN_9 #define OLEDB9_GPIO_Port GPIOB /* USER CODE BEGIN Private defines */ /* USER CODE END Private defines */ #ifdef __cplusplus } #endif #endif /* __MAIN_H */
时间: 2025-07-04 19:09:28 浏览: 9
你已经贴出了完整的 `main.h` 文件。这是一个标准的 STM32 HAL 工程中用于全局定义和函数声明的头文件。
---
## ✅ 当前问题回顾
你在编译时遇到如下错误:
```
error: #20: identifier "rx_temp" is undefined
```
说明:变量 `rx_temp` 在 `stm32f1xx_it.c` 中使用,但未被正确声明或包含。
---
## ✅ 解决方案:在 `main.h` 中添加必要的 `extern` 声明
你当前的 `main.h` 没有对串口接收用到的全局变量进行声明(如 `rx_temp`, `rx_buffer`, `rx_index`, `rx_complete_flag`)。
### 🔧 修改步骤如下:
### 📌 1. 在 `main.h` 的 `/* USER CODE BEGIN Private defines */` 区域 **添加以下内容**:
```c
/* USER CODE BEGIN Private defines */
#define RX_BUFFER_SIZE 20
// 串口接收相关全局变量声明
extern uint8_t rx_temp;
extern uint8_t rx_buffer[RX_BUFFER_SIZE];
extern uint8_t rx_index;
extern uint8_t rx_complete_flag;
/* USER CODE END Private defines */
```
这样,在 `stm32f1xx_it.c` 中包含了 `main.h` 后就可以访问这些变量了。
---
## ✅ 示例:确保这些变量在 `main.c` 中定义
在你的 `main.c` 文件中应该有如下定义:
```c
uint8_t rx_buffer[RX_BUFFER_SIZE] = {0};
uint8_t rx_temp = 0;
uint8_t rx_index = 0;
uint8_t rx_complete_flag = 0;
```
否则也会报错!
---
## ✅ 确保 `stm32f1xx_it.c` 包含了 `main.h`
在 `stm32f1xx_it.c` 的最顶部加上:
```c
#include "main.h"
```
这样才能访问 `main.h` 中通过 `extern` 声明的变量。
---
## ✅ 总结修改点
| 文件 | 修改位置 | 添加内容 |
|------|----------|-----------|
| `main.h` | `USER CODE BEGIN Private defines` | `#define RX_BUFFER_SIZE` 和 `extern` 声明 |
| `main.c` | 全局变量区 | 定义 `rx_temp`, `rx_buffer`, `rx_index`, `rx_complete_flag` |
| `stm32f1xx_it.c` | 文件开头 | `#include "main.h"` |
---
## ✅ 修改后效果
完成上述修改后,再次编译就不会出现 `identifier "rx_temp" is undefined` 错误了。
---
##
阅读全文
相关推荐









