Simple I²C communication using driverlib
You can use this example for simple I²C communication
<source lang="c"> void I2Cinit(void) { // Enable periph for I²C SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
// Enable Port B of GPIO // SDA 0 Bit 3 Output // SCL 0 Bit 2 Output GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_2 | GPIO_PIN_3);// Port B 2 y 3
// Initializes the I2C Master block with clock I2CMasterInitExpClk(I2C0_MASTER_BASE, SysCtlClockGet(), false); // true=400kbps / false=100kbps }
// Write one byte to slaveaddr // WARNING: SlaveAddr => whithout last write/read bit!!!! // return false if failed
tBoolean I2Cwrite(byte SlaveAddr, byte Data) { // Check I2C lines not busy while(I2CMasterBusy(I2C0_MASTER_BASE));
// Specify slave address I2CMasterSlaveAddrSet(I2C0_MASTER_BASE, SlaveAddr, false); // false = WRITE
// Place the character to be sent in the data register I2CMasterDataPut(I2C0_MASTER_BASE, Data);
// Initiate send of character from Master to Slave I2CMasterControl(I2C0_MASTER_BASE, I2C_MASTER_CMD_SINGLE_SEND);
// Delay while busy while(I2CMasterBusy(I2C0_MASTER_BASE));
// See if there was an error if(I2CMasterErr(I2C0_MASTER_BASE) == I2C_MASTER_ERR_NONE) { return false; }
else return true; }
byte I2Cread(byte SlaveAddr) { // Check I2C lines not busy while(I2CMasterBusy(I2C0_MASTER_BASE));
// Specify slave address I2CMasterSlaveAddrSet(I2C0_MASTER_BASE, SlaveAddr, true); // true = read
// Initiate read of character from Master to Slave I2CMasterControl(I2C0_MASTER_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE);
// Delay while busy while(I2CMasterBusy(I2C0_MASTER_BASE));
// See if there was an error if(I2CMasterErr(I2C0_MASTER_BASE) == I2C_MASTER_ERR_NONE) { return I2CMasterDataGet(I2C0_MASTER_BASE); }
else return 0x00; } </source>