Pages

7.6 Generating sound using PIC microcontroller

7.6 Generating sound

In microcontroller systems, beeper is used for indicating certain occurrences, such as push of a button or an error. To have the beeper started, it needs to be delivered a string in binary code - in this way, you can create sounds according to your needs. Connecting the beeper is fairly simple: one pin is connected to the mass, and the other to the microcontroller pin through a capacitor, as shown on the following image.

18

As with a button, you can employ a macro that will deliver a BEEP ROUTINE into a program when needed. Macro BEEP has two arguments:
BEEP macro  freq , duration:
freq:  frequency of the sound. The higher number produces higher frequency
duration: sound duration. Higher the number, longer the sound.
Example 1: BEEP 0xFF, 0x02
The output has the highest frequency and duration at 2 cycles per 65.3mS which gives 130.6 mS
Example2: BEEP 0x90, 0x05
The output has a frequency of 0x90 and duration of 5 cycles per 65.3mS. It is best to determine these macro parameters through experimentation and select the sound that best suits the application.
The following is the BEEP Macro listing:

beep_inc

The following example shows the use of a macro in a program. The program produces two melodies which are obtained by pressing T1 or T2. Some of the previously discussed macros are included in the program.

beep_asm