Project

General

Profile

Resolved: JNZ, Forth and temperature sensor tmp102

Added by Rolf 4 months ago

Got problems reading out the tmp102 on JNZ with the forth-i2c library. It should be easy, because the sensor just expects its bus-address and a register-address to transfer the temperatur in a 2 bytes word. Did anyone already do this successfully?


Replies (11)

RE: JNZ, Forth and temperature sensor tmp102 - Added by jcw 4 months ago

The h/w I2C driver for L052 is not quite right yet, I'm afraid.

Could you try ../flib/any/i2c-bb.fs instead? It's a compatible bit-banged implementation.

RE: JNZ, Forth and temperature sensor tmp102 - Added by Rolf 4 months ago

Thank you for the advice, but the result is the same. Maybe the mistake is on my side. The output shows unreasonable values and is changing rapidly. Thats what I did: After setting the GPIOs for powersupply (as in the BE280 example) I see the device on the bus (i2c.), address $48, perform "i2c-init" and then:

$48 i2c-addr 0 >i2c 2 i2c-xfer drop i2c>h .

Is this appropriate? The tmp102 expects its bus-address ($48) and afterwords the temperature register adress (0).

RE: JNZ, Forth and temperature sensor tmp102 - Added by jcw 4 months ago

$48 i2c-addr 0 >i2c 2 i2c-xfer drop i2c>h .

Yep, looks good to me. What result do you get? Maybe the byte order is different?

RE: JNZ, Forth and temperature sensor tmp102 - Added by Rolf 4 months ago

Thank you, it's already very helpful to now that the problem is not in this line of forth code. I flipped the byte order, that's not the problem. Here some output:

$48 i2c-addr 0 >i2c 2 i2c-xfer drop i2c>h . 36887 ok. (calc. temp: 104.5, fl. byte order: -120.6)
$48 i2c-addr 0 >i2c 2 i2c-xfer drop i2c>h . 20504 ok. (calc. temp: 5.0, ,fl. byte order: 4.0)
$48 i2c-addr 0 >i2c 2 i2c-xfer drop i2c>h . 24600 ok. (calc. temp: 70.0, fl. byte order: 0.2)
$48 i2c-addr 0 >i2c 2 i2c-xfer drop i2c>h . 28696 ok. (calc, temp: -121.4, fl. byte order: -105.5)

The output changes drastically and at random it seems. When I touch the sensor, the output is changing as well, but still unreasonable. I exchanged the sensor, no effect. - My first purpose was to learn how to handle the i2c-bus. I shall try with something else, perhaps that shall give a hint what's wrong with the tmp102.

RE: JNZ, Forth and temperature sensor tmp102 - Added by jcw 4 months ago

Do you have pull-up resistors to 3.3V of at least 10 kΩ on both I2C lines?

Update - oops, "at least" should really be at most. Resistors should be 2.2 .. 10 kΩ (lower if the wires are >50 cm)

RE: JNZ, Forth and temperature sensor tmp102 - Added by Rolf 4 months ago

No. Propably that's it! I shall try. - OK, I did: Rapid changing values seemed to indicate an electrical problem on the bus, reason for pull up resistors. But, shame on me, it was just a stupid mistake calculating the temperature, still aside with a C-program. Now it works, no need for pull up resistors. - Please find attached the tmp102 forth code for your example library.

tmp102.fs (765 Bytes)

tmp102.fs (809 Bytes)

RE: Resolved: JNZ, Forth and temperature sensor tmp102 - Added by Rolf 4 months ago

Resolved, details see last posting.

RE: Resolved: JNZ, Forth and temperature sensor tmp102 - Added by jcw 4 months ago

Congrats, I'm happy to hear that it's working now.

I've been testing the hardware I2C driver a bit more this morning, it appears to be robust after all - millions of packets have been exchanged over I2C without problems.

I've adapted and added your code to the embello repository (flib/i2c/tmp102.fs) - and have started thinking about how to make it much simpler for us all to share and exchange such drivers and other useful code snippets. We could of course use GitHub's pull-request model, but that's a bit tedious for what will often be just a few dozen lines of Forth code.

Maybe "gists" on GitHub would work, but that's quite GitHub-specific, and it's not easy to clone such collections.
Other options could be this Redmine site, or a wiki on GitHub - but they too make it hard to grab a full snapshot.

Anyway - probably better to start a new topic if we want to figure this out :)

RE: Resolved: JNZ, Forth and temperature sensor tmp102 - Added by Rolf 4 months ago

There was still a bug in tmp102.fs (calculation negative temperature values). The new version I put on the original place.

RE: Resolved: JNZ, Forth and temperature sensor tmp102 - Added by jcw 4 months ago

This code should also work, it's a bit simpler, I think:

: tmp102 ( -- i )  \ returns temp in steps of 0.1 °C
  TMP.ADDR i2c-addr  0 >i2c  2 i2c-xfer drop  \ sensor read out
  i2c> 24 lshift 16 arshift  \ sign extend
  i2c> or \ bits 31..4 now have the temperature, signed
  5 + 10 * 8 arshift  \ convert to rounded tenths of degrees centrigrade
;

RE: Resolved: JNZ, Forth and temperature sensor tmp102 - Added by Rolf 4 months ago

Yes, it's working. Thank you!

    (1-11/11)