I2C How to Read 2 Bytes (ESP32)

For general Flowcode discussion that does not belong in the other sections.
Post Reply
rchrislijs
Posts: 4
http://meble-kuchenne.info.pl
Joined: Wed Jan 10, 2024 11:01 am

I2C How to Read 2 Bytes (ESP32)

Post by rchrislijs »

Hi there,

I've been using Flowcode for a while, but now I need your help with my puzzle.

I'm using sensor TMP1075 which is an I2C temperature sensor and I am unable to read 2 bytes from this chip atm :-(

The temperature register of the TMP1075 is a 12-bit, read-only register that stores the result of the most recent
conversion (address 0x00). Data is represented in binary two's complement format. The first 12 bits are used
to indicate temperature, with all remaining bits equal to zero.

I have been using the GenericReadRegister call which is great as it's just 1 entry and works all the time. This is true for 1 byte readmm, but how do I read 2 bytes?
TMP1075_query.jpg
TMP1075_query.jpg (23.9 KiB) Viewed 1743 times
Thanks for your help,
R

chipfryer27
Valued Contributor
Posts: 1607
Joined: Thu Dec 03, 2020 10:57 am
Has thanked: 357 times
Been thanked: 565 times

Re: I2C How to Read 2 Bytes (ESP32)

Post by chipfryer27 »

Hi

I think you need to use Transaction Mode with an ESP. The WiKi has some information

https://www.flowcode.co.uk/wiki/index.p ... Interface)

Regards

rchrislijs
Posts: 4
Joined: Wed Jan 10, 2024 11:01 am

Re: I2C How to Read 2 Bytes (ESP32)

Post by rchrislijs »

Hi there thanks, I've checked this already and have tried TOO many times without success, hence coming to the Forum for someone who has already done this :-)

From the different examples on the Wiki, its not clear at all how to read 2 bytes one after the other with the current component code, I sniff I might need to modify the component directly and suit to my needs.

I do not want to hook up a logic analyzer to the circuit yet, so looking for someone how's done this already.

I'm trying to achieve this workflow to read 2 bytes (one word):
Read_Word.jpg
Read_Word.jpg (105.4 KiB) Viewed 1682 times
Cheers
R

rchrislijs
Posts: 4
Joined: Wed Jan 10, 2024 11:01 am

Re: I2C How to Read 2 Bytes (ESP32)

Post by rchrislijs »

Hi there thanks, I've checked this already and have tried TOO many times without success, hence coming to the Forum for someone who has already done this :-)

From the different examples on the Wiki, its not clear at all how to read 2 bytes one after the other with the current component code, I sniff I might need to modify the component directly and suit to my needs.

I do not want to hook up a logic analyzer to the circuit yet, so looking for someone how's done this already.

I'm trying to achieve this workflow to read 2 bytes (one word):
Read_Word.jpg
Read_Word.jpg (105.4 KiB) Viewed 1551 times
Cheers
R

chipfryer27
Valued Contributor
Posts: 1607
Joined: Thu Dec 03, 2020 10:57 am
Has thanked: 357 times
Been thanked: 565 times

Re: I2C How to Read 2 Bytes (ESP32)

Post by chipfryer27 »

Hi

Been a while since I used I2C (especially on an ESP) so this is just generic of what I think needs doing.

You need to use Transactions with the ESP and you will need to create a Byte Array to receive the data.

Use the Transaction Initialise to set the address
Then Transaction Read to grab the data. In here you specify the number of bytes to read into your array
Then Transaction Uninitialise

Once you have the two bytes in your array you need to convert into a 16-bit number.

Say your array was Rx[] and you had an integer variable called Result

Result = Rx[0] will load the first byte (MSB) into variable Result. Shift that left by eight positions (Result = Result <<8) then add the second byte (LSB) (Result = Result + Rx[1]. Hopefully this should now give you your value.

Apologies in advance if I'm totally wrong, but it has been a while.

Regards

LeighM
Valued Contributor
Posts: 458
Joined: Mon Dec 07, 2020 1:00 pm
Has thanked: 82 times
Been thanked: 246 times

Re: I2C How to Read 2 Bytes (ESP32)

Post by LeighM »

Create a byte array of size two bytes, example Data[2]

Call Transaction Initialise(Address) with the Address of the device. This is in the range 0x40 to 0x5F.
(This only needs to be done once)

Set the Pointer Register value, example Data[0] = 0
Call Transaction_Write(Data, 1) to set the pointer address.
Call Transaction_Read(Data, 2) to get the two byte result, into the Data array.

Data[0] will contain the high byte, Data[1] the low byte, which you can combine to give your integer result.

EDIT: Ah, chipfryer27 just beat me to it :lol:

chipfryer27
Valued Contributor
Posts: 1607
Joined: Thu Dec 03, 2020 10:57 am
Has thanked: 357 times
Been thanked: 565 times

Re: I2C How to Read 2 Bytes (ESP32)

Post by chipfryer27 »

Hi

Follow LeighM, he is far, far more knowledgeable than I.....

Regards

rchrislijs
Posts: 4
Joined: Wed Jan 10, 2024 11:01 am

Re: I2C How to Read 2 Bytes (ESP32)

Post by rchrislijs »

Hi guys,

MANY THANKS all works fine now with either approach.

Cheers again!
R

Post Reply