Nucleo-F401RE: a button interrupt
Count presses of the Nucleo's blue USER button with EXTI line 13 and the NVIC, all set up in registers, and toggle LD2 from the main loop.
Input · Beginner
In a real product: Smart doorbell (Smart home): A press wakes the device through an interrupt instead of being polled.
The blue B1 USER button on the Nucleo joins PC13 to ground while you hold it, and a resistor on the board holds PC13 high otherwise. This program catches each press with an interrupt, so the main loop never has to watch the pin.
What you'll learn
- Routing a pin to an EXTI line through
SYSCFG->EXTICR, and choosing the edge inEXTI->FTSR - Unmasking the line in
EXTI->IMRand enabling its interrupt in the NVIC - Clearing the pending bit in
EXTI->PRinside the handler, and why a shared variable isvolatile
How it works
Each EXTI line can listen to the pin of that number on one port: line 13 to PA13, PB13, PC13 and so on. Four bits of SYSCFG->EXTICR[3] pick the port, and 2 means port C. SYSCFG has a clock of its own, so SYSCFGEN comes first.
FTSR bit 13 makes the line fire on a falling edge, the moment the button is pressed, and IMR bit 13 lets it through to the NVIC. Lines 10 to 15 share one interrupt, EXTI15_10_IRQn, so the handler checks EXTI->PR to see which line fired and writes a 1 to clear it. If it didn't, the interrupt would fire again as soon as it returned.
The handler only adds one to presses. The loop sees the count change, toggles LD2 and prints it: printing inside the handler would block other interrupts while the characters went out. volatile makes the compiler read presses from memory every time, not keep an old copy in a register.
Try this
- Hold the button: the count goes up once, because only the falling edge fires.
- Set bit 13 of
EXTI->RTSRtoo, and count releases as well as presses. - Remove the line that clears
EXTI->PRand see what the count does.
main.c
// Bare-metal C on the Nucleo-F401RE: no Arduino core and no HAL. CMSIS names every register.
// B1 USER (PC13) counts presses on EXTI line 13; the loop toggles LD2 (PA5) and prints the count.
#include <stdio.h>
#include "stm32f4xx.h"
static volatile uint32_t presses;
void EXTI15_10_IRQHandler(void) {
if (EXTI->PR & EXTI_PR_PR13) {
EXTI->PR = EXTI_PR_PR13; // writing 1 clears the pending bit
presses++;
}
}
// printf sends each character here: USART2 on PA2, which the ST-LINK carries to the Serial Monitor.
int __io_putchar(int ch) {
while (!(USART2->SR & USART_SR_TXE)) {}
USART2->DR = ch;
return ch;
}
int main(void) {
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN | RCC_AHB1ENR_GPIOCEN;
RCC->APB1ENR |= RCC_APB1ENR_USART2EN;
RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN;
GPIOA->MODER = (GPIOA->MODER & ~(GPIO_MODER_MODER5 | GPIO_MODER_MODER2)) | GPIO_MODER_MODER5_0 | GPIO_MODER_MODER2_1;
GPIOA->AFR[0] = (GPIOA->AFR[0] & ~(0xFu << 8)) | (7u << 8); // PA2: AF7, USART2 TX
GPIOC->MODER &= ~GPIO_MODER_MODER13; // PC13: input; the board pulls it up
USART2->BRR = SystemCoreClock / 115200; // 16 MHz HSI after reset
USART2->CR1 = USART_CR1_TE | USART_CR1_UE;
SYSCFG->EXTICR[3] = (SYSCFG->EXTICR[3] & ~SYSCFG_EXTICR4_EXTI13) | SYSCFG_EXTICR4_EXTI13_PC;
EXTI->FTSR |= EXTI_FTSR_TR13; // fire on the falling edge: the press
EXTI->IMR |= EXTI_IMR_MR13;
NVIC_EnableIRQ(EXTI15_10_IRQn);
printf("Press the blue USER button\r\n");
uint32_t shown = 0;
while (1) {
uint32_t now = presses;
if (now != shown) {
shown = now;
GPIOA->ODR ^= GPIO_ODR_OD5;
printf("Presses: %lu\r\n", (unsigned long)shown);
}
}
}