Freqout
Syntax
Freqout Pin, Period, Freq1 {, Freq2}
Overview
Generate one or two sine-wave tones, of differing or the same frequencies, for a specified period.
Parameters
Pin is a Port-Bit combination that specifies which I/O pin to use. Period may be a variable, constant, or expression (0 - 65535) specifying the amount of time to generate the tone(s). Freq1 may be a variable, constant, or expression (0 - 32767) specifying frequency of the first tone. Freq2 may be a variable, constant, or expression (0 - 32767) specifying frequency of the second tone. When specified, two frequencies will be mixed together on the same I/O pin.
Example
' Generate a 2500Hz (2.5KHz) tone for 1 second (1000 ms) on bit 0 of PORTA.
Freqout PORTA.0, 1000, 2500
' Play two tones at once for 1000ms. One at 2.5KHz, the other at 3KHz.
Freqout PORTA.0, 1000, 2500, 30000
Notes
Freqout generates one or two sine waves using a pulse-width modulation algorithm. Freqout will work with a 4MHz crystal, however, it is best used with higher frequency crystals, and operates accurately with a 20MHz crystal. The raw output from Freqout requires filtering, to elimi-

Text in this figure or table
the tones through a speaker or audio amplifier.
| R1 | R2 | ||
| 1k | 1k | ||
| From PIC | To Audio | ||
| I/O pin | C1 | C2 | Amplifier |
| 0.1uF | 0.1uF |
C1
10uF Speaker
From PIC C2 I/O pin 10uF
nate most of the switching noise. The circuits shown below will filter the signal in order to play
The two circuits shown above, work by filtering out the high-frequency PWM used to generate the sine waves. Freqout works over a very wide range of frequencies (0 to 32767KHz) so at the upper end of its range, the PWM filters will also filter out most of the desired frequency. You may need to reduce the values of the parallel capacitors shown in the circuit, or to create an active filter for your application.
Example 2
' Play a tune using Freqout to generate the notes
Device = 18F26K40 ' Select the device to compile for
Declare Xtal = 20 ' Tell the compiler the device will be operating at 20MHz
Dim bMyLoop as Byte ' Counter for notes.
Dim wFreq1 as Word ' Frequency1.
Dim wFreq2 as Word ' Frequency2
Symbol C = 2092 ' C note
Symbol D = 2348 ' D note
Symbol E = 2636 ' E note
Symbol G = 3136 ' G note
Symbol R = 0 ' Silent pause.
Dim MyPin as PORTA.0 ' Sound output pin
ADCON1 = 7 ' Set PORTA and PORTE to all digital
bMyLoop = 0
Repeat ' Create a loop for 29 notes within the LookUpL table.
wFreq1 = LookUpL bMyLoop,[E,D,C,D,E,E,E,R,D,D,D,_
R,E,G,G,R,E,D,C,D,E,E,E,E,D,D,E,D,C]
If wFreq1 = 0 Then
wFreq2 = 0
Else
wFreq2 = wFreq1 - 8
EndIf
Freqout MyPin, 225, wFreq1, wFreq2
Inc bMyLoop
Until bMyLoop > 28

