CE-2800 Embedded Systems Software
Lab 9: Sound Synthesizer

Objectives

Assignment

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 one out from Tech support). You will make use of the Timer/Counter0 subsystem in CTC mode to generate a square wave of a specific frequencies on the OC0 pin (same as the PB3 pin on PortB). Connecting the speaker to this pin will generate an audible tone at the specific frequency.
 

Note: No interrupts need to be used for this program.

Hardware Configuration

Program Functionality

Your "main" program should be placed in a file named Lab9.asm, and implement or call subroutines that

  1. Initialize PortB for digital output
  2. Initialize the Timer/Counter0 subsystem for CTC mode with Output Comparator enabled to toggle OC0, initially stopped (not counting).
  3. Initialize the keypad (by including your keypad2_dd.asm from a previous lab).

In addition, you must implement a subroutine named playNote that takes R0 and R1 as arguments. More detail is provided below.

Once the program performs these initializations, it should enter an endless loop that reads the keypad over and over. If no key is pressed, no sound should play. Once a valid key is pressed, the note corresponding to the key should play until a different key is pressed (or the key is released). The frequencies emitted by the oscillation of OC0, based on a particular key press, are as follows:

  key 1:  262 Hz   note: C
  key 2:  294 Hz   note: D
  key 3:  330 Hz   note: E
  key 4:  349 Hz   note: F
  key 5:  392 Hz   note: G
  key 6:  440 Hz   note: A
  key 7:  494 Hz   note: B
  key 8:  524 Hz   note: C' (where C' is double the frequency of C - known as an octave)

In addition to these 8 keys for the various notes, use keys A, B, and C for the following:

When key A is pressed, all subsequent frequencies emitted by keys 1-8 are halved (e.g. pressing key 1 generates a frequency at 131 Hz)

When key C is pressed, all subsequent frequencies emitted by keys 1-8 are doubled (e.g. key 1 frequency is 524 Hz)

When key B is pressed, the original frequencies are restored.

The frequencies are determined by setting the TCCR0 prescaler and OCR0 to specific values. Use the formula on page 74 of the Atmega32.pdf manual to determine appropriate settings. When you want to play a specific frequency, you'll call the playNote subroutine, where you will implement the logic to set OCR0 and the prescaler bits in TCCR0. The subroutine's R0 argument should contain the value that OCR0 should be set to in order generate the desired base frequency (above), with R1 indicating whether key A (R1=1), B (R1=0), or C (R1=2) was recently pressed. You must precalculate the appropriate values for OCR0 to pass in via R0 when you call playNote.

As stated above, if no keys are pressed, then no sound should be played. In this case, set R0 to 0 and implement logic within playNote to stop the timer so that it doesn't generate any output waveform on PB3 (the value of the argument R1 should not matter in this case).

Technical note: Your program's endless loop will read the keypad continually and thus call playNote continually (hundreds or thousands of times per second). If you set the T/C configuration parameters and reset TCNT0 every time you execute playNote, the T/C will never really get a chance to count up to OCR0, and no tone will be generated.

Feel free to use SRAM to store precomputed values of OCR0 for various keys, or for storing whether key A, B, or C was last pressed, etc. However, fully document any SRAM variables you use.

Demonstration

You must demonstrate your working program on your board by the end of the lab next week.

Lab Submission (due 11:00pm, Thursday, February 16, 2012)

For your submission, you need only supply your working, fully commented Lab9.asm.

Upload your submission through Blackboard (assignment "Lab 9").

Be sure to keep copies of all your files, in case something gets lost.

Grading

Your lab grade will be determined by the following factors:

Program - comments and formatting are important aspects of assembly language programming! And it has to work correctly.

Timeliness of submission as stated in the course policies.