Basic Theory of Digital IO AVR

AVR IO C Languange

Basic Theory of Digital Input / Output Port on AVR Using C Language – The AVR® 8-bit microcontroller controls applications via its digital Input and Output (I/O) pins.

This pin can monitor any voltage present as a high impedance input and supply or output current as a high or low voltage digital output.

These pins are usually arranged in groups of eight and are referred to as PORTs.

AVR uses the alphabet to name these ports, for example: PortA, PortB, etc. The PortA pins are referred to as PA0 – PA7.

All AVR ports have a Read-Modify-Write function when used as digital I/O ports.

This means that the direction of one port pin can be changed without accidentally changing the direction of the other pins.

The same applies when changing drive values ​​(if configured as output) or enabling/disabling pull-up resistors (if configured as input).

Each output buffer has the characteristics of a symmetrical drive with high sink and source capabilities.

The pin drivers are strong enough to drive the LED display directly.

All port pins have individually selectable pull-up resistors with supply-voltage invariant resistance.

All I/O pins have protection diodes for VCC and Ground as shown in the figure below.

Basic Theory of Digital Input / Output Port on AVR

Configuring Digital I/O Pins

Each port consists of three registers:

DDRx – Data Direction Register

PORTx – Pin Output Register

PINx – Pin Input Register

where x = Port Name (A, B, C or D)

Bit Register

DDxn accessed at DDRx . I/O address

PORTxn bit on PORTx I/O address

PINxn bit on I/O address PINx

Where n = pin bit number in Port Register

DDxn

The DDxn bits in the DDRx Register select the direction of this pin.

a. If DDxn is written to ‘1’, Pxn is configured as the output pin.

b. If DDxn is written to ‘0’, Pxn is configured as an input pin.

PORTxn

The PORTxn bit in the PORTx register has two functions. They can control the output pin status and input pin settings.

As Output:

a. If ‘1’ is written to the bit when the pin is configured as an output pin, the port pin is pushed high.

b. If ‘0’ is written to the bit when the pin is configured as an output pin, the port pin is pushed low.

As Input:

Yes. if ‘1’ is written to the bit when the pin is configured as an input pin, the pull-up resistor is activated.

b. If ‘0’ is written to the bit when the pin is configured as an input pin, the port pin is tri-stated.

PINxn

The PINxn bit in the PINx register is used to read data from the pin port.

When a pin is configured as a digital input (in the DDRx register), and pull-up is enabled (in the PORTx register) the bit will indicate the signal status on the pin (high or low).

Note: If the port makes output, then reading the PINx register will give you the data that has been written to the port pin.

As Input Tri-State:

When the PORTx register disables the pull-up resistor, the input will be declared tri-state, leaving the pin left floating.

When left in this state, even a small static charge on surrounding objects can change the logic state of the pin. If you try to read the corresponding bit in the pin register, the state is unpredictable.

Example

1. PORTA as INPUT

DDRA = 0x00; // make PORTA all input

PORTA = 0xFF; // enable all pull-ups

data = PINA; // read the PORTA pin into variable data

2. PORTB set to tri-state input:

DDRB = 0x00; //make PORTB all inputs

PORTB = 0x00; //disable pull-ups and make all pins tri-state

Tips and Tricks

Switch Between Input and Output

When switching between tri-state ({DDxn, PORTxn} = 0b00) and output high ({DDxn, PORTxn} = 0b11) , the intermediate state with either enabled is enabled ({DDxn, PORTxn} = 0b01) or output is low ({ DDxn , PORTxn} = 0b10) should occur.

Usually, the pull-up activated state is completely acceptable, as a high impedance environment will not notice the difference between a powerful high driver and a pull-up. Otherwise, the PUD bit in the MCUCR List can be set to disable all pull-ups on all ports.

Switching between inputs with pull-ups and low outputs produces the same problem. You should use tri-state ({DDxn, PORTxn} = 0b00) or high output state ({DDxn, PORTxn} = 0b11) as intermediate steps.

Disable Pull-Ups Over-ride

The PUD Pull-up Disable bit in the MCUCR register can over-ride DDRx and PORTx pull-up settings

When this bit is written to either, the pull-ups on the I/O ports are disabled even if the DDxn and PORTxn registers are configured to enable pull-ups ({DDxn, PORTxn} = 0b01) .

Switching I/O Pins

Writing ‘1’ to PINxn changes the PORTxn value regardless of the DDRxn value. The SBI assembly instructions can be used to switch a single bit in the port.

Pin Not Connected

If some pins are not in use, we recommend that you ensure that these pins have a predetermined level, even though most digital inputs are disabled in deep sleep mode.

Floating inputs should be avoided to reduce current consumption in all other modes in which digital inputs are enabled (Reset, Active Mode and Idle Mode).

The simplest method of ensuring a defined level of unused pins is to activate the internal pull-ups. In this case, the pull-ups will be disabled during the reset.

If low power consumption during a critical reset, we recommend that you use an external pull-up or pull-down.

Connecting unused pins directly to VCC or GND is NOT recommended, as this can cause overcurrent if the pins are accidentally configured as outputs.

Hopefully this Basic Theory of Digital IO AVR article is useful.

Scroll to Top