Tech to Inspire
Published in

Tech to Inspire

How Counting in Binary Numbers can Cause Auditory Illusions

Let’s generate Shepard Scales from a PIC microcontroller in Assembly Language

The fundamentals of Shepard Tones

There is a family of auditory illusions called Shepard Tone. Hearing is believing. Here is a great example on YouTube:

Image from the original paper by Roger Shepard¹
Diagram showing how each pin on port B should be switched to produce wave signals at octave intervals. RB0-RB7 are bits on Register B, which are exported to Port B that maps onto the output pins.

Code for the Shepard Tone Generator

We start by implementing the core functionality of producing a Shepard Tone. The relevant code is in tone_generator.asm. Varying the base frequency for different keys, i.e. how fast it counts, is a little involved. For now, we can abstract that complexity away by calling a subroutine DLYSET which will provide us with an appropriate amount of delay. CNT0 is the counter that counts down from 0xFF, 0xFE, … down to 0x00 (this weird notation is called HEX, and it represents numbers with a base 16 where every digit is from 0–9 to A-F), using a DECFSZ command. You could easily substitute this with an INCFSZ command and count up if you’d prefer. CNT1 counts down the number of times this loop has to be repeated in order to make the note last for a quarter of a second. A PIC command consumes 1 μS or 2 μS, depending on whether it involves a jump. The amount of μS required for each command is shown in the comment. TONE_TM is there just to make it consume an extra 2 μS to make every loop consume the same amount of time.

Shepard Tone Generator Circuit + Extra Code

Circuit diagram for the Shepard Tone Generator
Completed Shepard Tone Generator

Generated samples

In case you don’t want to go through the daunting work of shopping for components, soldering, programming, and compiling, below are some example recordings, all taken from the Shepard Tone Generator that I have created. Apart from cropping and concatenating, no post-processing has been performed on the recorded signals.

Example of a Shepard Tone generated by the PIC circuit
Example of the same scale but without the superposition of waves at octave intervals
Example of another Shepard Tone generated by the PIC circuit



Articles on technology, programming, science, machine learning, AI and more, with the hope to make the world kinder and life more meaningful.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Shu Ishida

DPhil student at University of Oxford, researching in computer vision and deep learning. Enjoys programming, listening to podcasts, and watching musicals.