Creating a Music Generator
The key to creating music through a microcontroller is to generate the appropriate frequency
based on an equal-tempered music scale (A
4
= 440 Hz). The following chart illustrates the
frequencies generate to create all the notes you hear in your Singing Snowman.
Note
Frequency
Period
Note
Frequency
Period
C5
523.25 Hz
1911
µ
S
C6
1046.50 Hz
956
µ
S
C
#
5
554.37 Hz
1804
µ
S
C
#
6
1108.73 Hz
902
µ
S
D5
587.33 Hz
1703
µ
S
D6
1174.66 Hz
851
µ
S
D
#
5
622.25 Hz
1607
µ
S
D
#
6
1244.51 Hz
804
µ
S
E5
659.25 Hz
1517
µ
S
E6
1318.51 Hz
758
µ
S
F5
698.46 Hz
1432
µ
S
F6
1396.91 Hz
716
µ
S
F
#
5
739.99 Hz
1351
µ
S
F
#
6
1479.98 Hz
676
µ
S
G5
783.99 Hz
1276
µ
S
G6
1567.98 Hz
638
µ
S
A
b
5
830.61 Hz
1204
µ
S
A
b
6
1661.22 Hz
602
µ
S
A5
880.00 Hz
1136
µ
S
A6
1760.00 Hz
568
µ
S
B
b
5
932.33 Hz
1073
µ
S
B
b
6
1864.66 Hz
536
µ
S
B5
987.77 Hz
1012
µ
S
B6
1975.53 Hz
506
µ
S
The actual tone is generated by using the period of the selected note and dividing it by two to
determine the “on” and “off” time of each waveform. The microcontroller reads the note value
and length (stored as 16
th
through whole notes) and transfers these values to a subroutine that
calculates the duration of the note as the total number of waveform cycles. (Note: the duration
of a whole note is approximately one second).
For example, if the note is a half note of D5 then the following calculation is made:
Period = 1703
µ
S ÷ 2
→
852; Length = half note =
8 x 36
→
288
Thus, to create a half note of D5 the microcontroller will pulse the output high for 852
µ
S and
then set it low for the same duration. This will generate a square wave with a frequency of
about 587 Hz. To maintain a consistent length regardless of frequency, the microcontroller will
repeat this frequency a specific number of times based on each specific note (more for higher
notes and less for lower notes). In this case, the waveform above will be repeated 288 times to
equal a total duration of 490,464
µ
S. More or less the half second we want for a half note.