Skip to main content

Section 2.3 Configuring a Pin as an Input

Every GPIO pin on the STM32C031C6 can be configured as a digital input, digital output, analog input, or alternate function. For a pushbutton we use digital input mode. The internal structure of one I/O port bit is shown in Figure 2.2.12: an optional internal pull-up or pull-down resistor, a read path (IDR), and a write path (ODR).
Three register fields configure a pin as an input.
GPIOx->MODER, the Mode Register, has two bits per pin, and writing 00 into them selects input mode. Do not assume those bits are already 00: pins reset to 11, analog mode (the reference manual, RM0490 §6.4.1, reset value 0xEBFF FFFF for port A and 0xFFFF FFFF for the other ports), so clear both bits explicitly for every input pin.
GPIOx->PUPDR also has two bits per pin. Writing 01 enables the internal pull-up, 10 enables the pull-down, and 00 leaves the pin floating.
GPIOx->IDR is read-only, and bit \(n\) of it reflects the current logic level on pin \(n\text{.}\)

Insight 2.3.1. Why use a pull-up resistor?

A floating input pin picks up electrical noise and reads unpredictably. Connecting a pull-up resistor, from the pin to the 3.3 V supply, makes the idle state HIGH. When the button closes, it connects the pin to GND, pulling it LOW. This is called active-low logic: pressed = 0, released = 1. The STM32C031C6’s built-in pull-up resistors are typically 40 kΩ, so no external resistor is needed. Figure 2.2.10 shows the breadboard wiring using the internal pull-up. Figure 2.2.11 shows the alternative: an external pull-up resistor on the breadboard.
Setting up PB4 (D5 on the Nucleo) as an input with pull-up enabled, as in button.c:
#define GPIOBEN    (1U << 1)   // bit 1 of IOPENR enables GPIOB clock
#define BUTTON_PIN (1U << 4)   // PB4 = D5

// 1. Enable GPIOB clock
RCC->IOPENR |= GPIOBEN;

// 2. Configure PB4 as input: MODER bits [9:8] = 00
//    Pins reset to analog (11), so clear both bits explicitly
GPIOB->MODER &= ~(1U << 8) & ~(1U << 9);

// 3. Enable internal pull-up on PB4
//    PUPDR bits [9:8]: 01 = pull-up
GPIOB->PUPDR |=  (1U << 8);   // set bit 8
GPIOB->PUPDR &= ~(1U << 9);   // clear bit 9
After a short settling time the pin reads HIGH when the button is released and LOW when pressed. The pull-up has to charge the pin’s stray capacitance first (Figure 2.2.16), which takes a few microseconds; with the debounce capacitor of Subsection 2.5.1 fitted it takes milliseconds, which is why button.c waits 50 ms. Test it with:
// Active-low: IDR bit = 0 means button is pressed
if ((GPIOB->IDR & BUTTON_PIN) == 0) {
    // button is pressed - take action here
}

Check Your Understanding Check Your Understanding

1.

With the internal pull-up enabled on PB4 and the button wired from D5 to GND, what value does GPIOB->IDR & BUTTON_PIN return when the button is pressed?
  • 0 (zero)
  • Correct. Pressing the button pulls the pin to GND, so the IDR bit is 0 (active-low).
  • 1
  • The pin is HIGH (1) when the button is released, not pressed.
  • It depends on the MODER setting
  • MODER selects input vs. output mode, but once in input mode the IDR reflects the pin voltage regardless of PUPDR.
  • Undefined, floating
  • The pull-up resistor prevents the pin from floating; it is well-defined as HIGH when the switch is open.