AVR is 8 bit microcontroller therefore all its ports are 8 bit wide. Every port has 3 registers associated with it each one have size of 8 bits. Every bit in those registers configures the pins of particular port. Bit0 of these registers are associated with Pin0 of the port, Bit1 of these registers are associated with Pin1 of the port, and same as for other bits.
The three registers available in AVR microcontroller are as follows:
Data Direction Register configures the data direction of port pins. These registers are used for determining whether port pins will be used for input or output. On writing 1 to a bit in DDRx makes corresponding port pin as output, while writing 0 to a bit in DDRx makes corresponding port pin as input.
PINx register used to read the data from port pins. In order to read the data from port pin, first we have to change the port?s data direction to input. This is done by setting bits in DDRx to zero. If port is made output, then reading PINx register will give a data that has been output on port pins.
There are two input modes. Either we can use port pins as internal pull up or as tri stated inputs. It will be explained as shown below:
For reading the data from port A,
In general PORTx register can be used for two purposes:
To output data in variable x on port A
To output 0xFF data on port B
To output data on only 0th bit of port C
In input mode, when pull-up is enabled, default state of the pin is '1'. So if we don't add anything to pin and if we try to read it then it will read as 1.
Note: While using on chip Analog to Digital Converter (ADC), ADC port pins must be used as tri stated input.
To make lower nibble of port A as output, higher nibble as input with pull-ups enabled
To make port B as tri stated input
To make port C as input with pull-ups enabled and read data from port a