Search code examples
ci2c

TIva C Series problems with I2C Interface


I'm currently trying to interface my Tiva C Series with a Sparkfun Breakout Board, IMU Digital Combo Board - 6 Degrees of Freedom ITG3200/ADXL345 and I'm having trouble with the I2C interface.

currently this is my code:

#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/sysctl.h"
#include "driverlib/gpio.h"
#include "driverlib/i2c.h"

uint8_t SLAVE_ADDRESS = 0x68;

uint32_t first_byte, second_byte, temperature, result;

void i2c_setup(void) {


//Enable the I2C Module
SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C2);

SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);

//Wait at least 5 clock cycles
SysCtlDelay(2);

//Configure SDA and SCL
GPIOPinConfigure(GPIO_PE4_I2C2SCL);

GPIOPinConfigure(GPIO_PE5_I2C2SDA);

//Wait at least 5 clock cycles
SysCtlDelay(2);

//Set PE4 as SCL
GPIOPinTypeI2CSCL(GPIO_PORTE_BASE, GPIO_PIN_4);

//Set PE5 as SDA
GPIOPinTypeI2C(GPIO_PORTE_BASE, GPIO_PIN_5);

//Configure Master,
I2CMasterInitExpClk(I2C2_BASE, SysCtlClockGet(), false);

}

uint32_t i2c_read() {

I2CMasterSlaveAddrSet(I2C2_BASE, SLAVE_ADDRESS, false);

I2CMasterDataPut(I2C2_BASE, 0x1A);

I2CMasterControl(I2C2_BASE, I2C_MASTER_CMD_SINGLE_SEND);

while(I2CMasterBusBusy(I2C2_BASE)); //Loop until the bus is no longer busy

I2CMasterSlaveAddrSet(I2C2_BASE, SLAVE_ADDRESS, true );

I2CMasterControl(I2C2_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE);

while(I2CMasterBusBusy(I2C2_BASE)); //Loop until the bus is no longer busy

first_byte = I2CMasterDataGet(I2C2_BASE);

return first_byte;

}

void setup()
{
Serial.begin(9600);
i2c_setup();
}

void loop()
{

int test = i2c_read();
Serial.println(test);
delay(1000);
}

I'm using Energia to test my program, and when I try to read from the specified register, I get the same result, no matter which register I choose, the result is always decimal 229 (this is the Accelerometer's Device Address).

Can somebody point me in the right direction, I've been looking at my code for quite some time and still don't know whats wrong...

Thanks!


Solution

  • I skimmed through your code and everything seems Okay. Clearly something is working right if you get a response. But Like Martin said , figuring the problem without being there is somewhat difficult. Instead of Writing 0x1A can you try using one of the other I2C commands for the accelerametor ? Also if the jumper is connected to VDD your address should be 0x69 (105 decimal) are you sure it's 0x68 ?

    I looked up the documentation on sparkfuns website and they provided the following list of commands.

     char WHO_AM_I = 0x00;
     char SMPLRT_DIV= 0x15;
     char DLPF_FS = 0x16;
     char GYRO_XOUT_H = 0x1D;
     char GYRO_XOUT_L = 0x1E;
     char GYRO_YOUT_H = 0x1F;
     char GYRO_YOUT_L = 0x20;
     char GYRO_ZOUT_H = 0x21;
     char GYRO_ZOUT_L = 0x22;
    

    GL hope everything works out. Been meaning to buy my own to play around with so keep me posted !