Computer Architecture
How an output port works, from flip-flop to PORTB
An output port is a register whose bits leave the processor and drive pins. An instruction writes it, and between writes it holds, so an LED stays lit while the program gets on with something else. A one-bit port is a D flip-flop with set and clear logic in front of it: D = ON OR (Q AND NOT OFF).
Set, keep, clear
ON is the decoder line of a "set" instruction, OFF the line of a "clear". On ON's tick, the OR makes D 1. On OFF's tick, the keep path is shut, so D is 0. On every other tick, a NOP or any instruction that isn't about the port, the keep path feeds Q back and the pin stays as it was.
A blink is a program
Put ON at address 0, OFF at address 2 and NOP at 1 and 3, and let the program counter run round. The LED is on for two ticks out of every four, for ever. Move OFF to address 3 and it's on for three; take OFF out altogether and, once ON has run, it never goes out. The hardware never changes. Only the program does.
Live experiment
Program the blink
A counter runs addresses 0 to 3 and round, and each address holds ON, OFF or NOP. ON sets the port, OFF clears it and NOP leaves it alone. Tap the addresses to change the program and make the LED stay on for three ticks out of every four.
“Out to the world” is lesson 6 of 6 in Program Peninsula, Computer Architecture World 3. Free with an account. Lessons unlock in order, so you’ll do the 19 before it first.
On an Arduino: PORTB
The Uno's pins are bits of 8-bit port registers, and pin 13 is bit 5 of PORTB. digitalWrite(13, HIGH) ends up setting that one bit, after looking up which port and bit the pin is and checking for a timer on it. You can write the register yourself:
DDRB |= (1 << 5); // pin 13 is an output
PORTB |= (1 << 5); // set: LED on
PORTB &= ~(1 << 5); // clear: LED off
The compiler turns each of the last two lines into a single instruction, SBI or CBI: set bit in I/O register, and clear it. They're this page's ON and OFF, and each takes two clock cycles. Writing the whole register at once also changes up to eight pins at the same instant, which digitalWrite can't do.
Faster than you can see
A processor can write a port millions of times a second. That's how it makes sounds on a speaker pin and dims LEDs with PWM: the same set and clear, repeated faster than an eye or ear can follow.
Ports are addresses
On many chips the ports sit in the same address space as memory, so an ordinary store instruction writes them: memory-mapped I/O. Every pin, screen, speaker and motor driver a processor controls sits behind a register like this one, holding its last instruction until the next.
Free lesson
Now build it yourself
Each lesson comes in three short parts: see the idea, try it on a live board, then build it yourself, with quick checks as you go.
“Out to the world” is lesson 6 of 6 in Program Peninsula, Computer Architecture World 3. Free with an account. Lessons unlock in order, so you’ll do the 19 before it first.
Or start from the very first lesson, no account neededPractice questions
Three quick questions from the lessons. Pick an answer to check it.
Show the answer
Answer: Still on. NOP lights neither ON nor OFF, so the keep path feeds Q back. The port holds.
- Why not “Flickering”? The port changes only on a tick, and only when ON or OFF is lit. NOP leaves it steady.
- Why not “Off”? Only OFF clears the port. NOP lights neither line, so the keep path feeds the 1 back.
- Why not “Unknown”? Every input to the port's D is known: ON is 0, OFF is 0 and Q is 1, so D is 1.
Show the answer
Answer: ON OR (Q AND NOT OFF). ON sets it, Q AND NOT OFF keeps it until OFF comes along.
- Why not “Q XOR ON”? XOR would flip the port on every ON, so a second ON would turn it off, and OFF couldn't clear it.
- Why not “OFF OR Q”? OFF OR Q would turn the LED on with OFF and never let it go out.
- Why not “ON AND OFF”? ON AND OFF is never 1 when one instruction runs at a time, so the LED would never light.
Show the answer
Answer: Is on for only one tick each time round. ON sets it, then the next tick's keep gate needs OFF to be 1, which it isn't, so the LED drops straight away.
- Why not “Never lights”? ON still sets the port directly through the OR, so the LED does light, for one tick.
- Why not “Blinks as before”? The keep path is what holds the LED on between ON and OFF. Without it the on time shrinks to one tick.
- Why not “Stays on for ever”? To stay on it has to keep its 1, and the keep path now opens only while OFF is lit.
“Out to the world” is lesson 6 of 6 in Program Peninsula, Computer Architecture World 3. Free with an account. Lessons unlock in order, so you’ll do the 19 before it first.
Questions people ask
What is an output port in a microcontroller?
A register whose bits are wired to pins. Writing a 1 to a bit drives its pin high and a 0 drives it low, and the value stays until the program writes again. On an Arduino Uno the registers are PORTB, PORTC and PORTD.
Is direct port manipulation faster than digitalWrite?
Much faster. On an Uno, setting a bit of PORTB with a constant compiles to one two-cycle instruction, about 0.125 µs at 16 MHz, while digitalWrite spends a few microseconds looking up the pin. The cost is code that only works on that chip.
What does DDRB do on Arduino?
It's the data direction register for port B: a 1 in a bit makes that pin an output, a 0 an input. pinMode sets it for you. A pin has to be an output before its PORTB bit can drive it.
Related topics
- How a D flip-flop worksCopy D to Q at the clock's rising edge, then hold it: one bit of memory.
- How to blink an LED without delay() on ArduinoBlink an LED with millis() so loop() stays free for every other job.
- How a CPU's register file worksSeveral registers on one data line, and a decoder that makes sure only the named one takes each write.
- How does PWM work?How a pin that's only on or off dims an LED: duty cycle and analogWrite's 0–255.