Is this a bug in the i2c code?

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

cross mob
Anonymous
Not applicable

I spent better part of 4 hours trying to figure out why I couldn't communicate with my OLED lcd.  Worked fine on atmel, could see it under BusPirate but with a basic setup on the POSC it kept getting NACK's and freezing on MasterSendStop.  THEN I found out it was sending the wrong address?

I finally came across this line here:

I2C_1_DATA_REG = (uint8) (slaveAddress << I2C_1_SLAVE_ADDR_SHIFT);

Why is it shifting left the address?  I went ahead and change my address to shift left first and now everything is peachy.  Could there be an option in the component to turn this off and just check if the address is even?

PS - For the record I know the LSB is the read/write bit, but  I have yet to come across a device that is pre sifted liike that.

0 Likes
1 Reply
HeLi_263931
Level 8
Level 8
100 solutions authored 50 solutions authored 25 solutions authored

There are two ways of stating an I2C address: with or without the R/W bit. The former is 8 bit, the latter is 7 bit long. You need to look up the data sheets of both the I2C device you are using, and the I2C library, which format they want to use.

In the code above, slaveAddress is a 7-bit-address, whereas the library / MCU wants a 8-bit-adress, so the address is moved one bit to the left.

Since the (UDB) I2C master in the PSoC want to use a 7-bit-address, you don't need the bitshift.

0 Likes