Sound Library

The mikroC PRO for ARM 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).

Library Routines

Sound_Init

Prototype

void Sound_Init(unsigned long *snd_port, unsigned long snd_pin);

Description

Configures the appropriate MCU pin for sound generation.

Parameters
  • snd_port: sound output port address
  • snd_pin: sound output pin
Returns

Nothing.

Requires

Nothing.

Example

Stellaris

// Initialize the pin GPIO_PORTA.6 for playing sound
Sound_Init(&GPIO_PORTA_DATA, 6);

MSP432

// Initialize the pin DIO_P6OUT.6 for playing sound
Sound_Init(&DIO_P6OUT, 6);

STM32

// Initialize the pin GPIOE_ODR.14 for playing sound
Sound_Init(&GPIOE_ODR, 14);

CEC1x02

// Initialize the pin for playing sound
Sound_Init(&GPIO_OUTPUT_010_017, 1);
Notes

None.

Sound_Play

Prototype

void Sound_Play(unsigned int freq_in_hz, unsigned int duration_ms);

Description

Generates the square wave signal on the appropriate pin.

Parameters
  • freq_in_hz: signal frequency in Hertz (Hz)
  • duration_ms: signal duration in miliseconds (ms)
Returns

Nothing.

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);
Notes

None.

Copyright (c) 2002-2019 mikroElektronika. All rights reserved.
What do you think about this topic ? Send us feedback!
Want more examples and libraries? 
Find them on LibStock - A place for the code