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.

LDC1312EVM: Communicating with LCD1312 chip using I2C with an Arduino

Part Number: LDC1312EVM
Other Parts Discussed in Thread: LDC1312, LDC1314

Hi guys!


Sorry in advance if my question/issue is very elementary!

I am trying to communicate with the LCD1312 chip using I2C as claimed in the description. I'm using an Arduino to do so. I made the connection and after checking the data sheet carefully, I cannot read out the inductance. All I'm getting is 255. I tried to look up online if someone has done similarly, but it was not successful. Attached is my code in Arduino:

P.S. I have no experience in i2c previously, unfortunately. I do receive packages of bytes, but both are 255.
Also, I connected pull up resistors in series each with SDA and SCL.

////////////////////////////

#include <Wire.h>

void setup() {
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial communication at 9600bps
}

int reading1 , reading2 ;
byte d;

void loop() {
// step 1: instruct sensor to read echoes
Wire.beginTransmission(42); // 0x2A if ADDR is high
Wire.write(byte(0x00)); // Choosing whether to Read or Write
Wire.write(byte(0x00)); // Address to read (channel one is at address 0 according to the datasheet)

Wire.endTransmission(); // stop transmitting

delay(70); //

Wire.beginTransmission(42); //
Wire.write(byte(0x01)); // Read
Wire.endTransmission(); // stop transmitting

//
Wire.requestFrom(42, 2); //

//
int wireAv = Wire.available();
int index = 0;
Serial.println(wireAv);

if (wireAv)
{
d = Wire.read();
Serial.println("in while ");
Serial.print("d is ");
Serial.println(d,DEC);
Serial.println(" ");
}

// if (2 <= wireAv ) { // if two bytes were received
// reading1 = Wire.read(); // receive high byte (overwrites previous reading)
// /*reading1 = reading1 << 8; // shift high byte to be high 8 bits
// reading1 |= Wire.read(); // receive low byte as lower 8 bits
// */
// Serial.println(reading1); // print the reading

// }

delay(250); // wait a bit since people have to read the output :slightly_smiling_face:
}

//////////////////////

Thanks!

Cheers,

  • Hi Mohammad,

    Unfortunately, we do not really work with Arduinos, so we're unable to help you debug your code. I would suggest trying StackOverflow for help debugging the I2C. We may be able to share the communication protocol between the LDC1312 and the MSP430 on the EVM, but I'll have to check to make sure we adhere to export controls.
  • Hi Kristin,

    Thanks for the reply. I posted the issue on StackOverflow. I did achieve some result so far but not completed yet.
    I can share the results once I have a full working solution so it will be useful for others as well.

    Best regards,
    Mohammad
  • Hi Mohammad.

    Aren't these wonderful little toys?  I've got the kids coming up with all sorts of sensors based on these.

    One mischief-maker made a load sensor in the rubber bumper on a toilet seat - he learned a lot about 'TMI' and person space. :-)

      I've got an LDC1314 spewing out data wonderfully from an ATmega328( 8MHz ) - here's some snippets...

    My program gets commands from the PC - lots of them - mostly to set/get registers, but one of them is a 'Stream ON/OFF' command -> boolean flag bit called 'bStream' .

    Another flag bit is 'bCrLf' - so the output can either look nice on the serial monitor or look nice to another program.

    You're going to see some variables like 'bGAIN' - they're just 'snippet orphans' - ignore them.

    Some of my setup...

    const byte LDC0 = 0x2A;  //not for THIS PC board
    const byte LDC1 = 0x2B;  //for THIS PC board
    boolean bStream=true;
    boolean bSleep = false;
    boolean bCrLf = true; //default to appending CrLf to data packets
    byte bGAIN=1;  //for auto offset adj amount

    ConfigInit(LDC1);

    Most of my loop.... (CheckPCBuff is where i look for buffer not empty - get commands)

    void loop(){
      if (Serial.available()>0) {delay(100); CheckPCbuf(); }
      if (bStream==true) { Serial.print(":");
        word data0 = readData(LDC1,0); //byte error0=error;
        Serial.print(data0); Serial.print(",");
        word data1 = readData(LDC1,1); //byte error1=error;
        Serial.print(data1); Serial.print(",");
        word data2 = readData(LDC1,2);// byte error2=error;
        Serial.print(data2); Serial.print(",");
        word data3 = readData(LDC1,3); //byte error3=error;
        if (bCrLf==true) {Serial.println(data3);} else {Serial.print(data3);}
        delay(100); }
    }

    Now the fun parts...

    word readData(byte bADDR, byte bChan){ word wVal;  byte bReg=DATA0+ (bChan * 2);
      wVal = readReg(bADDR, bReg);  wVal =wVal & 0xFFF;  return wVal; }

    word readReg (byte bADDR, byte bREG) { byte bVal = 0;  word wVal = 0;
      Wire.beginTransmission( int(bADDR));  Wire.write( int(bREG));  Wire.endTransmission();
      Wire.requestFrom( int(bADDR),2);
      while (Wire.available())  {    wVal = Wire.read();    bVal = Wire.read();  wVal <<= 8;  wVal =wVal+ bVal;    }  return wVal;  }

    This is the one you're going to like... formatting needs adjustment to make bits line up (non-mono-font-width issues).

    void ConfigInit(byte addr){ 
      writeReg(addr, 0x08, 0x0600);//RCOUNT_CH0 (04D6 default)
      writeReg(addr, 0x0C, 0x2015);//CH0_OFFSET [ 15:0 ]
      writeReg(addr, 0x10, 0x000A);//SETTLECOUNT_CH0
      //CH0_SETTLECOUNT (15:0)
     
      writeReg(addr, 0x14, 0x2002);//CLOCK_DIVIDERS_CH0
      //CH0_FIN_DIVIDER [15:12]          0010 ---- ---- ----
      //reserved [11:10]                 ---- 00-- ---- ----
      //CH0_FREF_DIVIDER [9:0]           ---- --00 0000 1000
     
      writeReg(addr, 0x1E, 0x9800);//DRIVE_CURRENT_CH0
      //CH0_IDRIVE [15:11]              1001 1--- ---- ----
      //CH0_INIT_IDRIVE[10:6]           ---- -000 00-- ----
      //reserved [5:0]                  ---- ---- --00 0000

      //********************
      writeReg(addr, 0x09, 0x0600);//RCOUNT_CH1
      writeReg(addr, 0x0D, 0x2015);//CH1_OFFSET [ 15:0 ]
      writeReg(addr, 0x11, 0x000A);//SETTLECOUNT_CH1
      writeReg(addr, 0x15, 0x2002);//CLOCK_DIVIDERS_CH1
      writeReg(addr, 0x1F, 0x9800);//DRIVE_CURRENT_CH1
     
      //********************  
      writeReg(addr, 0x0A,0x0600);//RCOUNT_CH2
      writeReg(addr, 0x0E, 0x2015);//CH2_OFFSET [ 15:0 ]
      writeReg(addr, 0x12, 0x000A);//SETTLECOUNT_CH2
      writeReg(addr, 0x16, 0x2002);//CLOCK_DIVIDERS_CH2
      writeReg(addr, 0x20, 0x9800);//DRIVE_CURRENT_CH2
     
      //********************  
      writeReg(addr, 0x0B,0x0600);//RCOUNT_CH3
      writeReg(addr, 0x0F, 0x2015);//CH3_OFFSET [ 15:0 ]
      writeReg(addr, 0x13, 0x000A);//SETTLECOUNT_CH3
      writeReg(addr, 0x17, 0x2002);//CLOCK_DIVIDERS_CH3
      writeReg(addr, 0x21, 0x9800);//DRIVE_CURRENT_CH3
     
      //********************    
      writeReg(addr, 0x19, 0x0000);//ERROR_CONFIG
       
      writeReg(addr, 0x1A, 0x1601); //CONFIG
      //ACTIVE_CHAN [15:14]              00-- ---- ---- ----
      //SLEEP MODE [13]                      --0- ---- ---- ----
      //RP_OVERRIDE_EN [12]           ---1 ---- ---- ----
      //SENSOR_ACTIVATE_SEL [11]  ---- 0--- ---- ----
      //AUTO_AMP_DISable [10]          ---- -1-- ---- ----
      //REF_CLK_SRC [9] 1=external   ---- --1- ---- ----
      //reserved [8]                                   ---- ---0 ---- ----
      //INTB_DISable [7]                          ---- ---- 0--- ----
      //HIGH_CURRENT_DRV [6]         ---- ---- -0-- ----
      //reserved [5:0]                                ---- ---- --00 0001  

       
      writeReg(addr, 0x1B, 0xC005);//MUX_CONFIG
      //AUTOSCAN_EN [15]      1--- ---- ---- ----
      //RR_SEQUENCE [14:13]   -10- ---- ---- ----
      //DeGlitch [2:0]        ---- ---- ---- -101  (10MHz)

      writeReg(addr, 0x1C, 0x0600);//RESET_DEV - OUTPUT GAIN
      //RESET DEVIce [15]         0--- ----  ----  ----
      //reserved [14:11]          -000 0---  ----  ----
      // OUTPUT_GAIN [ 10:9 ]     ---- -11-  ----  ----
      //reserved [8:0]            ---- ---0  0000 0000
      bGAIN=4;  
    }

    // And these were just to play with data viewing options

    void ShowRegAll(byte bADDR) {
      ShowRegH(bADDR,0x00);ShowRegH(bADDR,0x02);ShowRegH(bADDR,0x04);ShowRegH(bADDR,0x06);
      for (byte reg=0x08; reg<=0x1C; reg++) {  ShowRegH(bADDR,reg); }
      ShowRegH(bADDR,0x1E); ShowRegH(bADDR,0x1F); ShowRegH(bADDR,0x20);
      ShowRegH(bADDR,0x21); ShowRegH(bADDR,0x7E); ShowRegH(bADDR,0x7F);
    }

    void ShowRegB(byte bADDR, byte bReg) { byte gap=0; word wVal = readReg(bADDR, bReg);
       PrntMisc(18,0); // Serial.print("#");
       Serial.print(bReg,HEX);  PrntMisc(6,0); // Serial.print(" ->");
      Serial.print(wVal,HEX); Serial.print(" ( ");
         for (unsigned int mask = 0x8000; mask; mask >>= 1) {gap++;
           Serial.print(mask&wVal?'1':'0');  if ((gap%4)==0){Serial.print(" ");} }
           Serial.println(")");
    }

    void ShowRegH(byte bADDR,byte bReg) {  word wVal = readReg(bADDR, bReg);
      PrntMisc(18,0); // Serial.print("#");
      Serial.print(bReg,HEX);  PrntMisc(6,0); //Serial.print(" ->");
      Serial.println(wVal,HEX);
    }

    void ShowRegD(byte bADDR,byte bReg) {  word wVal = readReg(bADDR, bReg); String sVal;
      PrntMisc(18,0); //Serial.print("#");
      Serial.print(bReg,HEX);  PrntMisc(6,0); //Serial.print(" ->");
      Serial.println(wVal,DEC);
    }

    void PrntMisc(byte x,byte LF) { char sMisc[20]; *sMisc=0;
      strcat_P(sMisc, (char*)pgm_read_word(&(table_MISC[x])));
      if (LF==1) {Serial.println(sMisc);} else {Serial.print(sMisc);}
    } //________ END OF PrntMisc _____________

    const char M00[] PROGMEM = "* LDC1314 Player";
    const char M01[] PROGMEM = "MFR ID- ";
    const char M02[] PROGMEM = "Device ID- ";
    const char M03[] PROGMEM = "Status- ";
    const char M04[] PROGMEM = "Menu = ?";
    const char M05[] PROGMEM ="Offset Channel #";
    const char M06[] PROGMEM = " -> ";
    const char M07[] PROGMEM = "Channel #";
    const char M08[] PROGMEM = "  RCOUNT -> ";
    const char M09[] PROGMEM = "  OFFSET -> ";
    const char M10[] PROGMEM ="  SETTLECOUNT -> ";
    const char M11[] PROGMEM = "  FIN_DIVIDER -> ";
    const char M12[] PROGMEM = "  FREF_DIVIDER-> ";
    const char M13[] PROGMEM = "EE";
    const char M14[] PROGMEM = "<- ";
    const char M15[] PROGMEM = "Working ";
    const char M16[] PROGMEM = ":DONE";
    const char M17[] PROGMEM = "* Final Offset ";
    const char M18[] PROGMEM = "#";

    PGM_P const table_MISC[] PROGMEM = {M00, M01, M02, M03, M04, M05, M06, M07, M08, M09,
                                        M10, M11, M12, M13, M14, M15, M16, M17, M18};

    It's not real pretty - just 'spewed out' in one sitting - but it worked so it should have something you can copy-paste-try.

    Good luck.

  • Hi Marlyn,

    Thanks for the greatly detailed reply! Looks like the code is not finished so I am going to work on it to completely finish it (undeclared variables)

    Meanwhile, I have another working code that seems to be doing the job! Here it is: 

    The setup() include registers whose bits are set according to the data sheet.

    ////////////////

    #include <Wire.h>
    #define PERIOD 2000 // period in us

    void setup() {
    Wire.begin();
    Serial.begin(9600);
    // Initialize all the registers
    Wire.beginTransmission(42);
    Wire.write(0x08);
    Wire.write(0x04);
    Wire.write(0xD6);
    Wire.endTransmission();

    Wire.beginTransmission(42);
    Wire.write(0x10);
    Wire.write(0x00);
    Wire.write(0x0A);
    Wire.endTransmission();

    Wire.beginTransmission(42);
    Wire.write(0x14);
    Wire.write(0x10);
    Wire.write(0x02);
    Wire.endTransmission();

    Wire.beginTransmission(42);
    Wire.write(0x19);
    Wire.write(0x00);
    Wire.write(0x00);
    Wire.endTransmission();

    Wire.beginTransmission(42);
    Wire.write(0x1B);
    Wire.write(0x02);
    Wire.write(0x0C);
    Wire.endTransmission();

    Wire.beginTransmission(42);
    Wire.write(0x1E);
    Wire.write(0xF0);
    Wire.write(0x00);

    Wire.endTransmission();

    Wire.beginTransmission(42);
    Wire.write(0x1A);
    Wire.write(0x10);
    Wire.write(0x01);
    Wire.endTransmission();

    }
    unsigned int reading = 0;
    ==// ADDR == LOW --> I2C == 0x2A
    // ADDR == HIGH --> I2C == 0x2B
    // SD == LOW --> Normal operation
    // SD == HIGH --> Inactive mode

    void loop() {

    ==Wire.beginTransmission(42);
    Wire.write(0x00);
    Wire.requestFrom(42,2);
    if(Wire.available() <= 2)
    {
    reading = Wire.read();
    reading = reading << 8;
    reading |= Wire.read();
    }
    Wire.endTransmission();
    Serial.print("The value is: ");
    Serial.println(reading);

    }

    ///////////////////

    Thanks and hopefully it is useful for others as well!

    Cheers,

  • That's nicely done - not complicated!
    Now for a 'Agh-h-h!' :-)
    I don't know how - the two were right next to each other - but I just noticed that 'writeReg()' got missed in my copy-pasting...

    void writeReg(byte bADDR, byte bREG, word wData){
    byte bMSB=byte(wData>>8); byte bLSB=byte(wData);
    Wire.beginTransmission( int(bADDR));
    Wire.write( int(bREG));
    Wire.write( int(bMSB));
    Wire.write( int(bLSB));
    Wire.endTransmission();
    }