在 STM32f4xx 上启用定时器中断时遇到问题
Having trouble getting timer interrupts to enable on the STM32f4xx
我正在做一个需要定时器中断的项目。
使用 STM32cubeIDE,我生成的代码应该 与定时器中断一起工作。这是我的 main.cpp 的截断:(htim1
是一个全局句柄)
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_TIM1_Init();
HAL_TIM_Base_Start_IT(&htim1);
while(1);
这是 MX_TIM1_INIT()
的样子:
static void MX_TIM1_Init(void)
{
/* USER CODE BEGIN TIM1_Init 0 */
/* USER CODE END TIM1_Init 0 */
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
TIM_OC_InitTypeDef sConfigOC = {0};
TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0};
/* USER CODE BEGIN TIM1_Init 1 */
/* USER CODE END TIM1_Init 1 */
htim1.Instance = TIM1;
htim1.Init.Prescaler = 84-1;
htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
htim1.Init.Period = 0xFFFF-1;
htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim1.Init.RepetitionCounter = 0;
htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
{
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}
if (HAL_TIM_PWM_Init(&htim1) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 0;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;
if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
{
Error_Handler();
}
sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE;
sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE;
sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;
sBreakDeadTimeConfig.DeadTime = 0;
sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;
sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;
sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
if (HAL_TIMEx_ConfigBreakDeadTime(&htim1, &sBreakDeadTimeConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN TIM1_Init 2 */
/* USER CODE END TIM1_Init 2 */
HAL_TIM_MspPostInit(&htim1);
}
这是我的中断处理程序的样子:
void TIM1_UP_TIM10_IRQHandler(void)
{
/* USER CODE BEGIN TIM1_UP_TIM10_IRQn 0 */
/* USER CODE END TIM1_UP_TIM10_IRQn 0 */
// HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_12); //O-SCOPE DEBUG on PA6
// if (timer1 == nullptr) return;
//timer1->TimerISR();
HAL_TIM_IRQHandler(&htim1);
/* USER CODE BEGIN TIM1_UP_TIM10_IRQn 1 */
/* USER CODE END TIM1_UP_TIM10_IRQn 1 */
}
这是我的回调函数:
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {
HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_12); //O-SCOPE DEBUG on PA6
}
无论我做什么我都无法让调试器进入处理程序...。谁能告诉我我做错了什么?谢谢!
我永远不会使用 HAL 库来设置计时器。这对我来说毫无意义。在下面的示例中,我将省略时钟(在我的例子中为 168MHz)和 GPIO 设置。由于您没有说明您使用的是什么型号的 STM32F4,因此该代码是使用 STM32F446RET uC 测试的。其他STM32F4有相同的定时器。
- 设置定时器:
__HAL_RCC_TIM1_CLK_ENABLE();
TIM1 -> PSC = (20000 - 1);
TIM1 -> ARR = (4200 - 1);
//168e6 / (20000 * 4200) = 2 - two interrupts per second
TIM1 -> EGR |= TIM_EGR_UG; // reinitialize the counter and reload registers
TIM1 -> DIER |= TIM_DIER_UIE;
NVIC_EnableIRQ(TIM1_UP_TIM10_IRQn);
TIM1 -> CR1 = TIM_CR1_CEN;
- 中断处理程序。如果您使用 C++ 编程,处理程序必须声明为
extern "C"
!!!
//if you compile as C++ you need to declare handlers as "normal" C functions
//#ifdef`s are not needed if this code will never be compiled as C progream
#ifdef __cplusplus
extern "C" {
#endif
void TIM1_UP_TIM10_IRQHandler(void)
{
if(TIM1 -> SR & TIM_SR_UIF)
{
TIM1 -> SR = ~(TIM_SR_UIF); // clear UIF flag
GPIOA -> ODR ^= 1 << 5; // toggle PA5
}
}
#ifdef __cplusplus
}
#endif
我连接到 PA5 的 LED 每 500 毫秒改变一次状态。
大功告成 - 是不是比 HAL 更简单?
我正在做一个需要定时器中断的项目。
使用 STM32cubeIDE,我生成的代码应该 与定时器中断一起工作。这是我的 main.cpp 的截断:(htim1
是一个全局句柄)
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_TIM1_Init();
HAL_TIM_Base_Start_IT(&htim1);
while(1);
这是 MX_TIM1_INIT()
的样子:
static void MX_TIM1_Init(void)
{
/* USER CODE BEGIN TIM1_Init 0 */
/* USER CODE END TIM1_Init 0 */
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
TIM_OC_InitTypeDef sConfigOC = {0};
TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0};
/* USER CODE BEGIN TIM1_Init 1 */
/* USER CODE END TIM1_Init 1 */
htim1.Instance = TIM1;
htim1.Init.Prescaler = 84-1;
htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
htim1.Init.Period = 0xFFFF-1;
htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim1.Init.RepetitionCounter = 0;
htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
{
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}
if (HAL_TIM_PWM_Init(&htim1) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 0;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;
if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
{
Error_Handler();
}
sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE;
sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE;
sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;
sBreakDeadTimeConfig.DeadTime = 0;
sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;
sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;
sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
if (HAL_TIMEx_ConfigBreakDeadTime(&htim1, &sBreakDeadTimeConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN TIM1_Init 2 */
/* USER CODE END TIM1_Init 2 */
HAL_TIM_MspPostInit(&htim1);
}
这是我的中断处理程序的样子:
void TIM1_UP_TIM10_IRQHandler(void)
{
/* USER CODE BEGIN TIM1_UP_TIM10_IRQn 0 */
/* USER CODE END TIM1_UP_TIM10_IRQn 0 */
// HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_12); //O-SCOPE DEBUG on PA6
// if (timer1 == nullptr) return;
//timer1->TimerISR();
HAL_TIM_IRQHandler(&htim1);
/* USER CODE BEGIN TIM1_UP_TIM10_IRQn 1 */
/* USER CODE END TIM1_UP_TIM10_IRQn 1 */
}
这是我的回调函数:
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {
HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_12); //O-SCOPE DEBUG on PA6
}
无论我做什么我都无法让调试器进入处理程序...。谁能告诉我我做错了什么?谢谢!
我永远不会使用 HAL 库来设置计时器。这对我来说毫无意义。在下面的示例中,我将省略时钟(在我的例子中为 168MHz)和 GPIO 设置。由于您没有说明您使用的是什么型号的 STM32F4,因此该代码是使用 STM32F446RET uC 测试的。其他STM32F4有相同的定时器。
- 设置定时器:
__HAL_RCC_TIM1_CLK_ENABLE();
TIM1 -> PSC = (20000 - 1);
TIM1 -> ARR = (4200 - 1);
//168e6 / (20000 * 4200) = 2 - two interrupts per second
TIM1 -> EGR |= TIM_EGR_UG; // reinitialize the counter and reload registers
TIM1 -> DIER |= TIM_DIER_UIE;
NVIC_EnableIRQ(TIM1_UP_TIM10_IRQn);
TIM1 -> CR1 = TIM_CR1_CEN;
- 中断处理程序。如果您使用 C++ 编程,处理程序必须声明为
extern "C"
!!!
//if you compile as C++ you need to declare handlers as "normal" C functions
//#ifdef`s are not needed if this code will never be compiled as C progream
#ifdef __cplusplus
extern "C" {
#endif
void TIM1_UP_TIM10_IRQHandler(void)
{
if(TIM1 -> SR & TIM_SR_UIF)
{
TIM1 -> SR = ~(TIM_SR_UIF); // clear UIF flag
GPIOA -> ODR ^= 1 << 5; // toggle PA5
}
}
#ifdef __cplusplus
}
#endif
我连接到 PA5 的 LED 每 500 毫秒改变一次状态。
大功告成 - 是不是比 HAL 更简单?