我正在使用STM32F401RE板,我希望每隔X秒(例如60秒)发生一次定时器中断。
中断回调起作用。问题是中断不会每60秒发生一次(而是每34秒发生一次)。我为预分频器和周期尝试了不同的值,但我尝试的任何方法都无法正常工作。
我正在另一个项目中使用CubeMX生成的功能:
main.c
TIM_HandleTypeDef htim10;
int main(void)
{
HAL_Init();
SystemClock_Config();
// Some other code
MX_TIM10_Init();
HAL_TIM_Base_Start_IT(&htim10);
while (1)
{
}
}
static void MX_TIM10_Init(void)
{
htim10.Instance = TIM10;
htim10.Init.Prescaler = 35999;
htim10.Init.CounterMode = TIM_COUNTERMODE_UP;
htim10.Init.Period = 60000;
htim10.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
if (HAL_TIM_Base_Init(&htim10) != HAL_OK)
{
Error_Handler();
}
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if(htim->Instance==TIM10)
{
printf("ABCDEFG\n\r");
}
}
void SystemClock_Config(void)
{
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = 16;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLM = 16;
RCC_OscInitStruct.PLL.PLLN = 288;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
RCC_OscInitStruct.PLL.PLLQ = 6;
HAL_RCC_OscConfig(&RCC_OscInitStruct);
RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1);
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();
}
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
__HAL_RCC_SYSCFG_CLK_ENABLE();
}
stm32f4xx_hal_msp.c
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base)
{
if(htim_base->Instance==TIM10)
{
__HAL_RCC_TIM10_CLK_ENABLE();
HAL_NVIC_SetPriority(TIM1_UP_TIM10_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(TIM1_UP_TIM10_IRQn);
}
}
void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* htim_base)
{
if(htim_base->Instance==TIM10)
{
__HAL_RCC_TIM10_CLK_DISABLE();
HAL_NVIC_DisableIRQ(TIM1_UP_TIM10_IRQn);
}
}
stm32f4xx_it.c
void TIM1_UP_TIM10_IRQHandler(void)
{
HAL_TIM_IRQHandler(&htim10);
}
谁能解释我在做什么错?如何配置计时器参数以达到所需的时间段?
提前谢谢!
答案 0 :(得分:1)
您能否捕获项目的CubeMX的Clock Configuration屏幕,它使我诊断问题变得更加容易!
编辑1:我检查您的代码,由于Timer 10由APB2时钟源运行,因此您的Timer 10的时钟运行速度快了两倍。您应该这样配置代码:
<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<div id="root"></div>
弗兰基
答案 1 :(得分:0)
将下面称为MX_NVIC_Init();
的函数添加到main()
函数
static void MX_NVIC_Init(void)
{
HAL_NVIC_SetPriority(TIM1_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(TIM10_IRQn);
}