Lab 4: Digital Audio

FA24 Checkoff Modification

Due to Prof. Brake’s travel schedule, there will be no standard checkoff for this lab. Instead of meeting with him to demonstrate the performance of your system, you must upload a video of your system in operation. Your video should document the entire song playing.

Uploading a video directly to your Github site is not recommended due to the large file sizes involved. Instead, you should upload your video to a hosting site like YouTube (you may used your HMC Google account) and then copy and paste the embed HTML code into your website. To do so, you may use the following Quarto shortcode as shown below. Simply replace the given URL with the URL to your video. More information can be found in the Quarto docs.

{{< video https://www.youtube.com/embed/wo9vZccmqwc >}}

Introduction

In this lab you will use your MCU to play music by using timers to generate square waves by toggling a GPIO pin at a specific frequency for specified durations.

Learning Objectives

By the end of this lab you will have…

  • Built a circuit to enable an I/O pin from your MCU to drive a speaker
  • Implemented the timer functionality available on the MCU by reading the datasheet and writing your own library in C from scratch

Requirements

Build a system to play music on a speaker. Use your MCU, an LM386 audio amplifier, and an 8-ohm speaker. The MCU should read a list of notes specifying the pitch (in Hz) and duration (in ms) of each note. It should generate a corresponding sequence of square waves. A frequency of 0 indicates a rest (silence for the given duration). A duration of 0 indicates the end of the song. Your system should play accurate pitches regardless of the frequency. Test your system on the score of Für Elise, which is provided. Your code should manually define #define macros and register structures for the memory-mapped registers you need to manipulate to help you practice developing your own device drivers from scratch using only the reference manual as a reference. In particular, this means that you are not allowed to use the CMSIS headers for your device.

Discussion

You can find starter code for this lab in the course Github repository. The code provides the Für Elise score as an array of ordered pairs of pitch frequencies and durations.

A goal of this lab is for you to learn to interpret a datasheet and figure out how the timer works. Write your own code from scratch to use the system timer.

The GPIO pins don’t generate enough output current to play satisfactory music directly on the speaker, so use an LM386 audio amplifier between the MCU and the speaker. Do not connect the MCU directly to the speaker, as the current draw could damage it. The datasheet shows AC coupling from the amplifier to the speaker, but you can leave out the capacitors and resistors and produce an acceptable square wave. Volume control is optional (but recommended for your own sanity and that of your roommates and labmates). There are only a limited number of speakers available in the lab so please leave the speakers in the supply cabinet when you leave the lab. Do not leave them attached to your breadboard when you are done working. If you kill a speaker, throw it away rather than putting it back in the cabinet for your unfortunate classmates.

Optional Exercise

In the past students have enjoyed composing their own tune to play on their MCU. The following information may help if you wish to compose your own piece of music.

The duration depends on an arbitrary choice of tempo (speed at which the piece is played). If a whole note is chosen to be 1/2 a second long, other notes follow accordingly:

Note Type Duration [s]
Whole 0.5
Half 0.25
Quarter 0.125
Eighth 0.0625
Sixteenth 0.03125

Recall that the A above middle C (called A4) is 440 Hz (at least in the United States) and that an octave spans a factor of 2 in frequency. There are twelve notes in an octave spaced evenly on a geometric scale, so each is separated in frequency by a factor of 21/12.

Note Frequency (Hz)
A3 220
A3 sharp / B3 flat 233.1
B3 246.9
C4 (middle C) 261.6
C4 sharp / D4 flat 277.2
D4 293.7
D4 sharp / E4 flat 311.1
E4 329.6
F4 349.2
F4 sharp / G4 flat 370.0
G4 392.0
G4 sharp / A4 flat 415.3
A4 440
A4 sharp / B4 flat 466.2
B4 493.9
C5 523.3
C5 sharp / D5 flat 554.4
D5 587.3
D5 sharp / E5 flat 622.2
E5 659.2
F5 698.4
F5 sharp / G5 flat 740.9
G5 784.0
G5 sharp / A5 flat 830.6
A5 880

Credits

This lab was originally developed in 2015 by Alex Alves ’16, redesigned for the μMudd Mark 5.1 in 2019 by Caleb Norfleet ’21, and redesigned for the μMudd MkVI in Fall 2021 by Prof. Josh Brake.

Share Your Feedback

Share Your Feedback!

If you caught any typos or have any suggestions for this page, please open an issue on the website Github repository. Click the link here for instructions on how to create an issue.