STM32F4 and CubeMX Creating Periodic Interrupts with TIMER
Hello everyone, in this article we are going to talk about How can we create self interrupts in STM32 with CubeMX with TIMER hardwares. We can make some operations periodically.Let's begin.
In this article, I will use the STM32F429ZI Nucleo Board. First create a Project with CubeMX and select the MCU or the development board according to your requirements. Then enable a Timer.
In here I have selected the TIM6 hardware. TIM6 has been connected to the APB1 bus and its maximum frequency is half of connected bus frequency. Also it is a basic type of Timer and has no channel and connected pin. This is the reason which I have selected this timer. My idea was avoiding the pins with configuration of this Timer. Maybe I will need them at another tasks.
As you can see above we have just activated the Timer and enabled the NVIC configuration. So we are going to make our operation at every period is completed. According to our configuration we have set it as 1 second.
Now we can generate code and switch to IDE. I use MDK-ARM and you can use whatever you want.
At the stm32f4xx_it.c file we have all interrupts callbacks. You will see the below TIM6_DAC_IRQHandler method in this file. In here I have added a GPIO toggle method to Open/Close the LED on the board and you will see the led is going to be blinking.
void TIM6_DAC_IRQHandler(void)
{
/* USER CODE BEGIN TIM6_DAC_IRQn 0 */
/* USER CODE END TIM6_DAC_IRQn 0 */
HAL_TIM_IRQHandler(&htim6);
/* USER CODE BEGIN TIM6_DAC_IRQn 1 */
HAL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);
/* USER CODE END TIM6_DAC_IRQn 1 */
}
BUT WHAT IF YOU WANT TO USE THIS IRQ HANDLER AT main.c FILE ?
In here we are going to use __weak callback methods.
You need to place place below method inside of main.c file and it will be calledback every period elapsed for all timers. So you need to control which timer called this method first.
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if(htim->Instance == TIM6){
HAL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);
}
}
AND WHAT IF YOU ONLY NEED A VARIABLE WHICH LOCATED AT MAIN FILE ?
Only thing you need to declare it as extern in main.h header file like below.
main.h
/* Exported types ------------------------------------------------------------*/
/* USER CODE BEGIN ET */
extern uint32_t SENSORS[6];
/* USER CODE END ET */
And redeclare it at main.c file without extern keyword like below :
main.c
/* USER CODE BEGIN 0 */
uint32_t SENSORS[6];
/* USER CODE END 0 */
Now you are ready to use these interrups all of three ways.
Have a good LED BLINKING with STM32F4 and TIMERS.
Burak Hamdi TUFAN.
Comments