CSE466-03au Lab Assignment 5
This is a 3 part lab. In it, you will:
- Add Envelope
(amplitude) to your control of the piezoelectric transducer.
- Parse MIDI-derived
birdsongs.
- Play the birdsongs
through the piezoelectric transducer.
In this lab you will learn the following:
- how to program a timer pulse-width modulator
output for pulse amplitude and frequency
- how to calculate frequencies and
phase-increments of musical scales, based on the MIDI standard
Steps
Part 1
- Sample code is provided as Birdie.c. Compile this file and load
it. You need to connect your piezoelectric transducer
directly to PD5 (pin 19) and to ground.
- Listen to the output. Observe the PD5 output
with your oscilloscope. Determine the PWM frequency. Based on the
data sheet, calculate the exact sample rate with a clock frequency of
7.3728MHz., and the PWM mode used.
- Calculate an RC filter with a -3dB point at
approximately 0.25 of the PWM frequency (using the nearest values you
can find) and connect it to PD5. Connect your oscilloscope probes to
the PD5 pin and to the output of the RC filter. Describe what you
see for turn-in, and show your calculations.
- Modify the program to read an ADSR envelope
table to control amplitude, using timer/counter 2 for control. See
comments in Birdie.c.
- Demonstrate your program to a TA, who will
check your code for timer 2 usage.
End of part 1
Part 2
- Add a phase accumulator to your code, that
addresses a two-sample table, consisting of “upper” and “lower”. The
waveshape is a square wave.
- The table is accessed by the MSB of your phase
accumulator, which is incremented each sample by a phase increment
value. b’0 = upper, b’1 = lower.
- phase_index = modL(previous_phase
+ increment)
- increment = (TableLength x DesiredFrequency)/
SampleRate
- Your program should output four notes,
corresponding to A5, 880Hz, A6, 1760Hz, A7, 3520Hz, and A8,
7040Hz. A push button should step between notes, with another button
starting and stopping the pitch. Amplitude should be 127.
- Observe the output with your oscilloscope. Your
low-pass filter may need to be adjusted for smooth performance. We
should be able to measure the frequencies.
- Demonstrate your pitch generator to a TA.
End of part 2
Part 3
- Birdsong MIDI files for listening are in Birdsongs.zip; the text versions
are in birdsmidtxt.zip for
adapting to your program.
Here is a sample birdsong in text
format:
//
\Birdsongs\btwa.mid
mthd
version 0
// single multichanneltrack
// 1 track
unit 100 //
is 1/4
end mthd
mtrk(1) //
track 1
/* U0
*/ /* 0ms */ trackname
"BTWA.MID"
/* U0
*/ /* 0ms */ text "created from WAV
file by WAV2MID (C) Tanager Productions"
/* U0
*/ /* 0ms */ beats 120.00000 /* 500000 microsec/beat
*/
/* U0
*/ /* 0ms */ program ChoirPd
96; /* U96
*/ /* 480ms */ +f#7 $64;
6; /* U102
*/ /* 510ms */ -f#7 $00;
27; /* U129
*/ /* 645ms */ +f#7 $64;
17; /* U146
*/ /* 730ms */ -f#7 $00;
24; /* U170
*/ /* 850ms */ +f#7 $64;
31; /* U201
*/ /* 1005ms */ -f#7 $00;
38; /* U239
*/ /* 1195ms */ +f#7 $64;
27; /* U266
*/ /* 1330ms */ -f#7 $00;
/* U266
*/ /* 1330ms */ +f7 $64;
4; /* U270
*/ /* 1350ms */ -f7 $00;
/* U270
*/ /* 1350ms */ +d#7 $64;
10; /* U280
*/ /* 1400ms */ -d#7 $00;
/* U280
*/ /* 1400ms */ +d7 $64;
17; /* U297
*/ /* 1485ms */ -d7 $00;
/* U297
*/ /* 1485ms */ +d#7 $64;
7; /* U304
*/ /* 1520ms */ -d#7 $00;
/* U304
*/ /* 1520ms */ +f#7 $64;
10; /* U314
*/ /* 1570ms */ -f#7 $00;
/* U314
*/ /* 1570ms */ +g7 $64;
4; /* U318
*/ /* 1590ms */ -g7 $00;
/* U318
*/ /* 1590ms */ +f#7 $64;
3; /* U321
*/ /* 1605ms */ -f#7 $00;
/* U321
*/ /* 1605ms */ +g7 $64;
4; /* U325
*/ /* 1625ms */ -g7 $00;
/* U325
*/ /* 1625ms */ -c-1 $00;
/* U325
*/ /* 1625ms */
end mtrk
MIDI
unit position and time position are shown in comments before each
command:
/* U7765 */ /*
34085ms */
Comments are inside /*
... */ or start with // till end of
line.
Pauses are shown before
commands
either in MIDI units ( 47; )or musical
notation (
536/4; ).
MIDI
notes consists of pairs of commands: Note on is displayed as + and Note
off is
displayed as -
MIDI
channel are displayed in mtrk(channel number) or as [channel number]
inside a
track before a command.
Values are shown decimal
(0-127) or hexadecimal ($00 - $7F).
Hint—use the lines with –
notes (note off events); the first
value is the number of milliseconds of duration of the event, in 5
millisecond
steps.
Standard MIDI file
format is here.
- Construct a new program based on Birdie.c, with the following
characteristics:
·
Timer2 controls the tempo and the length of each tone.
·
Timer1 with PWM gives the frequency.
·
The program plays the 16 birdsongs, selected by a button
·
One button selects which birdsong (displayed 1-16)
·
One button is start/stop
·
The output is a pulse wave to the transducer, amplitude
controlled by an ADSR envelope, scaled to the note
- Read the comments in Birdie.c for hints.
- Demonstrate your birdie to a TA.
- Turn in a hard copy of your code.
Deliverables
For all files turned in, the comments at the top
of the file
should contain:
- Both partners' full name, login and student
number.
- The lab number and the part of the lab (e.g.
Lab 4, part2 for the sine-wave generator).
- Any extra calculations or data that we've asked
for.
Part 1:
- Demonstrate your program and circuit to a
TA. You can either do this during this lab, or during the first 1/2
hour of the next lab.
- Turn in the requested calculations and
descriptions.
Part 2:
- Demonstrate your pitch generator to a TA. We
should be able to measure the frequencies. You can either do this
during this lab, or during the first 1/2 hour of the next lab.
Part 3:
- Demonstrate your birdie to a TA. You can
either do this during this lab, or during the first 1/2 hour of the
next lab.
- Turn in hardcopy of your commented C code.
End