Create Musical Tone using PIC Code

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
The mikroC PRO for PIC 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
  • Sound_PlayCreate Musical Tone using PIC Code

Sound_Init

Prototype void Sound_Init(char *snd_port, char snd_pin);
Returns Nothing.
Description Configures the appropriate MCU pin for sound generation.
Parameters :
  • snd_port: sound output port address
  • snd_pin: sound output pin
Requires Nothing.
Example
// Initialize the pin RC3 for playing sound
Sound_Init(&PORTC, 3);

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 :
  • freq_in_hz: signal frequency in Hertz (Hz)
  • duration_ms: signal duration in miliseconds (ms)
  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
// Play sound of 1KHz in duration of 100ms
Sound_Play(1000, 100);

Code Example

The example is a simple demonstration of how to use the Sound Library for playing tones on a piezo speaker.
 Create Musical Tone using PIC Code schematich
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.

About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter