Hi @iampete. I’m having a little trouble with i2c multi byte read.
I’m trying to write a lua driver for an MCP9808 temperature sensor. It’s very similar to the MCP9600 wich you already support.
The device is definitely streaming, the readings stay stable and when I blast it with a heat-gun they do go up, but the reading does not change as much as it should and I’m getting very unlikely values.
Page 33 of the data sheet specifies some other registers with expected values, the expected values are as follows:
address ->> expected value
0x05 ->> variable (this is where we expect to find the 16 bit temperature)
0x06 ->> 0x0054 (hex)
0x07 ->> 0x0400 (hex)
0x08 ->> 0x03 (hex)
my minimal script is as follows:
local MCP9808_ADDR_0 = 0x18
-- bus, and device address
local i2c_bus = i2c:get_device(0,MCP9808_ADDR_0)
i2c_bus:set_retries(10)
function update() -- this is the loop which periodically runs
i2c_bus:set_address(MCP9808_ADDR_0)
bytes = i2c_bus:read_registers(0x05,1)
gcs:send_text(0,"0x05: " .. tostring(bytes))
bytes = i2c_bus:read_registers(0x06,1)
gcs:send_text(0,"0x06: " .. tostring(bytes))
bytes = i2c_bus:read_registers(0x07,1)
gcs:send_text(0,"0x07: " .. tostring(bytes))
bytes = i2c_bus:read_registers(0x08,1)
gcs:send_text(0,"0x08: " .. tostring(bytes))
gcs:send_text(0,"----")
return update, 1000 -- reschedules the loop
end
return update() -- run immediately before starting to reschedule
With no read length specified I seem to reliably get the msb of the regisers and my print outs are as follows:
0x05: 193 (up to 200 if I cook the sensor)
0x06: 0 # expected 0x0054
0x07: 4 # expected 0x0400
0x08: 3 # expected 0x03
With a read lenght of 1, my results are as follows:
0x08: table: 0x2000BE18 (value alternates, which is not expected)
0x07: table: 0x2000B850
0x06: table: 0x2000B658
0x05: table: 0x20007998
----
0x08: table: 0x2000BF88
0x07: table: 0x2000B850
0x06: table: 0x2000B658
0x05: table: 0x20007998
With a read lenght of 2, my results are as follows:
0x08: table: 0x2000ADD0 (again the value alternates)
0x07: table: 0x2000A758
0x06: table: 0x2000A4D0
0x05: table: 0x200067E0
----
0x08: table: 0x2000ADF8
0x07: table: 0x2000A758
0x06: table: 0x2000A4D0
0x05: table: 0x200067E0
Since the values in 0x08
arn’t supposed to change and do when we add the second argument to i2c_bus:read_registers
, i’m assuming that something is going wrong.
Any ideas?
log_2_UnknownDate.zip (460.3 KB)