Summary of Create Musical Tone using PIC Code
The mikroC PRO for PIC includes a Sound Library to generate audio signals using Microcontroller Unit (MCU) pins. It provides two main routines: `Sound_Init` to configure the output pin and `Sound_Play` to generate square wave signals based on frequency and duration. The system requires external hardware, specifically a piezo speaker, connected to the designated port to produce audible sound.Parts used in the Sound Generation Project:
- mikroC PRO for PIC
- Sound Library
- Microcontroller Unit (MCU)
- Piezo-speaker
- Schematic diagram
- Delay_Cyc parameter
Library Routines
Sound_Init
| Prototype | void Sound_Init(char *snd_port, char snd_pin); |
|---|---|
| Returns | Nothing. |
| Description | Configures the appropriate MCU pin for sound generation. Parameters : |
| Requires | Nothing. |
| Example |
C source
|
Sound_Play
| Prototype | void Sound_Play(unsigned freq_in_hz, unsigned duration_ms); |
|---|---|
| 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
Freq_max = Fosc/(80*3). Minimum frequency is Freq_min = Fosc/(80*255). Generated frequency may differ from the freq_in_hz parameter due to integer arithmetics. |
| 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 |
Code listing (2)
|
Code Example
void Tone1() {
Sound_Play(659, 250); // Frequency = 659Hz, duration = 250ms
}
void Tone2() {
Sound_Play(698, 250); // Frequency = 698Hz, duration = 250ms
}
void Tone3() {
Sound_Play(784, 250); // Frequency = 784Hz, duration = 250ms
}
void Melody() { // Plays the melody "Yellow house"
Tone1(); Tone2(); Tone3(); Tone3();
Tone1(); Tone2(); Tone3(); Tone3();
Tone1(); Tone2(); Tone3();
Tone1(); Tone2(); Tone3(); Tone3();
Tone1(); Tone2(); Tone3();
Tone3(); Tone3(); Tone2(); Tone2(); Tone1();
}
void ToneA() {
Sound_Play( 880, 50);
}
void ToneC() {
Sound_Play(1046, 50);
}
void ToneE() {
Sound_Play(1318, 50);
}
void Melody2() {
unsigned short i;
for (i = 9; i > 0; i--) {
ToneA(); ToneC(); ToneE();
}
For more detail: Create Musical Tone using PIC Code
Quick Solutions to Questions related to Sound Generation Project:
- What does the Sound_Init function do?
It configures the appropriate MCU pin for sound generation by accepting a port address and a specific pin number. - How do you play a sound using this library?
You call the Sound_Play function with a specified frequency in Hertz and a duration in milliseconds. - Can I hear sound without additional hardware?
No, you need a piezo speaker or other hardware on the designated port to hear the sound. - Does the generated frequency always match the input exactly?
No, the generated frequency may differ from the input parameter due to integer arithmetics. - What is the maximum frequency limit for this function?
The maximum frequency is calculated as Fosc divided by 240, derived from the formula Fosc/(80*3). - Do I need to initialize the sound before playing it?
Yes, you must call Sound_Init to prepare the hardware for output before using the Sound_Play function. - What type of signal does Sound_Play generate?
It generates a square wave signal on the appropriate pin. - How can I create a melody like Yellow House?
You can define separate tone functions and call them sequentially within a loop structure to play the sequence.
Related projects
Audio Spectrum Analyzer
We developed an audio spectrum analyzer as our final project for ECE 4760. This analyzer presented a histogram-style representation of audio signals....
Audio, Sound & Radio · PIC12F675Audio CRO using PIC12F675 Microcontroller
This project is a miniature Audio CRO. For those not familiar with the abbreviation "CRO," it means "Cathode Ray Oscilloscope" and this...
Audio, Sound & RadioBuild a musical EKG with the Freescale FRDM-KL05
Short Introduction: I have been following the Battle of the DreamBoards Championship with interest and I enjoyed the variation of functionality features...
Audio, Sound & RadioPIC MicroController Volume Adjuster Program(Proteus 8 Stimulation)
Hello everyone, The important aspect of every learning is to induce curiosity and doing something meaningful to satisfy that.With the help of...
Audio, Sound & RadioWireless Radio Frequency Module Using PIC Microcontroller
It's a Microcontroller Based DIY Electronics Project. Named Wireless RF Module Using PIC Microcontroller. The RF module is a small electronic circuit...
Audio, Sound & Radio · PIC16F676Shrieker using PIC16F676 Microcontroller
Recently a cell phone ring tone was in the news - it's 17,000 Hertz and meant to be heard by teenagers, but...

