Skip to main content
Associate II
December 29, 2023
Solved

TIM2 on STM32F103RB board - how to set it up?

  • December 29, 2023
  • 2 replies
  • 1532 views

Hi, I have use MX to generate code for this timer. 

The generated code is as follows:

static void MX_TIM2_Init(void)
{

/* USER CODE BEGIN TIM2_Init 0 */

/* USER CODE END TIM2_Init 0 */

TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};

/* USER CODE BEGIN TIM2_Init 1 */

/* USER CODE END TIM2_Init 1 */
htim2.Instance = TIM2;
htim2.Init.Prescaler = 0;
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htim2.Init.Period = 65535;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
{
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_ENABLE;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN TIM2_Init 2 */

/* USER CODE END TIM2_Init 2 */

}

I can't get to see an interrupt happening, neither I see the TIM2->CNT register changing.

What can possibly be wrong?

 

Thanks,

Best answer by Tesla DeLorean

Clock on bus not enabled?

Timer not started?

Check RCC and TIM2 register content, or dump out for review

2 replies

Tesla DeLorean
Tesla DeLoreanBest answer
Guru
December 29, 2023

Clock on bus not enabled?

Timer not started?

Check RCC and TIM2 register content, or dump out for review

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
Associate II
December 29, 2023

It was an IDE problem. For some reason, it was keeping garbage on the registers, I restarted the computer and everything works.

Sorry for creating this post before trying to restart my PC.