$29
Objective:
To become familiar with the use of interrupts to service the timer overflow flag in computer applications. Specifically, to implement a piano like keyboard program using the ATmega2560 timer1 in Normal mode and an external speaker.
Procedure:
1. Write a C program for the Arduino Mega which will:
a. Monitor incoming characters from the Mega’s UART, for A, B, C, D, E, F, G, and Q, using the serial functions you wrote in the UART lab (U0Init(), kbhit(), getchar(), and putchar()).
b. Generate a square wave on Port B.6 of the appropriate frequency depending on the key pressed, as indicated in the table below.
c. Output logical 0 when the program first starts, and after the UART receives a ‘Q’ character
d. Generating the square wave will not take place in the main loop of the program, but instead in the overflow Interrupt Service Routine of Timer #1.
Hint: Start with the code you wrote in the Timer Lab, and make two changes:
a) Implement your own serial I/O routines U0Init(), kbhit(), getchar(), and putchar() instead of the Arduino library serial functions to read the note to be played from the UART.
b) Implement an ISR function for the Overflow Interrupt on Timer #1 (TIMER1 OVF) which will:
a. Stop the timer
b. Reload the timer counter
c. Toggle PortB.6
d. Restart the timer
2. Connect PB6 to an oscilloscope and measure the output frequency. Note any error between the actual output frequency and the desired output frequency.
3. Connect the lab speakers to PortB.6 with a current limiting resistor in series to play the notes so you can hear them.
BE SURE TO ALWAYS TURN OFF THE SBC POWER BEFORE CONNECTING OR DISCONNECTING ANYTHING TO IT.
4. Connect a sine wave signal generator to one of the lab speakers, set it to one of the musical note frequencies, and listen to it. Compare the sound of this note with the same frequency note generated from the SBC speaker. Explain why they sound different.
NOTE
FREQUENCY
A
440 Hz
A#
466 Hz
B
494 Hz
C
523 Hz
C#
554 Hz
D
587 Hz
D#
624 Hz
E
659 Hz
F
698 Hz
F#
740 Hz
G
784 Hz
G#
831 Hz
*The Sharp notes are included in the Table for completeness. If you choose to implement all of the notes including sharps you will need to decide on an interface protocol which will distinguish between regular and sharp notes. If you choose to implement all of the notes you will receive up to 50 points (on a scale of 100) extra credit on this lab.
Important Data Sheet Resources:
a) 2560 Interrupt Table (page 101)
b) 2560 Timer Interrupt Mask Register (page 161)