Sistemas Microprocesados

Port Configuration & I/O Management on the ATmega164P — Departamento de Automatización y Control Industrial (DACI) · Escuela Politécnica Nacional

Instructor: Marco Herrera · marco.herrera@epn.edu.ec

Start Learning
Course Outline
Temario
1
Introducción

ATmega164P pin layout, port overview, and equivalent circuit of an I/O pin.

2
Configuración de Puertos

Registers (DDRx, PORTx, PINx), port modes, pull-up resistors, and I/O schematics.

3
Ejercicios

Practical exercises: reading switches, driving LEDs, and rotating LED patterns.

Introducción
ATmega328P — 3 I/O Ports

The ATmega328 is a 28-pin microcontroller featuring four 8-bit I/O ports: B, C, and D. Every pin can be individually configured as input or output.

Port B

PB0 – PB7
SPI / Timers / PCINT8–15

Port C

PC0 – PC6
JTAG / TOSC / PCINT16–23

Port D

PD0 – PD7
UART / INT / PCINT24–31

Each port provides individually configurable pins.

Introducción
Equivalent Circuit of a PIN in/out
Pin Equivalent Circuit

Each I/O pin (Pxn) includes protection diodes (to VCC and GND), a pin capacitance Cpin, and an optional internal pull-up resistor Rpu controlled by a PMOS transistor.

Pull-up Resistor (Rpu)

The pull-up resistor ensures inputs settle at defined logic levels when external devices are disconnected or in high-impedance state. It also enables interfacing between devices operating at different supply voltages.

Configuración de Puertos
Port Registers

Each port is controlled by three registers accessible via I/O and RAM addresses:

Configuración de Puertos
Port Pin Configuration Table (Pag. 58)

The behavior of each pin is determined by DDRxn, PORTxn, and the PUD bit in MCUCR (bit 4, address 0x35):

Configuración de Puertos
Port as Input (Pull-up Enabled)
Assembly Code

Configure PUD=0 (pull-up enabled), set DDRx to 0x00 (all inputs), write 0xFF to PORTx to activate pull-ups, then read with PINx:

ldi ri, 0x00 out DDRx, ri ; all inputs ldi ri, 0xFF out PORTx, ri ; enable pull-ups in ri, PINx ; read port data

With DDRxn=0 and PORTxn=1 (PUD=0), the internal pull-up is active. The pin reads the external switch state via the synchronizer into PINx on the DATA BUS.

Configuración de Puertos
Port as Output — Driving an LED
Assembly Code

Set DDRx to 0xFF (all outputs), then write the desired value to PORTx. The data is driven directly to the pin (Pxn):

ldi ri, 0xFF out DDRx, ri ; all outputs out PORTx, rj ; drive pin value
Key I/O Instructions
  • IN Rd, PINx — Read 8 bits from port
  • OUT PORTx, Rr — Write 8 bits to port
  • SBIC PINx, b — Skip if bit = 0
  • SBIS PINx, b — Skip if bit = 1
  • SBI PORTx, b — Set single bit
  • CBI PORTx, b — Clear single bit
Pinout Arduino NANO- Atmega 328P (arduino.cc)


Exercise 2: "Button" Arduino

Usando el código de ejemplo de Arduino, simular el circuito con el código de programa"Button". Luego, configurar las entradas en registros y comparar el tamaño del programa.

Ejercicios
Exercise 3: Read Port B → Write Port B

Read 4 switches on Port B (0,1,2,3) and display their states on 4 LEDs connected to Port B (4,5,6,7).

Solution (Assembly)

Second Exercise


Exercise 4: Display 7 segmentos

Display 7 segmentos Ánodo Común (AC)

hetpro-store.com

//TABLA 7 SEGMENTOS AC 0BNgfedcba 0b11000000, // 0 0b11111001, // 1 0b10100100, // 2 0b10110000, // 3 0b10011001, // 4 0b10010010, // 5 0b10000010, // 6 0b11111000, // 7 0b10000000, // 8 0b10010000 // 9

Al estar todos los ánodos interconectados, cada segmento (led) se enciendo con 0L.

Display 7 Segmentos Cátodo Común (CC)

hetpro-store.com


//TABLA 7 SEGMENTOS CC
//Ngfedcba (Cátodo común) 0b00111111, // 0 0b00000110, // 1 0b01011011, // 2 0b01001111, // 3 0b01100110, // 4 0b01101101, // 5 0b01111101, // 6 0b00000111, // 7 0b01111111, // 8 0b01101111 // 9

Al estar todos los cátodos interconectados, cada segmento (led) se enciendo con 1L.


  1. Conectar un display de 7 segmentos ánodo común al puerto D. El display mostrará un valor desde 0 a 9 cada segundo. Implementar la tabla necesaria para la lectura.
  1. Conectar dos pulsadores (PB5 Y PB6). Con uno se inhecrementa el valor mostrado del display y con otro se reduce.