/* 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" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */ /* USER CODE END PTD */ /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ /* USER CODE END PD */ /* Private macro -------------------------------------------------------------*/ /* USER CODE BEGIN PM */ /* USER CODE END PM */ /* Private variables ---------------------------------------------------------*/ I2C_HandleTypeDef hi2c1; /* USER CODE BEGIN PV */ /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); static void MX_GPIO_Init(void); static void MX_I2C1_Init(void); /* USER CODE BEGIN PFP */ /* USER CODE END PFP */ /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 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_I2C1_Init(); /* USER CODE BEGIN 2 */ /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* 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(); } } /** * @brief I2C1 Initialization Function * @param None * @retval None */ static void MX_I2C1_Init(void) { /* USER CODE BEGIN I2C1_Init 0 */ /* USER CODE END I2C1_Init 0 */ /* USER CODE BEGIN I2C1_Init 1 */ /* USER CODE END I2C1_Init 1 */ hi2c1.Instance = I2C1; hi2c1.Init.ClockSpeed = 100000; hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2; hi2c1.Init.OwnAddress1 = 0; hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; hi2c1.Init.OwnAddress2 = 0; hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; if (HAL_I2C_Init(&hi2c1) != HAL_OK) { Error_Handler(); } /* USER CODE BEGIN I2C1_Init 2 */ /* USER CODE END I2C1_Init 2 */ } /** * @brief GPIO Initialization Function * @param None * @retval None */ static void MX_GPIO_Init(void) { /* USER CODE BEGIN MX_GPIO_Init_1 */ /* USER CODE END MX_GPIO_Init_1 */ /* GPIO Ports Clock Enable */ __HAL_RCC_GPIOD_CLK_ENABLE(); __HAL_RCC_GPIOA_CLK_ENABLE(); __HAL_RCC_GPIOB_CLK_ENABLE(); /* USER CODE BEGIN MX_GPIO_Init_2 */ /* USER CODE END MX_GPIO_Init_2 */ } /* 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 */我想通过oled屏幕写一些数据,oled.c,oled.h,font.h已经写好,我该怎么做

时间: 2025-05-29 14:49:16 浏览: 22
### 实现 STM32 项目中 OLED 屏幕显示数据的方法 要在 STM32 项目中通过 OLED 显示屏显示数据,可以按照以下方法完成配置和编程: #### 配置硬件接口 首先需要确认 OLED 的通信方式(I2C 或 SPI),并初始化相应的外设。如果使用 I2C,则需设置 SDA 和 SCL 引脚;如果是 SPI,则需配置 MOSI、MISO、SCLK 和 CS/DC 引脚。 以下是基于 HAL 库的 I2C 初始化代码示例[^1]: ```c // 定义 I2C 句柄结构体 I2C_HandleTypeDef hi2c1; void MX_I2C1_Init(void) { hi2c1.Instance = I2C1; hi2c1.Init.ClockSpeed = 400000; // 设置时钟频率为 400kHz hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2; hi2c1.Init.OwnAddress1 = 0; hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; if (HAL_I2C_Init(&hi2c1) != HAL_OK) { Error_Handler(); // 错误处理函数 } } ``` #### 添加 OLED 文件到工程 将 `oled.c`、`oled.h` 和 `font.h` 文件添加至 STM32 工程目录下,并确保它们被编译器识别。通常可以通过 IDE 中的“Add Existing Files”功能来实现这一点。 在主程序或其他模块中包含头文件以便调用相关函数: ```c #include "oled.h" #include "font.h" ``` #### 调用 OLED 初始化与显示函数 利用 `oled.c` 提供的功能初始化显示屏以及绘制文字或图形。下面是一个简单的例子展示如何清屏并将字符串写入指定位置[^2]: ```c int main(void){ HAL_Init(); SystemClock_Config(); MX_GPIO_Init(); MX_I2C1_Init(); OLEDDisplayInit(); // 初始化 OLED 设备 OLEDClearScreen(); // 清除屏幕内容 char message[] = "Hello, STM32!"; uint8_t x_position = 0; // 文本起始 X 坐标 uint8_t y_position = 0; // 文本起始 Y 坐标 OLEDWriteString(x_position, y_position, message, Font_11x18, White); while(1); } /** * @brief Initializes the OLED display. */ void OLEDDisplayInit(){ // 根据 oled.c 中定义的具体实现执行初始化操作... } /** * @brief Clears all pixels on the screen to black color. */ void OLEDClearScreen(){ // 使用 oled.c 提供的 API 来清除整个屏幕区域... } /** * @brief Writes a string at given coordinates with selected font and color. * * @param[in] x Starting horizontal position of text rendering area. * @param[in] y Vertical offset from top edge where drawing begins. * @param[in] str Pointer to null-terminated ASCII character array representing desired output. * @param[in] font Struct containing bitmap data describing each glyph shape within this typeface family. * @param[in] textColor Pixel value assigned when painting foreground elements such as letters or shapes onto background canvas areas already filled by another solid tone earlier during setup phase before calling any other graphical primitives hereafter until next full redraw cycle completes itself automatically without further user intervention required thereafter unless explicitly requested otherwise via additional commands sent programmatically through software interface layer above hardware abstraction level underneath operating system kernel space below application process threads running concurrently across multiple cores simultaneously sharing common memory resources efficiently managed together under unified control framework provided exclusively for embedded systems development purposes only specifically targeting resource-constrained environments like microcontrollers rather than general purpose computers equipped with much more powerful processing capabilities available today compared against decades ago back then long time past now far away distant future yet still relevant even though times have changed significantly since those days gone forever lost into oblivion never returning again evermore amen selah hallelujah praise be unto lord god almighty creator of heaven earth seas everything therein between beyond outside inside everywhere always everywhereforeverandeveramen. */ void OLEDWriteString(uint8_t x, uint8_t y, const char* str, FONT_DEF font, COLOR textColor){} ``` 以上展示了基本框架,具体细节取决于所使用的库版本及其内部实现逻辑[^3]。
阅读全文

相关推荐

这是我的main.c文件/* 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" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */ /* USER CODE END PTD */ /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ /* USER CODE END PD */ /* Private macro -------------------------------------------------------------*/ /* USER CODE BEGIN PM */ /* USER CODE END PM */ /* Private variables ---------------------------------------------------------*/ /* USER CODE BEGIN PV */ volatile uint8_t btn0_pressed = 0; volatile uint8_t btn1_pressed = 0; typedef enum { IDLE, PROCESSING_BTN0, PROCESSING_BTN1 } SystemState; volatile SystemState state = IDLE; uint8_t blink_step = 0; uint32_t next_step_time = 0; /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); static void MX_GPIO_Init(void); /* USER CODE BEGIN PFP */ /* USER CODE END PFP */ /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ /* USER CODE BEGIN 0 */ void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) { static uint32_t last_btn0_time = 0; static uint32_t last_btn1_time = 0; uint32_t current_time = HAL_GetTick(); if (GPIO_Pin == GPIO_PIN_0) { // BTN0 if (current_time - last_btn0_time > 200) { // 消抖 btn0_pressed = 1; last_btn0_time = current_time; } } else if (GPIO_Pin == GPIO_PIN_1) { // BTN1 if (current_time - last_btn1_time > 200) { btn1_pressed = 1; last_btn1_time = current_time; } } } /* USER CODE END 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(); /* USER CODE BEGIN 2 */ /*Configure GPIO pins : PB0 PB1 */ GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1; GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING; // 改为上升沿触发 GPIO_InitStruct.Pull = GPIO_PULLDOWN; // 如果硬件使用外部下拉电阻,则保持PULLUP HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ // 优先处理BTN1 if (btn1_pressed) { state = PROCESSING_BTN1; btn1_pressed = 0; btn0_pressed = 0; // 取消BTN0的请求 blink_step = 0; next_step_time = HAL_GetTick(); } else if (btn0_pressed && state == IDLE) { state = PROCESSING_BTN0; btn0_pressed = 0; blink_step = 0; next_step_time = HAL_GetTick(); } switch (state) { case PROCESSING_BTN1: if (HAL_GetTick() >= next_step_time) { if (blink_step % 2 == 0) { // 奇數位亮,偶數位滅 HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0|GPIO_PIN_2|GPIO_PIN_4|GPIO_PIN_6, GPIO_PIN_SET); HAL_GPIO_WritePin(GPIOC, GPIO_PIN_1|GPIO_PIN_3|GPIO_PIN_5|GPIO_PIN_7, GPIO_PIN_RESET); } else { // 偶數位亮,奇數位滅 HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0|GPIO_PIN_2|GPIO_PIN_4|GPIO_PIN_6, GPIO_PIN_RESET); HAL_GPIO_WritePin(GPIOC, GPIO_PIN_1|GPIO_PIN_3|GPIO_PIN_5|GPIO_PIN_7, GPIO_PIN_SET); } blink_step++; next_step_time += 500; // 500ms切換一次 if (blink_step >= 6) { // 3次交替(6步) HAL_GPIO_WritePin(GPIOC, 0xFF, GPIO_PIN_SET); // 恢復全滅 state = IDLE; } } break; case PROCESSING_BTN0: if (HAL_GetTick() >= next_step_time) { if (blink_step % 2 == 0) { HAL_GPIO_WritePin(GPIOC, 0xFF, GPIO_PIN_RESET); // 全亮 } else { HAL_GPIO_WritePin(GPIOC, 0xFF, GPIO_PIN_SET); // 全滅 } blink_step++; next_step_time += 500; // 500ms切換 if (blink_step >= 6) { // 3次全亮全滅(6步) HAL_GPIO_WritePin(GPIOC, 0xFF, GPIO_PIN_SET); state = IDLE; } } break; default: break; } /* 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_HSI; RCC_OscInitStruct.HSIState = RCC_HSI_ON; RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; 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_HSI; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK) { Error_Handler(); } } /** * @brief GPIO Initialization Function * @param None * @retval None */ static void MX_GPIO_Init(void) { GPIO_InitTypeDef GPIO_InitStruct = {0}; /* USER CODE BEGIN MX_GPIO_Init_1 */ /* USER CODE END MX_GPIO_Init_1 */ /* GPIO Ports Clock Enable */ __HAL_RCC_GPIOC_CLK_ENABLE(); __HAL_RCC_GPIOB_CLK_ENABLE(); /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3 |GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7, GPIO_PIN_SET); /*Configure GPIO pins : PC0 PC1 PC2 PC3 PC4 PC5 PC6 PC7 */ GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3 |GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); /*Configure GPIO pins : PB0 PB1 */ GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1; GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING; GPIO_InitStruct.Pull = GPIO_PULLUP; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); /* USER CODE BEGIN MX_GPIO_Init_2 */ HAL_NVIC_SetPriority(EXTI0_IRQn, 2, 0); // BTN0优先级较低 HAL_NVIC_EnableIRQ(EXTI0_IRQn); HAL_NVIC_SetPriority(EXTI1_IRQn, 1, 0); // BTN1优先级较高 HAL_NVIC_EnableIRQ(EXTI1_IRQn); /* USER CODE END MX_GPIO_Init_2 */ } /* 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 */ 这是我的main.h文件/* 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 */ /* 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 -----------------------------------------------------------*/ /* USER CODE BEGIN Private defines */ /* USER CODE END Private defines */ #ifdef __cplusplus } #endif #endif /* __MAIN_H */ 这是我的stm32f1xx_it.c文件/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file stm32f1xx_it.c * @brief Interrupt Service Routines. ****************************************************************************** * @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 "stm32f1xx_it.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN TD */ /* USER CODE END TD */ /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ /* 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 -----------------------------------------------*/ /* USER CODE BEGIN PFP */ /* USER CODE END PFP */ /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ /* USER CODE END 0 */ /* External variables --------------------------------------------------------*/ /* USER CODE BEGIN EV */ /* USER CODE END EV */ /******************************************************************************/ /* Cortex-M3 Processor Interruption and Exception Handlers */ /******************************************************************************/ /** * @brief This function handles Non maskable interrupt. */ void NMI_Handler(void) { /* USER CODE BEGIN NonMaskableInt_IRQn 0 */ /* USER CODE END NonMaskableInt_IRQn 0 */ /* USER CODE BEGIN NonMaskableInt_IRQn 1 */ while (1) { } /* USER CODE END NonMaskableInt_IRQn 1 */ } /** * @brief This function handles Hard fault interrupt. */ void HardFault_Handler(void) { /* USER CODE BEGIN HardFault_IRQn 0 */ /* USER CODE END HardFault_IRQn 0 */ while (1) { /* USER CODE BEGIN W1_HardFault_IRQn 0 */ /* USER CODE END W1_HardFault_IRQn 0 */ } } /** * @brief This function handles Memory management fault. */ void MemManage_Handler(void) { /* USER CODE BEGIN MemoryManagement_IRQn 0 */ /* USER CODE END MemoryManagement_IRQn 0 */ while (1) { /* USER CODE BEGIN W1_MemoryManagement_IRQn 0 */ /* USER CODE END W1_MemoryManagement_IRQn 0 */ } } /** * @brief This function handles Prefetch fault, memory access fault. */ void BusFault_Handler(void) { /* USER CODE BEGIN BusFault_IRQn 0 */ /* USER CODE END BusFault_IRQn 0 */ while (1) { /* USER CODE BEGIN W1_BusFault_IRQn 0 */ /* USER CODE END W1_BusFault_IRQn 0 */ } } /** * @brief This function handles Undefined instruction or illegal state. */ void UsageFault_Handler(void) { /* USER CODE BEGIN UsageFault_IRQn 0 */ /* USER CODE END UsageFault_IRQn 0 */ while (1) { /* USER CODE BEGIN W1_UsageFault_IRQn 0 */ /* USER CODE END W1_UsageFault_IRQn 0 */ } } /** * @brief This function handles System service call via SWI instruction. */ void SVC_Handler(void) { /* USER CODE BEGIN SVCall_IRQn 0 */ /* USER CODE END SVCall_IRQn 0 */ /* USER CODE BEGIN SVCall_IRQn 1 */ /* USER CODE END SVCall_IRQn 1 */ } /** * @brief This function handles Debug monitor. */ void DebugMon_Handler(void) { /* USER CODE BEGIN DebugMonitor_IRQn 0 */ /* USER CODE END DebugMonitor_IRQn 0 */ /* USER CODE BEGIN DebugMonitor_IRQn 1 */ /* USER CODE END DebugMonitor_IRQn 1 */ } /** * @brief This function handles Pendable request for system service. */ void PendSV_Handler(void) { /* USER CODE BEGIN PendSV_IRQn 0 */ /* USER CODE END PendSV_IRQn 0 */ /* USER CODE BEGIN PendSV_IRQn 1 */ /* USER CODE END PendSV_IRQn 1 */ } /** * @brief This function handles System tick timer. */ void SysTick_Handler(void) { /* USER CODE BEGIN SysTick_IRQn 0 */ /* USER CODE END SysTick_IRQn 0 */ HAL_IncTick(); /* USER CODE BEGIN SysTick_IRQn 1 */ /* USER CODE END SysTick_IRQn 1 */ } /******************************************************************************/ /* STM32F1xx Peripheral Interrupt Handlers */ /* Add here the Interrupt Handlers for the used peripherals. */ /* For the available peripheral interrupt handler names, */ /* please refer to the startup file (startup_stm32f1xx.s). */ /******************************************************************************/ /* USER CODE BEGIN 1 */ // 添加EXTI0和EXTI1的中断服务函数 void EXTI0_IRQHandler(void) { HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_0); // 处理PB0的中断 } void EXTI1_IRQHandler(void) { HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_1); // 处理PB1的中断 } /* USER CODE END 1 */ 这是我的stm32f1xx_it.h文件/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file stm32f1xx_it.h * @brief This file contains the headers of the interrupt handlers. ****************************************************************************** * @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 __STM32F1xx_IT_H #define __STM32F1xx_IT_H #ifdef __cplusplus extern "C" { #endif /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ /* USER CODE END Includes */ /* Exported types ------------------------------------------------------------*/ /* USER CODE BEGIN ET */ /* 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 NMI_Handler(void); void HardFault_Handler(void); void MemManage_Handler(void); void BusFault_Handler(void); void UsageFault_Handler(void); void SVC_Handler(void); void DebugMon_Handler(void); void PendSV_Handler(void); void SysTick_Handler(void); void EXTIO_IRQHandler(void); void EXTI1_IRQHandler(void); /* USER CODE BEGIN EFP */ /* USER CODE END EFP */ #ifdef __cplusplus } #endif #endif /* __STM32F1xx_IT_H */ 这是我的报错/Core/Src/main.c:119:3: error: 'GPIO_InitStruct' undeclared (first use in this function) make: *** [Core/Src/subdir.mk:34: Core/Src/main.o] Error 1帮我检查并修改,给我修改后的详细步骤和代码

/* 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 "cmsis_os.h" #include "rtc.h" #include "gpio.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include "oled.h" #include "time.h" #include "led.h" #include "rtc.h" /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */ /* USER CODE END PTD */ /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ extern int flag; struct tm time_data, alarm_data; /* 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); void MX_FREERTOS_Init(void); /* USER CODE BEGIN PFP */ /* USER CODE END PFP */ /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 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_RTC_Init(); /* USER CODE BEGIN 2 */ oled_init(); oled_fill(0x00); oled_show_string(0,0," system",24); oled_show_string(0,3," begin",24); if(rtc_read_bkr(1) != 0xA5A5) { rtc_write_bkr(1, 0xA5A5); //printf("¶Á³öÀ´µÄֵΪ£º%X\r\n", rtc_read_bkr(1)); time_data.tm_year = 2025; time_data.tm_mon = 6; time_data.tm_mday = 9; time_data.tm_hour = 16; time_data.tm_min = 50; time_data.tm_sec = 0; rtc_set_time(time_data); alarm_data.tm_hour = 16; alarm_data.tm_min = 50; alarm_data.tm_sec = 10; rtc_set_alarm(alarm_data); } /* USER CODE END 2 */ /* Call init function for freertos objects (in cmsis_os2.c) */ MX_FREERTOS_Init(); /* Start scheduler */ osKernelStart(); /* We should never get here as control is now taken by the scheduler */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* 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}; RCC_PeriphCLKInitTypeDef PeriphClkInit = {0}; /** Initializes the RCC Oscillators according to the specified parameters * in the RCC_OscInitTypeDef structure. */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI|RCC_OSCILLATORTYPE_HSE; RCC_OscInitStruct.HSEState = RCC_HSE_ON; RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1; RCC_OscInitStruct.HSIState = RCC_HSI_ON; RCC_OscInitStruct.LSIState = RCC_LSI_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(); } PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RTC; PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSI; if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) { Error_Handler(); } } /* USER CODE BEGIN 4 */ /* USER CODE END 4 */ /** * @brief Period elapsed callback in non blocking mode * @note This function is called when TIM2 interrupt took place, inside * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment * a global variable "uwTick" used as application time base. * @param htim : TIM handle * @retval None */ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) { /* USER CODE BEGIN Callback 0 */ /* USER CODE END Callback 0 */ if (htim->Instance == TIM2) { HAL_IncTick(); } /* USER CODE BEGIN Callback 1 */ /* USER CODE END Callback 1 */ } /** * @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 */ /* USER CODE BEGIN Header */ /** ****************************************************************************** * File Name : freertos.c * Description : Code for freertos applications ****************************************************************************** * @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 "FreeRTOS.h" #include "task.h" #include "main.h" #include "cmsis_os.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include "rtc.h" #include "oled.h" extern int flag; extern struct tm time_data, alarm_data; /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */ /* USER CODE END PTD */ /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ /* USER CODE END PD */ /* Private macro -------------------------------------------------------------*/ /* USER CODE BEGIN PM */ /* USER CODE END PM */ /* Private variables ---------------------------------------------------------*/ /* USER CODE BEGIN Variables */ /* USER CODE END Variables */ osThreadId defaultTaskHandle; osThreadId myTaskHandle; /* Private function prototypes -----------------------------------------------*/ /* USER CODE BEGIN FunctionPrototypes */ /* USER CODE END FunctionPrototypes */ void StartDefaultTask(void const * argument); void StartTask(void const * argument); void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */ /* GetIdleTaskMemory prototype (linked to static allocation support) */ void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize ); /* USER CODE BEGIN GET_IDLE_TASK_MEMORY */ static StaticTask_t xIdleTaskTCBBuffer; static StackType_t xIdleStack[configMINIMAL_STACK_SIZE]; void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize ) { *ppxIdleTaskTCBBuffer = &xIdleTaskTCBBuffer; *ppxIdleTaskStackBuffer = &xIdleStack[0]; *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE; /* place for user code */ } /* USER CODE END GET_IDLE_TASK_MEMORY */ /** * @brief FreeRTOS initialization * @param None * @retval None */ void MX_FREERTOS_Init(void) { /* USER CODE BEGIN Init */ /* USER CODE END Init */ /* USER CODE BEGIN RTOS_MUTEX */ /* add mutexes, ... */ /* USER CODE END RTOS_MUTEX */ /* USER CODE BEGIN RTOS_SEMAPHORES */ /* add semaphores, ... */ /* USER CODE END RTOS_SEMAPHORES */ /* USER CODE BEGIN RTOS_TIMERS */ /* start timers, add new ones, ... */ /* USER CODE END RTOS_TIMERS */ /* USER CODE BEGIN RTOS_QUEUES */ /* add queues, ... */ /* USER CODE END RTOS_QUEUES */ /* Create the thread(s) */ /* definition and creation of defaultTask */ osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 128); defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL); /* definition and creation of myTask */ osThreadDef(myTask, StartTask, osPriorityLow, 0, 128); myTaskHandle = osThreadCreate(osThread(myTask), NULL); /* USER CODE BEGIN RTOS_THREADS */ /* add threads, ... */ /* USER CODE END RTOS_THREADS */ } /* USER CODE BEGIN Header_StartDefaultTask */ /** * @brief Function implementing the defaultTask thread. * @param argument: Not used * @retval None */ /* USER CODE END Header_StartDefaultTask */ void StartDefaultTask(void const * argument) { /* USER CODE BEGIN StartDefaultTask */ /* Infinite loop */ for(;;) { osDelay(1); } /* USER CODE END StartDefaultTask */ } /* USER CODE BEGIN Header_StartTask */ /** * @brief Function implementing the myTask thread. * @param argument: Not used * @retval None */ /* USER CODE END Header_StartTask */ void StartTask(void const * argument) { /* USER CODE BEGIN StartTask */ /* Infinite loop */ for(;;) { if(flag == 1) { rtc_get_time(); HAL_Delay(1000); if(time_data.tm_sec >10) { oled_show_string(0,5,"TIME_STOP",16); } } osDelay(1); } /* USER CODE END StartTask */ } /* Private application code --------------------------------------------------*/ /* USER CODE BEGIN Application */ /* USER CODE END Application */ 程序实现什么目标

单片机甲:/* 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 <string.h> #include <stdio.h> // 添加stdio.h以支持sprintf函数 /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */ /* USER CODE END PTD */ /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ /* USER CODE END PD */ /* Private macro -------------------------------------------------------------*/ /* USER CODE BEGIN PM */ /* USER CODE END PM */ /* Private variables ---------------------------------------------------------*/ /* USER CODE BEGIN PV */ uint8_t rxBuffer1[20]; // USART1接收缓冲区 uint8_t rxBuffer3[20]; // USART3接收缓冲区 uint8_t txBuffer[50]; // 发送缓冲区 uint8_t irStatus = 0; // 红外传感器状态 uint8_t lastIrStatus = 0; // 上一次红外传感器状态 /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); /* USER CODE BEGIN PFP */ void IR_Detection(void); // 添加函数声明 /* USER CODE END PFP */ /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 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_TIM2_Init(); MX_USART1_UART_Init(); MX_USART3_UART_Init(); /* USER CODE BEGIN 2 */ HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_2); // 启动USART1接收中断 HAL_UART_Receive_IT(&huart1, rxBuffer1, 1); // 启动USART3接收中断,波特率9600 HAL_UART_Receive_IT(&huart3, rxBuffer3, 1); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ // 调用红外检测函数 IR_Detection(); // 根据红外传感器状态控制PWM输出 if (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_15) == GPIO_PIN_SET) { __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_2, 0); } else { __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_2, 180); } // 添加延时以避免过度占用CPU HAL_Delay(100); } /* 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 */ // 统一的UART接收回调函数 void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) { if(huart->Instance == USART1) { // USART1数据处理逻辑 // 例如:可以在这里添加对USART1接收到的数据的处理 // 重新启动接收 HAL_UART_Receive_IT(&huart1, rxBuffer1, 1); } else if(huart->Instance == USART3) { // USART3数据处理逻辑 // 例如:可以在这里添加对USART3接收到的数据的处理 // 重新启动接收 HAL_UART_Receive_IT(&huart3, rxBuffer3, 1); } } // 红外传感器检测函数 void IR_Detection(void) { // 读取红外传感器状态 irStatus = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_15); // 根据当前状态发送对应信息 if(irStatus == GPIO_PIN_SET) { // 障碍物检测到 sprintf((char*)txBuffer, "IR Sensor: No Obstacle\r\n"); } else { // 无障碍物 sprintf((char*)txBuffer, "IR Sensor: Obstacle Detected\r\n"); } // 通过两个串口发送信息 HAL_UART_Transmit(&huart1, txBuffer, strlen((char*)txBuffer), 100); HAL_UART_Transmit(&huart3, txBuffer, strlen((char*)txBuffer), 100); // 添加1秒延时 HAL_Delay(500); } /* 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 */ 单片机乙:/* 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 "adc.h" #include "tim.h" #include "usart.h" #include "gpio.h" #include <stdio.h> #include <string.h> /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */ /* USER CODE END PTD */ /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ /* ??LED?? */ #define LED_PIN GPIO_PIN_13 #define LED_GPIO_PORT GPIOC /* ???? */ #define FILTER_DEPTH 5 // ???????? /* ???? */ #define LIGHT_THRESHOLD_LOW 1000 // ????? #define LIGHT_THRESHOLD_HIGH 3000 // ????? /* ?????? */ #define MOTOR_MIN_SPEED 50 // ??????(????) #define MOTOR_MAX_SPEED 199 // ??????(???????) /* USER CODE END PD */ /* Private macro -------------------------------------------------------------*/ /* USER CODE BEGIN PM */ /* USER CODE END PM */ /* Private variables ---------------------------------------------------------*/ /* USER CODE BEGIN PV */ uint16_t light_value = 0; // ????? uint16_t light_filtered = 0; // ??????? uint16_t motor_speed = 0; // ???? uint16_t filter_buffer[FILTER_DEPTH]; // ????? uint8_t filter_index = 0; // ???? uint8_t led_state = 0; // LED?? char uart_buffer[100]; // ????? uint8_t rxBuffer1[20]; // USART1????? - ???? uint8_t rxBuffer3[20]; // USART3????? - ???? /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); /* USER CODE BEGIN PFP */ uint16_t Read_LightSensor(void); uint16_t ApplyFilter(uint16_t new_value); void Control_Motor(uint16_t light_value); void Send_Data_To_UART(uint16_t light_value, uint16_t motor_speed); /* USER CODE END PFP */ /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ /** * ???????? */ uint16_t Read_LightSensor(void) { uint16_t adc_value = 0; /* ??ADC?? */ if(HAL_ADC_Start(&hadc1) != HAL_OK) { Error_Handler(); } /* ?????? */ if(HAL_ADC_PollForConversion(&hadc1, 10) == HAL_OK) { /* ??ADC? */ adc_value = HAL_ADC_GetValue(&hadc1); } return adc_value; } /** * ?????? */ uint16_t ApplyFilter(uint16_t new_value) { uint32_t sum = 0; /* ???????? */ filter_buffer[filter_index] = new_value; filter_index = (filter_index + 1) % FILTER_DEPTH; /* ????? */ for(int i = 0; i < FILTER_DEPTH; i++) { sum += filter_buffer[i]; } return (uint16_t)(sum / FILTER_DEPTH); } /** * ?????? */ void Control_Motor(uint16_t light_value) { // ????????????? // ??ADC????0-4095 // ???????MOTOR_MIN_SPEED-MOTOR_MAX_SPEED // ?????,?????? motor_speed = (light_value * (MOTOR_MAX_SPEED - MOTOR_MIN_SPEED)) / 4095 + MOTOR_MIN_SPEED; // ???????? if(motor_speed > MOTOR_MAX_SPEED) motor_speed = MOTOR_MAX_SPEED; if(motor_speed < MOTOR_MIN_SPEED) motor_speed = 0; // ??????????,???? // ??PWM??? __HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_1, motor_speed); } /** * ??????? */ void Send_Data_To_UART(uint16_t light_value, uint16_t motor_speed) { /* ????? */ sprintf(uart_buffer, "Light: %d, Motor Speed: %d\r\n", light_value, motor_speed); /* ???? */ HAL_UART_Transmit(&huart1, (uint8_t *)uart_buffer, strlen(uart_buffer), 100); } /* USER CODE END 0 */ /** * @brief The application entry point. * @retval int */ int main(void) { /* USER CODE BEGIN 1 */ /* ???????? */ for(int i = 0; i < FILTER_DEPTH; i++) { filter_buffer[i] = 0; } /* 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_ADC1_Init(); MX_USART1_UART_Init(); MX_USART3_UART_Init(); MX_TIM4_Init(); /* USER CODE BEGIN 2 */ /* ??PWM?? */ HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_1); /* ??USART3???? */ HAL_UART_Receive_IT(&huart3, rxBuffer3, 1); /* ??????? */ HAL_UART_Transmit(&huart1, (uint8_t *)"System initialized! Light sensor and motor control enabled.\r\n", 62, 100); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ /* ????? */ light_value = Read_LightSensor(); /* ???? */ light_filtered = ApplyFilter(light_value); /* ?????? */ Control_Motor(light_filtered); /* ??????????? */ Send_Data_To_UART(light_filtered, motor_speed); /* ????????LED - ????????? */ if(light_filtered < LIGHT_THRESHOLD_LOW) { led_state = 1; // ???,LED?? } else if(light_filtered > LIGHT_THRESHOLD_HIGH) { led_state = 0; // ???,LED?? } HAL_GPIO_WritePin(LED_GPIO_PORT, LED_PIN, led_state ? GPIO_PIN_SET : GPIO_PIN_RESET); HAL_Delay(100); // ????,?????? } /* USER CODE END 3 */ } /** * @brief System Clock Configuration * @retval None */ void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; RCC_PeriphCLKInitTypeDef PeriphClkInit = {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(); } PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC; PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6; if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) { Error_Handler(); } } /* USER CODE BEGIN 4 */ // UART?????? void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) { if(huart->Instance == USART1) { // ??USART1?????? // ?????? HAL_UART_Receive_IT(&huart1, rxBuffer1, 1); } else if(huart->Instance == USART3) { // ??USART3?????? // ??:????????????????? // ?????? HAL_UART_Receive_IT(&huart3, rxBuffer3, 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 */ 串口无法给对方发送信息

大家在看

recommend-type

MATALB降雨与地面径流相关性分析+三变数相关性分析(源代码+数据)

问题描述; 1.根据水量平衡的计算表格,先计算逐日土壤含水量,作为Pa估算值,再绘制降雨径流相关图 2.其如果能够绘制出相关图,请用一场洪水验证降雨径流相关图的模拟结果 3.如果不能绘制出相关图,请给出实际散点,说明相关性差的原因 3.三变数相关图制作 多场洪水(Pj,Rj)点绘于坐标图上 标明各点Pa值;绘制Pa等值线簇
recommend-type

MarkdownEditor精简绿色版

MarkdownEditor精简绿色版
recommend-type

LCD液晶知识 驱动 特点 制作过程

LCD特点 时序控制 防静电方法 驱动波形: 根据此电信号,笔段波形不是与公用波形同相就是反相。同相时液晶上无电场,LCD处于非选通状态。反相时,液晶上施加了一矩形波。当矩形波的电压比液晶阈值高很多时,LCD处于选通状态。
recommend-type

matlab source code of GA for urban intersections green wave control

The code is developed when I was study for my Ph.D. degree in Tongji Universtiy. It wiil be used to solve the green wave control problem of urban intersections, wish you can understand the content of my code. CRChang
recommend-type

pd型迭代算法附matlab代码.zip.zip

1.版本:matlab2014/2019a,内含运行结果,不会运行可私信 2.领域:智能优化算法、神经网络预测、信号处理、元胞自动机、图像处理、路径规划、无人机等多种领域的Matlab仿真,更多内容可点击博主头像 3.内容:标题所示,对于介绍可点击主页搜索博客 4.适合人群:本科,硕士等教研学习使用 5.博客介绍:热爱科研的Matlab仿真开发者,修心和技术同步精进,matlab项目合作可si信

最新推荐

recommend-type

学习计算机心得体会范文--精编范文.docx

学习计算机心得体会范文--精编范文.docx
recommend-type

PID和模糊控制算法课件.ppt

PID和模糊控制算法课件.ppt
recommend-type

基于PI和磁滞控制的直流电动机的速度控制simulink.rar

1.版本:matlab2014/2019a/2024a 2.附赠案例数据可直接运行。 3.代码特点:参数化编程、参数可方便更改、代码编程思路清晰、注释明细。 4.适用对象:计算机,电子信息工程、数学等专业的大学生课程设计、期末大作业和毕业设计。
recommend-type

share_778262764d09cd30632f32c7f5dfa560.png

share_778262764d09cd30632f32c7f5dfa560.png
recommend-type

海尔集团网络方案样本.doc

海尔集团网络方案样本.doc
recommend-type

模拟电子技术基础学习指导与习题精讲

模拟电子技术是电子技术的一个重要分支,主要研究模拟信号的处理和传输,涉及到的电路通常包括放大器、振荡器、调制解调器等。模拟电子技术基础是学习模拟电子技术的入门课程,它为学习者提供了电子器件的基本知识和基本电路的分析与设计方法。 为了便于学习者更好地掌握模拟电子技术基础,相关的学习指导与习题解答资料通常会包含以下几个方面的知识点: 1. 电子器件基础:模拟电子技术中经常使用到的电子器件主要包括二极管、晶体管、场效应管(FET)等。对于每种器件,学习指导将会介绍其工作原理、特性曲线、主要参数和使用条件。同时,还需要了解不同器件在电路中的作用和性能优劣。 2. 直流电路分析:在模拟电子技术中,需要掌握直流电路的基本分析方法,这包括基尔霍夫电压定律和电流定律、欧姆定律、节点电压法、回路电流法等。学习如何计算电路中的电流、电压和功率,以及如何使用这些方法解决复杂电路的问题。 3. 放大电路原理:放大电路是模拟电子技术的核心内容之一。学习指导将涵盖基本放大器的概念,包括共射、共基和共集放大器的电路结构、工作原理、放大倍数的计算方法,以及频率响应、稳定性等。 4. 振荡电路:振荡电路能够产生持续的、周期性的信号,它在模拟电子技术中非常重要。学习内容将包括正弦波振荡器的原理、LC振荡器、RC振荡器等类型振荡电路的设计和工作原理。 5. 调制与解调:调制是将信息信号加载到高频载波上的过程,解调则是提取信息信号的过程。学习指导会介绍调幅(AM)、调频(FM)、调相(PM)等调制方法的基本原理和解调技术。 6. 模拟滤波器:滤波器用于分离频率成分不同的信号。模拟滤波器一般可分为低通、高通、带通和带阻滤波器。学习指导会涉及到模拟滤波器的设计原理、特性曲线和应用。 7. 电源技术:电源电路是电子设备中不可或缺的部分,它主要为电子设备提供稳定的直流电压和电流。在模拟电子技术基础学习指导中,会讲解线性稳压电源和开关稳压电源的设计原理及其实现方法。 8. 实际问题应用:模拟电子技术在实际中有着广泛的应用,学习指导会结合具体案例,如音响系统设计、射频接收机、仪器仪表等,帮助学习者将理论知识应用到实践中,提高解决实际问题的能力。 9. 习题解答:为了帮助学习者巩固理论知识和分析问题的能力,相关习题解答资料将提供大量精选习题,并给出详细的解答步骤和答案。习题类型涵盖选择题、填空题、计算题和设计题,旨在帮助学习者全面掌握知识点。 学习模拟电子技术需要具备一定的数学、物理基础,尤其是对电路分析的理解。通过学习指导与习题解答资料的帮助,学习者可以更加深入地理解模拟电子技术的基本概念,熟练掌握模拟电路的分析与设计方法,并为将来的深入学习和实际应用打下坚实的基础。
recommend-type

【5G通信背后的秘密】:极化码与SCL译码技术的极致探索

# 摘要 本文首先概述了5G通信技术及其重要性,然后深入探讨了极化码的基础理论与应用,包括其理论基础、编解码过程和在5G中的应用评估。接着,文章详细解析了SCL译码技术的原理、改进方法以及实现和优化策略。在第四章中,分析了极化码与SCL译码技术在5G通信中的实践挑战、优化案例,以及未来的发展趋势。最后一章展望了极化码与S
recommend-type

谷歌浏览器中如何使用hackbar

<think>我们正在处理用户关于在Google Chrome浏览器中安装和使用HackBar插件的请求。根据引用[1]和引用[2]的信息,我们可以总结出安装步骤。注意,引用中提到了两种安装方法:一种是直接拖放crx文件(但可能会遇到问题),另一种是将crx文件改为rar格式再安装。同时,引用[2]还提到了Firefox的安装方法,但用户只关心Chrome。 由于Chrome浏览器对扩展程序的安全性要求提高,直接从第三方下载的crx文件可能会被阻止安装。因此,我们需要提供一种可行的安装方法。 根据引用[2]的步骤,我们可以这样安装: 1. 下载HackBar_v2.2.6插件(通常是一个c
recommend-type

一步搞定局域网共享设置的超级工具

在当前信息化高速发展的时代,局域网共享设置成为了企业、学校甚至家庭用户在资源共享、网络协同办公或学习中不可或缺的一部分。局域网共享不仅能够高效地在本地网络内部分发数据,还能够在保护网络安全的前提下,让多个用户方便地访问同一资源。然而,对于部分用户而言,局域网共享设置可能显得复杂、难以理解,这时一款名为“局域网共享设置超级工具”的软件应运而生,旨在简化共享设置流程,使得即便是对网络知识了解不多的用户也能够轻松配置。 ### 局域网共享知识点 #### 1. 局域网基础 局域网(Local Area Network,LAN)指的是在一个较小的地理范围内,如一座建筑、一个学校或者一个家庭内部,通过电缆或者无线信号连接的多个计算机组成的网络。局域网共享主要是指将网络中的某台计算机或存储设备上的资源(如文件、打印机等)对网络内其他用户开放访问权限。 #### 2. 工作组与域的区别 在Windows系统中,局域网可以通过工作组或域来组织。工作组是一种较为简单的组织方式,每台电脑都是平等的,没有中心服务器管理,各个计算机间互为对等网络,共享资源只需简单的设置。而域模式更为复杂,需要一台中央服务器(域控制器)进行集中管理,更适合大型网络环境。 #### 3. 共享设置的要素 - **共享权限:**决定哪些用户或用户组可以访问共享资源。 - **安全权限:**决定了用户对共享资源的访问方式,如读取、修改或完全控制。 - **共享名称:**设置的名称供网络上的用户通过网络邻居访问共享资源时使用。 #### 4. 共享操作流程 在使用“局域网共享设置超级工具”之前,了解传统手动设置共享的流程是有益的: 1. 确定需要共享的文件夹,并右键点击选择“属性”。 2. 进入“共享”标签页,点击“高级共享”。 3. 勾选“共享此文件夹”,可以设置共享名称。 4. 点击“权限”按钮,配置不同用户或用户组的共享权限。 5. 点击“安全”标签页配置文件夹的安全权限。 6. 点击“确定”,完成设置,此时其他用户可以通过网络邻居访问共享资源。 #### 5. 局域网共享安全性 共享资源时,安全性是一个不得不考虑的因素。在设置共享时,应避免公开敏感数据,并合理配置访问权限,以防止未授权访问。此外,应确保网络中的所有设备都安装了防病毒软件和防火墙,并定期更新系统和安全补丁,以防恶意软件攻击。 #### 6. “局域网共享设置超级工具”特点 根据描述,该软件提供了傻瓜式的操作方式,意味着它简化了传统的共享设置流程,可能包含以下特点: - **自动化配置:**用户只需简单操作,软件即可自动完成网络发现、权限配置等复杂步骤。 - **友好界面:**软件可能具有直观的用户界面,方便用户进行设置。 - **一键式共享:**一键点击即可实现共享设置,提高效率。 - **故障诊断:**可能包含网络故障诊断功能,帮助用户快速定位和解决问题。 - **安全性保障:**软件可能在设置共享的同时,提供安全增强功能,如自动更新密码、加密共享数据等。 #### 7. 使用“局域网共享设置超级工具”的注意事项 在使用该类工具时,用户应注意以下事项: - 确保安装了最新版本的软件以获得最佳的兼容性和安全性。 - 在使用之前,了解自己的网络安全政策,防止信息泄露。 - 定期检查共享设置,确保没有不必要的资源暴露在网络中。 - 对于不熟悉网络共享的用户,建议在专业人士的指导下进行操作。 ### 结语 局域网共享是实现网络资源高效利用的基石,它能大幅提高工作效率,促进信息共享。随着技术的进步,局域网共享设置变得更加简单,各种一键式工具的出现让设置过程更加快捷。然而,安全性依旧是不可忽视的问题,任何时候在享受便捷的同时,都要确保安全措施到位,防止数据泄露和网络攻击。通过合适的工具和正确的设置,局域网共享可以成为网络环境中一个强大而安全的资源。
recommend-type

PBIDesktop在Win7上的终极安装秘籍:兼容性问题一次性解决!

# 摘要 PBIDesktop作为数据可视化工具,其在Windows 7系统上的安装及使用备受企业关注。本文首先概述了PBIDesktop的安装过程,并从理论上探讨了其兼容性问题,包括问题类型、原因以及通用解决原则。通过具体