Instructor: Marco Herrera · marco.herrera@epn.edu.ec
ATmega164P pin layout, port overview, and equivalent circuit of an I/O pin.
Registers (DDRx, PORTx, PINx), port modes, pull-up resistors, and I/O schematics.
Practical exercises: reading switches, driving LEDs, and rotating LED patterns.
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.
PB0 – PB7
SPI / Timers / PCINT8–15
PC0 – PC6
JTAG / TOSC / PCINT16–23
PD0 – PD7
UART / INT / PCINT24–31
Each port provides individually configurable pins.

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.
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.

Each port is controlled by three registers accessible via I/O and RAM addresses:
The behavior of each pin is determined by DDRxn, PORTxn, and the PUD bit in MCUCR (bit 4, address 0x35):

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.

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

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.
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).
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 // 9Al 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 // 9Al estar todos los cátodos interconectados, cada segmento (led) se enciendo con 1L.
Port Configuration & I/O Management on the ATmega164P — Departamento de Automatización y Control Industrial (DACI) · Escuela Politécnica Nacional