}

10/18/2012

TI Launchpad MSP430 i2c communication with Arduino using Energia



This is the second part in an i2c demo I have been working on.  In the first part, I had an Arduino Mega communicating with an Arduino Uno.  Now we are going to add a TI Launchpad MSP430 to the i2c bus.

First, we need to connect the TI Launchpad MSP430 to the i2c bus.  Simply connect pin 1.6 on the Launchpad to the SCL channel on the breadboard and pin 1.7 to the SDA channel on the breadboard.

Second, we need to change the voltage of the i2c bus.  The MSP430 is a 3.3 volt device, so I just moved the +5 feed to the i2c bus on the Arduino Mega 2560 to the 3.3 volt pin.

Next, I connected a speaker to the TI Launchpad MSP430.  I have the red lead of the speaker going to pin 1.4 on the Launchpad and the black lead connected to ground.

Now for the code.  The Arduino Mega will first tell the Uno to turn it's LED on, wait 3 seconds, tell the Launchpad to play a 1900 Hertz tone using the tone() command, wait 3 seconds, then turn the LED and speaker OFF.

The code for the Launchpad is almost exactly the same as the code I previously used for the Arduino.  The only difference is that we are now playing a tone instead of lighting an LED.  The other difference is the i2c ID # of 6.

Below is the code for the Arduino Mega 2560 and the TI Launchpad MSP430.  I used the Energia IDE to program the MSP430 using Arduino code.  The Launchpad is using a M430G2553 microcontroller, although a 2231 should work just as well.

i2c master code - Arduino Mega 2560


//i2c Master Code(MEGA)
#include <Wire.h>

void setup()
{
  Wire.begin();
  delay(2000);
}

void loop()
{
      
      Wire.beginTransmission(5);
      Wire.write('H');
      Wire.endTransmission();
      
      delay(3000);
      
      Wire.beginTransmission(6);
      Wire.write('H');
      Wire.endTransmission();
      
      delay(3000);
      
      Wire.beginTransmission(6);
      Wire.write('L');
      Wire.endTransmission();

      Wire.beginTransmission(5);
      Wire.write('L');
      Wire.endTransmission();
      
      delay(3000);
     
}

i2c slave code (TI Launchpad MSP430 with MSP430G2553 installed)


//i2c Slave Code(LAUNCHPAD)
#include <Wire.h>

void setup()
{
  Wire.begin(6);
  Wire.onReceive(receiveEvent);
  
  pinMode(6,OUTPUT);

}

void loop()
{
}

void receiveEvent(int howMany)
{
  while(Wire.available())
  {
    char c = Wire.read();
    
    if(c == 'H')
    {
      tone(6,1900);
    }
    else if(c == 'L')
    {
      noTone(6);
    }
  }
}

I'd like to thank the Energia folks for creating the Energia IDE.  If you haven't heard of it, it allows you to program an MSP430 chip using the same code as you would an Arduino.  For more information on Energia, you can go to the Energia website, or the Energia forum at 43oh.com.