Sound Library
The mikroPascal PRO for 8051 provides a Sound Library to supply users with routines necessary for sound signalization in their applications. Sound generation needs additional hardware, such as piezo-speaker (example of piezo-speaker interface is given on the schematic at the bottom of this page).
External dependencies of Sound Library
| The following variables must be defined in all projects using Sound Library: | Description: | Example : |
|---|---|---|
var Sound_Play_Pin: sbit; bdata; sfr; external; |
Sound output pin. | var Sound_Play_Pin: sbit at P0_3_bit; |
Library Routines
Sound_Init
| Prototype |
procedure Sound_Init(); |
|---|---|
| Returns |
Nothing. |
| Description |
Configures the appropriate MCU pin for sound generation. |
| Requires |
|
| Example |
// Initialize the pin P0.3 for playing sound var Sound_Play_Pin : sbit at P0_3bit; ... Sound_Init(); |
Sound_Play
| Prototype |
procedure Sound_Play(var freq_in_Hz: word; var duration_ms: word); |
|---|---|
| Returns |
Nothing. |
| Description |
Generates the square wave signal on the appropriate pin. Parameters :
Note: frequency range is limited by Delay_Cyc parameter. Maximum frequency that can be produced by this function is |
| Requires |
In order to hear the sound, you need a piezo speaker (or other hardware) on designated port. Also, you must call Sound_Init to prepare hardware for output before using this function. |
| Example |
// Play sound of 1kHz in duration of 100ms Sound_Play(1000, 100); |
Library Example
The example is a simple demonstration of how to use the Sound Library for playing tones on a piezo speaker.
program Sound;
// Sound connections
var Sound_Play_Pin : sbit at P0_3_bit;
// End Sound connections
procedure Tone1();
begin
Sound_Play(659, 250); // Frequency = 659Hz, duration = 250ms
end;
procedure Tone2() ;
begin
Sound_Play(698, 250); // Frequency = 698Hz, duration = 250ms
end;
procedure Tone3() ;
begin
Sound_Play(784, 250); // Frequency = 784Hz, duration = 250ms
end;
procedure Melody() ; // Plays the melody "Yellow house"
begin
Tone1(); Tone2(); Tone3(); Tone3();
Tone1(); Tone2(); Tone3(); Tone3();
Tone1(); Tone2(); Tone3();
Tone1(); Tone2(); Tone3(); Tone3();
Tone1(); Tone2(); Tone3();
Tone3(); Tone3(); Tone2(); Tone2(); Tone1();
end;
procedure ToneA() ; // Tones used in Melody2 function
begin
Sound_Play(880, 50);
end;
procedure ToneC() ;
begin
Sound_Play(1046, 50);
end;
procedure ToneE() ;
begin
Sound_Play(1318, 50);
end;
procedure Melody2() ; // Plays Melody2
var i : byte;
begin
i := 1;
while (i < 9) do
begin
ToneA();
ToneC();
ToneE();
Inc(i);
end;
end;
begin
P1 := 255; // Configure PORT1 as input
Sound_Init(); // Initialize sound pin
Sound_Play(2000, 1000); // Play starting sound, 2kHz, 1 second
while TRUE do // endless loop
begin
if (P1_7 = 0) then // If P1.7 is pressed play Tone1
begin
Tone1();
while ( P1_7 = 0) do nop ; // Wait for button to be released
end;
if ( P1_6 = 0) then // If P1.6 is pressed play Tone2
begin
Tone2(); //
while ( P1_6 = 0) do nop; // Wait for button to be released
end;
if ( P1_5 = 0) then // If P1.5 is pressed play Tone3
begin
Tone3(); //
while ( P1_5 = 0) do nop ; // Wait for button to be released
end;
if ( P1_4 = 0 ) then // If P1.4 is pressed play Melody2
begin
Melody2(); //
while ( P1_4 = 0 ) do nop; // Wait for button to be released
end;
if ( P1_3 = 0) then // If P1.3 is pressed play Melody
begin
Melody(); //
while ( P1_3 = 0 ) do nop; // Wait for button to be released
end;
end;
end.
HW Connection

Example of Sound Library connection
What do you think about this topic ? Send us feedback!



