INTERRUPT – ARM

Photo of author

By Jackson Taylor

As an Amazon Associate we earn from qualifying purchases.

An interrupt lets a microcontroller stop what it is doing, handle an urgent event, and then carry on where it left off. This article shows a working interrupt example for an ARM Cortex-M4 board, the Texas Instruments Tiva C series TM4C123G LaunchPad, programmed with Energia. One LED blinks in the main loop, and a push button toggles a second LED through an interrupt.

What an interrupt is and why you need one

The simplest way to read a button is polling: your code checks the pin again and again inside loop(). This works, but it has a drawback. If the program is busy or sitting in a delay(), it can miss a short button press or react late.

With an interrupt, the hardware watches the pin for you. When the chosen event happens, the processor pauses the main program and runs a small function called an interrupt service routine (ISR). When the ISR finishes, the main program resumes.

Hardware Hack

Stop Buying New Batteries for Your Tech

Electronics are expensive, but their power sources don't have to be. Discover the simple engineering trick to recondition dead batteries and bring your gadgets back to 100% capacity.

Watch the Free Video
  • Polling: your code asks “has anything happened?” over and over.
  • Interrupt: the hardware tells your code “something just happened”.

What you need

  • A Tiva C series TM4C123G LaunchPad (ARM Cortex-M4). If you are new to the board, see our overview of the Tiva C series TM4C123G.
  • The Energia IDE installed, with the board selected.
  • A USB cable. The on-board RGB LED and the user push buttons are all you need, so no extra wiring is required.

On this LaunchPad, the blue LED is on pin PF_2 and the green LED is on PF_3. The push button used here is PUSH2, which is connected to pin PF_0. If you want a refresher before continuing, read about digital output on the LaunchPad and digital input on the M4.

The example code

The sketch below blinks the blue LED once per second in loop(). Whenever the button is pressed, the interrupt runs and flips the state of the green LED.

volatile int a=0;
                      void setup()
                      {
                        pinMode(PF_2, OUTPUT); //for blue led
                        pinMode(PF_3, OUTPUT); //for green led
                        pinMode(PUSH2, INPUT_PULLUP);
                        attachInterrupt(PF_0, blinky, FALLING); // Interrupt is fired whenever button is pressed
                                                               //  Here blinky is the interrupt service function
                      }
                      void loop()  //normally this loop runs
                      {
                        digitalWrite(PF_2, 1); //blue LED turns ON
                        delay(1000);
                        digitalWrite(PF_2, 0); //blue LED turns OFF
                        delay(1000);
                      }
                      void blinky() //this function executes when the interrupt occurs
                      {
                        a = !a;
                       digitalWrite(PF_3,a);  // the green LED output is toggled
                      }

How the code works

setup()

Two pins are set as outputs for the LEDs. The button pin is set to INPUT_PULLUP, which turns on an internal pull-up resistor. The pin therefore reads HIGH when the button is released and LOW when it is pressed.

See also
A Unified Treatment of a Family of ZVS and ZCS Resonant Inverters-SIMULATION

The call attachInterrupt(PF_0, blinky, FALLING) links three things: the pin to watch, the function to run, and the type of signal change that triggers it.

Why FALLING?

Because of the pull-up, pressing the button pulls the pin from HIGH to LOW. That is a falling edge, so FALLING fires once at the moment of the press. The usual modes are:

  • FALLING: HIGH to LOW.
  • RISING: LOW to HIGH.
  • CHANGE: either direction.

loop()

The main loop only blinks the blue LED with delay(). It knows nothing about the button. Even while the processor is waiting inside a one-second delay, the interrupt still fires and the green LED responds right away.

The ISR: blinky()

The function blinky() flips the variable a between 0 and 1 and writes it to the green LED. Each press toggles the green LED on or off, and the blue LED keeps its own rhythm.

Why the variable is volatile

The variable a is changed inside the ISR, which the compiler cannot see being called from the main code. Declaring it volatile tells the compiler that its value can change at any time, so it should not cache it. In this tiny sketch it works either way, but it is a good habit, and it matters as soon as the main loop reads a value that the ISR writes.

Rules for writing a good ISR

  • Keep it short. Set a flag or toggle a pin, then return. Do the heavy work in loop().
  • Avoid delay() inside an ISR. Waiting there blocks everything else.
  • Avoid long serial printing inside the ISR for the same reason.
  • Share data with volatile variables.

Dealing with button bounce

A mechanical button does not switch cleanly. The contacts can bounce for a few milliseconds, so one press may produce several falling edges. You may then see the green LED toggle more than once, which makes it look like the button sometimes does nothing.

See also
CD4066B Quad Bilateral Switch

A simple software fix is to ignore edges that arrive too soon after the last accepted one:

Sustainable Tech

Keep Toxic E-Waste Out of Landfills

Don't throw away your failing laptop, phone, or car batteries. Learn how to recondition them at home in just a few hours. It's better for the planet—and your wallet.

Learn How It Works
volatile int a=0;
volatile unsigned long lastPress=0;

void blinky()
{
  unsigned long now = millis();
  if (now - lastPress > 200)   // ignore edges within 200 ms
  {
    a = !a;
    digitalWrite(PF_3, a);
    lastPress = now;
  }
}

The 200 ms value is only a starting point. Increase or decrease it depending on how your button behaves.

Things to try next

  • Change FALLING to RISING and notice that the LED now toggles when you release the button.
  • Use CHANGE so the green LED follows the button: on when pressed and off when released.
  • Attach the second on-board button (PUSH1) to a different ISR that controls the red LED.
  • Replace the delay-based blink with a timer-based approach so the main loop is never blocked.

If you want to practise reading the button without interrupts first, our push button and LED tutorial covers the polling method. For comparison, the same idea on an Arduino uses an almost identical attachInterrupt() call.

Frequently asked questions

What is the difference between polling and an interrupt?

Polling means your program checks an input repeatedly. An interrupt means the hardware signals the processor when an event happens, so the processor only reacts when needed and can do other work in between.

Can I use any pin for an interrupt on the Tiva C LaunchPad?

The GPIO pins on the TM4C123G generally support interrupts, and Energia lets you use attachInterrupt() on them. Check the Energia reference and the board pin map for the exact pins on your setup.

Why does my LED toggle more than once per press?

Most likely it is button bounce. The contacts open and close several times in a few milliseconds, creating multiple edges. Add a short time check in the ISR, as shown above, or use a hardware debounce circuit.

Can I use delay() inside an interrupt service routine?

It is best not to. An ISR should finish as quickly as possible. Use a flag or a variable to pass the event to loop() and handle slow tasks there.

See also
AD620

Revive Your Dead Electronics

Got a drawer full of dead gadgets or a failing car battery? Instead of paying a premium for replacements, use this simple trick to easily recondition them right from home.

See How It Works