Check Your Understanding2.1.4Check Your Understanding
1.
A GPIO pin is configured as an input with no pull-up or pull-down resistor enabled, and nothing is connected to it. What value will GPIOC->IDR show for that pin?
An unpredictable value: the pin is floating and its voltage is undefined.
Correct. A floating pin picks up electrical noise and nearby signals, so its IDR bit cannot be trusted. Always enable a pull-up or pull-down, or supply an external one.
Always 0, because an undriven pin defaults to ground.
An undriven pin does not default to ground: it floats. Only a pull-down resistor would guarantee a 0.
Always 1, because the STM32C031C6 has an implicit internal pull-up on every pin.
The STM32C031C6’s internal pull-ups are off at reset on every pin you will use (only PA13, a debug pin, starts with its pull-up on), so a pull-up must be enabled explicitly in PUPDR.
The last value that was written to the ODR for that pin.
The IDR reflects the voltage present on the pin, not what was last written to the ODR. In input mode the ODR value is irrelevant to what IDR reads.
A button connects PC13 to ground when pressed. An internal pull-up is enabled on PC13. You write if (GPIOC->IDR & (1U << 13)) to detect a press. Will this detect the button press correctly?
No: with an active-low button, pressing gives IDR bit 13 = 0, so the condition is false when pressed and true when released.
Correct. The pull-up holds the pin HIGH (1) when open; pressing pulls it LOW (0). To detect a press, the condition should check for 0, not 1, for example, !(GPIOC->IDR & (1U << 13)).
Yes: the pull-up drives the pin HIGH when pressed, so bit 13 is 1 and the condition is true.
It is the opposite: pressing connects the pin to ground, overriding the pull-up and driving the pin LOW (0). The pull-up is active when the button is open, not when it is pressed.
Yes: the IDR always returns 1 for pins with pull-ups enabled, regardless of button state.
The pull-up sets the default state to 1 only when nothing else is driving the pin. A pressed button actively drives it to 0, overriding the pull-up.
No: you cannot read a pin that has a pull-up enabled; PUPDR and IDR cannot be used simultaneously.
Pull-ups and IDR work together: that is exactly how buttons are normally wired. The pull-up sets the default level; IDR reads whatever voltage is currently on the pin.
Your code increments a counter each time it detects a falling edge on a button pin. The counter is 0 before you press the button. After pressing the button once slowly and deliberately, the counter shows 23. What is the most likely explanation?
Contact bounce: the button contacts chattering during the press generated 23 falling edges instead of one.
Correct. Without debouncing, each press can produce many edges as the contacts bounce. The solution is hardware debouncing (RC filter) or software debouncing (ignore edges for ~20 ms after the first one).
The pull-up resistor value is too large, causing the pin to charge slowly and cross the threshold multiple times.
A large pull-up slows the rise time but does not by itself cause multiple threshold crossings: the signal rises monotonically. Multiple edges are the signature of bounce.
The microcontroller’s clock is too fast, so it samples the IDR more times than expected during a single press.
The clock speed is irrelevant here: the issue is not how often IDR is sampled but how many actual edges appear on the pin due to bouncing contacts.
The counter variable overflowed and wrapped around to 23.
A counter starting at 0 would need to overflow past its maximum value to wrap to 23, unlikely for a single press. The extra counts come from bounce, not overflow.