Timer Interrupt

Thread Starter

Saiteja chinthalapati

Joined Oct 25, 2018
97
Did anyone have idea on how to write a code to blink LED using timer delay and Timer interrupt in STM32 controller. Timer interrupt is for during delay if any other input comes the system shouldn't get hanged.
 

danadak

Joined Mar 10, 2018
4,057
The IDE must have an example project to blink an LED,. thats almost
universal standard feature in IDE these days.

Also they must have ap notes on configuring and handling interrupts.

Regards, Dana.
 

MrSoftware

Joined Oct 29, 2013
1,970
I'm not familiar with that processor, but here's some pseudo code, it's not the most efficient but should accomplish your task. You could just toggle the pin state inside your interrupt handler since changing a pin state is typically very fast, but in general you want to get in and out of an interrupt handler as quickly as possible. So this code shows how to use a flag to prevent spending too much time inside your handler, in case you decide to do more than just toggle a pin.

Code:
volatile bool LEDOn = false;

// Called when the timer expires
InterruptHandler()
{
  // Toggle the LED flag
  LEDOn = !LEDOn;
}

Init()
{
  // Create a timer that calls InterruptHandler() every 2 seconds
  CreateTimer(2_seconds, &InterruptHandler);
}

main_loop()
{
  while(1)
  {
     if(LEDon)
        SetLEDPin(high);
      else
        SetLEDPin(low);
    // Do other stuff here, nothing is hung....
  }
}

main()
{
  Init();
  main_loop();
}
 
Top