About the Project
This was one of the activities I completed for the course Computer Organization (ENCM 369) at the University of Calgary. I used the C programming language to generate and play musical tunes using a speaker.
I initialized interrupts and an array containing values used to generate a
sine wave. I then defined the notes of the song in a header file,
music.h, with each note corresponding to its respective frequency.
Since only a single sine wave was defined, I manipulated the frequency of the
sine wave to produce different musical notes.
The sine wave consists of 256 values representing one complete period. By incrementing through the array by 4 values at a time, there are 256 / 4 = 64 steps per period. To generate a specific note, I used an interrupt function that controls the time interval between each step of the sine wave. For example, middle C has a frequency of approximately 262 Hz, giving it a period of 1 / 262 ≈ 3.78 ms. Dividing this period by the 64 steps gives approximately 59 μs between each step of the sine wave.
The resulting signal is connected to the non-inverting input of an op-amp, which drives the speaker. By controlling the frequency of the generated sine wave, the speaker produces the corresponding musical notes, which are combined to play the desired tune.