Recent Posts

Blogroll

                                          My PIC12F683 Development Board
Office Map Circuit diagram for each experiment may not be available separately because they are conducted with PIC12F683 development board that I made. So, the readers should first see the schematic of my development board.

Sunday, November 28, 2010

Experiment No. 7: 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 a chart 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


6 comments:

begzee said...

hi i use atmel atmega8. how to write Sound_Play function pls help

=clever= said...

can u plzz givw me the HEX file

sahu said...

how to write other Sound in place of Happy Birthday

Marc P. said...

clever, msg me at : nodoubtman@hotmail.com, i will give you the .hex code produce with mikroC PRO for PIC

thank you! :)
Have a good day! :)

Marc P. said...

Sahu : note frequency here : http://jeanjacques.dialo.free.fr/Anglais/afrequenc.htm

have a good day !:)
enjoy!
send me your song ;)

ard said...

hi
I want to know about the common used pic term in HITECH or MicroPC kindly would you teach me I am new in PIC although I have knowledge in C programming.
TQ

Post a Comment

Online Embedded Systems Lab

This online laboratory teaches you the fundamentals of microcontroller-based embedded system development through a series of laboratory exercises. Most of the time, students and hobbyists could not afford expensive development kits and software to learn these things. These tutorials are prepared such that you will be building microcontroller projects at a minimum cost. Check this out