← All projects
Project

Digital Audio Player

Microprocessor Systems (E155) · Fall 2024

Bare-metal STM32 firmware that plays music through a speaker, using two hardware timers for pitch and note duration, with frequency error checked across three octaves.

  • STM32
  • Timers
  • Embedded C
  • LM386
  • MATLAB
Schematic of the STM32 driving an LM386 amplifier and speaker

Overview

The STM32L432KC plays a song from a table of note frequencies and durations. One timer generates the tone on a GPIO pin, and a second one times how long each note lasts. An LM386 amplifier (gain of 50) drives an 8 Ω speaker, with a 10 kΩ potentiometer for volume.

Timer design

Timer roles and program flow
Timer roles and program flow

With an 80 MHz system clock:

Tone timer (TIM16)Delay timer (TIM1)
Prescaler1997999
Base frequency400 kHz10 kHz
Range6.1 Hz – 200 kHz0.1 ms – 6.55 s

The auto-reload value for each note is ARR = 80 MHz / ((199 + 1) · f). Rather than checking every note by hand, I wrote a MATLAB script that computes the actual frequency for every pitch from A2 to A5 and every note length in the song.

Frequency and duration error for every note used
Frequency and duration error for every note used
← All projects