Interrupt is important as it enable software to transfer works to hardware and get notified when the hardware has completed its works. A good example is our previous interrupt tutorial. By passing the input checking to hardware, software no longer require to constantly poll the pin state and check if there is state change.
However, interrupt does make the system more complex in the sense that now any code in the main() function can be interrupted and interrupt service routine(ISR) get executed. Refer below for more explanation of interrupt to avoid buggy software.
Lesson 3: Part 2: Interrupt - External Input
In this lesson we will be showing way to connect STM32 board with external push button. Do take note we are using the source code in previous lesson. The easier way of doing this is following using the same circuit in STM32 board as below:
![]() |
| On Board Push Button Circuit |
*At the time of writing I was unable to find a push button to use, thus I had replace the button with a white wire.
![]() |
| Blue: PA0, Red:3.3V, Green: Ground White: Button |
![]() | |
| White Wire is Not Connect/Button is Release LED is Off |
![]() | ||
|
Lesson 3: Part 1: Interrupt - On-Board Input
Now we are able to read input status from the input pin. In the previous lesson, we have to keep reading the input pin to detect for button press. In a DVD player example, it is very unlikely user will be pressing the button(e.g. play button). But in order to cater for this user interaction, system have to keep reading the input, which is quite inefficient.
The better method would be configure the MCU in such that any input pin change, notify to the program via interrupt. The function that will be called when interrupt occur is called Interrupt Service Routine(ISR).
The following settings will be require to get this function to work:
For input interrupt, 2 configuration is possible, namely: falling interrupt or rising interrupt. This essentially represent the signal state changes. Taking button as example, idle button state will have voltage high, when user press the button, voltage will change from high to low - falling interrupt. Similarly, rising interrupt occur when user release the pressed button.
Scanning the document up and down doesn't have much, expect I spot a section on NVIC (Nested Vector Interrupted Controller), but no SFR at end of section. And later on I discovered this in debugging session:
With this, I believe a section of the NVIC description has left out in technical document. Further digging into Keil header file leads me to below:
/** \brief Structure type to access the Nested Vectored Interrupt Controller (NVIC).
*/
typedef struct
{
__IO uint32_t ISER[1]; /*!< Offset: 0x000 (R/W) Interrupt Set Enable Register */
uint32_t RESERVED0[31];
__IO uint32_t ICER[1]; /*!< Offset: 0x080 (R/W) Interrupt Clear Enable Register */
uint32_t RSERVED1[31];
__IO uint32_t ISPR[1]; /*!< Offset: 0x100 (R/W) Interrupt Set Pending Register */
uint32_t RESERVED2[31];
__IO uint32_t ICPR[1]; /*!< Offset: 0x180 (R/W) Interrupt Clear Pending Register */
uint32_t RESERVED3[31];
uint32_t RESERVED4[64];
__IO uint32_t IP[8]; /*!< Offset: 0x300 (R/W) Interrupt Priority Register */
} NVIC_Type;
Above looks promising with SFR 'ISER', which finally lead to below line to enable interrupt line 0:
NVIC->ISER[0] |= (0x01<<EXTI0_1_IRQn);
And it works! The bad thing, I spend about an hour just because of the missing specification.
**After contacted with ST FAE, they have point me to the document that have description on above SFR register. I do not understand why it is in separate document, my guess is they use the same document for all M0 core, which may include other STM series.
What we need to do is just declare a function with same name to 'connect' into it.
Lastly, in our ISR we may need to include a checking to ensure the interrupt source is indeed from button, and not from others by checking SFR below:
Each time ISR is being called, we also need to clear the bit so that next interrupt could happen.
The better method would be configure the MCU in such that any input pin change, notify to the program via interrupt. The function that will be called when interrupt occur is called Interrupt Service Routine(ISR).
The following settings will be require to get this function to work:
- Configure input (as shown in previous lesson here)
- Configure MCU such that the input state change, it will trigger an interrupt
- Select rising/falling interrupt
- Enable interrupt
- Created ISR source code
For input interrupt, 2 configuration is possible, namely: falling interrupt or rising interrupt. This essentially represent the signal state changes. Taking button as example, idle button state will have voltage high, when user press the button, voltage will change from high to low - falling interrupt. Similarly, rising interrupt occur when user release the pressed button.
Configure MCU For Input Interrupt
We need to figure out the SFR that enable interrupt to be generated, when our input change.![]() |
| SYSCFG_EXTICR1:EXTI0 = 0000 Notifying MCU Tie PA0 To Interrupt Line 0 |
![]() |
| EXTI_IMR:MR0 = 1 Enable Line 0 Interrupt |
Select Falling/Rising Interrupt
![]() | |
| EXTI_FTSR:TR1 Falling Trigger Enable For Button Press |
![]() |
| EXTI_RTSR: TR1 Rising Trigger Enable For Button Release |
Enable Interrupt
This is the best part of writing this lesson. I do not found any description on the technical document. Thus I have no choice but skip this section while writing the source code, and the result: things does not work.Scanning the document up and down doesn't have much, expect I spot a section on NVIC (Nested Vector Interrupted Controller), but no SFR at end of section. And later on I discovered this in debugging session:
![]() |
| NVIC Discovery in Debugger Mode |
/** \brief Structure type to access the Nested Vectored Interrupt Controller (NVIC).
*/
typedef struct
{
__IO uint32_t ISER[1]; /*!< Offset: 0x000 (R/W) Interrupt Set Enable Register */
uint32_t RESERVED0[31];
__IO uint32_t ICER[1]; /*!< Offset: 0x080 (R/W) Interrupt Clear Enable Register */
uint32_t RSERVED1[31];
__IO uint32_t ISPR[1]; /*!< Offset: 0x100 (R/W) Interrupt Set Pending Register */
uint32_t RESERVED2[31];
__IO uint32_t ICPR[1]; /*!< Offset: 0x180 (R/W) Interrupt Clear Pending Register */
uint32_t RESERVED3[31];
uint32_t RESERVED4[64];
__IO uint32_t IP[8]; /*!< Offset: 0x300 (R/W) Interrupt Priority Register */
} NVIC_Type;
Above looks promising with SFR 'ISER', which finally lead to below line to enable interrupt line 0:
NVIC->ISER[0] |= (0x01<<EXTI0_1_IRQn);
And it works! The bad thing, I spend about an hour just because of the missing specification.
**After contacted with ST FAE, they have point me to the document that have description on above SFR register. I do not understand why it is in separate document, my guess is they use the same document for all M0 core, which may include other STM series.
Writing ISR
Writing ISR is a bit tedious as you have to know how to 'tell' your compiler this is an ISR. Thus it generally varies from one compiler to the other one. In our case here, the vector is define in startup file as below:![]() |
| ISR in startup_stm32f030.s |
Lastly, in our ISR we may need to include a checking to ensure the interrupt source is indeed from button, and not from others by checking SFR below:
![]() |
| EXTI_PR:PR0 Reading Interrupt Source, and clearing it |
The Source Code
The complete source code has been push into git.Lesson 2: Part 2: Controlling GPIO - On-Board Input - Input Reading
Recite from Part 1, 3 configurations is require as below:
- Enable GPIO section for respective pin
- SFR: RCC_AHBENR , bit: IOPAEN, value: 1
- Configure pin to input function
- SFR: GPIOA_MODER, bit: MODER0, value: 00
- Reading input status
- SFR: GPIOA_IDR, bit: IDR0
- Enable GPIO section for respective pin
- RCC->AHBENR |= RCC_AHBENR_GPIOAEN;
- Configure pin to input function
- GPIOA->MODER &= ~GPIO_MODER_MODER0; //mask the bit to 00
- GPIOA->MODER |= MODE_INPUT; //set the bit
- Reading pin status into a variable
- InputState = GPIOA->IDR & GPIO_IDR_0;
#include "stm32f0xx.h"
void MCUInit(void)
{
/* Input config */
//Enable GPIO_A block
RCC->AHBENR |= RCC_AHBENR_GPIOAEN;
//Configure PA0 as Input
GPIOA->MODER &= ~GPIO_MODER_MODER0;//mask the bit to 00
}
int main(void)
{
unsigned char InputState;
MCUInit();
while(1)
{
//read input state
InputState = GPIOA->IDR & GPIO_IDR_0;
}
}
Real Life Application
- Pressing play button in DVD to start video playback process.
- Pressing the power button at hand phone to lit up screen for phone unlock process
Exercise
- Since reset setting on GPIOA->MODER is already as input, try remove the configurations and see if input detection still works.
- Base on USER button state, change the LD3 LED state, for example:
- when USER button press, turn on LD3 LED
- when USER button release, turn off LD3 LED
Lesson 2: Part 1: Controlling GPIO - On-Board Input - SFR Study
Introduction
Push button provide a method for human to give input to the system. It can be requesting a system to start a process, or requesting a system to provide system state and etc. For example, pressing a button in remote control to perform volume up action, pressing start button in washing machine to initiate wash status.In this lesson, we are be using out board button as system input.
Working Principles
![]() |
| USER Button (B1) <-> PA0 |
We need the following configuration to enable an input pin
- Enable GPIO section for respective pin
- Enable pin to be input function
- Read the current input state
Step 1: Enable PA0 GPIO Function
- Similar to lesson 1 part 1, we use the same SFR(RCC_AHBENR) to enable GPIO function, but PA0 require us to enable bit (IOPAEN)
![]() |
| Enable Bit IOPAEN In Register RCC_AHBENR |
Step 2: Configure Pin To Be Input Pin
- Similar to lesson 1 part 1, we use the same SFR(GPIOx_MODER). Since we are looking at Port A, the right register is GPIOA_MODER
- PA0 refer to bit 0, thus we should be configure MODER0 to '00'
- If we refer to below, the reset state of the pin is 00, that means after power up the pin has been configure to input state. Thus, we can actually skip this setting in our coding.
![]() |
| Set MODER0 To '00' In Register GPIOA_MODER |
Step 3: Reading Input State
- By reading register GPIOA_IDR, bit IDR0, we will know the current state of the pin.
![]() |
| Reading IDR0 In Register GPIOA_IDR |
Lesson 1: Part 4: Controlling GPIO - External Output - LED Control
To control external LED, the circuit would be as below:
*Note: Use 30 Ohm resistor, image shown is NOT 30 Ohm resistor
*The resistor is use to limit the current, thus it is OK to put put before or after the LED.
As the voltage line at PC9 is only 3.3V, the LED may not achieve maximum brightness. But this example is good enough to demonstrate how we can connect to external device(LED).
A transistor(2N3904) can be use in order to increase control voltage to 5V.
![]() |
| PC9 With External LED |
![]() |
| PC9 -> Orange Wire -> 30 Ohm Resistor* -> LED -> Black Wire -> GND |
*The resistor is use to limit the current, thus it is OK to put put before or after the LED.
As the voltage line at PC9 is only 3.3V, the LED may not achieve maximum brightness. But this example is good enough to demonstrate how we can connect to external device(LED).
A transistor(2N3904) can be use in order to increase control voltage to 5V.
Lesson 1: Part 3: Controlling GPIO - On-Board Output - LED Control
In this section we will be using what we learn from Part 1 and write some C code. Recite from Part 1, 3 configurations is require as below:
Inside the SFR files define all the SFR that we use in source code below.
Then the corresponding C code for 3 cases above are:
- Enable GPIO section for respective pin
- SFR: RCC_AHBENR , bit: IOPCEN, value: 1
- Pin to be output function
- SFR: GPIOC_MODER, bit: MODER9, value: 01
- Control the pin to be high state or low state
- High state: SFR: GPIOC_ODR, bit: ODR 9, value: 1
- Low state: SFR: GPIOC_ODR, bit: ODR 9, value: 0
- #include stm32f0xx.h
![]() |
| Open SFR Define File |
Then the corresponding C code for 3 cases above are:
- Enable GPIO section for respective pin
- RCC->AHBENR |= RCC_AHBENR_GPIOCEN;
- Pin to be output function
- GPIOC->MODER &= ~GPIO_MODER_MODER9; //mask the bit to 00
- GPIOC->MODER |= MODE_OUTPUT; //set the bit
- Control the pin to be high state or low state
- High state:
- GPIOC->ODR |= GPIO_ODR_9;
- Low state:
- GPIOC->ODR &= ~GPIO_ODR_9;
![]() |
| Full Source Code To Control LD3 |
Real life applications:
- With proper hardware circuit, instead of turn on/off LED, we can turn on/off fan, light.
- Replacing the LED with IR transmittor, and with correct timing on turning on/off, we can use it as remote control, to control electrical appliances: TV, DVD.
Exercise
- In the source code we have turn on LED and turn off LED, why the LED is constantly turn on when program is running?
- Repeating Part 1 to 3 process, this time try controlling LD4 LED
Subscribe to:
Posts (Atom)

















