Sound
Syntax
Sound Pin, [ Note, Duration {, Note, Duration...} ]
Overview
Generates tone and/or white noise on the specified Pin. Pin is automatically made an output.
Parameters
Pin is a Port.Pin constant that specifies the output pin on the device. Note can be an 8-bit variable or constant. 0 is silence. Notes 1-127 are tones. Notes 128-255 are white noise. Tones and white noises are in ascending order (i.e. 1 and 128 are the lowest frequencies, 127 and 255 are the highest). Note 1 is approx 78.74Hz and Note 127 is approx 10,000Hz. Duration can be an 8-bit variable or constant that determines how long the Note is played in approx 10ms increments.
Example
' Star Trek The Next Generation...Theme and ship take-off
Device = 24HJ128GP502
' Select the device to compile for
Declare Xtal = 16
Dim bLoop as Byte
Symbol SoundPin = PORTB.0
Do
Sound SoundPin,[50,60,70,20,85,120,83,40,70,20,50,20,70,20,90,120,90,20,98,160]
DelayMs 500
For bLoop = 128 to 255
' Ascending white noises
Sound SoundPin, [bLoop, 2] ' For warp drive sound
Next
Sound SoundPin, [43,80,63,20,77,20,71,80,51,20,_
90,20,85,140,77,20,80,20,85,20,_
90,20,80,20,85,60,90,60,92,60,87,_
60,96,70,0,10,96,10,0,10,96,10,0,_
10,96,30,0,10,92,30,0,10,87,30,0,_
10,96,40,0,20,63,10,0,10,63,10,0,_
10,63,10,0,10,63,20]
DelayMs 10000
Loop
Notes.
With the excellent I/O characteristics of the PIC24® and dsPIC33®, a speaker can be driven through a capacitor directly from the pin of the microcontroller. The value of the capacitor should be determined based on the frequencies of interest and the speaker load. Piezo speakers can be driven directly.