Announcement

Collapse
No announcement yet.

I2C Bytes Array

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

  • I2C Bytes Array

    Hi,

    we want to read in I2C a bytes array sended from Arduino.

    Here is the loop in arduino :

    HTML Code:
    void loop()
    {
      delay(100);
      i += 100;
      buffer[0] = 0x01;
      buffer[1] = 0x15;
      if(digitalRead(2)){
      fonctionnement != fonctionnement;
        if(fonctionnement){
          buffer[3] = 0x01;
        } else {
          buffer[3] = 0x00;
        }
      }
      buffer[4] = 0x19; 
      buffer[5] = 0x3C;
    }
    
    // function that executes whenever data is requested by master
    // this function is registered as an event, see setup()
    void requestEvent()
    {
      String data = String(i);
      //Wire.write(0x3c);
      //Wire.write(0x02);
      Wire.write(buffer, 6);
      //Wire.write("HELLO WORLD"); // respond with message of 6 bytes
                           // as expected by master
      
    }
    And here the code from Visi :

    HTML Code:
    repeat
    
        gfx_MoveTo(0, 50);
        print("received: ",countBytes(), "\n");
        //var r[2];
        //r := I2C_readbyte();
        gfx_MoveTo(0, 100);
        print("received: ",[HEX2]I2C_readbyte(), "\n");
    
        forever
    
    endfunc
    
    func I2C_readbyte()
    var r[2];
    var r1;
    var i;
    var y;
    
        for(i := 0 ; i < 2; i++)
        I2C1_Open(I2C_MED, PA2, PA3);
        //I2C1_Idle();                        // Wait for bus Idle
        I2C1_Start();                    // Generate Start condition
        I2C1_Write(0x09);                // send slave address
        I2C1_Idle();         // Wait for bus Idle
        r[i] := I2C1_Read();                // read the byte
        I2C1_Nack();                        // finished reading, send Not Ack
        I2C1_Stop();                        // Send Stop Condition
        I2C1_Close();
        y := 150 + (i * 50);
        gfx_MoveTo(0, y);
        print("received: ",r[i], "\n");
    next
    
    return r;                       // return the register value
    
    endfunc
    
    func countBytes()
    var r ;
    I2C1_Open(I2C_MED, PA2, PA3);
    //I2C1_Idle();                        // Wait for bus Idle
    I2C1_Start();                    // Generate Start condition
    I2C1_Write(0x09);                // send slave address
    I2C1_Idle();         // Wait for bus Idle
    r := I2C1_Getn(buffer, 6);                // read the byte
    I2C1_Nack();                        // finished reading, send Not Ack
    I2C1_Stop();                        // Send Stop Condition
    I2C1_Close();
    return r;
    endfunc
    In this case i read value 29 and 1 byte to read with the countByte function...

    We have try many solutions but any success ....

    How can we read all the bytes from Arduino ? It's a mistake from the Arduino code or Visi ?

    PS : Sorry for my english ...

    Thanks to everyone !

  • #2
    Hi,

    Welcome to the forum:

    Thank you for sharing your code.

    I've tried your code and I noticed that there are a few lines in your code that need to be changed.

    You can try the sample code that shows the sending of data from Arduino to Display.
    This will give you an idea and also you can modify it.


    Here's the code:

    For Arduino IDE :

    Code:
    #include <Wire.h>
    
    uint8_t _buffer[5];
    
    
    void setup() {
      // put your setup code here, to run once:
      Wire.begin(4);                // join i2c bus with address #2
      Wire.onRequest(requestEvent); // register event
    }
    
    void loop()
    {
    
      _buffer[0] = 71; //G
      _buffer[1] = 72; //H
      _buffer[2] = 73; //I
      _buffer[3] = 74; //J
      _buffer[4] = 75; //K
    
      delay(3000);
    
      _buffer[0] = 65; //A
      _buffer[1] = 66; //B
      _buffer[2] = 67; //C
      _buffer[3] = 68; //D
      _buffer[4] = 69; //E
    
    
      delay(3000);
      
      _buffer[0] = 76; //L
      _buffer[1] = 77; //M
      _buffer[2] = 78; //N
      _buffer[3] = 79; //O
      _buffer[4] = 80; //P
    
      delay(3000);
    
    
    }
    
    
    void requestEvent()
    {
      Wire.write(_buffer,5);
    }


    For WS4 IDE(ViSi):


    Code:
    var buffer[20];
    
    func main()
        gfx_ScreenMode(LANDSCAPE) ; // change manually if orientation change
    
        print("Hello World\n") ;      // replace with your code
    
        I2C1_Open(I2C_SLOW,PA2,PA3);
        //I2C1_Open(I2C_MED,PA2,PA3);
        //I2C1_Open(I2C_FAST,PA2,PA3);
    
        repeat
            I2C1_Idle();
            I2C1_Start();                // Generate Start condition
            I2C1_Write(0x09);            // send slave address, R/W bit is 1
            I2C1_Idle();
    
            I2C1_Gets(buffer,20);
    
            I2C1_Nack();                 // finished reading, send Not Ack
            I2C1_Stop();                 // finished with bus
    
            gfx_MoveTo(0, 20);
            print("received: ",[STR]buffer, "\n");
    
            gfx_CircleFilled(100,100,10, YELLOW);
            pause(950);
            gfx_CircleFilled(100,100,10, BLACK);
            pause(50);
    
        forever
    
    endfunc

    Here's the connection:

    gen4-PA | Arduino

    PA2/GPIO2 ---->A5/SCL
    PA3/GPIO1 ---->A4/SDA
    GND ------------->GND

    If you have questions, please don't hesitate to ask here.

    Best regards,
    Kayven

    Comment

    Working...
    X