Sound Projects:Electronic Siren (Using the Timer Interrupt)

PROJECT 15 – Electronic Siren (Using the Timer Interrupt)

Function

This project shows how we can interface our microcontroller to a small speaker type audible device and generate a siren sound. When power is

Sound Projects-0011

Sound Projects-0012

applied to the circuit, a continuous siren type sound is output from the speaker. Both timer 0 and timer I of the microcontroller are used to generate the required tones.

Circuit Diagram

The circuit diagram of this project is the same as the one in Project I2 (i.e. Fig. 4.3) but there is nopush-button switch and 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 speaker will turn on when the port output is at logic HIGH (+5 V).

Program Description

In this project the frequency of the generated tone is varied continuously from 500 Hz to I0 kHz, thus producing a siren sound. Both timer 0 and timer I run at the same time and generate interrupts. Timer I generates the output tone and timer 0 changes the frequency of the generated tone continuously. This is how the timers operate:

Timer I is in 8-bit auto-reload mode and the timer register is loaded with 50 !ls (count of 206). Thus a timer I interrupt is generated every 50 !ls. Inside the timer I interrupt service routine, a counter is incremented and its value compared to a global variable called int_rate. When the twoare equal the speaker output is changed. The frequency of the generated output waveform is then as follows (notice that the period is twice the timer count value since half of the period is off and the other half is on):

Sound Projects-0013

In general, we can say that the frequency of the generated tone is given by: freq = I0/int_rate where freq is in kHz.

In this project, variable int_rate is varied from I00 down to I, i.e. the frequency of the generated tone varies between I00 Hz and I0 kHz.

Timer 0 of the microcontroller is used to change the frequency of the tone by changing the value of variable int_rate. Timer 0 is in 8-bit auto-reload mode and the timer register is loaded with 56 so that it generates interrupts at every 200 !ls. Inside the timer 0 interrupt service routine a counter is used and variable int_rate is decremented by I when the counter counts by 200. Thus, variable int_rate will be decremented every 200 x 200 !ls = 40,000 !ls or 40 ms.

In summary, the frequency of the generated tone will vary every 20 ms from I00 Hz to I0 kHz. The result is that a siren type output will be generated on the speaker.

The following PDL describes the functions of the program:

Sound Projects-0014

Sound Projects-0015

Program Listing

The full program listing is shown in Fig. 4.7. Variable SPEAKER is assigned to bit 7 of port I. When the program starts, variables count and timer1_overlow are set to0. The speaker is then turned off. Timers 0 and I are initialized by calling function init_timers. The program then enters an endless loop and waits until the timer interrupts occur.

Inside the timer I interrupt service routine, variable count is incremented and compared to variable int_rate. When the twoare equal, count is reset to0 and the speaker output is complemented.

Inside the timer 0 interrupt service routine, variable timer1_overlow is incremented and when it reaches 200 (i.e. 200 x 200 !ls = 40 ms), it is reset to

0 and int_rate is decremented sothat a higher frequency tone could be generated by timer I. When int_rate reaches 0 it is reset back toI00 sothat the process can repeat.

Sound Projects-0016

Sound Projects-0017

Leave a comment

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