AVR Mini Piano
DEGREE OBJECTIVE 2
Demonstrate embedded microprocessor system and circuit skills.
How it meets the objective: Every function runs at the register level — port configuration, bit manipulation, and Timer2 hardware PWM — demonstrating direct command of the microprocessor with no library layer in between.
Playing the five-button piano on real hardware: four sustained notes plus an interruptible "Ode to Joy" melody, all generated by Timer2 hardware PWM.
A five-button piano on an ATmega328P written in bare-metal C: no Arduino libraries, no tone() — just direct register manipulation, Timer2 Fast PWM, and clean edge-detected input. Built as the RBT211 final project in C with Microchip (Atmel) Studio and flashed over ISP.
How it works
Four buttons play sustained notes (C, D, E, F) while a fifth starts an interruptible "Ode to Joy" melody. Timer2 runs in Fast PWM mode with OCR2A as TOP; a hz_to_top() conversion sets the pitch, and OCR2B drives a piezo buzzer with roughly 50% duty. Buttons use internal pull-ups and are read directly from PIND/PINB with a small amount of debouncing.
Engineering highlights
Idle-hiss fix at the register level: even at zero duty, the timer leaked noise into the piezo. The fix disconnects OC2B from the pin and drives it low when idle — attach on play, detach on silence.
Interruptible melody playback: an edge detector lets a new tap cancel the melody mid-note without blocking playback, and waits for the button release before starting playback, so the song doesn't instantly cancel itself.
Frequency math for an 8-bit timer: TOP values are computed from F_CPU and the prescaler, clamped to sane limits so every note stays within Timer2's usable range.