Javatpoint Logo
Javatpoint Logo

AVR Registers

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:

  • DDRx register
  • PORTx register
  • PINx register

DDRx register:

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.

For example:

  • For making all pins of port A as output pins:
  • For making all pins of port A as input pins:
  • For making lower nibble of port B as output and higher nibble as input:

PINx register:

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,


PORTx register:

In general PORTx register can be used for two purposes:

  • To output data: when port is configured as output then PORTx register is used. When we set bits in DDRx to 1, corresponding pins becomes output pins. Now we can write the data into respective bits in PORTx register. This will immediately change the output state of pins according to data we have written on the ports.For example:
  • 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

  • To activate/deactivate pull up resistors: when port is configured as input we set the bits in DDRx to 0, i.e. make port pins as inputs the corresponding bits in PORTx registers used to activate/deactivate pull-up registers associated with that pin. In order for activating pull-up resistor, set the bit in PORTx register to 1, and for deactivating (i.e. to make port as tri stated) set it to zero.
  • 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.

    For example:

    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






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA