Skip to main content
chriskuku
Senior II
November 28, 2022
Question

Starting one shot TIM1 and catching interrupt of count down event

  • November 28, 2022
  • 11 replies
  • 4634 views

My idea is, to implement a timer (TIM1 in this case).

Given a certain key pressed, I want to fire it up, let it count down causing an interrupt.

This are the parameters I gave it in the init procedure. (STM32F103C8T6)

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};
 
 /* USER CODE BEGIN TIM1_Init 1 */
 
 /* USER CODE END TIM1_Init 1 */
 htim1.Instance = TIM1;
 htim1.Init.Prescaler = 7200;
 htim1.Init.CounterMode = TIM_COUNTERMODE_DOWN;
 htim1.Init.Period = 5000;
 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_OnePulse_Init(&htim1, TIM_OPMODE_SINGLE) != HAL_OK)
 {
 Error_Handler();
 }
 sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
 sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
 if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
 {
 Error_Handler();
 }
 /* USER CODE BEGIN TIM1_Init 2 */
 
 /* USER CODE END TIM1_Init 2 */
 
}

1\ How to I start the timer?

2\ where do I find the IR service routine?

This topic has been closed for replies.

11 replies

Piranha
Principal III
December 18, 2022

> What is the interrupt that is fired, when the timer reaches 65536 (overflow). Is it the Tim1_BREAK interrupt? Or is it the Capture_Compare Interrupt?

These and other questions about configuration are trivial and are explained very well in the reference manual and application notes. Clearly you haven't read any of those. There is a simple truth - using the broken bloatware (HAL) doesn't cancel the reading of documentation for understanding how the hardware works. What you are doing now, is effectively: "I don't want to read the documentation, therefore I will ask other people to read it for me and write code and click CubeMX for me."

> is the logic reversed ?

Maybe analyze what that feature does? Ironically the HAL is used by the people, who do not understand it, because the ones, who go deep enough to understand it, eventually realize it's a junk and stop using it.

You say you are using the HAL, but then the code does something ridiculous with the GPIOx_ODR register directly instead of just using the HAL. You say that you are using TIM1, but then show an ISR for TIM4. You didn't understood that an example is just an example. (How can one fail at that?) Again probably because you think that forum is a place where other people should do the trivial things for you just because you don't want to read the documentation. And, of course, everyone, who criticizes such attitude, should be banned...