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.

I2C masterbus loop issue

Other Parts Discussed in Thread: TM4C123GH6PM

I'm using the TM4C launchpad and interfacing it with the sht21 temperature and relative humidity sensor using the I2C module.  I struggle with trying to understand why this code returns true for the first while loop which checks if the master is busy. Also, if it does run, the only "data" it displays is 255. I am unable to understand what the goof up in my code is.

#include "inc/tm4c123gh6pm.h"
#include <stdbool.h>
#include <stdint.h>
#include "inc/hw_i2c.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/hw_gpio.h"
#include "utils/uartstdio.h"
#include "driverlib/i2c.h"
#include "driverlib/sysctl.h"
#include "driverlib/gpio.h"
#include "driverlib/pin_map.h"
#include "i2cheader.h"
#include "uart.h"
void InitI2C0(void);
void BlinkLed(void);

int main(void)

{
uint8_t data1, data2 = 0;
SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|
SYSCTL_OSC_MAIN);

InitConsole();
DisplaySetup();
InitI2C0();

I2CMasterSlaveAddrSet(I2C0_BASE, 0X40, false);
I2CMasterDataPut(I2C0_BASE, 0XE3);
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_START);
while(I2CMasterBusy(I2C0_BASE));//ALWAYS RETURNS TRUE
SysCtlDelay(1000);

I2CMasterSlaveAddrSet(I2C0_BASE, 0x40, true);

//READ FIRST BYTE
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_START);

while(I2CMasterBusy(I2C0_BASE));


data1= I2CMasterDataGet(I2C0_BASE);

//READ SECOND BYTE
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH);

while (I2CMasterBusy(I2C0_BASE));

data2 = I2CMasterDataGet(I2C0_BASE);

//display data onto the console

}

 I'm quite the novice at microcontroller coding, so any help would be appreciated. Thank you!

  • Hi Aiswarya,

    I have not got the time to check what's particularly wrong with your code. But, I just want to let you know that there is a example program for SHT21 Humidity and Temperature Sensor at Tivaware, which is located at "C:\EK-TM4C123GXL-KEIL-753\examples\boards\ek-tm4c123gxl-boostxl-senshub\humidity_sht21". Maybe you can just modify that to your requirements.

    - kel

  • Aiswarya Akumalla said:
    I'm quite the novice at microcontroller coding, so any help would be appreciated. Thank you!

    Might your task be a "bit" too demanding - for one just beginning?     And - you should know - that many more experienced than you - have stumbled badly with that device.

    One poster suggest you (almost blindly) "cut/paste" other's code.   I'm no fan of that - how, when, where: "Will you learn - via that "suggestion?"

    My small tech firm has been unable to find, "Quick/Easy" shortcuts.   You've much to read - much to code - if your (real) goal is true understanding & tech growth.

    You may google "KISS" that should reveal that, "One small, manageable step at a time" so often trumps, "Mt. Everest - as a first-ever climb!"

    Your "beginning" instead with a far simpler device (such as low capacity, I2C eeprom) provides many of the necessary "building blocks" needed for more advanced work - later!

  • Hello Aiswarya,

    Missing details on IO Configuration and InitI2C0() function call, and whether a Pull Up has been placed.

    Regards
    Amit
  • Hi Amit,

    I am not using any pull up resistors and InitI2C0() function call for initialising the I2C Module is the exact same as in the example code provided, except with the loopback mode disabled.
  • Hello Aiswarya,

    I2C "requires" Pull Up. Either you can use the IO Weak Pull Up or connect external Strong Pull Ups. Only then will I2C work.,

    Regards
    Amit