Menu

all about electronic and microcontrollers

Tuesday, December 14, 2010

Lesson nr.20-Sound generation

Hardware setup:


Circuit Diagram:
For those who want to build it on their own breadboard or other platform, here is the electronic scheme built in Eagle Cad, free version:


Software:
Here is the C program written for MikroC PRO for PIC 2010 (version v4.15).
/*
'*******************************************************************************
'  Lesson nr.20:
'          Sound generation.
'  Done by:
'          Aureliu Raducu Macovei, 2010.
'  Description:
'          In this experiment we will work with sounds.
'          I'll show how to generate and make sound helped by microcontroller 
'          and some buttons.
'          Communication with the buzzer will be performed  at PORT RB3 ( as PWM mode).
'          The Push buttons are connected to PORT RA4 (generates Tone1), RA6
'          (generates Tone2) and RA7(generates melody2). 
'          Of course we need pull-up resistors (4k7 is required).
'  Test configuration:
'    MCU:                        PIC16F628A
'    Test.Board:                 WB-106 Breadboard 2420 dots
'    SW:                         MikroC PRO for PIC 2010 (version v4.15)
'  Configuration Word
'    Oscillator:                 INTOSC:I/O on RA.6, I/O on RA.7
'    Watchdog Timer:             OFF
'    Power up Timer:             Disabled
'    Master Clear Enable:        Enabled
'    Browun Out Detect:          Enabled
'    Low Voltage Program:        Disabled
'    Data EE Read Protect:       Disabled
'    Code Protect:               OFF
'*******************************************************************************
*/
void Tone1()
{
 Sound_Play(5000, 1000);         // Frequency = 659Hz, duration = 250ms
}

void Tone2() 
{
 Sound_Play(4000, 1000);         // Frequency = 698Hz, duration = 250ms
 }

void Tone3() 
{
 Sound_Play(3000, 1000);         // Frequency = 784Hz, duration = 250ms
 }

void Melody1()                  // Make funny melody 1;
{
 Tone1(); Tone2(); Tone3();
 Tone3(); Tone2(); Tone1();
 Tone1(); Tone3(); Tone2();
 Tone3(); Tone2(); Tone1(); Tone3();
 Tone1(); Tone2(); Tone3();
 Tone3(); Tone1(); Tone2(); Tone2(); Tone1();
 }

void ToneA()                    // Tone A;
{
 Sound_Play(1618, 50);          // (frequency, duration)
}

void ToneB()                    // Tone B;
{
 Sound_Play(1918, 50);          // (frequency, duration)
}

void ToneC()                    // Tone C;
{
 Sound_Play(2212, 50);          // (frequency, duration)
}

void Melody2()                  // Make funny melody 2;
{
 unsigned short i;
 for (i = 12; i > 0; i--)
 {
  ToneA(); ToneB(); ToneC();
  }
 }

void main() 
{
 CMCON |=7;                     // Disable comparator
 TRISB = 0xF0;                  // Pins RB7-RB4 are configured as inputs,
                                // RB3 is configured as an output
 Sound_Init(&PORTB, 3);
 Sound_Play(5000,100);          // Start with this song (frequency, duration)

 while (1)                      // infinite loop
 {
  if (Button(&PORTA,4,1,0))     // RB4 generates Tone1
  Tone1();
  
  if (Button(&PORTA,6,1,0))     // RB6 generates Tone2
  Tone2();

  if (Button(&PORTA,7,1,0))     // RA7 generates melody 2
  {
   Melody2();
   Delay_ms(1500);              // Delay 1,5s
   Melody2();
   Delay_ms(1500);              // Delay 1,5s
   Melody2();
   }
  }
}

4 comments:

  1. thanks for your useful posts. Ducu.
    i'm interested in your projects. Can you post a tutorial about led matrix ?

    ReplyDelete
  2. With pleasure. I am happy to see people passionate about electronics.
    Article about LED dot matrix display is completed, I will climb as soon as possible.

    ReplyDelete
  3. Dear friend, your tutorials are very good and informative. Can you please send me a code for peoples counting using two IR for IN and OUT for this microcontroller.

    ReplyDelete

If you do not understand something, or if you make some aplication helped by this blog, let me know.