Книга: Advanced PIC Microcontroller Projects in C

4.3.5 Sound Library

4.3.5 Sound Library

Functions in the sound library make it possible to generate sounds in our applications. A speaker (e.g., a piezo speaker) should be connected to the required microcontroller port. The following functions are offered by the sound library:

• Sound_Init

• Sound_Play

Sound_Init

The Sound_Init function initializes the sound library and requires two parameters: the name and the bit number of the port where the speaker is connected. The address of the port name should be passed to the function. For example, if the speaker is connected to bit 3 of PORTB, then the function should be called as:

Sount_Init(&PORTB, 3);

Sound_Play

The Sound_Play function plays a sound at a specified port pin. The function receives two arguments: the period divided by 10 (TDIV) and the number of periods (N). The first parameter is the period in microcontroller cycles divided by 10. The second parameter specifies the duration (number of clock periods) of the sound.

The following formula calculates the value used as the first parameter:

 

where

 TDIV is the number to be used as the first parameter

 F is the required sound frequency (Hz)

 f is the microcontroller clock frequency (Hz)

Example 4.15

Write a program to play a sound at 1KHz, assuming the clock frequency is 4MHz. The required duration of the sound is 250 periods.

Solution 4.15

The first parameter is calculated as follows:

 

Since the required duration is 250 periods, the function is called with the parameters:

Sound_Play(100, 250);

Оглавление книги


Генерация: 2.106. Запросов К БД/Cache: 3 / 0
поделиться
Вверх Вниз