Pins through registers (macros and inline functions)
Drive LEDs and read a button through DDRB, PORTB and PIND with bit macros and inline functions, and time it against digitalWrite.
Firmware · Intermediate
In a real product: Motor controller (Automotive): Direct register access is fast enough for the control loop.
digitalWrite looks up which port and bit a pin number is, checks for PWM, and turns interrupts off and on: about 4 µs on an Uno. Writing the port register yourself is one instruction. This project wraps that in small macros and an inline function, so the fast version is still readable.
What you'll learn
- The three registers of an AVR port: DDRx (direction), PORTx (output, or pull-up for inputs), PINx (input)
- Bit macros (
BIT_SET,BIT_CLEAR,BIT_READ) and why their arguments are wrapped in parentheses - A
static inlinefunction instead of a macro when there is a value to return, with type checking - Measuring both ways with
micros()
How it works
Pins 8–10 are bits 0–2 of port B, so DDRB |= 0b111 makes them outputs, and PORTB sets their levels. Pin 2 is bit 2 of port D: PORTD |= _BV(2) with it an input turns on its pull-up, and PIND reads it.
The LEDs show a 3-bit counter that counts while the button is held. Every second the sketch times 1000 writes each way and prints them: with the register macros the write is a fraction of a microsecond.
Try this
- Remove the parentheses around
bitinBIT_SETand call it with1 + 1: see why macro arguments need them. - Make
buttonPressed()a macro and pass itPIND++: an argument evaluated twice. - Write
PINB = _BV(0): on AVR, writing a 1 to PINx toggles that output, in one instruction.
sketch.ino
// LEDs on pins 8-10 (port B bits 0-2) count in binary while the button on pin 2 (port D bit 2) is held, all through
// the port registers. Every second, 1000 register writes are timed against 1000 digitalWrite calls.
#define BIT_SET(reg, bit) ((reg) |= (1 << (bit)))
#define BIT_CLEAR(reg, bit) ((reg) &= ~(1 << (bit)))
#define BIT_READ(reg, bit) (((reg) >> (bit)) & 1)
const uint8_t LED_MASK = 0b00000111; // PB0-PB2: pins 8, 9, 10
const uint8_t BUTTON_BIT = 2; // PD2: pin 2
static inline bool buttonPressed() { return !BIT_READ(PIND, BUTTON_BIT); } // pull-up: pressed reads 0
static inline void showCount(uint8_t n) {
PORTB = (PORTB & ~LED_MASK) | (n & LED_MASK); // change the LED bits, keep the others
}
void setup() {
Serial.begin(9600);
DDRB |= LED_MASK; // outputs
BIT_CLEAR(DDRD, BUTTON_BIT); // input
BIT_SET(PORTD, BUTTON_BIT); // with its pull-up
}
void loop() {
static uint8_t count = 0;
static unsigned long lastStep = 0, lastReport = 0;
if (buttonPressed() && millis() - lastStep >= 200) {
lastStep = millis();
showCount(++count);
}
if (millis() - lastReport >= 1000) {
lastReport = millis();
unsigned long t0 = micros();
for (int i = 0; i < 1000; i++) { BIT_SET(PORTB, 5); BIT_CLEAR(PORTB, 5); }
unsigned long t1 = micros();
for (int i = 0; i < 1000; i++) { digitalWrite(13, HIGH); digitalWrite(13, LOW); }
unsigned long t2 = micros();
Serial.print("count ");
Serial.print(count);
Serial.print(" register: ");
Serial.print((t1 - t0) / 2000.0, 3);
Serial.print(" us a write, digitalWrite: ");
Serial.print((t2 - t1) / 2000.0, 3);
Serial.println(" us");
}
}