Sound Projects:Two-tone Small Speaker Interface (Using the Timer Interrupt)

PROJECT 14 – Two-tone Small Speaker Interface (Using the Timer Interrupt)

Function

This project shows how we can interface our microcontroller to a small speaker type audible device and generate two different tones. When power is applied to the circuit, a continuous single tone of frequency I kHz is output to the speaker. When a push-button switch is pressed, the tone frequency is changed to 500 Hz. Timer I interrupt of the microcontroller is used to generate the time delay required for the tones.

Circuit Diagram

The circuit diagram of this project is the same as the one in Project I2 (i.e. Fig. 4.3) except that the buzzer is replaced with a small speaker. Bit 7 of port I is connected directly to a small speaker via a MOSFET transistor. The port output is in voltage mode so that the buzzer will turn on when the port output is at logic HIGH ( + 5 V). Bit 0 of port 3 is connected to a push-button switch, which is normally held at logic HIGH by the pull-up resistor R2.

Program Description

The speaker is initially turned OFF. Timer I of the microcontroller is then initialized to generate a continuous tone with a frequency of I kHz, as in Project I3. The push-button switch is then checked and when the switch is pressed, the timer register value is doubled, i.e. the interrupt interval is increased from 500 !ls to I ms. A waveform with a I ms on and I ms off time has a frequency of 500 Hz.

The following PDL describes the functions of the program:

Sound Projects-0009

Sound Projects-0010

Program Listing

The full program listing is shown in Fig. 4.6. When the program starts a variable called count is set to0 and the speaker is turned off. Timer I is then initialized with the int_rate=2 sothat interrupts are generated every 500 !ls, i.e. an output frequency of I kHz. The state of the push-button switch is then checked. If the switch is pressed, the interrupt rate, int_rate, is changed to4 so that interrupts will be generated at every 4 x 250 !ls = I ms i.e. the frequency of the generated waveform is changed to 500 Hz (I ms on time and I ms off time).

timer1() is the timer I interrupt service routine with interrupt number 3. In this routine, variable count is incremented and compared with the int_rate. When the two are equal, the speaker output is complemented, i.e. if the speaker is on it is turned off, and if off it is turned on.

The frequency of the generated tones can easily be changed by loading a different value into the timer register or by changing the value of variable int_rate.

Leave a comment

Your email address will not be published. Required fields are marked *