A microcontroller seen as a whole represents a structure composed of a microprocessor and a set of peripheral modules. A microcontroller can have a range of peripheral modules depending on its series. A peripheral module as a rule is a specialized circuit that can perform a certain function. See above some function examples:
• GPIO allows setting or collection of logic level on a microcontroller pin
• EEPROM allows long-term data storage.
• ADC allows conversion of analog signals
• Serial UART allows serial communication
In a certain case we could say that peripherals represent the microcontroller interface with the external world.
The microprocessor (CPU) interaction with peripherals is performed via peripheral registers. The AVR architecture has 64 registers, called peripheral registers or I/O registers.
Each peripheral module has in the peripheral registers space a reserved set of registers. For example, each GPIO module has reserved a set of 3 registers called PORT, PIN and DDR. By use of these registers the microprocessor is able to work with the pin logic levels. We could do a classification of peripheral registers depending on their destination as follows:
• Status Registers - offer the possibility to visualize the current state of a peripheral (flags). As a rule status registers are available only for reading.
• Control Registers - allow configuration of the peripheral module. They are available for both writing and reading.
• Data Registers - enable data exchange between the peripheral module and kernel.
• Selection Registers - allow selection of resources / configurations in a peripheral module.
In many cases peripheral registers don't have a well-defined purpose, ie a register might contain a collection of status bits, selection control. The purpose of peripheral registers can be recognized by their name and often by their termination: _SR, _CR, _DR, _AR, however it's not a rule.
The microprocessor (CPU) doesn't distinguish the peripheral modules. It "sees" the peripheral modules through peripheral registers and the the work with the peripherals occurs through data transfer with an address from the peripheral registers space. The access to peripheral registers will be performed by a special set of commands.
Transfer commands. These commands involve transfers on 8 bits:
in R, P - content transfer of a peripheral register to a general purpose register.
out P, R - content transfer of a general purpose register to a peripheral register.
The commands of setting/resetting of unique bits in a peripheral register will affect only the bit specified by n, the remaining bits will keep the value they had before the operation:
cbi P, n - assigning logical "0", resetting the bit n of port P n = 0 .. 7
sbi P, n assigning logical "1", setting the bit n of port P n = 0 .. 7
Verification / testing commands of status bits within a peripheral register, ignoring the next operation if the check is true:
sbic P, n - ignoring the following command in case the bit in the port P is "0", reset.
sbis P, n - ignoring the following command in case the bit in the port P is "1", set.
Before you start working with a specific peripheral in most cases will be required its configuration / initialization. The process of initialization will be performed through setting the configuration to certain control peripheral registers. In some cases the configuration after reset can be ok, but it is recommended each peripheral module to be initialized on the entire set of configuration bits. The configurations of each peripheral module can be found in the technical documentation of the corresponding peripheral module.
In conclusion we can affirm that peripheral modules represent electrical devices attached to processor with access to peripheral registers.
In order to understand better what a peripheral module is, we could imagine a complex panel, for example inside an airplane. What is seen on this panel are many light bulbs, switches, indicators, etc. Each bulb can be associated with a status bit, a switch with a configuration bit, an alphanumeric indicator - output data etc.
Peripheral modules can generate interrupts in case of appearance of special situations detected by peripheral, such as changing peripheral logic level on the pin, completion of data transfer, conversion completion etc. But we will talk about interruptions later on.
GPIO Peripheral Module - Generic I/O Port.
Any microcontroller has a set of pins, most of which can be configured as generic input/output (GPIO) pins so that it will be possible to assign a logical value to the terminal pin or this logical value to be read from it in case the pin is configured to the input.
The input/output pins are grouped into ports by 8. The work with the port is executed as a whole, therefore, the single transfer operation to port can change at once the configuration of all 8 pins. In result, the transfer operations to port will affect all its pins.
Since GPIO module is a peripheral module of the microcontroller it has reserved a set of registers in the address space of peripheral registers.
Each GPIO module of a microcontroller of AVR architecture has 3 peripheral registers: PINx, DDRx and PORTx.
The explanation of how does the GPIO module work is presented by the following figure:
• PINx - used for reading the logical value of the physical terminal. This register has read only access. The write operation to this register will not affect the physical value of the physical terminal.
• PORTx - register for setting a value of port when it is set to output and activating the pull-up resistance in case when it is set to input.
• DDRx - register for setting the direction of port, input or output.
In case the location of DDRx contains "1", the corresponding pin will be set to output and the logical value from PORTx from the same location will be transfered towards physical terminal. In this case the value of PORTx register will conect the pull-up resistance with logical value "1", the logical value "0" will set the pin in the high impedance state - "HiZ".
It is recommended to avoid the HiZ on terminal, fact that occurs when the terminal isn't conected to a signal source in order to avoid the noise appearance inside the microcontroller chip that in critical cases can result in circuit failure.
In this way the recommendation for the unused pins is to set their configuration as input with activated pull-up resistance - action which will avoid accidental short circuits when it is set as output and avoid noise appearance on unwanted inputs when it is set to input.