Home
Product Description
Application Notes
FAQ
Sales
Support
 

AN-003 Various Sound Effects

This application note presents a series of short programs that produce various sounds. The circuit required is quite simple. Figure 1 shows a minimum parts configuration circuit. It relies on the AT90S1200A so no external crystal is required and drives a piezo speaker. Better sound quality is obtainable by driving a speaker. Figure 2 shows a simple amplifier that is capable of producing reasonable volumes depending on your choice of speakers. Note that the value of R1 can be changed to lower or raise the volume. As you alter its value be careful not to exceed the current carrying capacity of Q1. Both of these circuits use the internal oscillator of the AT90S1200A. Figure 3 and figure 4 show the same circuits using a crystal oscillator and an AT90S1200. 

All of the programs presented in this application note were written around a 4 MHz oscillator and not the AT90S1200A's internal 1 MHz oscillator. This was done so they are usable without alteration on the Atmel development boards. Altering the values used in the Sound statements in the following programs will make them usable with the AS90S1200A. The waveform driving the speaker is a square wave. As a result it may sound a bit harsh or harmonically rich. Filtering the output from the processor before amplification will alter its tonal quality resulting in a smoother sound.

The programs begin with the declaration of the variables used and a constant declarations as needed. After the declarations an openSound procedure is executed. This defines the pin that will be used to output a sound and gives it a name by which it is referenced later. It also defines the resting state of the pin. In this case the resting state is a 0 due to the type of amplifier used. What happens next depends on the sound produced. These programs were written and "tuned" by ear. Doing this sort of thing can be quite a bit of fun - dive in and see what sort of sounds you can create.

 

Program Listings

1. Phaser Shot

'This sound effect might be used for a toy
'First dimension a variable
DIM Byte var1

'Now set the port D data direction using the predefined constant DDRB
SetPortDirection (DDRD, 255)

OpenSound(piezo,portd,pin0,0)

For var1 = 1 to 3
   Sound(piezo,10,10)
   Sound(piezo,30,2)
Next var1

For var1 = 15 to 20
   Sound(piezo,var1,1)
Next var1
Sound(piezo,255,1)

END

 

2. Musical Notes

'This program simply plays a sequence of tones that correspond to
'musical notes.  The pitch of these notes is not exact (some are
'partially sharp or flat) but are as close as can be obtained.

'First set the port D data direction using the predefined constant DDRB
SetPortDirection (DDRD, 255)

'Second create a sound "port"
OpenSound(piezo,portd,pin0,0)

'Now play a sequence of notes followed by a short pause.
Sound(piezo,111,18)  'D
pause(1000000)

Sound(piezo,99,21)   'E
pause(1000000)

Sound(piezo,93,22)   'F
pause(1000000)

Sound(piezo,83,25)   'G
pause(1000000)

Sound(piezo,74,28)   'A
pause(1000000)

Sound(piezo,66,31)   'B
pause(1000000)

Sound(piezo,62,33)   'middle C
pause(1000000)

Sound(piezo,55,37)   'D
pause(1000000)

Sound(piezo,49,41)   'E
pause(1000000)

Sound(piezo,46,43)   'F
pause(1000000)

Sound(piezo,41,48)   'G
pause(1000000)

Sound(piezo,37,55)   'A
pause(1000000)

Sound(piezo,33,62)   'B
pause(1000000)

Sound(piezo,31,65)   'C
pause(1000000)

Sound(piezo,28,73)   'D
pause(1000000)

Sound(piezo,25,82)   'E

'All done
END





3. DeDa Siren

'This program reproduces a European police siren sound 
'Now set the port D data direction using the predefined constant DDRB

SetPortDirection (DDRD, 255)

OpenSound(piezo,portd,pin0,0)

forever:
Sound(piezo,50,20)
Sound(piezo,20,50)
goto forever

END





4.Slow Tone Sequence

'A sequence of tones

'First dimension a variable
DIM Byte var1, temp

'Now set the port D data direction using the predefined constant DDRB
SetPortDirection (DDRD, 255)

OpenSound(piezo,portd,pin0,0)

For var1 = 1 to 254
   temp = 255 - var1
   Sound(piezo,var1,temp)
Next var1

END




5. Fast Single Slide Tone

'A quick wistle

'First dimension a variable
DIM Byte var1

'Now set the port D data direction using the predefined constant DDRB
SetPortDirection (DDRD, 255)

OpenSound(piezo,portd,pin0,0)

For var1 = 1 to 25
   Sound(piezo,var1,3)
Next var1

END


6. Yelp Siren

'This sounds alot like most car alarms

'First dimension a variable
DIM Byte var1, var2, temp

'Now set the port D data direction using the predefined constant DDRB
SetPortDirection (DDRD, 255)

OpenSound(piezo,portd,pin0,0)

forever:
For var1 = 10 to 20
   if var1 < 10
      var2 = 3
   else
      var2 = 2
   end if
   Sound(piezo,var1,var2)
Next var1

For var1 = 10 to 20
   temp = 21 - var1
   if temp < 10
      var2 = 3
   else
      var2 = 2
   end if
   Sound(piezo,temp,var2)
Next var1

goto forever

END


7. Random Sounds

'This will drive you nuts after a while

'First dimension a variable
DIM Byte pitch, length
DIM Word rand

'Now set the port D data direction using the predefined constant DDRB
SetPortDirection (DDRD, 255)

OpenSound(piezo,portd,pin0,0)

'now seed the variable that will hold the random value.
rand = 1234    'use a 16 bit variable to get longer sequences

forever:

'get some random values
random(rand)            'generate a random number
pitch = highByte(rand)  'get one eight bit random number from it
length = lowbyte(rand)  'get another

'to equal out the times for low and high sounds a bit manipulte length
if pitch > 64       'if pitch is greater than 64 divide the length by 2
   shiftright(length)
end if
if pitch > 126      'if pitch is greater than 64 and 126 divide the length by 4
   shiftright(length)
end if
if pitch > 189      'if pitch is greater than 64 and 126 and 189 divide the length by 8
   shiftright(length)
end if

'make a sound
Sound(piezo,pitch,length)

goto forever
END
   
Copyright ©1997-99 Optical Countermeasures