I2C returns wrong value

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
lock attach
Attachments are accessible only for community members.
JoDe_1334571
Level 1
Level 1

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

   

}

0 Likes
8 Replies
rola_264706
Level 8
Level 8
50 likes received 25 likes received 10 likes received

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

0 Likes
JoDe_1334571
Level 1
Level 1

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

0 Likes
lock attach
Attachments are accessible only for community members.
EvPa_264126
Level 7
Level 7
500 replies posted 250 replies posted 100 likes received

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.

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

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

0 Likes
JoDe_1334571
Level 1
Level 1

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

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

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

0 Likes
EvPa_264126
Level 7
Level 7
500 replies posted 250 replies posted 100 likes received


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.

0 Likes
JoDe_1334571
Level 1
Level 1

Finally got it to work, thank you guys

0 Likes