Quantcast
Channel: Forums - Recent Threads
Viewing all articles
Browse latest Browse all 262198

Compiler/MSP430G2553: I2c Problem using Energia

$
0
0

Part Number:MSP430G2553

Tool/software: TI C/C++ Compiler

Hello,

I have a question regarding using Arduino ode on Energia.

I am trying to use the sample code from Single tact which is in Arduino in MSP430 using Energia.

I am using P1_7 as SDA abd P1_6 as SCL. I am not able to use I2C communication?

Is there any modification required for using it in Energia?

Here is the sample code:

#include <Wire.h> //For I2C/SMBus

void setup()
{
Wire.setModule(0);
Wire.begin(); // join i2c bus (address optional for master)
//TWBR = 12; //Increase i2c speed if you have Arduino MEGA2560, not suitable for Arduino UNO
Serial.begin(57600); // start serial for output
Serial.flush();
}

void loop()
{
byte i2cAddress = 0x04; // Slave address (SingleTact), default 0x04
short data = readDataFromSensor(i2cAddress);
Serial.print("I2C Sensor Data:");
Serial.print(data);
Serial.print("\n");
delay(100); // Change this if you are getting values too quickly
}


short readDataFromSensor(short address)
{
byte i2cPacketLength = 6;//i2c packet length. Just need 6 bytes from each slave
byte outgoingI2CBuffer[3];//outgoing array buffer
byte incomingI2CBuffer[6];//incoming array buffer

outgoingI2CBuffer[0] = 0x01;//I2c read command
outgoingI2CBuffer[1] = 128;//Slave data offset
outgoingI2CBuffer[2] = i2cPacketLength;//require 6 bytes

Wire.beginTransmission(address); // transmit to device
Wire.write(outgoingI2CBuffer, 3);// send out command
byte error = Wire.endTransmission(); // stop transmitting and check slave status
if (error != 0) return -1; //if slave not exists or has error, return -1
Wire.requestFrom(address, i2cPacketLength);//require 6 bytes from slave

byte incomeCount = 0;
while (incomeCount < i2cPacketLength) // slave may send less than requested
{
if (Wire.available())
{
incomingI2CBuffer[incomeCount] = Wire.read(); // receive a byte as character
incomeCount++;
}
else
{
delayMicroseconds(10); //Wait 10us
}
}

short rawData = (incomingI2CBuffer[4] << 8) + incomingI2CBuffer[5]; //get the raw data

return rawData;
}


Viewing all articles
Browse latest Browse all 262198

Trending Articles