EMBEDDED SYSTEMS & MICROCONTROLLERS · PAGE 70
AVR-Style Ports: Direction, Output and Input Registers
Three registers per port.
Many 8-bit chips give each group of eight pins three registers: one sets each pin's direction, one sets output levels, and one reads input levels. Writing a 1 to a direction bit makes that pin an output.
Writing a 1 to the output bit of an input pin often switches on its pull-up resistor. Changing several pins at once with a single write is faster than changing them one by one.
Read-modify-write operations on shared ports should not be interrupted by code touching the same port.
EXAMPLE
/* generic names, not tied to a real chip */ PORT_DIR = 0b00001111; /* low four pins out, high four in */ PORT_OUT = 0b00000101; /* drive pins 0 and 2 high */ uint8_t in = PORT_IN & 0xF0; /* read the upper four */