8 Replies Latest reply on Nov 27, 2015 5:27 AM by user_358545575

    I2C returns wrong value

    user_358545575

      I am using the Adafruit light sensor TSL2591 and I am not able to read the device ID (should return 0x50) however the ID returns the ID address rather that the value. In this code deviceID = tsl2591_ReadReg(WHO_AM_I) returns a value of 0x12 not 0x050 as expected. Any thoughts? (I am using a Pioneer kit)

         

       

         

      uint8 tsl2591_ReadReg(uint8 Reg)

         

      {

         

          uint8 Write_Buf[1] = {0};

         

          Write_Buf[0] = Reg;

         

          uint8 Read_Buf[1] ={0} ;

         

             

         

      I2C_1_I2CMasterWriteBuf(TSL2591_ADDR, (uint8*)Write_Buf,1,I2C_1_I2C_MODE_NO_STOP);

         

            while ((I2C_1_I2CMasterStatus() & I2C_1_I2C_MSTAT_WR_CMPLT) ==0) {}

         

         

         

      I2C_1_I2CMasterReadBuf(TSL2591_ADDR,(uint8*)Read_Buf,1, I2C_1_I2C_MODE_REPEAT_START);

         

             while((I2C_1_I2CMasterStatus() & I2C_1_I2C_MSTAT_RD_CMPLT) ==0){}

         

       

         

          return Read_Buf[0];

         

      }

        • 1. Re: I2C returns wrong value
          user_242978793

          You didn't mention what Psoc 4 board kit you are using.  Do you have your pull up resistors?

          • 2. Re: I2C returns wrong value
            user_358545575

            I am using the CY8CKIT-042, PSOC4    I do have  pull up resistors.

            • 3. Re: I2C returns wrong value
              user_49271930

              I have no TSL2591. I tested your program, along with the other device (BMP-085).
              Reading is performed correctly, but there are differences:
              My program uses another flag (I2C_MODE_COMPLETE_XFER):
              31   I2C_I2CMasterWriteBuf(TSL2591_ADDR, (uint8 *)write_Buf,1,I2C_I2C_MODE_COMPLETE_XFER);

                 

              If you have CY8CKIT-049-42xx,   I can offer you my program for testing I2C devices:  I2C_finder.zip
              description here is unfortunately only in Russian.

              • 4. Re: I2C returns wrong value
                user_1377889

                Writing with "MODE_NO_STOP" should be correct, except when stated differently in the slave's datasheet. Usually a read transaction is ended with a stop condition freeing the bus. An intermediate "stop" could be an error for ther slave.

                   

                What I do miss is the interpretation of the MasterReadBuf() and MasterWriteBuf() returned result. Both deliver a completion code which could hint to errors that occurred.

                   

                 

                   

                Bob

                • 5. Re: I2C returns wrong value
                  user_358545575

                  Pavloven Thank you for the download, It is a great tool.  When I tried it id did not detect the sensor (perhaps I am not using it correctly). I checked wiring voltage etc...all is fine. I even tried it on an arduino and it worked, but I cannot get it to work on the Pioneer kit. SO the sensor appears to be working. 

                     

                   Bob, I checked the datasheet unfortunately it is not clear to me if an intermediate "stop" could be an error for the slave. I will try the PSOC 049 kit maybe I will have more luck. Thanks

                  • 6. Re: I2C returns wrong value
                    user_1377889

                    Stay with the -042 kit (or get a -044 board) the -049 kit has got no debug capabilities and the core PSoC does not differ from the -042.

                       

                    On your -042 Kit you may set a breakpoint just after the I2C-calls and inspect the returned values (assign result to a variable)

                       

                    I checked the datasheet and it seems that you have to write the register number you want to access into the command register.

                       

                    Use 0xa0 | (RegNumber & 0x1f) to address the register. this will probably need a complete xfer.

                       

                    Then read from the device which will result in the value of the addressed register.

                       

                     

                       

                    Bob

                    • 7. Re: I2C returns wrong value
                      user_49271930


                      Sorry, JDEBS.
                      To work with my program you have to break off a part of the board with chip CY7C65211 and reprogram it as they say here:
                      CY8CKIT-049, USB-Serial CY7C65211 use as an I2C master.
                      when you connect the board with chip CY7C65211 to USB and run programm  - you should see a "1" in the box USB-I2C

                         

                      I2C device is connected to the connector J6:
                      MISO/SCL -> CLK  (1-5Kohm to VDD)  
                      MOSI/SDA -> DATA (1-5Kohm to VDD)
                      If your device requires a 3.3V VDD that is a simple way:
                      install any green LED instead of F1.

                      • 8. Re: I2C returns wrong value
                        user_358545575

                        Finally got it to work, thank you guys