UNIT 07 · INTERACTING WITH HARDWARE
Digital Input and Output
Pins that read, drive and survive the real world.
A general-purpose I/O pin looks like the simplest thing on a microcontroller: write a 1, the LED lights; read a 0, the button is pressed. In practice every pin is a small configurable circuit with a multiplexer, an output driver that can push or only pull, an input buffer with pulls and hysteresis, and protection diodes, and the firmware around it decides whether a press is seen once, twice or not at all.
The unit in six ideas
- 1A pin is a multiplexer (who owns it: software GPIO or a peripheral) in front of a pad (input buffer, pulls, Schmitt trigger, drive strength, slew, protection).
- 2Push-pull drives both levels hard with two transistors; open-drain only pulls low, and a pull-up makes the high level.
- 3Every input needs a driver or a pull; use a pull-up with a button to GND (active low) and a pull-down with a button to VDD.
- 4Contacts bounce for milliseconds; sample the input periodically and decide from its history instead of reacting to edges.
- 5Budget both per-pin and total chip current; anything more than a few milliamps, or a sum near the limit, needs a driver transistor.
- 6delay() blocks the whole loop; an input shorter than the delay can be missed entirely.
Lessons
Pin Modes and Alternate Functions
Who decides which peripheral owns a pin, and how do you tell the chip?
Push-Pull and Open-Drain Outputs
What is different about those pins?
Reading Inputs and Handling Floating Pins
What else does a reliable input need?
Debouncing Buttons and Switches
How do you turn that burst into exactly one event, and what does it cost?
Driving Loads and Protecting Pins
What does a pin need around it to drive real loads safely, and to survive the outside world?
Designing a Nonblocking Input-Output Loop
How should a loop be structured so that inputs are never missed and one new feature cannot break the others?