Skip to content

Bare Metal Blink

You'll drive an AVR's pins, timer and UART with nothing but the registers from the datasheet, no Arduino framework, and compare the binary size.

About

digitalWrite is dozens of instructions that end in one bit flip. Writing that bit yourself from the datasheet's register map is the whole of embedded programming in miniature, and the moment it clicks, every chip becomes the same chip with different names. You'll set a data direction bit, flip a port bit, configure a timer by hand, and write a byte to the UART, compiled with avr-gcc and flashed with avrdude from the command line. The Arduino IDE's verbose output shows the exact commands it runs, which is your cheat sheet. The error you'll make is setting a timer and forgetting the prescaler, so your blink runs 64 times too fast or too slow.

At a glance

Per requirement
35 XP
Completion bonus
300 XP
Requirements
6
Takes
Two or three weeks
Skill level
Intermediate (months of practice)
Cost
Cheap (under $10)
Supplies
You need some
Where
At home
Other people
On your own
Locked for now
Earn two more Uncommon badges in Microcontrollers & Robotics to open this one. Log in to see how far along you are.

Requirements

  1. 1
    Blink an LED by writing DDRB and PORTB directly, compiled with avr-gcc and flashed with avrdude. A main() with a while loop, _delay_ms from util/delay.h, and F_CPU set to 16000000 for an Uno. Pin 13 is PB5, which you can confirm in the Uno schematic. Then run avr-size on your file and on the Arduino Blink build, and note the difference. Take a screenshot of your compile command, the avrdude output ending in bytes of flash verified, and the two sizes. Photo
  2. 2
    Configure Timer1 from its registers to toggle the LED once a second with no delay loop. CTC mode through the WGM bits, a prescaler of 256, and OCR1A of 62499 at 16 MHz gives one compare match per second. Toggle the pin in the compare-match interrupt, with sei() to enable interrupts. Check it against a stopwatch over 60 seconds. Film ten toggles with a clock in frame. Video
  3. 3
    Send hello out of the UART by writing UBRR0 and UDR0 yourself. Work out UBRR for 9600 baud at 16 MHz (it comes to 103), wait on the UDRE0 bit before each byte, and read it on your laptop with a serial terminal. Take a screenshot of the terminal showing your text. Photo
  4. 4
    Generate PWM from Timer0 by registers and dim an LED on pin 6. Fast PWM mode, the compare output mode bits set for non-inverting output on OC0A, and the duty written to OCR0A. Film the LED at four brightnesses. Video
  5. 5
    Read an ADC channel by registers and print the value over your UART. Set the reference and channel in ADMUX, enable and start a conversion with ADCSRA, wait for the ADSC bit to clear, and read ADC. Take a screenshot of a potentiometer sweep printed in the terminal. Photo
  6. 6
    Write the one-page cheat sheet you wish you'd had. Register, bits, and what each did, for the pin, the timer, the UART and the ADC. Take a photo of the sheet. Photo