11 Replies Latest reply on Nov 16, 2016 4:27 AM by user_1377889

    I2C + PSoC4

    srujani.meela7_1971856

      Hi ,

         

      I am using CY8CKIT-042 BLE (CY8C4247LQI-BL483) and i am unable to check the device address of HTS221 I2C sensor.

         

      can I know where i am missing out.

         

      Thanks

         

      Srujani

        • 1. Re: I2C + PSoC4
          user_1377889

          The device address for your HTS221 is 0x5f, so far you are right.

             

          Byte I2C interface is quite simple: After setting up the component and starting it you use

             

              I2C_MasterSendStart(DeviceAddress,I2C_WRITE_XFER_MODE);    // Initialize a transaction for writing
              I2C_MasterWriteByte(Register);                // Indicate which register you want to write to
              I2C_MasterWriteByte(Value);                // Write to register
              I2C_MasterSendStop();                    // End of transaction

             

          When you want to read from a device you use (example for reading two bytes

             

              I2C_MasterSendStart(DeviceAddress,I2C_WRITE_XFER_MODE);    // Initialize a transaction for writing
              I2C_MasterWrite(Register);                // Indicate which register you want to write to
              I2C_MasterSendRestart(DeviceAddress,I2C_READ_XFER_MODE);
              I2C_MasterReadByte(I2C_ACK_DATA);            // Read from register
              I2C_MasterReadByte(I2C_NAK_DATA);            // Read from register, last byte is NAKed
              I2C_MasterSendStop();                    // End of transaction

             

          Not too difficult. Keep in mind that most of the APIs (except those for reading a byte) return a status byte which, when non-zero indicate an error condition.

             

          The high-level APIs must be used in this way:

             

          Writing to slave Count bytes
          I2C_MasterWriteBuf(SlaveAddress,DataPtr,Count,I2C_MODE_COMPLETE_XFER);

             

          Reading from Slave sending register byte first:
          I2C_MasterWriteBuf(SlaveAddress,&RegAddress,1,I2C_MODE_NO_STOP);
          I2C_MasterReadBuf(SlaveAddress,DataPtr,Count,I2C_MODE_REPEAT_START);

             


          Bob

             

           

             

           

             

          Bob

          • 2. Re: I2C + PSoC4
            srujani.meela7_1971856

            Hi Bob,

               

            I have done modifications in the code, I am able to some string but not getting the result data.can u please tell me where i have gone wrong.

               

            Thanks

               

            Srujani

            • 3. Re: I2C + PSoC4
              user_1377889

              I cannot follow what your intent is with the code

                 

                  if(I2C_1_I2CMasterReadBuf (HTS221_ADDR,(uint8 *)l_data,1,I2C_1_I2C_MODE_REPEAT_START)!= 1)
                  {
                      UART_1_UartPutString("Erorr2: Input/output Erorr \n\r");
                      //exit(1);
                  }

                 


              which you use frequently.

                 

              I would suggest you to check the returned value from the preceding I2CMasterWriteBuf() and ReadBuf() for != 0 which indicates an error.

                 

               

                 

              Bob

              • 4. Re: I2C + PSoC4
                srujani.meela7_1971856

                Hi ,

                   

                I am unable to get the device address,and after changes on the code i am unable to get the data.can you please help me.

                   

                Thanks

                   

                Srujani

                • 5. Re: I2C + PSoC4
                  user_1377889

                  Set I2C and UART to byte mode!

                     

                  Stick literally to my advices in my first post, your while-loops are unnecessary. Check every returned result!

                     

                   

                     

                  Bob

                  • 6. Re: I2C + PSoC4
                    srujani.meela

                    Hi Bob,

                       

                    I am connecting hts221 sensor externally and just want to check the temperature after reaeading the values from the register and converting.I have configured my external sensor as master, as i am connecting external we have to configure it as slave ai am in confuse now.I have followed the instructions to read from the slave but I am not getting the values on the UART.Can u please help me with it.

                       

                    Thanks

                       

                    Srujani

                    • 7. Re: I2C + PSoC4
                      user_1377889

                      As I said before, stick to my suggestions!

                         

                          I2C_1_I2CMasterWriteBuf(HTS221_ADDR, (uint8 *)l_reg, 2,I2C_1_I2C_MODE_NO_STOP); // a different mode will not work

                         

                       

                         

                      Bob

                      • 8. Re: I2C + PSoC4
                        srujani.meela7_1971856

                        Hi Bob,

                           

                        I followe the steps you have told and was bot able to read the register values output i need to read value form the register (0x28 | 0x80) and print the values reg[0],reg[1],reg[2],reg[3].Can you lease tell me where i went wrong.

                           

                        thanks

                           

                        srujani

                        • 9. Re: I2C + PSoC4
                          user_1377889

                          How can I help you, when you do not follow my suggestions???

                             

                          You did not set UART to byte mode.

                             

                          See attached project and perform needed changes

                             

                           

                             

                          Bob

                          • 10. Re: I2C + PSoC4
                            srujani.meela7_1971856

                            Thanks Bob 

                               

                            The values are getting perfectly.

                            • 11. Re: I2C + PSoC4
                              user_1377889

                              Phew! Wiping the sweat from my brow! ;-)

                                 

                               

                                 

                              Bob