void TIM3_init(uint32_t Period,uint32_t Prescaler) { __HAL_RCC_TIM3_CLK_ENABLE(); TIM_HandleTypeDef TimHandle; TimHandle.Instance = TIM3; /* Select TIM3 */ TimHandle.Init.Period = Period - 1; /* Auto-reload value */ TimHandle.Init.Prescaler = Prescaler - 1; /* Prescaler is set to 1000-1 */ TimHandle.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; /* No clock division */ TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; /* Up counting mode */ TimHandle.Init.RepetitionCounter = 1 - 1; /* No repetition counter */ TimHandle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; /* Auto-reload register not buffered */ HAL_NVIC_SetPriority(TIM3_IRQn, 2, 2); HAL_NVIC_EnableIRQ(TIM3_IRQn); /* TIM3 initialization */ if (HAL_TIM_Base_Init(&TimHandle) != HAL_OK) { APP_ErrorHandler(); } /* Start TIM3 and enable interrupts */ if (HAL_TIM_Base_Start_IT(&TimHandle) != HAL_OK) { APP_ErrorHandler(); } } #ifndef __TIM_H #define __TIM_H #include "py32f0xx_hal.h" #include "main.h" void TIM3_init(uint32_t Period,uint32_t Prescaler); extern TIM_HandleTypeDef TimHandle;/** ****************************************************************************** * @file main.c * @author MCU Application Team * @brief Main program body ****************************************************************************** * @attention * * <h2><center>© Copyright (c) 2023 Puya Semiconductor Co. * All rights reserved.</center></h2> * * This software component is licensed by Puya under BSD 3-Clause license, * the "License"; You may not use this file except in compliance with the * License. You may obtain a copy of the License at: * opensource.org/licenses/BSD-3-Clause * ****************************************************************************** * @attention * * <h2><center>© Copyright (c) 2016 STMicroelectronics. * All rights reserved.</center></h2> * * This software component is licensed by ST under BSD 3-Clause license, * the "License"; You may not use this file except in compliance with the * License. You may obtain a copy of the License at: * opensource.org/licenses/BSD-3-Clause * ****************************************************************************** */ /* Includes ------------------------------------------------------------------*/ #include "main.h" #include "Dr_Driver\bsp.h" #include "TIM\tim.h" #include "KEY\key.h" #include "MIC\mic.h" //TIM_HandleTypeDef TimHandle; static void APP_SystemClockConfig(void); uint8_t key_num; /** * @brief Main program. * @retval int */ int main(void) { /* Reset of all peripherals, Initializes the Systick */ HAL_Init(); //HAL_Delay(1000); uint8_t MIC_status=0; //mic状态 uint32_t smoking_tim=0;//吸烟时间 /* Configure the system clock */ APP_SystemClockConfig(); iwdg_init(); Dr_system_init(); //uart Dr_Uart_Debug_Printf("SystemCoreClock = %d\n\r",SystemCoreClock); TIM3_init(32000,1000);//100ms adc_Config(); KEY_init(); MIC_init(); #if (DEBUG_PRINTF == 1) Dr_Uart_Debug_Printf("SystemCoreClock = %d\n\r",SystemCoreClock); #endif while (1) { MIC_status= HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_4); switch(key_num) { case 1:smoking_tim=300;break; case 2:smoking_tim=200;break; case 3:smoking_tim=100;break; default:break; } if(timer_1s_flag) { Dr_Uart_Debug_Printf("vbat = %d\r\n",(4095 * 1200) / g_get_adc[2]); //原电池电压 Dr_Uart_Debug_Printf("outvbat = %d\r\n",(g_get_adc[0]* 1200)/g_get_adc[2]); //待测电池电压 Dr_Uart_Debug_Printf("output = %d\r\n",(g_get_adc[1]* 1200)/g_get_adc[2]); //输出电压 Dr_Uart_Debug_Printf("smokingtim = %d\r\n",smoking_tim); //输出电压 Dr_Uart_Debug_Printf("MIC_status = %d\r\n",MIC_status); //输出电压 Dr_Uart_Debug_Printf("\r\n"); //输出电压 } if(timer_10ms_flag ) { adc_get(); timer_10ms_flag = 0; } if(timer_100ms_flag ) { timer_100ms_flag = 0; if(MIC_status) smoking_tim++; } if(timer_1s_flag) { timer_1s_flag=0; } fwdt_feed(); } } void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) { //BSP_LED_Toggle(LED_GREEN); //BSP_LED_On(LED_GREEN); smoking_tim=500; } void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) { static uint32_t last_time[3] = {0}; uint32_t current_time = HAL_GetTick(); Dr_Uart_Debug_Printf("KEY0 Pressed!\n"); // 判断是哪个按键触发的中断 if (GPIO_Pin == GPIO_PIN_5) { // KEY0 if (current_time - last_time[0] > 20) { // 消抖处理(20ms) last_time[0] = current_time; // 处理KEY0按下事件 Dr_Uart_Debug_Printf("KEY0 Pressed!\n"); } } else if (GPIO_Pin == GPIO_PIN_6) { // KEY1 if (current_time - last_time[1] > 20) { last_time[1] = current_time; // 处理KEY1按下事件 Dr_Uart_Debug_Printf("KEY1 Pressed!\n"); } } else if (GPIO_Pin == GPIO_PIN_7) { // KEY2 if (current_time - last_time[2] > 20) { last_time[2] = current_time; // 处理KEY2按下事件 Dr_Uart_Debug_Printf("KEY2 Pressed!\n"); } } } /** * @brief System Clock Configuration * @param None * @retval None */ static void APP_SystemClockConfig(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; /* Oscillator Configuration */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE; /* Select oscillators HSE, HSI, LSI, LSE */ RCC_OscInitStruct.HSIState = RCC_HSI_ON; /* Enable HSI */ RCC_OscInitStruct.HSIDiv = RCC_HSI_DIV1; /* HSI not divided */ RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_24MHz; /* Configure HSI clock as 8MHz */ RCC_OscInitStruct.HSEState = RCC_HSE_OFF; /* Disable HSE */ // RCC_OscInitStruct.HSEFreq = RCC_HSE_16_32MHz; RCC_OscInitStruct.LSIState = RCC_LSI_OFF; /* Disable LSI */ RCC_OscInitStruct.LSEState = RCC_LSE_OFF; /* Disable LSE */ /*RCC_OscInitStruct.LSEDriver = RCC_LSEDRIVE_MEDIUM;*/ RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; /* Disable PLL */ RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI; /* Configure oscillators */ if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { APP_ErrorHandler(); } /* Clock source configuration */ RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1; /* Select clock types HCLK, SYSCLK, PCLK1 */ RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; /* Select HSI as the system clock */ RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; /* AHB clock not divide */ RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; /* APB clock not divided */ /* Configure clock source */ if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK) { APP_ErrorHandler(); } } /** * @brief This function is executed in case of error occurrence. * @param None * @retval None */ void APP_ErrorHandler(void) { /* Infinite loop */ while (1) { } } /************************ (C) COPYRIGHT Puya *****END OF FILE******************/为什么只打印了一行SystemCoreClock = 48000000
时间: 2025-06-20 20:56:19 AIGC 浏览: 38
### TIM3初始化后只打印一次SystemCoreClock的原因分析及解决方法
用户提到的问题是TIM3初始化后仅打印了一次`SystemCoreClock`值,这可能与定时器中断逻辑或串口打印逻辑有关。以下是对此问题的详细分析和解决方案。
---
#### 1. 定时器中断未正确触发
如果定时器中断未被正确触发,则`HAL_TIM_PeriodElapsedCallback`函数不会被调用,导致后续的`SystemCoreClock`值无法继续打印。根据提供的信息[^2],需要确保以下几点:
- **中断标志位是否清除**:在启动定时器中断之前,必须确保中断标志位已被清除。例如:
```c
__HAL_TIM_CLEAR_FLAG(&htim3, TIM_FLAG_UPDATE);
HAL_TIM_Base_Start_IT(&htim3);
```
如果未清除中断标志位,可能会导致中断逻辑异常[^1]。
- **NVIC配置是否正确**:检查NVIC中是否正确启用了TIM3中断,并设置了适当的优先级。
```c
HAL_NVIC_SetPriority(TIM3_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(TIM3_IRQn);
```
---
#### 2. 中断服务程序逻辑问题
中断服务程序中的回调函数`HAL_TIM_PeriodElapsedCallback`需要正确实现以确保每次定时器更新事件发生时都能执行相应的任务。如果该函数未被调用或逻辑错误,则可能导致`SystemCoreClock`值只打印一次。
以下是一个典型的回调函数实现:
```c
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {
if (htim->Instance == TIM3) {
// 在这里添加打印SystemCoreClock的代码
printf("SystemCoreClock: %lu\n", SystemCoreClock);
}
}
```
此外,还需要确保`TIM3_IRQHandler`函数正确调用了`HAL_TIM_IRQHandler`:
```c
void TIM3_IRQHandler(void) {
HAL_TIM_IRQHandler(&htim3);
}
```
---
#### 3. 串口打印逻辑问题
如果串口打印逻辑存在问题,也可能导致`SystemCoreClock`值只打印一次。以下是一些常见原因及解决方法:
- **缓冲区溢出**:如果使用的是阻塞式串口打印(如`printf`),可能会因为缓冲区满而导致后续数据无法打印。可以通过增加缓冲区大小或使用非阻塞式打印来解决。
- **波特率设置不匹配**:确保串口波特率与接收端一致,否则可能导致数据丢失或乱码。
- **打印频率过高**:如果定时器中断频率过高,而串口打印速度较慢,可能会导致数据丢失。可以通过降低定时器频率或减少打印内容来缓解此问题。
---
#### 4. 定时器参数配置问题
定时器的`Prescaler`和`Period`参数决定了定时器的计数周期。如果这些参数配置不当,可能导致定时器未能按预期工作。例如:
- 假设系统时钟为84MHz,希望定时器产生1Hz的频率,则预分频器值应为8399,自动重装载值应为999。
```c
htim3.Init.Prescaler = 8399;
htim3.Init.Period = 999;
```
如果这些参数配置错误,可能导致定时器未能按时触发中断。
---
#### 5. 示例代码
以下是一个完整的示例代码,展示如何正确初始化TIM3并打印`SystemCoreClock`值:
```c
#include "stm32fxxx_hal.h"
TIM_HandleTypeDef htim3;
void MX_TIM3_Init(void) {
htim3.Instance = TIM3;
htim3.Init.Prescaler = 8399; // 假设系统时钟为84MHz
htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
htim3.Init.Period = 999; // 定时1秒
htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
HAL_TIM_Base_Init(&htim3);
// 清除中断标志位
__HAL_TIM_CLEAR_FLAG(&htim3, TIM_FLAG_UPDATE);
// 启动定时器中断
HAL_TIM_Base_Start_IT(&htim3);
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {
if (htim->Instance == TIM3) {
printf("SystemCoreClock: %lu\n", SystemCoreClock);
}
}
void TIM3_IRQHandler(void) {
HAL_TIM_IRQHandler(&htim3);
}
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* tim_baseHandle) {
if (tim_baseHandle->Instance == TIM3) {
__HAL_RCC_TIM3_CLK_ENABLE();
HAL_NVIC_SetPriority(TIM3_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(TIM3_IRQn);
}
}
```
---
###
阅读全文
相关推荐



















