Blink with registers, no library
About this lesson
digitalWrite(13, HIGH) feels like one step, but it is a library function that looks up a port and a bit, then sets that bit in a register. On an Arduino Uno R3 pin 13 is PB5, bit 5 of port B, and port B is three bytes in the ATmega328P's data memory: PINB at 0x23, DDRB at 0x24 and PORTB at 0x25. Here you blink the board's LED by writing those registers yourself in plain C, with avr-libc and no Arduino functions. The lesson "Peripheral registers and bit masks" explains masks in general and the C++ lesson "Pointers" explains what *(volatile uint8_t *)0x25 points at; here we use both on one real pin.
Inside digitalWrite(13, HIGH)
digitalWrite(13, HIGH) first looks up pin 13 in tables stored in flash: port B, bit 5. It checks whether a timer drives the pin, pauses interrupts, sets that one bit in a register, then restores the interrupt setting it saved.
digitalWrite(13, HIGH) Arduino core, wiring_digital.c timer = table[13] -> none on pin 13 bit = table[13] -> _BV(5) = 0x20 port = table[13] -> port B save SREG, cli() pause interrupts *out |= bit PORTB |= 0x20 restore SREG interrupts back on all that work, for one bit
Read the lesson step by step
Inside digitalWrite(13, HIGH)
digitalWrite(13, HIGH) first looks up pin 13 in tables stored in flash: port B, bit 5. It checks whether a timer drives the pin, pauses interrupts, sets that one bit in a register, then restores the interrupt setting it saved.
digitalWrite(13, HIGH) Arduino core, wiring_digital.c timer = table[13] -> none on pin 13 bit = table[13] -> _BV(5) = 0x20 port = table[13] -> port B save SREG, cli() pause interrupts *out |= bit PORTB |= 0x20 restore SREG interrupts back on all that work, for one bitPin 13 is really PB5
The Uno's LED, marked L, sits on pin 13. Inside the ATmega328P that pin is PB5: bit 5 of port B. A port is a group of up to eight pins, and each pin owns one bit in three registers.
board pin 13 = LED_BUILTIN, the L LED chip pin PB5 = port B, bit 5 port B 8 bits: PB7 .. PB0 each pin one bit in PINB, DDRB and PORTBRegisters live at addresses
Port B's three registers are bytes in the chip's data memory: PINB at 0x23, DDRB at 0x24, PORTB at 0x25. avr-libc turns PORTB into *(volatile uint8_t *)0x25, a pointer to that byte. volatile makes every read and write really happen.
data memory map (ATmega328P) 0x0000-0x001F 32 registers 0x0020-0x005F 64 I/O registers 0x0060-0x00FF 160 ext I/O 0x0100-0x08FF SRAM 2 KB PINB 0x23 (I/O 0x03 + 0x20) reads the pins; writing a 1 toggles PORTB DDRB 0x24 (I/O 0x04 + 0x20) direction: 1 = output, 0 = input PORTB 0x25 (I/O 0x05 + 0x20) output level: 1 = high, 0 = low #define PORTB _SFR_IO8(0x05) -> (*(volatile uint8_t *)0x25)Line 1: make PB5 an output
1 << 5 shifts a 1 five places left: the mask 0b00100000, or 0x20. DDRB |= mask reads DDRB, ORs in the mask and writes it back. Bit 5 becomes 1, so PB5 is an output. The other seven bits keep their values.
line 1 DDRB |= 1 << 5; mask 1 << 5 = 0b00100000 = 0x20 read DDRB = 0b00000000 OR result = 0b00100000 write DDRB bit 5 = 1 -> PB5 is an outputLine 3: LED on
PORTB |= 1 << 5 sets bit 5 of PORTB the same way. PB5 is an output, so the pin is driven high, to 5 volts, and the LED lights. PINB bit 5 reads the pin, so it shows 1 too.
line 3 PORTB |= 1 << 5; PORTB 0b00000000 | 0b00100000 = 0b00100000 PB5 output + 1 -> driven high, 5 V LED on PINB bit 5 reads the pin: 1Line 4: wait half a second
_delay_ms(500) from avr-libc busy-waits: it just burns clock cycles. It must know the clock, F_CPU, which is 16 MHz on the Uno, so 500 ms is 8,000,000 cycles. Meanwhile PORTB keeps its value and the LED stays on.
line 4 _delay_ms(500); F_CPU 16,000,000 Hz (#define F_CPU 16000000UL) cycles 16,000,000 / 1000 * 500 = 8,000,000 PORTB unchanged: the LED stays onLine 5: LED off
~ flips every bit of the mask: ~0x20 is 0xDF, 0b11011111. PORTB &= 0xDF keeps every other bit and forces bit 5 to 0. The pin drops to 0 volts, the LED goes dark, and PINB bit 5 reads 0 again.
line 5 PORTB &= ~(1 << 5); mask ~0b00100000 = 0b11011111 = 0xDF PORTB 0b00100000 & 0b11011111 = 0b00000000 PB5 output + 0 -> driven low, 0 V LED off PINB bit 5 reads the pin: 0Lines 6 and 7: forever
Another 500 ms wait, then while (1) jumps back to line 3: on, wait, off, wait, forever. That is one blink a second with no Arduino functions at all: one register sets the direction, one sets the level.
line 6 _delay_ms(500); line 7 } -> back to line 3 on 500 ms + off 500 ms = 1 blink per second (1 Hz) // Arduino Uno R3 (ATmega328P, 16 MHz): blink the L LED // avr-gcc + avr-libc, no Arduino core #define F_CPU 16000000UL #include <avr/io.h> #include <util/delay.h> int main(void) { DDRB |= 1 << 5; while (1) { PORTB |= 1 << 5; _delay_ms(500); PORTB &= ~(1 << 5); _delay_ms(500); } }Shortcut: write PINB to toggle
The datasheet adds a shortcut: writing a 1 to a bit of PINB flips that bit of PORTB. PINB = 1 << 5; toggles the LED with one write and no read first. The 0 bits in the value change nothing.
PINB = 1 << 5; one write, no read PORTB bit 5 0 -> 1 -> 0 -> ... bits written as 0: unchanged a shorter loop: while (1) { PINB = 1 << 5; _delay_ms(500); }Faster, but tied to one chip
With optimisation on, avr-gcc can turn PORTB |= 1 << 5 into one sbi instruction: 2 clock cycles, 125 ns at 16 MHz. digitalWrite repeats its lookups on every call. The price: this code only runs on chips with this port B.
PORTB |= 1 << 5; -> sbi 0x05, 5 sbi 2 cycles = 125 ns at 16 MHz digitalWrite(13, HIGH) table lookups, timer check, save SREG, cli(), restore registers: fast and small, but tied to the ATmega328P digitalWrite: slower, but the same code on many boards
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01On an Arduino Uno R3, which register bit decides whether pin 13 is an output?
Pin 13 is PB5, bit 5 of port B. DDRB is the data direction register: a 1 in bit 5 makes PB5 an output. There is no bit 13: each port register has only 8 bits.
02PORTB holds 0b01000001. What does it hold after PORTB |= 1 << 5; ?
1 << 5 is 0b00100000. OR keeps every bit that was already 1 and adds bit 5: 0b01000001 | 0b00100000 = 0b01100001.
03Why does the code turn the LED off with PORTB &= ~(1 << 5) instead of PORTB = 0?
~(1 << 5) is 0xDF, all ones except bit 5. AND with it forces bit 5 to 0 and keeps the rest. PORTB = 0 would also switch off anything else on pins 8 to 12.
04Which of these does digitalWrite(13, HIGH) NOT do inside the Arduino core?
digitalWrite only writes the output register (PORTB). Making the pin an output is pinMode's job: it sets the DDRB bit. digitalWrite does look up the tables, check for a timer, and pause interrupts around the write.
05What does the single write PINB = 1 << 5; do on the ATmega328P?
The datasheet: writing a logic one to PINxn toggles the value of PORTxn. Bits written as 0 change nothing, so only PB5 flips.
Glossary
- Register
- A small storage cell inside the chip with a fixed address. Peripheral registers control hardware: writing one changes what a pin does.
- Port
- A group of up to eight pins controlled together. Port B on the ATmega328P is PB0 to PB7, one bit per pin in each port register.
- DDRx (data direction register)
- A 1 in a bit makes that pin an output; a 0 makes it an input.
- PORTx (data register)
- For an output pin, a 1 drives it high (5 V on the Uno) and a 0 drives it low (0 V).
- PINx (input pins address)
- Reading it gives the level of each pin. On the ATmega328P, writing a 1 to a bit toggles the matching PORTx bit.
- Bit mask
- A value with 1s only in the bits you care about, such as 1 << 5 = 0b00100000. Combine it with |= to set bits, or with &= ~ to clear them.
- Memory map
- The list of which addresses hold what. On the ATmega328P the 32 working registers come first, then the 64 I/O registers (0x20-0x5F), then 160 extended I/O registers (0x60-0xFF), then 2 KB of SRAM from 0x100.
- Bare metal
- Firmware that talks to the hardware's registers directly, without an operating system or a library like the Arduino core in between.
Notes and sources
- The card shows the body of main(). The whole program defines F_CPU as 16000000UL, includes <avr/io.h> and <util/delay.h>, and is built with avr-gcc and avr-libc for the ATmega328P, with no Arduino core. _delay_ms needs optimisation turned on and a constant argument to be exact.
- In C, ~(1 << 5) is an int: 0xFFDF on the 16-bit AVR int. Only the low 8 bits, 0xDF, matter when the result is stored in the 8-bit PORTB.
- PORTB's I/O address is 0x05; as a data-memory address it is 0x05 + 0x20 = 0x25. avr-libc writes this as _SFR_IO8(0x05), which becomes a volatile byte pointer to 0x25. The sbi instruction uses the I/O address and only works on I/O addresses 0x00 to 0x1F.
- Port B has 8 bits, but on the Uno PB6 and PB7 are the XTAL pins of the 16 MHz clock, so only PB0 to PB5 reach the headers, as pins 8 to 13. The figure shows the unused PINB bits as 0; a real input pin with nothing connected can read either value.
- digitalWrite also works out which pin you meant on any board, and turns off PWM if a timer was driving the pin. Pin 13 on the Uno has no timer, so for it that check finds nothing to do. The board's L LED follows the level of pin 13: high lights it, low turns it off.
- Microchip ATmega48A/PA/88A/PA/168A/PA/328/P datasheet (DS40002061B): Data Memory Map, I/O Ports, Register Description, Instruction Set Summary
- Arduino AVR core: wiring_digital.c (digitalWrite source)
- Arduino AVR core: variants/standard/pins_arduino.h (pin to port and bit tables)
- Arduino AVR core: boards.txt (Uno: atmega328p, 16 MHz)
- Arduino docs: Blink example (LED_BUILTIN is D13 on the UNO), source
- avr-libc: <util/delay.h> busy-wait delays
- avr-libc: <avr/sfr_defs.h> special function registers (_BV, sbi through |=)
- avr-libc: additional notes from <avr/sfr_defs.h> (_SFR_IO8, __SFR_OFFSET 0x20)
Quiz Unlocks after the last step.