IOT

Working with Interrupts in STM32F103C8T6 Blue Pill Board

One of the key ideas of microcontroller interrupts, will be covered in this course. The STM32F103C8T6 Blue Pill Board interrupts are also something I’ll demonstrate for you. With this understanding of STM32 Interrupts, you can create better code for the MCU’s effective use.

Introduction

One of the most crucial components of every MCU is definitely the interrupt system. No exemption applies to STM32F103C8T6. In a microcontroller, an interrupt is a hardware device that instructs the processor to halt the execution of the currently running instructions in favour of a different set of instructions that are typically higher priority.

I covered a lot of information regarding interrupts, polling, and other relevant subjects in the Arduino Interrupts Tutorial. I advise you to read that tutorial before continuing because interrupts are generally explained the same way for all microcontrollers.

The actual hardware implementation of interrupts inside the microcontroller will be the primary difference. In the following section, I’ll discuss interrupts in STM32F103C8T6.

In this project, I’ll interface a button to the MCU and trigger an external interrupt to the MCU in order to demonstrate interrupts on the STM32F103C8T6 Blue Pill Board.

Components Required

  • STM32F103C8T6 Blue Pill Board
  • Push Button
  • USB to UART Converter (if programming via UART)
  • Connecting Wires
  • USB Cable

Interrupt Types and ISR

Basically, interrupts are classified into two types. They are:

  • Hardware Interrupts
  • Software Interrupts

Hardware Interrupts

Hardware interrupts are any interruptions that originate from sources outside the processor core. The Interrupt from an External Button is the most basic illustration.

Software Interrupts

A Software Interrupt will be produced if there is an issue with the programme that is running on the MCU.

Interrupt Service Routine or Interrupt Handler

Every time an interrupt happens, the accompanying Interrupt Service Routine (ISR), or Interrupt Handler in ARM nomenclature, is called. When an interrupt occurs, the currently running instructions are momentarily suspended and the Interrupt Handler’s instructions are carried out instead. The execution then switches back to the paused instructions.

Interrupt Service Routines should be noted for two reasons. The first need is that the ISR’s instructions be as brief as possible. The second is that blocking functions like delay () cannot be used in ISR.

Interrupts in STM32F103C8T6

A unique hardware component in STM32 Blue Pill, more specifically the STM32F103C8T6 MCU, is called NVIC (short for Nested Vectored Interrupt Controller), and it is in charge of controlling all external interrupts and peripheral interrupts.

If you recall, Arduino only supports two external interrupts: INT0 and INT1, which are located on digital IO pins 2 and 3. On any GPIO pin, however, the STM32F103C8T6 supports up to 16 external interrupts. Simply choose the appropriate pin in the programme based on your circuit design.

How to Enable Interrupts in STM32F103C8T6?

Since we are using the Arduino IDE to create STM32 Blue Pill Applications, we can configure external interrupts on the STM32F103C8T6 using the same syntax. This is accomplished with the “attachInterrupt ()” function, whose syntax is as follows:

attachInterrupt(digitalPinToInterrupt(pin), ISR, mode);

As you can see from the above syntax, the attachInterrupt () function takes three arguments. They are:

  • digitalPinToInterrupt(pin)
  • ISR
  • Mode

You must specify the Pin of the MCU from which an external interrupt is anticipated in the first parameter of the digitalPinToInterrupt(pin) function. For instance, the parameter will be “digitalPinToInterrupt(PA0)” if a button is connected to PA0.

The Interrupt Handler or Interrupt Service Routine (ISR) function name is the second argument. The function shouldn’t accept any parameters and must have a void return type.

The third argument in the attachInterrupt () method is used to specify the manner of interrupt triggering or the point at which interruptions must be triggered during signal transitions. Normally, this parameter can have any one of the five predefined constants in the Arduino environment, however, with the STM32, you have the option to select one of the following three values.

  • CHANGE: When the pin value changes, the interrupt is activated.
  • RISING: When the pin value increases from LOW to HIGH, the interrupt is triggered.
  • FALLING: When the pin value decreases from HIGH to LOW, the interrupt is triggered.

Circuit Diagram

After learning a little bit more about interrupts in the STM32F103C8T6 MCU, let’s move on to the project’s demonstration. A straightforward circuit diagram for connecting a button to an STM32 MCU and causing an interrupt is shown in the image below.

Working with Interrupts in STM32F103C8T6 Circuit Diagram 1

Programming STM32 Blue Pill for Button Interrupt

The Button is initially linked to Pin PA0. Moreover, a 4.7K resistor is used to pull the PA0 pin HIGH. The button’s opposite end is attached to GND.

The button ISR will assist in switching the LED on and off when the following code is used to initiate the interrupt on pin PA0.

Code

int ledPin = PC13;
int buttonPin = PA0;
int ledToggle;
int previousState = HIGH;
unsigned int previousPress = 0;
volatile int buttonFlag;
int buttonDebounce = 20;
void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
attachInterrupt(digitalPinToInterrupt(buttonPin), button_ISR, CHANGE);
}
void loop()
{
if((millis() – previousPress) > buttonDebounce && buttonFlag)
{
previousPress = millis();
if(digitalRead(buttonPin) == LOW && previousState == HIGH)
{
ledToggle =! ledToggle;
digitalWrite(ledPin, ledToggle);
previousState = LOW;
}
else if(digitalRead(buttonPin) == HIGH && previousState == LOW)
{
previousState = HIGH;
}
buttonFlag = 0;
}
}
void button_ISR()
{
buttonFlag = 1;
}

Conclusion

Hope this blog helps you to understand how to Work with Interrupts in STM32F103C8T6 Blue Pill Board. We, MATHA ELECTRONICS  will come back with more informative blogs.

One thought on “Working with Interrupts in STM32F103C8T6 Blue Pill Board

  1. Naveed kazmi says:

    Good effort. I appreciate it .

Leave a Reply

Your email address will not be published. Required fields are marked *