Playing music with msp430 launchpad

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
  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 

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) {

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

  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.

36 replies on “Playing music with msp430 launchpad”

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?



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

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

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


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 in Four-Three-oh! Forums.

Thanks again.

This example does not work on my MSP430G2xxx. I have changed the code as suggested above by old_cow_yellow:
int notes[] = { 0, …};
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, 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 service in order to keep clean the site.

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, 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.

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

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.

@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.

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.



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

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???

@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.

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!!!!

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…

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

Please help me! Thanks 😀


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

Leave a Reply

Your email address will not be published. Required fields are marked *