Peripheral registers and bit masks
About this lesson
A peripheral register is a byte at a fixed address whose bits are wired to hardware. On an Arduino Uno R3, PORTB sits at data address 0x25 and each bit drives a pin. Follow one byte as a mask built with 1 << 5 sets, clears, flips and tests bit 5 (pin 13) while pin 8 on bit 0 stays untouched. Then see why PORTB = m is a bug and what digitalWrite() really does underneath. The lesson “The memory map: flash, RAM and peripherals” shows where these addresses live, and the lesson “Pointers” explains the * that turns an address into a variable.
A register has an address
Inside the Uno's ATmega328P, PORTB is a register: one byte at data address 0x25. Unlike RAM, each bit is wired to a pin: bit 0 drives pin 8, bit 5 drives pin 13. Now PORTB holds 0x01, so only pin 8's LED glows.
PORTB I/O 0x05 + 0x20 = data address 0x25 bits b7 b6 b5 b4 b3 b2 b1 b0 now 0 0 0 0 0 0 0 1 = 0x01 b0 pin 8 (PB0) LED on b5 pin 13 (PB5) LED off
Read the lesson step by step
A register has an address
Inside the Uno's ATmega328P, PORTB is a register: one byte at data address 0x25. Unlike RAM, each bit is wired to a pin: bit 0 drives pin 8, bit 5 drives pin 13. Now PORTB holds 0x01, so only pin 8's LED glows.
PORTB I/O 0x05 + 0x20 = data address 0x25 bits b7 b6 b5 b4 b3 b2 b1 b0 now 0 0 0 0 0 0 0 1 = 0x01 b0 pin 8 (PB0) LED on b5 pin 13 (PB5) LED offPORTB is a pointer
In C, PORTB is a macro (a name swapped for code) for *(volatile uint8_t *)0x25: the byte at address 0x25. Reading it reads the register; writing it changes the pins. volatile makes the compiler do every read and write for real.
#define PORTB _SFR_IO8(0x05) // avr/iom328p.h _SFR_IO8(a) = _MMIO_BYTE(a + 0x20) _MMIO_BYTE(a) = (*(volatile uint8_t *)(a)) so PORTB = *(volatile uint8_t *)0x25Build a mask
Line 1 builds a mask: a byte with a 1 only in the bits you want to touch. 1 << 5 shifts a 1 five places left, giving 0b00100000, which is 0x20. Bit 5 is pin 13.
line 1 uint8_t m = 1 << 5; 1 0b00000001 1 << 5 0b00100000 = 0x20 same as _BV(5) in avr-libcOR sets a bit
PORTB |= m reads PORTB, ORs it with the mask and writes the result back. x OR 0 keeps x; x OR 1 gives 1. Only bit 5 changes: 0x01 becomes 0x21. Pin 13 lights, pin 8 stays on.
line 2 PORTB |= m; PORTB 0b00000001 m 0b00100000 OR result 0b00100001 = 0x21AND NOT clears a bit
~m flips every bit of the mask: 0b11011111. PORTB &= ~m ANDs with it. x AND 1 keeps x; x AND 0 gives 0. Bit 5 clears, 0x21 becomes 0x01: pin 13 goes dark, pin 8 stays lit.
line 3 PORTB &= ~m; PORTB 0b00100001 ~m 0b11011111 AND result 0b00000001 = 0x01XOR flips a bit
PORTB ^= m flips bit 5. x XOR 0 keeps x; x XOR 1 flips it. 0x01 becomes 0x21 and pin 13 lights. Run the same line again and it goes dark: a blink in one line.
line 4 PORTB ^= m; PORTB 0b00000001 m 0b00100000 XOR result 0b00100001 = 0x21 again 0b00000001 = 0x01AND tests a bit
PORTB & m keeps only bit 5 and writes nothing back. The result is 0x20, not zero, so on is true. Had bit 5 been 0, the result would be 0, so false. To read input pins, you read PINB instead.
line 5 bool on = PORTB & m; PORTB 0b00100001 m 0b00100000 AND result 0b00100000 = 0x20 -> true PORTB unchanged (nothing written)Why not PORTB = m?
A plain write puts all eight mask bits into PORTB at once. Bit 5 stays 1, but bit 0 becomes 0 too, so pin 8's LED goes out by accident. |=, &= and ^= change only the bits you mean.
line 6 PORTB = m; before 0b00100001 after 0b00100000 = 0x20 b0 1 -> 0 pin 8 off: a bugWhat digitalWrite really does
digitalWrite(13, LOW) looks up port B and mask 0x20 in tables, checks for PWM (pin 13 has none), pauses interrupts (events that jump in mid-code), then runs *out &= ~bit: the same AND NOT. 0x20 becomes 0x00.
line 7 digitalWrite(13, LOW); bit digitalPinToBitMask(13) = _BV(5) = 0x20 out portOutputRegister(PB) = &PORTB PWM turnOffPWM(timer) if the pin has a timer; pin 13 has none oldSREG = SREG; cli(); *out &= ~bit; SREG = oldSREG;
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01On the ATmega328P, what is PORTB in C?
avr-libc defines PORTB as _SFR_IO8(0x05), which expands to *(volatile uint8_t *)(0x05 + 0x20). Writing that byte changes the output pins (those set as outputs in DDRB) at once; there is no copy step.
02PORTB holds 0b00000001. What does it hold after PORTB |= (1 << 5)?
1 << 5 is 0b00100000. OR keeps every bit where the mask has a 0 and makes a 1 where the mask has a 1, so bit 5 is set and bit 0 stays 1: 0x21.
03Which line turns pin 13 (PB5) off without touching any other pin?
~(1 << 5) is 0b11011111. AND with 1 keeps a bit and AND with 0 clears it, so only bit 5 becomes 0. PORTB = 0 clears every pin; &= (1 << 5) clears every bit except 5.
04PORTB holds 0x21 and the code runs PORTB = (1 << 5). What happens?
A plain assignment writes all eight bits. Bit 5 stays 1, but bit 0 becomes 0, so the LED on pin 8 goes out. Use |=, &= ~ or ^= to change only the bits you mean.
05Why does digitalWrite() save SREG and call cli() around *out |= bit?
|= is a read, an OR and a write. If an interrupt changed another PORTB bit in between, the write would put the old value back. cli() turns interrupts off for those few instructions, and SREG = oldSREG puts them back as they were. Turning off PWM is a separate step before it.
Glossary
- Peripheral register
- A byte (or word) at a fixed address whose bits are wired to hardware, such as a pin, a timer or a UART. Reading or writing it talks to the hardware, not to RAM.
- Memory-mapped I/O
- Putting hardware registers in the same address space as memory, so ordinary load and store instructions (and C pointers) can reach them.
- volatile
- A C and C++ keyword that tells the compiler every read and write of a variable must really happen, because something outside the program (here, hardware) can see or change it.
- Bit mask
- A value with 1s in the bits you want to work on and 0s elsewhere, such as 1 << 5 = 0b00100000 for bit 5. The shift 1 << n moves a 1 left n places, into bit n.
- Read-modify-write
- Reading a register, changing some bits, and writing the result back. If something else changes the register in between, its change can be lost.
- Interrupt
- A hardware event (a timer tick, a pin change, a byte arriving) that pauses the main code, runs a short handler, then returns to where it stopped.
- SREG (status register)
- The AVR's status byte. Its bit 7 (I) turns interrupts on or off, so saving SREG and calling cli() pauses them; SREG = oldSREG restores them.
- PWM
- Pulse-width modulation: a timer switches a pin on and off very fast so its average level sits between LOW and HIGH. On an Uno only pins 3, 5, 6, 9, 10 and 11 have it.
Notes and sources
- Before line 1 the sketch has already made pins 8 and 13 outputs (DDRB = 0x21, with pinMode) and set pin 8 HIGH. Each LED in the figure stands for an LED and its series resistor; the lesson "Digital output: driving an LED" sizes that resistor.
- The ATmega328P numbers its first 64 I/O registers twice: I/O address 0x05 for the IN and OUT instructions, and data address 0x25 (0x05 + 0x20) when they are reached as memory. C uses the data address. PORTB's bits 6 and 7 go to the crystal on the Uno, so only bits 0 to 5 are usable pins.
- The chip can also set or clear one bit of an I/O register at addresses 0x00 to 0x1F in a single instruction, SBI or CBI. PORTB (I/O 0x05) is in that range, so with a constant mask the compiler can turn PORTB |= m into one SBI.
- A read-modify-write takes several steps. If an interrupt changed PORTB between the read and the write, its change would be lost. That is why digitalWrite saves the status register SREG, turns interrupts off with cli(), and restores SREG afterwards.
- Microchip ATmega48A/PA/88A/PA/168A/PA/328/P datasheet (DS40002061B): I/O memory §8.5, I/O ports §14, register summary
- avr-libc: include/avr/iom328p.h (PINB, DDRB and PORTB definitions)
- avr-libc: include/avr/sfr_defs.h (_SFR_IO8, _MMIO_BYTE and _BV)
- avr-libc user manual: Special function registers
- Arduino AVR core: wiring_digital.c (digitalWrite)
- Arduino AVR core: variants/standard/pins_arduino.h (Uno pin to port map)
- Arduino docs: Port Manipulation (source)
- J. W. Valvano, Embedded Systems: Shape the World, ch. 6: Microcontroller ports (UT Austin)
Quiz Unlocks after the last step.