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; 26 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


                      • #12
                        Hi Guys,

                        I am struggling to convert this code to 4DGL commands that my uLCD-70DCT-CLB-AR can send:

                        void BV4627::click(char rly, char on, int del)
                        {
                        if(!del) del=1; // 0 is not allowed

                        if(rly <1) rly = 1;
                        if(rly>8) rly = 8;

                        rly+=9; // relays range 10 to 18

                        // format is rly, on/off, high_del, low_del
                        Wire.beginTransmission(_i2rlyadr);
                        Wire.write(rly); // relay comand
                        Wire.write(on); // on or off
                        Wire.write((del>>8)&0xff); // high byte
                        Wire.write(del & 0xff); // low byte
                        Wire.endTransmission();
                        }

                        This is a BV4627, its a board with 8 relays on it that can be addressed individually by I2C. I have converted the other functions that turn all the relays on or off, but this one lets you specify which relay you wish to activate.

                        this is what I have so far:

                        func I2C_RelayOn(var relay, var on, var delay)

                        I2C1_Open(I2C_SLOW, SCLpin, SDApin);
                        I2C1_Start();
                        I2C1_Write(0x64); <----- address of the unit
                        I2C1_Write(relay);
                        I2C1_Write(on); <--- on is equal to 1

                        I2C1_Write(HIbyte(delay)); //Send High Address
                        I2C1_Write(LObyte(delay));
                        I2C1_Stop();

                        endfunc

                        From the datasheet:
                        For example to switch a relay on requires 4 bytes after the address, the first to specify the relay, the second for on/off and the third and forth to specify a delay if any. delay of 1 is no delay. I get nothing when I press the button.
                        Like I said I am communicating with the board and it is working for the other functions.

                        Thank you for you help,

                        Mark

                        Comment


                        • #13
                          Hi Mark,

                          Everything looks right to me. You could try setting the speed to I2C_MED to see if it makes a difference

                          Could you try the code below to see if it switches on relay A

                          I2C_RelayOnTest(1,0,0,0,0,0,0,0);

                          func I2C_RelayOnTest(var relayA, var relayB, var relayC, var relayD, var relayE, var relayF, var relayG, var relayH)

                          var relaySet;

                          relaySet := relayA | (relayB << 1) | (relayC << 2) | (relayD << 3) | (relayE << 4) | (relayF << 5) | (relayG << 6) | (relayH << 7);

                          I2C1_Open(I2C_MED, SCLpin, SDApin);
                          I2C1_Start();
                          I2C1_Write(0x64); //<----- address of the unit
                          I2C1_Write(20); // bin comand
                          I2C1_Write(relaySet); // where the value is the binary bit of the relay to be on 1 = Relay A
                          I2C1_Stop();

                          endfunc

                          Comment


                          • markl
                            markl commented
                            Editing a comment
                            Thank you for getting back to me, I will try this and let you know what happens.
                            Regards,
                            Mark

                        • #14


                          Hello, thanks for your answer, I have the Iod-09 in use and wanted to transfer data to the SD card via FTP. But have no esp8266ftpserver.h found where it is possible to access the SD. Do you have any solution for this?



                          Comment


                          • #15
                            Hi,

                            Welcome to the forum

                            You can download esp8266 ftp Server from here

                            https://github.com/nailbuster/esp8266FTPServer

                            but I'm not sure haw to save the data to the SD card. I found this forum post also that may be useful

                            https://forum.arduino.cc/index.php?topic=614900.0

                            Best regards

                            Paul

                            Comment

                            Working...
                            X