![]() ZBasic System Library
129
ZBasic Microcontrollers
Type
Subroutine
Invocation
FreqOut(pin, freqA, freqB, duration)
Parameter
Method
Type
Description
pin
ByVal
Byte
The pin on which the signal will be created.
freqA
ByVal
Integer
The primary frequency, in Hertz.
freqB
ByVal
Integer
The secondary frequency, in Hertz.
duration
ByVal
Single or Integer
The duration of the signal, in seconds or units. See
the discussion below for more details.
Discussion
This routine generates a signal on the specified pin that is a digital approximation of two superimposed
sine waves having the specified frequencies. The method used to produce the signal is a pseudo-PWM
technique similar to that used for DACPin(). The output signal is actually purely digital, consisting of a
series of precisely timed pulses that have an average value approximating that of two superimposed sine
waves. This signal must be filtered to get an analog approximation. Depending on what you want to do
with the signal, it may need to be amplified as well.
The duration of the signal may be specified in seconds by providing a Single value. Alternately, the time
may be specified in units of approximately 1 millisecond by giving duration as an Integer or
UnsignedInteger value. In either case, the valid range is approximately 1ms to 32 seconds.
Before beginning the frequency generation, the specified pin will be made an output. When the routine
returns, the pin will still be an output.
If the pin is invalid, or both frequencies are zero, or the duration is zero, this routine does nothing. The
maximum frequency that can be produced is approximately 14.4KHz. Requesting higher frequencies will
produce undefined results.
Resource Usage
This routine uses the I/O Timer and disables interrupts until the signal generation is completed. RTC ticks
are accumulated during the process so long signal durations should not cause a loss in RTC accuracy.
Example
Call FreqOut(pin, 440, 880, 5.0) ' play middle C/high C for 5 seconds
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.
|