MSP430 News, Projects and Forums

Creating Tunes with the Launchpad

[E]nrico aka [e]ch0s managed to play the “Imperial March” tune through one of the MSP430′s pins using a speaker. The code uses a pin(P1.2) to generate PWM waveforms at a particular frequency. This is easily done by raising the in high for a certain time and then lowering it for the same time. Each note has its period or frequency. [E]nrico accomplishes this with a beep() function:

//This function generates the square wave that makes the piezo speaker sound at a determinated frequency.
void beep(unsigned int note, unsigned int duration)
{
    int i;
    long delay = (long)(10000/note);  //This is the semiperiod of each note.
    long time = (long)((duration*100)/(delay*2));  //This is how much time we need to spend on the note.
    for (i=0;i<time;i++)
    {
        P1OUT |= BIT2;     //Set P1.2...
        delay_us(delay);   //...for a semiperiod...
        P1OUT &= ~BIT2;    //...then reset it...
        delay_us(delay);   //...for the other semiperiod.
    }
    delay_ms(20); //Add a little delay to separate the single notes
}

You can see his detailed post on the TI Wiki site here.

Edited:  Its a normal speaker, not a piezo. Thanks ignoblegnome!



3 comments

  1. interface /

    Nifty! That’s very simple code.

  2. ignoblegnome /

    Nice sound project. Thanks for posting it.

    Just one comment. The speaker used is not a piezo; it is just a regular speaker. Piezo speakers tend to be much smaller and don’t typically require a limiting resitor when interfaced to a microcontroller.

Leave a Reply