Sine Wave Synthesizer

Sine Wave Synthesizer


Sine Wave Synthesizer


Every group wants their final project to be something that will be remembered long after they’re gone. Some do highly sophisticated and complex projects that entail upwards of a hundred hours to complete. Yet others go out of their way to develop something ’cool’ and ’fun’. Luke and I decided that we wanted to be in this second category, because developing something that’s ’cool’ would also be fun to do. So in deciding what to design we tried to think of something that would catch the attention of the people in the lab. The easiest way of doing this is to create something that would make noise or play music so everyone in the lab could enjoy it. Realizing this would be the best way to go, we decided to create a synthesizer that could record and playback notes, ’teach’ the user how to play a simple melody, and also play some prerecorded tunes. After all, don’t you think being remembered as the group that played back the Imperial March theme from Star Wars is cooler than a paper tape reader? (no offense to those groups doing paper tape readers)

Sine Wave Synthesizer

High Level Design

Seeing the synthesizer not just as a keyboard for playback but something with multiple options, we opted to use a simple state machine to section off each option. In other words, there would be states for the different options (one for single note play, record play, etc…) Since the main program would be this state machine, it was wise to have the generation of the sine wave output to be interrupt driven. This gives a lot of freedom to the main program functions because the state machine for the main program doesn’t have to worry about creating these tones.

Program/Hardware Design

State Machine Design

Since our design involved the user being able to choose from several different playback option, it was obvious that the program would entail the use of a simple state machine. Each option of the synthesizer (Default or waiting, Single Note Play, Record Play, Mem Playback, LCD play, and Recorded Tunes) would correspond with one of these states. The buttons on the proto-board would be used to change from one state to another.

Proto-board button layout:

Button 0: Single Note Play

Button 1: Record Play

Button 2: Memory Playback

Button 3: LCD Play

Button 4: End Single Note Play

Button 5: End LCD Play

Button 6: Recorded Tunes

Since the synthesizer defaults to the ’default state’, pushing button’s 0-3 or 6 will transfer the user to that option. Pushing button 4 and 5 are only valid if the user is already in the single note play or lcd play option respectively.

Default State

The program spins through the instructions of how to use the synthesizer. At even intervals, the LCD displays text on how to select different options. The following can be seen by the user on the LCD:

0 Single Note Play

1 Record Play

2 Mem Playback

3 LCD play

6 Recorded Tunes

When button’s 0-3 or 6 are pressed on the proto-board, the synthesizer changes to the state of the option selected.

Single Note Play

Once the user hits the button on the proto-board corresponding with the single note play option, the state machine enters to this ’Single Note Play’ state. A tone is then generated for each button pushed on the keypad. This state can be exited once button 4 on the proto-board is pushed. The synthesizer then resets back to the default state.

Record Play

The user can play and record up to 100 notes into memory and then play them back in .25 second intervals. When a button is pushed on the keypad, the number corresponding to the button is stored in RAM. If the user enters 100 notes, the synthesizer will output a ’Recording Done’ message to the LCD and return to the default state to await input. The recorded notes can then be played by selecting the memory playback option. But if the user doesn’t want to enter 100 notes, they can hit the button for mem playback and the recording will cease and memory playback will start.

Mem Playback

Once the user records a certain number of notes, they can be played back in sequence. If no notes were recorded, then nothing is played.

LCD play (aka Keyboard Karaoke)

In this state, a welcome message is flashed on the LCD letting the user that they have entered the ’LCD Play’ option. After a small delay, a series of characters is displayed one at a time on the LCD. These characters represent the buttons that the user should hit in order to play the notes of a song. Once the ’karaoke’ lines are finished, the program will jump back to the default state and await input. At any time during the karaoke playback, the user can hit button 5 to stop LCD play and go back to the default state.

Recorded Tunes

A fun not but very useful feature, once the user enters this option, they have the opportunity to playback some of the pre-recorded songs in the program. For this project, only the Imperial March from Star Wars was hard-coded into the program and available for user playback.

Sine wave generation

In this design, we used the National Semiconductor’s DAC0808 to convert 8-bit PCM data into an analog, sinusoidal signal. We calculated samples of the notes of the scale from C4-G#5 (262 Hz – 523 Hz) including the black keys and stored them in flash memory. The synthesizer can only play the first 15 notes (C4-D#5), but the microcontroller can output the full range of PCM data. The sampling rate we used was 20 kHz, which is approximately 20 times the Nyquist frequency for the highest frequency note. We used an op-amp on the output of the signal to generate a voltage from the output current and to amplify the signal. We had problems with our original op-amp (LM358) chopping of the top of the sine wave output. However, then we switched over to an audio op-amp, the LF353. This op-amp was much better because it only chopped off the very top of the signal. We corrected the problem by lowering the gain resistor on the op-amp. This method provided reasonable sine waves. In order to output the data at 20kHz, we used Timer 1 with compare match A and B. We set up the timer so that it would call the compare match B interrupt when the Timer 1 is 25. We also have the timer reset on a compare match with A, which is 26. In this way, Timer 1 counts to 25, resets, and counts to 25 in an endless loop. In the compare match B ISR, the program outputs the current sample to PORTC. At button push time, the program finds the starting address of the samples for one period of a sine wave. Several registers store the beginning address of the sine wave and the pointer to the current sample. At each interrupt, the pointer to the current sample gets incremented, and the next sample is sent to the DAC. Once the pointer reaches the end of the samples, it resets to the start of the samples for that particular note. Since sine waves are periodic, we need only to store the samples for one period.

N.B. The DAC is connected to Port C rather than Port D

Click on the above image to get a larger version

Results of the Design

Given the time allotted, we were able to generate a reasonable approximation for a sine wave and allow the user to play these tones, record and playback these tones, and learn a simple song. Having a little more time, we could have added the ability to play multiple tones at the same time. All that is needed is to add more entries into the keypad lookup table to cover multiple keypresses and add code that would sum the two sets of samples from each waveform. Also, another improvement in the tone generation would be to filter the output analog signal to remove unwanted frequencies.

Looking back in retrospect, we tried to encompass a lot of features in the project and didn’t focus exclusively on one area (if we had concentrated our efforts on the sine wave generation and didn’t do the memory playback etc.., we could have done the multiple tone output.) Also, we would probably have used a different LCD, one with a larger display and more expanded features, since the LCD we used limited us to write 16 characters at a time. Finally, with some financial support, we could have used a keypad that actually resembled a piano keyboard and had more keys, rather than a telephone keypad.

Keypad buttons | Note Value

1 C4

2 C#4

3 D4

A D#4

4 E4

5 E#4

6 F4

B G4

7 G#4

8 A4

9 A#4

C B4

* C5

0 C#5

# D5

D D#5

Sine Wave Synthesizer

Sine Wave Synthesizer

Sorry, comments are closed!