ATtiny2313 Ports, Pins and Registers

Created on: 30 January 2013

Part 2 of the ATtiny2313 Tutorial

A look at the ports and pins of an ATtiny2313 AVR microcontroller and the registers used to control them. ATtiny2313 pins can be individually configured as inputs, outputs or connected to an internal peripheral of the microcontroller. Internal registers in the ATtiny2313 are programmed in software to configure the microcontroller pins and control them.

ATtiny2313 Ports and Pins

The ATtiny2313 has three ports, namely, port A, port B and port D. These ports are connected to the external pins on the ATtiny2313 package.

Pin numbering starts with P (for port) and then the letter of the port, followed by the pin number e.g. PB3 is pin number 3 of port B.

The three ports have the following pins connected to them:

  • Port A – 3 pins, PA0 to PA2
  • Port B – 8 pins, PB0 to PB7
  • Port D – 7 pins, PD0 to PD6

That totals 18 pins, plus two power pins (VCC and GND) make up the 20 pins of the ATtiny2313.

The number of pins on a port are always one more than the number of the last pin of a port because we are counting from 0 and not from 1, e.g. PA0 to PA2 are 3 pins (PA0, PA1, PA2); PA1 to PA2 would be 2 pins.

The image below shows the pins of the three ports highlighted – port A in red, port B in blue and port D in green.

ATtiny2313 ports and pins

Port Registers

Each port has its own set of registers for controlling it. The port registers are accessed in software (e.g. from a C program) and data written to the registers will configure the pins of a port as input pins or output pins or whether the pins are connected to one of the internal peripheral devices of the ATtiny2313.

The pins are individually configurable, so it is possible to have one pin of a port configured as an input next to a pin from the same port that is configured as an output.

Input Pins

When a pin is configured as an input, then the state of that pin can be read in software. The state of a switch (i.e. on or off) that is interfaced to a pin can then be read.

Output Pins

When a pin is configured as an output, it can switch on or off a device that is interfaced to it, e.g. switch on and off an LED or transistor.

Register Details

Each port has a set of three registers for controlling its pins as inputs / outputs (I/O):

  • DDR register: data direction register – configures the pin as input or output.
  • PORT register: data register – when pin is configured as output, writing to this register will drive the pin high or low. When pin is configured as input, writing to this register will enable / disable the internal pull-up resistor on the pin.
  • PIN register: input pins address – reading this register will give the state (0 or 1) of the pins of the port.

Each bit in a register corresponds to a pin on the port that the register applies to. Pin 0 on a port corresponds to bit zero in the registers, pin 1 on a port corresponds to bit 1 in the registers, etc.

On the ATtiny2313, the port registers are labelled with the above names followed by the port letter, e.g. DDRB, PORTB and PINB for the registers that correspond to port B.

In the next parts of this tutorial the registers will be used to configure the port pins of the ATtiny2313 as inputs and outputs, so you will be able to see exactly how this all works. This will be done in a C program using Atmel Studio.

Where to Find Information

Whenever learning a new microcontroller, the primary source of information is the datasheet for the microcontroller. The datasheet is available from the microcontroller manufacturer's website.

The datasheet for the ATtiny2313 can be found on the web page for the ATtiny2313 and the datasheet for the low power ATtiny2313A can be found on the ATtiny2313A page.

The datasheet can look rather cryptic, so keep following this set of tutorials to learn more about the ATtiny2313.