GPIO

GPIO stands for General Purpose Input/Output. To connect microcontrollers with various available devices, this simple standard interface is used i.e. GPIO. GPIO is used in three main modes: input, output and UART interface. Input mode is the default mode wherein input is received from the connected device, output mode data is delivered to the connected device. So a GPIO port handles both incoming and outgoing digital signals. 

GPIO | Attiny85

As shown in the upcoming figures, ATtiny85 has a total 8-pin interface. 6 pins out of 8 are I/O pins and the rest being power pins. As Attiny85 does not require any extra passive components for functioning, it is considered a good fit for various applications.

Associated Registers

There are three registers associated with attiny85, each name represented with red colored font is a register. Also read about

8051 Registers 8086 Registers

DDRB Register

  • The configuration of ATtiny85 GPIO input pins initiates with DDRB register which helps to use PB5 pin as GPIO. As shown in the diagram this register has 5 Readable/Writable bits. Each of these bits (DDB1, DDB2, DDB3, DDB4, DDB5) is associated with 5 pins (PB1, PB2, PB3, PB4, PB5). Setting a pint to ‘1’ will configure the GPIO pin as ‘output’ and ‘0’ will configure it as ‘input’.

For example, to configure PB5 pin as an output pin set DDB5 bit as ‘1’. So, this register is addressed as DDRB = 0b00100000 

GPIO Register

DDRB Register

  • This register is mainly used to toggle the output state of the pins in the controller. The bits of this register resembles the pin similar to the DDRB register. So to make the output of PB5 pin as high set the PORTB5 bit to ‘1’ and ‘0’ to turn the output of PB5 to low. To toggle the output of PB5 pin this register is addressed as PORTB = 0b00100000.
GPIO Register

PINB Register

  1. Why do we need two registers to address the output of Port B pins? This register is bit addressable due to which any single bit can be addressed to toggle its value without changing/disturbing the rest of the bits. The above registers are byte-addressable where a single bit cannot be addressed individually. For example, to activate PB5: PINB5=1 will toggle the corresponding bit.
GPIO Register

LED using ATtiny85

Components

  • ATtiny85
  • LED 
  • 470 Ω Resistor

Circuit Diagram

First, the power is given to ATtiny85 by connecting 5V to Vcc and connecting the GND pin to the ground. Attach the output pin of ATtiny85 to the anode terminal of the LED. The LED cathode end is then further attached to PB0/GND. Any PB3, PB4, PB5 pin can be used as an output GPIO pin. Connect a transistor to the output to amplify the current. 

#include <avr/io.h>
#include <util.delay.h>

int main(void)
{
	while(1)
	{
		DDRB = 0xff;		//Sets all the pins as output
		PORTB = 0xff;		//Sets all the bits of PORT B as ‘high’
		_delay_ms(500);	//Keeps the LED turned on for half a second
		PORTB = 0x00;	//This turn the LED off
		_delay_ms(500);	//Keeps the LED turned off for half a second
	}
}

Applications of Attiny85 | GPIO

By changing the configuration of ATtimny85 I/O pins, it can be used for various applications such as:

  • ATtiny85 can be used as input
  • ATtiny85 timer to create Time delays
  • ATtiny85 counter to count events or some signals
  • ATtiny85 to compare match
Spread knowledge

Leave a Comment

Your email address will not be published. Required fields are marked *