Announcement

Collapse
No announcement yet.

I2C Bus communication

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • I2C Bus communication

    I have a Gen4-uLCD-43DT display and I am trying to read data from a smart battery via I2C bus system.
    The problem is that the display cannot read the data correctly (everything is zero).

    Currently, I am using the <wire.h> library from Arduino (but I just found internal functions that support the communication via I2C, so I will try using those, too).
    Here is part of the code I used to read the value 'Relative State of Charge' as an example:

    #platform "Gen4-uLCD-43DT"
    #inherit "4DGL_16bitColours.fnc"
    #inherit "VisualConst.inc"
    #inherit "SMBUSConst.inc"
    #inherit "Wire4D.inc"

    #CONST
    RelativeStateOfCharge 0x0D
    var RelStateofCharge;

    RelStateofCharge := readSMBWord(RelativeStateOfCharge);

    func readSMBWord(var command)
    var vals[2];
    vals[0] := 0;
    vals[1] := 0;
    var count;
    count := 0;
    var r;

    r := I2C1_Idle();
    if (r == 1)

    Wire_beginTransmission(Batt_Address);
    Wire_write(command);
    Wire_endTransmission();
    I2C1_Idle();
    Wire_requestFrom(Batt_Address, 3);
    I2C1_Idle();
    count := 0;
    while (Wire_available())//Solange eine Verbinung besteht, wird gesendet.
    vals[count] := Wire_read();//Inkrementiert den Zähler und schreibt jeweils ein Byte in das Array
    I2C1_Idle();
    count := count + 1;
    wend
    endif
    var value;
    value := ((vals[1] << 8) | vals[0]);
    return value;

    endfunc



    Also, I would like to change timing settings, how can I do that? Do timings depend on the used library (Arduino vs I2C internal functions)?

    Thanks for your support!

    Regards
    Ali31

  • #2
    Hi,

    Would you be able to send me the Wire4D.inc file so I can check it here.

    Best regards

    Paul

    Comment


    • #3
      Good morning,
      I'd love to, but I am not allowed to upload files with the extension .inc and I cannot send private messages either so I'll just copy the text of the file and hope this is working for you.
      If there is another option I missed, please let me know. Thank you.
      ---

      #CONST
      BUFFERLENGTH 32
      #END



      var txAddress;
      var txBuffer[32];
      var txBufferIndex;
      var txBufferLength;


      var rxBuffer[32];
      var rxBufferIndex;
      var rxBufferLength;

      var transmitting;

      /*var I2C1SPEED;
      var SCLPIN, SDAPIN;
      var I2C1SPEED;
      var SCLPIN, SDAPIN; */

      /*func Wire_setSDA(var sda)
      SDAPIN := sda;
      endfunc

      func Wire_setSCL(var scl)
      SCLPIN := scl;
      endfunc

      func Wire_setClock(var clock)
      I2C1SPEED := clock;
      endfunc */

      func Wire_begin()
      rxBufferIndex := 0;
      rxBufferLength := 0;

      txBufferIndex := 0;
      txBufferLength := 0;

      I2C1_Open(I2C_SLOW, PA4, PA5);

      endfunc


      func Wire_requestFrom(var address, var quantity)
      if(quantity > BUFFERLENGTH)
      quantity := BUFFERLENGTH;
      endif
      I2C1_Start();
      I2C1_Write((address << 1) | 1);
      I2C1_AckStatus();
      var i;
      for(i := 0; i < quantity; i++)
      rxBuffer[i] := I2C1_Read();
      if(i < quantity-1) I2C1_Ack();
      next
      I2C1_Nack();
      I2C1_Stop();
      rxBufferIndex := 0;
      rxBufferLength := quantity;
      return 1;
      endfunc


      func Wire_beginTransmission(var address)
      I2C1_Start();
      I2C1_Write(address << 1);
      // indicate that we are transmitting
      transmitting := 1;
      // set address of targeted slave
      txAddress := address;
      // reset tx buffer iterator vars
      txBufferIndex := 0;
      txBufferLength := 0;
      endfunc

      // This provides backwards compatibility with the original
      // definition, and expected behaviour, of endTransmission
      //
      func Wire_endTransmission()
      I2C1_Stop();
      transmitting := 0;
      return 1;
      endfunc

      func Wire_write(var daten)
      if(transmitting)
      // in master transmitter mode
      I2C1_Write(daten);
      return 1;
      endif
      endfunc


      func Wire_available()

      return rxBufferLength - rxBufferIndex;

      endfunc

      func Wire_read()
      var value;
      value := -1;

      // get each successive byte on each call
      if(rxBufferIndex < rxBufferLength)
      value := rxBuffer[rxBufferIndex];
      rxBufferIndex :=rxBufferIndex + 1;
      endif
      return value;
      endfunc





      func Wire_peek()

      var value;
      value := -1;

      if(rxBufferIndex < rxBufferLength)
      value := rxBuffer[rxBufferIndex];
      endif

      return value;
      endfunc

      func Wire_flush()
      // XXX: to be implemented.
      endfunc

      ---
      Best regards
      Ali31




      ​​​​​​​

      Comment


      • #4
        Hi,

        Ok, thanks for that. It does match what I have here already so I can carry out some tests.

        Best regards

        Paul

        Comment

        Working...
        X