Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Basic for PIC Microcontrollers ( M. Nebojsa, 2000).pdf
Скачиваний:
131
Добавлен:
12.08.2013
Размер:
2.46 Mб
Скачать

Chapter 6 - Samples

Previous page

Table of contents

Chapter overview

Next page

Generating a sound

A Piezo diaphragm can be added to an output line of a microcontroller to deliver a "speaker" tones, beeps and signals.

It is important to know there are two main types of piezo sound-emitting devices. One has active components inside the case and only requires a DC supply for the "speaker" to emit a tone or beep. Generally the tones or beeps emitted by these "speaker" or "beepers" cannot be changed - they are fixed by the internal circuitry. This is not the type we are discussing in this article.

The other type consists of a piezo diaphragm and requires a signal to be delivered to it for it to function. Depending on the frequency of the waveform, the output can be a tone, tune, alarm or even voice messages.

In order for them to work we must deliver a cycle consisting of a HIGH and LOW. It is the change from HIGH to LOW or LOW to HIGH that causes the diaphragm to "dish" (move) to produce the characteristic "tinny" sound. The waveform can be a smooth change from one value to the other (called a sinewave) or a fast change (called a SQUARE WAVE). A computer is ideal for producing a square wave. The square wave delivery produces a slightly harsher output.

Connecting a piezo diaphragm is very simple. One pin is connected to the negative rail and the other to an output of a microcontroller, as shown in the diagram below. This will deliver a 5v waveform to the piezo diaphragm. To produce a higher output, the waveform must be increased and this requires a driver transistor and inductor.

Connecting a piezo diaphragm to a microcontroller

As with a key, you can employ a macro that will deliver a BEEP ROUTINE into a program when needed.

BEEP macro freq , duration:

freq: frequency of the sound. The higher number produces higher frequency

http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_07Poglavlje.htm (1 of 4) [4/2/2003 16:18:57]

Chapter 6 - Samples

duration: sound duration. The higher the number, the longer the sound.

Example 1: BEEP 0xFF, 0x02

The output of the piezo diaphragm has the highest frequency and duration at 2 cycles per 65.3mS which gives 130.6 mS

Example2: BEEP 0x90, 0x05

The output of the piezo diaphragm has a frequency of 0x90 and duration of 5 cycles per 65.3mS. It is best to determine these macro arguments through experimentation and select the sound that best suits the application.

The following is the BEEP Macro listing:

http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_07Poglavlje.htm (2 of 4) [4/2/2003 16:18:57]

Chapter 6 - Samples

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.

http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_07Poglavlje.htm (3 of 4) [4/2/2003 16:18:57]

Chapter 6 - Samples

Previous page

Table of contents

Chapter overview

Next page

© Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster.

http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_07Poglavlje.htm (4 of 4) [4/2/2003 16:18:57]