Digital IO

Reading and writing to digital IO pins is one of the most common tasks done on an Arduino board and so it will be the first thing we will look at replicating.

Unlike on Arduino boards, AVR pins are not numbered 0 upwards but instead numbered 0 – 7 and then grouped into a port A. B, C etc. Each port is controlled by 3 registers that are responsible for reading, writing and setting up the ports. Each bit of a register refers to a single pin of the port with the first bit referring to x0 and the last bit referring to x7, where x is the port letter.

The IO Registers

Digital IO is controlled by three registers on the microcontroller:

  1. DDRx – The Data Direction Register which controls the direction that data flow through the port.
  2. PINx – Used to read data from the pins.
  3. PORTx. – Used to output data if the pin is configured as an output and enable internal pull-up resistors in input mode.

In the previous tutorials we learned how to write to these registers, now we can apply this to control the IO.