EENG 383
Lab 3 - Music Box with Timers
InLab 3
Some self-guided
activities.
Lab 3 assignment
You may work in teams of one or two.
Lab assignment
Build an embedded system which plays a song every time the upper button is
pressed.
Note
If you have an existing configuration (say from the inlab03) that you want to
copy over to a new project (say for your lab03), then:
- Copy the xxx.mc3 file (from your inlab03) into the main
directory of the new project (lab03),
- In the project manager area of the new project (lab03) add the
xxx.mc3 file into the Important Files folder (in the project navigator
window) by right clicking and selecting "Add Item to Important Files...",
- Open the Microchips code configurator,
- The inlab03 configuration will load,
- Edit the (inlab03) configuration as needed,
- Generate the MCC support files.
Program Requirements
A whole note should take 1 second, half note 0.5 seconds, and so on,
Have a 32nd rest between notes,
Use Timer 1 with a 1:1: prescaler to generate the note frequencies,
Store at least two octaves worth of notes (half periods of timer 1)
in an array called scale. For example,
my code has the following (partial) declaration:
uint16_t scale[NUM_OCTAVES*12] = {12864,12144,11472,10816,10208,9632,…
Note, I used a #define to set NUM_OCTAVES to 2,
Use Timer 0 with a 1:256 prescaler to generate the note durations,
Store your song in two arrays, one for notes and one for duration,
- The array storing the notes of the song should have the index
of the scale of each note. For example, my code which plays Hot
Crossed Buns, has the following (partial) declaration:
uint16_t notes[SONG_LENGTH] = {B5, A5, G4, B5, A5,…
The symbols in the notes array "B5", "A5", etc. are #defined
to indicies in the scale
array which contains the half period of that note. For example,
since the half period of a G4 note is 10208 timer 1 counts (which
is index 4 in the scale array defined above), my code contains the
following #define at the top of the program:
#define G4 4
- The array storing the duration of the notes of the song, For
example my code has the following (partial) declaration:
uint16_t duration[SONG_LENGTH] = {QUA, QUA, HAL, QUA,…
The symbols in the duration array, "QUA", "HAL" are #define to
the number of 1:256
prescaled timer 0 counts each duration required. For example, a quarter
note is 0.25 seconds long, corresponding to 15625 counts on a 1:256
prescaled timer 0. So my code contains the following define:
#define QUA 15625
Set the speaker pin to logic 0 when you are not playing a note,
Use the MCC functions to:
- write the timers,
- check if the timer has overflowed,
- read the button, and
- toggle speaker pin.
Do not use the TMR1_ReadTimer or TMR0_ReadTimer functions.
Verification:
Play the notes of a song with at least 12 notes and that includes
at least 2 different note durations.
Turn-in
You may work with a single partner (or alone) to complete this lab.
Submit your main.c file on Canvas using the instructions posted
there. You should take note of the Rubric that will be used to evaluate
your assignment. Please form a group before submitting using the
instructions posted on Canvas. You will demonstrate your code at the
beginning of lab.