Problems with Microcontroller Projects? You may face many Problems, but do not worry we are ready to solve your Problems. All you need to do is just leave your Comments. We will assure you that you will find a solution to your project along with future tips. On Request we will Mail you the Codes for Registered Members of this site only, at free service...Follow Me.

Generating Melody with a Microcontroller

Introduction
This experiment is about generating a melody tune using a PIC microcontroller. Well, this won't be difficult if you know the notes of the tune, and their respective frequencies. Here's achart for note to frequency conversion. We will play the 'Happy Birthday' tune with a PIC12F683 microcontroller. The microcontroller will generate the frequencies of the various notes (with proper timing) in the tune and the melody will be played in a piezoelectric buzzer.

Circuit Setup
The piezo buzzer is connected to the GP2 port of PIC12F683.



Software

The notes for Happy Birthday tune is like this.

Happy   birthday to  you
C4  C4  D4   C4  F4 E4

Happy   birthday  to you
C4  C4  D4   C4  G4 F4

Happy birthday dear xxxx
C4 C4 C5   A4  F4    E4 D4

Happy     birthday to  you $$$.
B4b B4b  A4  F4  G4 F4 

By using the chart, these notes are first converted into respective frequencies. Then the tones for these frequencies are generated using MikroC's built in sound function, Sound_Play(). The syntax is,
Sound_Play(frequency in Hz, duration in ms). 


/*
  Experiment No. 7: Playing Happy Birthday Tune with PIC12F683
  Compile with MikroC Pro for PIC
  Internal clock @ 4.0 MHz, MCLR disabled, PWRT OFF

*/

void Delay_100(){
 Delay_ms(100);
}

void main() {
CMCON0 = 7;
TRISIO = 0b00001000;  // GP5, 5 I/P's, Rest O/P's
GPIO = 0;
Sound_Init(&GPIO,2); // Initialize sound o/p pin


do {
   
   Sound_Play(262, 400); // Hap
   Delay_100();
   Sound_Play(262, 400); // Py
   Delay_100();
   Sound_Play(294, 800); // Birth
   Delay_100();
   Sound_Play(262, 800); // Day
   Delay_100();
   Sound_Play(349, 800);  // To
   Delay_100();
   Sound_Play(330, 1000);  // You
   Delay_100();
   Delay_100();
   // Another Para
   Sound_Play(262, 400); // Hap
   Delay_100();
   Sound_Play(262, 400); // Py
   Delay_100();
   Sound_Play(294, 800); // Birth
   Delay_100();
   Sound_Play(262, 800); // Day
   Delay_100();
   Sound_Play(392, 800);  // To
   Delay_100();
   Sound_Play(349, 1000);  // You
   Delay_100();
   Delay_100();
   // Another Para
   Sound_Play(262, 400); // Hap
   Delay_100();
   Sound_Play(262, 400); // Py
   Delay_100();
   Sound_Play(523, 700); // Birth
   Delay_100();
   Sound_Play(440, 800); // Day
   Delay_100();
   Sound_Play(349, 800);  // Dear
   Delay_100();
   Sound_Play(330, 800);  // XX
   Delay_100();
   Sound_Play(294, 700);  // XX
   Delay_100();

   Sound_Play(466, 400); // Hap
   Delay_100();
   Sound_Play(466, 400); // Py
   Delay_100();
   Sound_Play(440, 700); // Birth
   Delay_100();
   Sound_Play(349, 800); // Day
   Delay_100();
   Sound_Play(392, 800);  // To
   Delay_100();
   Sound_Play(349, 1600);  // You $ $ $
   Delay_ms(1000); // Wait for 1 sec and replay

   }while(1);
}

Output

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...

Recent Comments

Popular Projects

Give Support

Give Support
Encourage Me through Comments

Microcontroller Projects

Total Pageviews