Hardware Reference
In-Depth Information
8.6.4 Making Sound Using the Output-Compare Function
Using the output-compare function to make sound is easy. A sound can be made by creating
a digital waveform of appropriate frequency and using it to drive a speaker or a buzzer. A small
speaker of 8-
resistance that consumes between 10 and 20 mW can produce clear sound. The
next example illustrates how to use an output-compare channel to generate a siren.
Ω
Example 8.7
Describe the circuit for making a sound and write a program that uses an output-compare
channel to generate a siren that oscillates between 300 and 1200 Hz.
Solution: A simple 8-mW speaker (or a buzzer on a demo board) has two terminals: one terminal
is for signal input, whereas the other terminal is for ground connection. The circuit connection
for siren generation is shown in Figure 8.21.
HCS12DP256
3.3 µ F
PT5
Buzzer
Figure 8.21 Circuit connection for a buzzer
The algorithm for generating the siren is as follows:
Step 1
Enable an appropriate OC channel (OC5 in this example) to drive the speaker circuit.
Step 2
Start an output-compare operation and enable its interrupt with a delay count equal to
half the period of the siren.
Step 3
Wait a certain amount of time (say half a second). During the waiting period, interrupts
will be requested by the output-compare match many times. The interrupt service routine
simply starts the next output-compare operation and then returns.
Step 4
At the end of the delay, choose a different delay count for the output-compare operation so
the siren sound with a different frequency can be generated.
Step 5
Wait for the same amount of time as in step 3. Again, the interrupt caused by the output-
compare match will be requested many times. At the end of the delay, switch back to the
delay count used in step 2.
Step 6
Go to step 2.
 
Search WWH ::




Custom Search