![]() ZBasic System Library
227
ZBasic Microcontrollers
Type
Subroutine
Invocation
PlaySound(pin, address, length, rate, repeat)
Parameter
Method
Type
Description
pin
ByVal
Byte
The output pin.
address
ByVal
int16
The Program Memory address of the sound data.
length
ByVal
int16
The number of bytes of sound data.
rate
ByVal
int16
The sample rate for the sound data.
repeat
ByVal
int16
The number of times to repeat the sound.
Discussion
This routine uses a pseudo-PWM technique to create an approximation to a sine wave on the specified
output pin. The frequency of the sine wave is given by successive bytes in Program Memory beginning at
the specified address and continuing for the given length. The rate parameter specifies the rate at
which the data elements will be utilized. It is equivalent to the sampling rate at which an original analog
sound might have been digitized. Lastly, the repeat parameter tells how many times to repeat the
production of the output using the supplied data. If zero is specified, the sound will be repeated 65,536
times.
The minimum supported sample rate is 250Hz. If a smaller value is specified, 250Hz will be used instead.
The actual output will be a pulse stream that has an average value that approximates the target analog
signal. Because of the high frequency nature of the pulse train used to synthesize the waveform some
filtering is required. The example circuit below may be used to couple the output to a high impedance
speaker (> 40O) or an amplifier. Note, however, that the signal is too large to be fed to the microphone
input of an amplifier. Instead, the Auxiliary or Line input should be used.
Resource Usage
This routine uses the I/O Timer and disables interrupts during the generation process. In particular, this
means that serial input that arrives during the generation will likely be missed and serial output on
channels 3-6 will be disrupted.
Task switching is suspended and other interrupts are disabled while the sound is being produced.
However, RTC ticks are accumulated during the process and the RTC is updated when the process has
completed so that the RTC does not lose time.
|