简体   繁体   English

STM32定时器中断

[英]STM32 Timer Interrupts

I am trying to use timer peripheral in STM32L073 but i have problems. 我正在尝试在STM32L073中使用定时器外设,但是我遇到了问题。 I've generated setup code in STM32Cube and using only functions that HAL API provides. 我已经在STM32Cube中生成了设置代码,并且仅使用了HAL API提供的功能。 Problem is that timer interrupt occurs only once and it should occur always then it overflows. 问题是定时器中断仅发生一次,并且应该总是发生,然后溢出。 I don't know if timer is even running or my setup is wrong (which I believe it shouldn't be since I am using Cube generated code). 我不知道计时器是否正在运行,或者我的设置是否错误(我认为这不应该,因为我使用的是Cube生成的代码)。 Anyone can help? 有人可以帮忙吗?

This is called in main before while loop 这在while循环之前在main中调用

void MX_TIM7_Init(void){

TIM_ClockConfigTypeDef sClockSourceConfig;
TIM_MasterConfigTypeDef sMasterConfig;

htim7.Instance = TIM7;
htim7.Init.Prescaler = 00;
htim7.Init.CounterMode = TIM_COUNTERMODE_UP;
htim7.Init.Period = 00;
if (HAL_TIM_Base_Init(&htim7) != HAL_OK) {
    Error_Handler();
    }

sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim7, &sMasterConfig) != HAL_OK){
     _Error_Handler(__FILE__, __LINE__);
    }

}

BaseMspInit Method BaseMspInit方法

void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* tim_baseHandle){

if(tim_baseHandle->Instance==TIM7){
/* USER CODE BEGIN TIM7_MspInit 0 */

/* USER CODE END TIM7_MspInit 0 */
/* Peripheral clock enable */
__HAL_RCC_TIM7_CLK_ENABLE();

/* TIM7 interrupt Init */
HAL_NVIC_SetPriority(TIM7_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(TIM7_IRQn);
/* USER CODE BEGIN TIM7_MspInit 1 */

/* USER CODE END TIM7_MspInit 1 */
   }
}

Then in main I call this 然后总的来说我称之为

HAL_TIM_Base_Start_IT(&htim7);

which is doing this 这样做

HAL_StatusTypeDef HAL_TIM_Base_Start_IT(TIM_HandleTypeDef *htim){
/* Check the parameters */
assert_param(IS_TIM_INSTANCE(htim->Instance));

/* Enable the TIM Update interrupt */
__HAL_TIM_ENABLE_IT(htim, TIM_IT_UPDATE);

/* Enable the Peripheral */
__HAL_TIM_ENABLE(htim);

/* Return function status */
return HAL_OK;
}

and my Interrupt handler calls PeriodElapsedCallback 我的中断处理程序调用PeriodElapsedCallback

 void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {
    HAL_GPIO_TogglePin(led_GPIO_Port, led_Pin);
    }

It compiles there are no errors no warning, in debugger I see this interrupt is triggered only once and then never. 它编译没有错误,没有警告,在调试器中,我看到此中断仅触发一次,然后再也不会触发。 Looking forward for answers 期待答案

PS using SEGGER Embedded Studio 使用SEGGER Embedded Studio的PS

由于未设置ARR,因此每个时钟都会发生溢出

Firstly you need to set these parameters to your destination period 首先,您需要将这些参数设置为目标时间段

Prescaler for the clock source (check the which bus): 时钟源的预分频器(检查哪个总线):

htim7.Init.Prescaler = 00;

How much timer needs to count for ISR occurs: ISR需要计数多少计时器:

htim7.Init.Period = 00;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM