In this lab you will make your own rudimentary musical instrument. Pressing different keys on the keypad will generate different musical notes on a piezoelectric speaker (check out from Tech support).
You will make use of the Timer/Counter1 subsystem to generate a square wave of a desired frequency on the OC1A pin. Connecting the speaker to this pin will generate the desired sound.
When the program starts, no sound should play. Once a valid key is pressed, the note corresponding to the key should play until a different key is pressed. In addition to the 12 keys for the various notes, you may wish to designate one key such that, when it is pressed, the program stops playing sounds until another key is pressed.
Be sure to configure your GCC project correctly. For example, make sure that you adjust the stack pointer location, specify that the C99 standard should be used, etc…
Create a GCC project in AVR Studio and add all of the files from lab 3 except lab3.c. The lab3.c file should be replaced by three files: lab5.h, lab5.c, and init.s.
The lab5.h file should contain the following prototypes and be included in lab5.c:
/** * Initializes the LCD panel using the specified port and sets the data * direction register for PORTD in order to use OC1A as output. * * The function is implemented in assembly and calls lcd_init (found in * lcd.h) to initialize the LCD panel. * * @param lcd_port: The port on which the LCD panel is connected ('A', * 'B', 'C', or 'D'). */ extern void init(char lcd_port); /** * Configures the Timer/Counter1 subsystem such that the frequency of * the square wave on OC1A corresponds to the desired note. * * @param note The note to be played. An uppercase letter signifies a * half-step above the specified note. For example, 'C' * signifies C sharp. * * Notes: * The frequencies generated correspond to the following table: * 'a' 220 Hz note: A * 'A' 233 Hz note: A# * 'b' 247 Hz note: B * 'c' 262 Hz note: C * 'C' 277 Hz note: C# * 'd' 294 Hz note: D * 'D' 311 Hz note: D# * 'e' 330 Hz note: E * 'f' 349 Hz note: F * 'F' 370 Hz note: F# * 'g' 392 Hz note: G * 'G' 415 Hz note: G# * This function makes use of play_freq(). */ void play_note(char note); /** * Configures the Timer/Counter1 subsystem to produce a square wave with * the desired frequency on OC1A. * * @param freq The desired frequency to be generated on OC1A. */ void play_freq(uint16_t freq); /** * Clears the LCD and displays the note passed. * * @param note The note to be displayed. If a lowercase letter is passed, * the note is displayed with a uppercase character. If an * uppercase character is passed, the character followed by * a '#' is displayed. For example, if passed 'a', then 'A' * is displayed. If passed 'A', then "A#" is displayed. * "Error" should be displayed if the character passed does * not correspond to one of the following: a, A, b, c, C, d * D, e, f, F, g, and G. */ void display_note(char note); /** * Waits until one or more keys on the keypad is/are pressed and then * returns the number corresponding to the lowest numbered key/switch. * * @return The lowest number corresponding to the key/switch labels. */ uint8_t read_keypad();
You must create the lab5.c and init.s files.
The Timer/Counter1 subsystem should be used in CTC mode with a prescale factor of 1. Setting the value of OCR1A sets the frequency of the square wave generated on OC1A.
Students may wish to refer to the ATmega32 Datasheet for details on the Timer/Counter1 subsystem. The T/C1 subsystem is described on pp. 84-111. Of particular interest are pp. 94-97 which describe the CTC mode of the Compare Match Output unit and pp. 105-111 which describe the registers used by the subsystem.
Each student must demonstrate the successful implementation of their program on their SunRom hardware. This demonstration is due by the end of the laboratory session.
You should indicate how much time (in minutes) you spend on this assignment in the FAST database. You are encouraged to log your activity as you work on the project. At a minimum, you should log all of the time spent on this assignment before the due date given above. All time spent on this assignment should be entered into the week 5 column (even if you worked on it in week 4 or 6).