This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Problem with SensorLib I2c functions

Hello,

Im trying to connect the MPU6050 (adress 0x68) to the Tiva C Launchpad however I cant even read the WHO_AM_I register (0x75). Instead of the desired value 0x68 I get zero as result.Can someone please tell me which part of my code is wrong? Thank you a lot!

bool done;
tI2CMInstance sI2CInst;

void callback(void *pvData, uint_fast8_t ui8Status)
{
	if(ui8Status!=I2CM_STATUS_SUCCESS)
	{
		//FEHLER
	}
	else
		done=true;
}

void I2C1IntHandler(void)
{
	//UARTprintf("I2C IRQ\n");
	I2CMIntHandler(&sI2CInst);

}

void tiva_i2c_init(uint32_t ui32Base, uint_fast8_t ui8Int, uint32_t ui32Clock)
{
	I2CMInit(&sI2CInst, ui32Base, ui8Int, 0xff, 0xff,ui32Clock);
}

int tiva_i2c_read(unsigned char slave_addr,unsigned char reg_addr,unsigned char length, uint8_t *data)
{
	done=false;

	uint_fast8_t result=I2CMRead(&sI2CInst, slave_addr,reg_addr,1,data,length,callback,0);
	while(!done)
	{
	}
	if(result==1)
		return 0;
	else
		return 1;
}


int main (void)

{
        SysCtlClockSet(SYSCTL_SYSDIV_3 | SYSCTL_USE_PLL | SYSCTL_XTAL_16MHZ | SYSCTL_OSC_MAIN);
        ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
	ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C1);
	ROM_GPIOPinConfigure(GPIO_PA6_I2C1SCL);
	ROM_GPIOPinConfigure(GPIO_PA7_I2C1SDA);
	ROM_GPIOPinTypeI2CSCL(GPIO_PORTA_BASE, GPIO_PIN_6);
	ROM_GPIOPinTypeI2C(GPIO_PORTA_BASE, GPIO_PIN_7);
        tiva_i2c_init(I2C1_BASE, INT_I2C1, ROM_SysCtlClockGet());

	unsigned char test=2;

	tiva_i2c_read(0x68,0x75,1, &test);
	if(test==0x68)
		UARTprintf("Connected...\n");
	else
	{
		UARTprintf("ERROR\n");
		UARTprintf("WHO_AM_I: %i\n",test); //Result is always ZERO
		UARTprintf("***STOP***");
		while(1){}
	}

while(1){}

}


  • Hi dinu,

    What launchpad are you using? There are 2 Tiva launchpads now.

    Did you use pull-ups? if memory serves, the data doesn't have internal pull-ups and the clk also doesn't

  • Hi Luis,

    I'm using the TM4C123GXL launchpad. No I haven't added pull-ups.

    I thought they would be activated automatically on the launchpad when the I2C block is activated but maybe I misunderstood something...

  • I already had this problem before. You should be able to set up manually internal pull-ups using the GPIO API. With 

    GPIOPadConfigSet(GPIO_PORTF_BASE,GPIO_PIN_4,GPIO_STRENGTH_2MA,GPIO_PIN_TYPE_STD_WPU);

    Just change to your pins. Remember to never put the SCL in open drain. Try this before you configure the pins as SCL and DATA.
    Before:
        ROM_GPIOPinConfigure(GPIO_PA6_I2C1SCL);
        ROM_GPIOPinConfigure(GPIO_PA7_I2C1SDA);
        ROM_GPIOPinTypeI2CSCL(GPIO_PORTA_BASE, GPIO_PIN_6);
        ROM_GPIOPinTypeI2C(GPIO_PORTA_BASE, GPIO_PIN_7);

    Look into 16.3 in the datasheet. There says you need external pull-ups.

  • Ok I tried it with the config code

    GPIOPadConfigSet(GPIO_PORTA_BASE,GPIO_PIN_6,GPIO_STRENGTH_2MA,GPIO_PIN_TYPE_STD_WPU);
    GPIOPadConfigSet(GPIO_PORTA_BASE,GPIO_PIN_7,GPIO_STRENGTH_2MA,GPIO_PIN_TYPE_STD_WPU);
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C1);
    ROM_GPIOPinConfigure(GPIO_PA6_I2C1SCL);
    ROM_GPIOPinConfigure(GPIO_PA7_I2C1SDA);
    ROM_GPIOPinTypeI2CSCL(GPIO_PORTA_BASE, GPIO_PIN_6);
    ROM_GPIOPinTypeI2C(GPIO_PORTA_BASE, GPIO_PIN_7);

    but I still get the wrong results when reading from the device.

  • I said to try because i didn't remember if it worked before. Without sending anything are the pins at 3.3V? It would be easier to check with a logic analyzer.

    You probably really have to add the external pull-ups

  • I have added two pull-ups but the result is still the same.