« »

Playing music with msp430 launchpad

February
12
member
sucotronic

This is a simple demonstration about how to make sound using the launchpad platform and a simple piezoelectric buzzer. To do it I use mainly the Timer A to produce the sound oscillation to a certain frequency, and the watchdog timer to keep trace of time passing.

Here you can see a video demonstration:

First we have to configure the watchdog to be feed by the main clock and to generate an interrupt every 512 cycles, which equals to 1 interrupt every 0.512 ms because we are going to use the calibrate 1Mhz data for the main clock:

  WDTCTL = WDTPW+WDTTMSEL+WDTCNTCL+WDTIS1; // Set interval mode, set to zero and interval to 0.5 ms
  IE1 |= WDTIE; // Enable WDT interrupt
  BCSCTL1 = CALBC1_1MHZ;  // set main clock to calibrated 1Mhz value
  DCOCTL = CALDCO_1MHZ;
  eint();  // enable interrupts

Now we need to create an interrupt routine that will keep track of time incrementing a global integer variable on every call:

interrupt(WDT_VECTOR) watchdog_timer (void) //__interrupt void watchdog_timer 
{
  time++;
}

With this variable now we can build a simple delay function with milliseconds accuracy:

delay(unsigned int ms) {
  volatile unsigned int i,ms2;
  i = time;
  ms2 = ms*2;  
  while ((time-i) < ms2) {
    nop();
  }
}

To generate the wave for the piezoelectric buzzer we need to initialize the Timer A module in the main section:

  P1DIR |= BIT2; // P1.2 to output, connected to one buzzer wire. The other wire goes to GND or VCC
  P1SEL |= BIT2; // P1.2 to TA0.1
  CCTL1 = OUTMOD_7; // CCR1 reset/set

And we only need to implement two functions to start making sound and stop it:

play(unsigned int hz){
  CCR0 = (1000000/hz) -1;
  CCR1 = (1000000/hz)/2;
  TACTL = TASSEL_2 + MC_1;  //start timer
}

stop(){
  TACTL = TASSEL_2 + MC_3;  //stop timer
  CCR0 = 0;
}

With the play(), stop(), and delay() functions now you can tweak the arduino tone library and play with their examples like me.

You can download code here.

Source: mspgcc manual, justintech and arduino Tone library.


date Posted on: Saturday, February 12, 2011 at 10:50 am
Category electrónica.
Puedes seguir las respuestas de esta entrada a traves de l feed RSS 2.0.

Puedes dejar una respuesta, o trackback desde tu propio sitio.



35 Respuestas to “Playing music with msp430 launchpad”

  1. old_cow_yellow

    Very interesting project.

    I did something with those same hardware — a Launchpat + a piezo buzzer. But instead of generating tones of the music scale, I use it to mimic the sounds of Castañuelas.

    I also use the TimerA to generate the waveform for the piezo buzzer. But I use P1.6 instead of P1.2 for sound.

    I use P1.1 & P1.2 as TXD & RXD — to communicate with a PC terminal-emulator. And I use that to “program” the sequence of the Castañuelas clicks. This pseudo-user “program” is stored in Flash. I use 1.5KB of the 2KB Flash for firmware and 0.5KB for the pseudo-user “program”.

    I do use c and I do not know how to use gcc. Is it possible that you take a look at “ACOACO.ZIP” in the “Four-Three-oh! Forums” and tell me what you think?

    Regards,

    OCY

    [Responder]

    sucotronic Reply:

    @old_cow_yellow, I promise to take a look at your code, but if it is programmed in assemble it will take me a while to understand well 😛

    [Responder]

    old_cow_yellow Reply:

    @sucotronic,
    Thanks.

    My code is messy. I was trying to get you to review the approach, not my messy code. Is it possible to load the ACOAC.hex or ACOAC.txt file to the G2xx without going through assembler/linker?

    BTW. The zip file is ACOAC.zip in Four-Three-oh! Forums.

    Thanks again.

    [Responder]

    sucotronic Reply:

    @old_cow_yellow, I’m sorry to say it, but my assembler knowledge is far away and I don’t have the required time to debug your code and help with it. Anyway, thanks for sharing such a curious project.

    February 12th, 2011 at 18:08 pm
     
  2. old_cow_yellow

    Typo in my previous comentario. I do NOT use c.

    [Responder]

    February 12th, 2011 at 18:21 pm
     
  3. old_cow_yellow

    I tested your c source code with IAR KickStart. Works great!

    I do need to change one of the statements from:
    int notes[] = { 0, …};
    into:
    const int notes[] = { 0, …);

    Otherwise, IAR c-startup will copy those 98 bytes from Flash to RAM. Does gcc generates similar object code?

    [Responder]

    sucotronic Reply:

    @old_cow_yellow, I don’t. I’ll try to check it next time I have time to play with the msp430.

    [Responder]

    February 14th, 2011 at 1:55 am
     
  4. justin

    Very cool!!! now to bother my co-workers

    [Responder]

    March 7th, 2011 at 13:10 pm
     
  5. Eric (USA)

    Great example, thanks for sharing.

    [Responder]

    March 26th, 2011 at 21:49 pm
     
  6. 430 work shop links and source code — Justin's Tech

    […] Music demo Original source here […]

    April 25th, 2011 at 2:34 am
     
  7. albert

    Hi,
    This example does not work on my MSP430G2xxx. I have changed the code as suggested above by old_cow_yellow:
    int notes[] = { 0, …};
    into:
    const int notes[] = { 0, …);
    But I am still getting this error:
    Error[e104]: Failed to fit all segments into specified ranges. Problem discovered in segment CODE. Unable to place 14 block(s) (0x36e byte(s) total) in 0x24e byte(s) of memory. The problem occurred while processing the segment placement command “-P(CODE)CODE=FC00-FFDF”, where at the moment of placement the available memory ranges were “CODE:fd92-ffdf”
    I think this is caused by the poor ammount of RAM on my MSP453.

    Can anyone help, plese?!
    Albert

    [Responder]

    May 16th, 2011 at 11:28 am
     
  8. albert

    Here’s my modified code. I get the programm compiled with no errors nut.. it doesn’t work. Putting a buzzer between P!.1 and GND I can only hear a single note constantly.
    Any help?

    http://pastebin.com/2KNwxCwc

    [Responder]

    sucotronic Reply:

    @albert, sorry, I cannot test your code just now. But I recommend you to debug it step by step using the launchap capability to do it (using mspdebug or the similar tool in windows).

    PS: next time you want to post your code please use http://pastebin.com service in order to keep clean the site.

    [Responder]

    May 17th, 2011 at 10:12 am
     
  9. albert

    OK, thanks a lot for answer! I realised I was trying to compile your code in IAR Embedded Workbench and failed because it has to be compiled using mspgcc. I tried all day to make mspgcc work on my windows but with no result.
    Thanks anyway, and forgive my unformatted post.
    Albert

    [Responder]

    May 17th, 2011 at 20:09 pm
     
  10. albert

    Hi,
    It’s working now, but only under Linux, using mspgcc.
    Thanks!
    Albert

    [Responder]

    sucotronic Reply:

    @albert, good to hear you finally got it 😀 Sorry to ‘force’ you to use linux, but it’s better to have free, open and easily available tools than not a proprietary, only one-sourced program.

    [Responder]

    May 18th, 2011 at 13:13 pm
     
  11. albert

    Yes indeed, I am very content of that. Thanks for your article, great work!
    Albert

    [Responder]

    May 19th, 2011 at 11:28 am
     
  12. kemal cagri ozcan

    Hi, I also have the ATMEGA chip on my Arduino . To me, the MSP430 is similar to ATMEGA in that they are both microcontrollers that have USB interfaces for programming. I am still playing around with these and learning as I go. I like the MSP430 better for less power consumption, less components required to run off the board, and I think it’s cheaper, too. However, the Arduino is easier to code because it is more intuitive for humans to read.

    [Responder]

    sucotronic Reply:

    @kemal cagri ozcan, glad to know you want to learn and improve your electronics skills. I would say that the msp430 is more for people who want to go one step forward after trying arduino. You can now experiment with simple hardware at low level, but programming it in C.

    [Responder]

    June 16th, 2011 at 23:00 pm
     
  13. Birm

    Hello,

    If you want it to be run with code composer studio under windows, you can use the following code…

    http://pastebin.com/66iRLtse

    Hope this helps.

    [Responder]

    Jose Reply:

    @Birm,

    Do you have the signal.h file I keep getting an error when it tries to link the buzzer.out

    [Responder]

    January 12th, 2012 at 10:03 am
     
  14. Ben

    Hi there, I was wondering if I could use your code for a school project that I am currently doing. I did not see any permissions on its terms of use but would greatly appreciate if I could use this for my project. I tested it out and modified it slightly to suit my needs but the core of the code is still yours. If you could reply asap I would greatly appreciate it.

    Thanks,

    Ben

    [Responder]

    sucotronic Reply:

    @Ben, hi Ben, of course you can use it for whatever you want, the code is free to use for everybody 🙂

    [Responder]

    March 16th, 2012 at 3:18 am
     
  15. nthdiep66

    hi everyboby,this example is very interesting. but when i want to play a different sound (my exercise is creating sound of AK47 use msp430). can you tell me what should i do???

    [Responder]

    sucotronic Reply:

    @nthdiep66, I recommend you to find a MIDI file with your desired sound, use a conversion tool (like this), and play with it. Maybe you need a normal speaker and a transistor to get more power in your sounds.

    [Responder]

    nthdiep66 Reply:

    @sucotronic,
    Thank! I will try…

    [Responder]

    nthdiep66 Reply:

    @sucotronic,
    I can convert file *.midi to code and play it with msp430 but the sound is different from my desired sound (file AK47.mp3 that I found). Do you know how to convert file *mp3 or * .wav and unchaged sound (like file *.mp3). Please help me!!!!

    [Responder]

    March 25th, 2012 at 20:00 pm
     
  16. nthdiep66

    Can someone show me method to convert file *.mp3 to *.midi (keep sound as mp3 file???). I can not do this…I use WIDI 4.0 Pro converter…

    [Responder]

    sucotronic Reply:

    @nthdiep66, maybe you can try something like this.

    [Responder]

    April 4th, 2012 at 15:42 pm
     
  17. nthdiep66

    Can someone help me convert this sound (http://ringring.vn/ringtone/nhac-chuong-tieng-sung-ak-47-hot-hot/136198) to midi? or guiding me use wikipro4.0?

    [Responder]

    sucotronic Reply:

    @nthdiep66, instead of trying to get a machine gun sound from a midi file, I recommend you to try the msp430 booster pack with sound enabled.

    [Responder]

    April 29th, 2012 at 17:14 pm
     
  18. Kim Trien

    Hello!
    I used code of you. When I debug, I see error: Fatal Error[Pe1696]: cannot open source file “signal.h”

    Please help me! Thanks 😀

    [Responder]

    sucotronic Reply:

    @Kim Trien, be sure to have the latest version of mspgcc.

    [Responder]

    Jose Reply:

    @sucotronic,

    I get the error that it can not build the buzzer.out

    [Responder]

    October 22nd, 2012 at 11:17 am
     
  19. Cristian

    Hi,

    Please give me an advice, i had to play multiple simultaneous tones. Can you help me?

    [Responder]

    August 6th, 2014 at 6:21 am
     

Escribe un comentario


a trastear un poco is powered by WordPress
Theme is Coded&Designed by Wordpress Themes at ricdes