Announcement

Collapse
No announcement yet.

quick question about i2c

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

  • quick question about i2c

    Hi,


    I need read charcaters in ascii for i2c. I have tried the function i2c_Read and i2c_gets but i cannot get the values. Som example for me, please?


    Do you see something wrong in my code?


    code:


    func main()

    I2C1_Open( I2C_SLOW , PA8 , PA9);

    putstr("Mounting...\n");
    if (!(file_Mount()))
    while(!(file_Mount()))
    putstr("Drive not mounted...");
    pause(200);
    gfx_Cls();
    pause(200);
    wend
    endif
    // hndl := file_LoadImageControl("xxxx~1.dat", "xxxx~1.gci", 1);

    gfx_Set(SCREEN_MODE,LANDSCAPE) ;
    take_data();

    I2C1_Close();
    repeat
    forever
    endfunc


    func take_data()

    var ch;
    var buffer[4];

    I2C1_Start();

    if ( I2C1_Write(0x70) == 1)
    print("XIP 1 OK "); putch('\n');

    if ( I2C1_Write(0x72) == 1)
    print("XIP 2 OK"); putch('\n');

    I2C1_Gets(buffer,4);
    print("Received: ",[STR]buffer);

    endif
    endif
    I2C1_Stop();

    endfunc



    Thank youuu
    Last edited by Drogon; 26th July 2018, 06:15 PM.

  • #2
    Hello,

    Could you describe your setup a bit more to include the device that the I2C is connected to, just in case the device needs more structure in the I2C commands being sent. It is usual to have an address and a register to write to followed by a restart and the address sent again with the WR bit set.

    This is an example of an I2C read of a RTC single regsiter.

    Code:
    var DSWR := 1;
    
    func readds(var address)
    var b;
    I2C1_Start(); // Generate Start condition
    I2C1_Write(DS1321); // send slave address
    I2C1_Write(LObyte(address)); // Send register address
    I2C1_Restart(); // Generate Restart
    I2C1_Write(DS1321+DSWR); // send control byte for Read
    b := I2C1_Read(); // read the byte
    I2C1_Nack(); // finished reading, send Not Ack
    I2C1_Stop(); // Send Stop Condition
    return b; // return the register value
    endfunc
    If the device is another microcntroller with an I2C library, it may need the same type of command structure.

    I hope this helps

    Best regards

    Paul

    Comment


    • #3
      Hello,


      I have tested with the restart and write again, and others ways too, but no works. With the sniffer I can read the sequence for I2C: <NUL>HXXX 0.00 <ESC>[<SOH>; . I just wanna read with 4d the values HXXX 0.00 to show on the display.


      I don't have specific information of the other device. I know that I can read it because the sniffer show the data.



      Thank you for your time :=)

      Comment


      • #4
        Hello,

        If the address of the device is 0x70 you could try left shifting or right shifting the address by one eg (0x70 << 1) or (0x70 >> 1) just in case the sniffer is interpreting the address with the write bit stripped.

        Paul

        Comment


        • #5
          Hello,


          I have seen the datasheet of my xip and the i2c bus slave 8-bit read address are x71 and x73. Then I have done this code seeing your code:


          func take_data()

          var ch;
          var buffer[4];

          I2C1_Start();

          I2C1_Write(0x70);
          I2C1_Write(0x72);
          I2C1_Restart();
          I2C1_Write(0x71);
          I2C1_Write(0x73);
          ch := I2C1_Read();
          I2C1_Ack();
          //I2C1_Nack();
          print("Received: ");
          putstr(ch);
          /*I2C1_Gets(buffer,4);
          print("Received: ",[STR]buffer);*/
          I2C1_Stop();

          endfunc


          But it still does not show anything -.-

          Any advice?



          Thank you so much

          Comment


          • #6
            Ok,

            Try it like this

            func take_data()

            var ch;
            var buffer[4];

            I2C1_Start();

            I2C1_Write(0x70);
            //I2C1_Write(0x72);
            I2C1_Restart();
            I2C1_Write(0x71);
            //I2C1_Write(0x73);
            ch := I2C1_Read();
            I2C1_Ack();
            putstr(ch);
            ch := I2C1_Read();
            I2C1_Ack();
            putstr(ch);
            ch := I2C1_Read();
            I2C1_Ack();
            putstr(ch);
            ch := I2C1_Read();
            I2C1_Nack();
            putstr(ch);

            //I2C1_Nack();
            print("Received: ");
            putstr(ch);
            /*I2C1_Gets(buffer,4);
            print("Received: ",[STR]buffer);*/
            I2C1_Stop();

            endfunc

            if it isn't right, then like this


            func take_data()

            var ch;
            var buffer[4];

            I2C1_Start();

            //I2C1_Write(0x70);
            I2C1_Write(0x72);
            I2C1_Restart();
            //I2C1_Write(0x71);
            I2C1_Write(0x73);
            ch := I2C1_Read();
            I2C1_Ack();
            putstr(ch);
            ch := I2C1_Read();
            I2C1_Ack();
            putstr(ch);
            ch := I2C1_Read();
            I2C1_Ack();
            putstr(ch);
            ch := I2C1_Read();
            I2C1_Nack();
            putstr(ch);

            //I2C1_Nack();
            print("Received: ");
            putstr(ch);
            /*I2C1_Gets(buffer,4);
            print("Received: ",[STR]buffer);*/
            I2C1_Stop();

            endfunc

            Comment


            • #7
              Hi

              Can you send me the datasheet here.

              Best regards

              Paul

              Comment


              • #8
                Hi,


                I have tried your code and no work. Okay, http://www.ti.com/lit/ds/symlink/pcf8574a.pdf the datasheet of my two xip.



                Thank you so much



                Comment


                • #9
                  Hi,

                  Thanks for the datasheet, do you know how a0,a1,a2 are set on your device.

                  Best regards

                  Paul

                  Comment


                  • #10
                    Hi,

                    According to the datasheet it is a simple command to read

                    func take_data()

                    var ch;
                    var buffer[4];

                    to read :-

                    I2C1_Start();
                    I2C1_Write(0x71);
                    ch := I2C1_Read();
                    print(ch);
                    I2C1_Stop();

                    to write:-

                    I2C1_Start();
                    I2C1_Write(0x70);
                    ch := 0xFF;
                    I2C1_Write(ch);
                    I2C1_Stop();

                    Thats assuming that XIP 1's a0,a1,a2 are all set to GND

                    I haven't seen I2C handled like that before so I am still not certain it is correct.

                    Best regards

                    Paul

                    Comment


                    • #11
                      Hello,


                      I was talking with the person responsible of programming the other device. He must fix some things, the problem is not on the display.


                      Sorry, I did not know this either



                      Thank you for your time Paul :=)

                      Comment

                      Working...
                      X