HomeProjects

Four-channel audio synthesizer

Hardware and assembly firmware for an 8-bit micro with 8 kB of flash and 256 bytes of RAM, designed in 2001.

2001 · Atmel, assembly, analog, audio

I designed the hardware and firmware for this synthesizer in 2001. Assembly language looks ancient now, but it was our standard practice for new hardware on Atmel micros, with a mature code library and toolchain behind it. We used C for other projects, but with 8 kB of flash and 256 bytes of RAM, assembly earned its keep.

Why assembly

Hardware

The synthesizer circuit board: an Atmel micro, four audio output channels with heatsinks, volume pots, a 7-segment display, and an RS-485 serial port
The board. Four output channels, each with its own volume control.

The schematic was drawn in Protel (later Altium). One page is below; the full PDF and all the source are in the GitHub repo, which exists for sharing. The project predates GitHub, so it isn't a development repo.

One page of the Protel schematic: audio amplifier and tune number display
Audio amplifier and tune-number display page of the schematic.

Getting decent audio from an 8-bit micro

The micro can only toggle a pin, so what comes out is a square wave. The hardware turns it into something you'd want to hear with a multi-stage passive RC low-pass filter.

The reason that works is Fourier. A square wave of frequency f is the sum of an infinite series of odd harmonics:

square(t) = (4/π) · [ sin(2πft) + ⅓ sin(2π·3ft) + ⅕ sin(2π·5ft) + … ]

The fundamental is a sine wave at f; the 3rd harmonic is a sine at 3f scaled by ⅓, the 5th at 5f scaled by ⅕, and so on. Even harmonics cancel in a symmetric square wave. Run it the other way and you can extract the fundamental sinusoid from a square wave by filtering out the harmonics, which is exactly what the RC stages do.

Firmware

We had a solid assembly library for serial communication and 7-segment displays. The audio code is original. The micro produces a square wave at the desired note frequency and the filter above does the rest.

A tune is a count of notes followed by a (value, duration) pair for each note:

Assembly data block for tune 255: a note count followed by note value and duration pairs
Tune data: note count, then value and duration per note.

The output toggles on a timer overflow. Timer 2 is a 16-bit up-counter that runs from a preload value to 0xFFFF, overflows, and restarts, so the number of counts per overflow is 65536 − preload. The timer ticks at the 11.0592 MHz oscillator divided by 12, or 921,600 Hz, so one count is about 1.085 µs.

For a preload of 0xEFBF (61,375), that's 4,161 counts, roughly 4.5 ms per overflow. Each overflow toggles the output, so that's half a period: the full period is about 9 ms and the note is about 110 Hz, an A2. Working backwards from the frequency of each note gives the preload table for four octaves of twelve notes:

Assembly lookup table of timer preload values for four octaves of twelve notes, with the resulting frequency in a comment on each line
The note lookup table, with frequencies in the comments.

The entire source is in the repo.