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.

CCS/TM4C1294NCPDT: ccs/tm4c1294ncpdt simple i2c read from a i2c based sensor

Part Number: TM4C1294NCPDT
Other Parts Discussed in Thread: TMP100

Tool/software: Code Composer Studio

I'm using TM4C1294NCPDT  with code composer studio.

and I want to read data from an I2C based sensor.

there are lots of function in i2c.c file in the libraries.

which functions should I need to read the i2c sensor's data?

I want to perform a simple read operation.

  • m_vedsharma said:

    which functions should I need to read the i2c sensor's data?

    I want to perform a simple read operation.

    As you list an, "I2C sensor" - it is likely that any such "sensor read" will prove, "Far from simple."

    Such sensors often provide multiple operating modes - and potentially "many" bytes of data - that escapes "simple" - does it not?

    "Simple" as you list - is best obtained by your selecting (instead) a small capacity, I2C based EEProm, which may actually qualify as, "simple."

    This vendor has example code which details how to implement the "read" of such an EEProm.    (EEProm is small, inexpensive and "simple.")

    KISS dictates that, "Learning & Experimentation" start SMALL and CONTROLLED - both (likely) violated - when choosing an (unspecified) I2C sensor.

    The MCU's manual and Peripheral Driver Library User's Manual provide "in depth" coverage of I2C operation - you've not noted your familiarity w/either - such read/review is "sure to guide/assist."

  • Here is a sample program that reads the TMP100 temperature sensor on the DK-TM4C129X development board. It is based on code originally from this thread: e2e.ti.com/.../2158708

    #include <stdbool.h>
    #include <stdint.h>
    #include "inc/hw_i2c.h"
    #include "inc/hw_ints.h"
    #include "inc/hw_memmap.h"
    #include "inc/hw_types.h"
    #include "driverlib/gpio.h"
    #include "driverlib/i2c.h"
    #include "driverlib/interrupt.h"
    #include "driverlib/pin_map.h"
    #include "driverlib/sysctl.h"
    #include "driverlib/pin_map.h"
    #include "driverlib/rom_map.h"
    
    #define SLAVE_ADDRESS 0x4A
    
    union temp_tag
    { int16_t i16;
      struct u8_tag
      { uint8_t low;
        uint8_t high;
      } u8;
    };
    
    static	float temperature;
    
    int main()
    
    {
    
    	union temp_tag temp;
    	uint32_t sys_clock, error;
    	sys_clock = SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ |
                                           SYSCTL_OSC_MAIN |
                                           SYSCTL_USE_PLL |
                                           SYSCTL_CFG_VCO_480), 120000000);
    
    	SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C6);
    	while(!(SysCtlPeripheralReady(SYSCTL_PERIPH_I2C6)))
    	{
    	}
    	SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
    	while(!(SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOB)))
    	{
    	}
    	GPIOPinConfigure(GPIO_PB6_I2C6SCL);
    	GPIOPinConfigure(GPIO_PB7_I2C6SDA);
    
    	GPIOPinTypeI2CSCL(GPIO_PORTB_BASE, GPIO_PIN_6);
    	GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_7);
    
    	I2CMasterInitExpClk(I2C6_BASE, sys_clock, false);	//False = 100KHz
    
    
    	while(1)
    	{
    		I2CMasterSlaveAddrSet(I2C6_BASE, SLAVE_ADDRESS, false);
    
    		/*  Configuration Register */
    		I2CMasterDataPut(I2C6_BASE, 0x01);
    		I2CMasterControl(I2C6_BASE, I2C_MASTER_CMD_BURST_SEND_START);
    		MAP_SysCtlDelay(100);
    		while(I2CMasterBusy(I2C6_BASE));
    		error = I2CMasterErr(I2C6_BASE);
    		if (I2CMasterErr(I2C6_BASE) != I2C_MASTER_ERR_NONE)
    		{
    			I2CMasterIntClear(I2C6_BASE);	// clear the interrupts if any occurred.
    			return error;
    		}
    		I2CMasterDataPut(I2C6_BASE, 0xA1);
    		I2CMasterControl(I2C6_BASE, I2C_MASTER_CMD_BURST_SEND_FINISH);
    		MAP_SysCtlDelay(100);
    		while(I2CMasterBusy(I2C6_BASE));
    		I2CMasterSlaveAddrSet(I2C6_BASE, SLAVE_ADDRESS, false);
    		I2CMasterDataPut(I2C6_BASE, 0x00);                  //P1 P0 = 01 = configuration reg. Byte goes to PR
    		I2CMasterControl(I2C6_BASE, I2C_MASTER_CMD_BURST_SEND_START);
    		MAP_SysCtlDelay(100);
    		while(I2CMasterBusy(I2C6_BASE));
    		I2CMasterSlaveAddrSet(I2C6_BASE, SLAVE_ADDRESS, true);// true indicates that master is initiating the receive.
    		I2CMasterControl(I2C6_BASE, I2C_MASTER_CMD_BURST_RECEIVE_START);
    		MAP_SysCtlDelay(300);
    		while(I2CMasterBusy(I2C6_BASE));
    		temp.u8.high = I2CMasterDataGet(I2C6_BASE);
    		I2CMasterControl(I2C6_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH);
    		MAP_SysCtlDelay(300);
    		while(I2CMasterBusy(I2C6_BASE));
    		temp.u8.low = I2CMasterDataGet(I2C6_BASE);
    		temperature = (temp.i16>>6)  * 0.25;
    		//temperature now holds floating point value of temperature in degrees C
    	}
    }