oSpeaker Object

Main Index
Object List
Back to top of pageDescription:
 A Hardware Object that generates and outputs a tone of up to 9.7khz to a speaker.

It is capable of generating a frequency output between 38Hz and 9,765Hz.

 The following table lists the size and availability of the oSpeaker Object.
 ObjectSizeDescriptionA1A2B1B2C1
oSpeaker2 BytesControls a Speaker.xx
Back to top of pageOperation:
 When the Operate property is 1, the oSpeaker Object cycles I/O line 21 at a rate specified by the Tone property thereby producing a tone output that can drive a speaker.

The Tone property specifies the frequency of the tone.  The specified frequency is expressed as a 16-Bit divisor to a fixed frequency of 2.5 MHz.  The valid range of the divisor is 1 to 65279.  The higher the value, the higher the frequency.  If 0 is given as the divisor, then the output is shut off.  The maximum usable value for the divisor is 65279 and if the divisor is between 65280 and 65536 then the divisor is read as (divisor - 65279).

The value to use as a divisor for the desired frequency can be calculated with the following formula.
Divisor = 65,535 - (2,500,000 /  Frequency)
For example: The frequency to product the musical note E5 is 659.26Hz. 
To produce the frequency 659.26Hz the divisor 61743 is specified as show in the following example:
65,535 - (2,500,000 /  659.26) =  ~61742.87 which would be rounded to 61743

The lowest frequency that can be generated is 38.1481Hz  [(65,535 - (2,500,000 / 38.1481)) = 1]
The highest frequency that can be generated is 9,765.625  [(65,535 - (2,500,000 / 9,765.625)) = 65279]

The oSpeaker Object generates one frequency.  Note that two oSpeaker Objects will not produce two independent frequencies.   if you need two or more independent frequencies or need music capabilities, see the oSoundgin object. 

The Freq property is an instance of the oFreqH object which the oSpeaker Object uses to generate a square wave output to drive the speaker.  Since all instances of the oFreqH Object use the same PIC hardware then all instances of the oSpeaker Object will output at the same frequency.   Also note that all other objects that use the oFreqH object will also use the same 16-Bit value as well.  (see oFreqH Object for more detail)

Back to top of pageProperties:

The following table lists the properties of the oSpeaker Object:

Property

Description

Tone
A value that specifies the tone the Speaker is to make.
Object Class: oWord Value Range: 0 - 65535
Data Type: Numeric Default Value: 0
The Tone property is a 16-Bit divisor to a fixed frequency of 2.5 MHz.
The following table lists the values of the Tone property.
ValueDescription
0The frequency output is disabled.
1 - 65279The frequency (2,500,000 / (65535 -Value )) is generated.
65280 - 65536The frequency (2,500,000 / Value) is generated.
Operate
A value that selects if the Speaker is on or off.
Object Class: oOperate Value Range: 0 - 1
Data Type: Numeric Default Value: 0
OperateConstantDescription
0cvOffThe Speaker tone is not generated.
1cvOnThe Speaker tone is generated.
Freq
The I/O function object used to output the tone.
Object Class: oFreqH Value Range: 0 - 255
Data Type: Numeric Default Value: 0
Note: Freq is an instance of the oFreqH Object.  All instances of the oFreqH Object use the same PIC hardware, Therefore all instances of the oSpeaker Objects will output the same tone.  Also note that setting its properties can affect the way the oSpeaker operates.  (for more see the oFreqH Object)
Address
Returns a pointer to the address of the oCounter Object instance.
Object Class: oAddress Value Range: 0 - 127
Data Type:Pointer (Read Only) Default Value: Address of Object

Back to top of pageMethods:
 The following table lists the Methods of the oSpeaker Object:
MethodsDescription
Beep(x,y,z)Puts x into the Tone property, sets Operate to 1, does a delay of y milliseconds, then sets Operate to 0 and does a delay of z milliseconds.  If no value is specified a default value of 62346 (Musical Note G5 / 783.9Hz) is used.

y has a range of 65.536 seconds and a default of 250 (1/4 second).

z has a range of 65.536 seconds and a default of 0.

Back to top of pageExamples:
In the following example, the oSpeaker Object is told to beep.
Visual Basic Syntax C and Java Syntax
'This program uses an oSpeaker
'with the Beep method.

Dim Spk As New oSpeaker

Sub Main()
	Do
    Spk.Beep(64000,100)
    Spk.Beep(64500,100)
    Spk.Beep(65000,100)
    Spk.Beep(63500,50)
    Spk.Beep(63600,50)
    Spk.Beep(0,100)
    Spk.Beep(5500,100)
    Spk.Beep(0,100)
    Spk.Beep(20500,100)
    Spk.Beep(0,100)
    Spk.Beep(5500,100)
    Spk.Beep(0,100)
	Loop
End Sub 
// This program uses an oSpeaker
// with the Beep method.

oSpeaker Spk = New oSpeaker;

Void Main(Void){
  Do{
    Spk.Beep(64000,100);
    Spk.Beep(64500,100);
    Spk.Beep(65000,100);
    Spk.Beep(63500,50);
    Spk.Beep(63600,50);
    Spk.Beep(0,100);
    Spk.Beep(5500,100);
    Spk.Beep(0,100);
    Spk.Beep(20500,100);
    Spk.Beep(0,100);
    Spk.Beep(5500,100);
    Spk.Beep(0,100);
  } While (1);
}
Basic Syntax 
'This program uses an oSpeaker
'with the Beep method.

Spk As oSpeaker

Do
   Spk.Beep(64000,100)
   Spk.Beep(64500,100)
   Spk.Beep(65000,100)
   Spk.Beep(63500,50)
   Spk.Beep(63600,50)
   Spk.Beep(0,100)
   Spk.Beep(5500,100)
   Spk.Beep(0,100)
   Spk.Beep(20500,100)
   Spk.Beep(0,100)
   Spk.Beep(5500,100)
   Spk.Beep(0,100)
Loop
 
Back to top of pageConnections:
 The oSpeaker Object uses I/O line 21.  The output of the I/O line needs to be filtered before it can be connected to a speaker.  A simple filter can be constructed with two resistors and two capacitors as shown in the wiring diagram. 

Caution: Do NOT connect the outputs of the ooPIC directly to a speaker. The TTL circuitry of the ooPIC is not designed to drive the power requirements of a speaker.  Doing so can potentially damage the ooPIC's TTL outputs.

In addition, if volume control is needed, an amplified circuit can be used.  The LM386 is a commonly used operational amplifier.  Shown in the following schematic, it is used to amplify the audio signal after it has gone through the filter circuit.

Back to top of page Related Items:

 The following table lists objects with related functions
 ObjectDescriptionA1A2B1B2C1
oSoundginControls a Soundgin Sound Effects Engine / Voice Synthesizer chip.xxx
oToneOutputs a tone.xxx
Back to top of page Version History and Bug List:
 Firmware Ver B2: Introduced.

Bugs: No known bugs.


ooPIC Compiler Ver 6.0 (c) Copyright 1997 - 2007 Savage Innovations, LLC.