GPIO handling

Why "General Purpose"?

The general purpose inputs and outputs of microcontrollers do not have a predefined function. It is up to the user to decide what functionality a GPIO should have in a particular program, whether it is a digital output or input, an analog output or input, or one of the connectors for a communication protocol.

Configuration

In each case it must specified how a used GPIO is to be handled by the program, this setting is usually not changed at runtime. The way it is specified and the possible options depend on the IDE and the MCU used.

Pull-up and Pull-down

A digital input is used to detect the high and low levels of signals. The state of such an input is unpredictable, random noise will result in a random behaviour. This phenomenon is called floating pin. For reliable readings, a pull-up or pull-down resistor is required, the difference being the direction of the signal level change when the input becomes active. Pull-up is 1 by default, 0 when active, pull-down is the opposite. Microcontrollers usually include internal pull-up and pull-down resistors which can be activated in firmware.

Interrupt

When using microcontrollers, the value of an input can be checked cyclically, just as someone would check their email client continuously to see if a new message has arrived. This is polling, which is not an efficient method. It is much more efficient to receive a notification when a new email has arrived, then the user can immediately see its contents. With polling it is possible that the user does not open an important email for minutes. A notification generated by an MCU is called an Interrupt, which tells the program immediately that a certain event has happened. An Interrupt Service Routine (ISR) can also be configured to a timer.