Hardware Reference
In-Depth Information
TIOS
| 5 BIT5;
// enable OC5
TCTL1 5 0x04;
// select toggle for OC5 pin action
dlycnt 5 HiFreq;
// use high-frequency delay count first
TC5 5 TCNT 1 dlycnt;
// start an OC5 operation
TIE
| 5 BIT5;
// enable TC5 interrupt
asm(“cli”);
while(1) {
delayby100ms(5);
// wait for half a second
dlycnt 5 LoFreq;
// switch to low-frequency tone
delayby100ms(5);
// wait for half a second
dlycnt 5 HiFreq;
// switch to high-frequency tone
}
}
interrupt void oc5ISR(void)
{
TC5 15 dlycnt;
}
The required interrupt-vector setup function (vectors_0807.c) is as follows:
extern void near oc5ISR(void);
#pragma CODE_SEG __NEAR_SEG NON_BANKED
#pragma CODE_SEG DEFAULT // Change code section to DEFAULT.
typedef void (*near tIsrFunc)(void);
const tIsrFunc _vect[] @0xFFE4 5 {
oc5ISR
};
A siren can be considered as a song with only two notes. A song can be created by modify-
ing the siren program. To make the switching of frequency easier, the whole score should be
stored in a table. A note has two components: one is the frequency and the other is the dura-
tion. The duration of a quarter note is about 0.4 s. The durations of other notes can be derived
proportionally. The complete list of the music notes and their frequencies is in Appendix F.
To play a song from the speaker, one places the frequencies and durations of all the notes
in a music score in a table. For every note, the user program uses the output-compare function
to generate the digital waveform with the specified frequency and duration. The following ex-
ample illustrates this idea.
It is common for a song to have several contiguous identical notes in different places. When
playing multiple contiguous identical notes, the buzzer will generate one long note instead of
multiple notes. This problem can be avoided by inserting a short note with an inaudible fre-
quency. The following example uses this method:
Example 8.8
Use the circuit shown in Figure 8.21 and write a program to play “The Star-Spangled
Banner.” Assume that the demo board is running with a 24-MHz E-clock.
Solution: The following assembly program plays the national anthem:
#include “c:\miniide\hcs12.inc”
G3
equ
7653
; delay count to generate G3 note (with 1:8 prescaler)
B3
equ
6074
; delay count to generate B3 note (with 1:8 prescaler)
 
Search WWH ::




Custom Search