From 4c037bfeace37687e4feb23662cea2c3f0516bf0 Mon Sep 17 00:00:00 2001 From: elgarbe Date: Wed, 6 Jan 2016 15:05:32 -0300 Subject: [PATCH] Add support for Temperature Comparison register set/get functions --- lua_modules/lm92/README.md | 177 +++++++++++++++++++++++++++++++++++++ lua_modules/lm92/lm92.lua | 104 +++++++++++++++++++++- 2 files changed, 280 insertions(+), 1 deletion(-) diff --git a/lua_modules/lm92/README.md b/lua_modules/lm92/README.md index 261e1ebe..99fb62e2 100644 --- a/lua_modules/lm92/README.md +++ b/lua_modules/lm92/README.md @@ -1,6 +1,7 @@ # LM92 module This module adds basic support for the LM92 +-0.33C 12bit+sign temperature sensor. More details in the [datasheet](http://www.ti.com/lit/ds/symlink/lm92.pdf). Works: +- 06/01/2016: Add get/set comparison registers. Thyst, Tlow, Thigh and Tcrit - getting the temperature - entering the chip's to shutdown mode (350uA -> 5uA power consumption) - waking up the chip from shutdown @@ -94,5 +95,181 @@ shutdown() ```lua LM92.shutdown() ``` + +##setThyst() +####Description +Set hysteresis Temperature. + +####Syntax +setThyst(data_wr) + +####Parameters +data_wr: 130~-55 ºC, hysteresis Temperature + +####Returns +nil + +####Example +```lua +LM92.setThyst(3) +``` + +##setTcrit() +####Description +Set Critical Temperature. + +####Syntax +setTcrit(data_wr) + +####Parameters +data_wr: 130~-55 ºC, Critical Temperature + +####Returns +nil + +####Example +```lua +LM92.setTcrit(100.625) +``` + +##setTlow() +####Description +Set Low Window Temperature. + +####Syntax +setTlow(data_wr) + +####Parameters +data_wr: 130~-55 ºC, Low Window Temperature + +####Returns +nil + +####Example +```lua +LM92.setTlow(32.25) +``` +##setThigh() +####Description +Set High Window Temperature. + +####Syntax +setThigh(data_wr) + +####Parameters +data_wr: 130~-55 ºC, High Window Temperature + +####Returns +nil + +####Example +```lua +LM92.setThigh(27.5) +``` + +##getThyst() +####Description +Get hysteresis Temperature. + +####Syntax +getThyst() + +####Parameters +-- + +####Returns +Hysteresis Temperature in degree Celsius. + +####Example +```lua +t = LM92.getThyst() +print("Got hysteresis temperature: "..t.." C") +``` + +##getTcrit() +####Description +Get Critical Temperature. + +####Syntax +getTcrit() + +####Parameters +-- + +####Returns +Critical Temperature in degree Celsius. + +####Example +```lua +t = LM92.getTcrit() +print("Got Critical temperature: "..t.." C") +``` + +##getTlow() +####Description +Get Low Window Temperature. + +####Syntax +getTlow() + +####Parameters +-- + +####Returns +Low Window Temperature in degree Celsius. + +####Example +```lua +t = LM92.getTlow() +print("Got Low Window temperature: "..t.." C") +``` + +##getThigh() +####Description +Get High Window Temperature. + +####Syntax +getThigh() + +####Parameters +-- + +####Returns +High Window Temperature in degree Celsius. + +####Example +```lua +t = LM92.getThigh() +print("Got High Window temperature: "..t.." C") +``` + +##Full example +--node.compile("lm92.lua") +LM92 = require("lm92") +gpio0 = 3 +gpio2 = 4 +sda = gpio0 +scl = gpio2 +addr = 0x48 +LM92.init(sda, scl,addr) + +t = LM92.getTemperature() +print("Got temperature: "..t.." C") + +--Seting comparison temperatures +LM92.setThyst(3) +LM92.setTcrit(40.75) +LM92.setTlow(28.5) +LM92.setThigh(31.625) + +t = LM92.getThyst() +print("Got hyster: "..t.." C") +t = LM92.getTcrit() +print("Got Crit: "..t.." C") +t = LM92.getTlow() +print("Got Low: "..t.." C") +t = LM92.getThigh() +print("Got High: "..t.." C") + #### TODO: - add full support of the features, including interrupt and critical alert support diff --git a/lua_modules/lm92/lm92.lua b/lua_modules/lm92/lm92.lua index fcec7098..0d3daeb5 100644 --- a/lua_modules/lm92/lm92.lua +++ b/lua_modules/lm92/lm92.lua @@ -3,6 +3,8 @@ -- -- Written by Levente Tamas -- +-- modify by Garberoglio Leonardo +-- -- GNU LGPL, see https://www.gnu.org/copyleft/lesser.html -- ****************************************************** @@ -47,6 +49,15 @@ local function write_reg(reg_addr, data) i2c.stop(id) end +--write comparison reg with 2 bytes +local function write_comp_reg(reg_addr, msb, lsb) + i2c.start(id) + i2c.address(id, address, i2c.TRANSMITTER) + i2c.write(id, reg_addr) + i2c.write(id, msb) + i2c.write(id, lsb) + i2c.stop(id) +end -- initialize i2c -- d: sda -- c: scl @@ -90,5 +101,96 @@ function M.wakeup() write_reg(0x01,0x00) end +-- Write the LM92 Thyst SET POINT +function M.setThyst(data_wr) + local tmp = data_wr / 0.0625 + if (tmp>=0x1000) then + tmp= tmp-0x2000 --convert the two's complement + end + tmp = bit.lshift(tmp,3) + local lsb = bit.band(tmp, 0x00FF) + tmp = bit.rshift(bit.band(tmp, 0xFF00),8) + write_comp_reg(0x02,tmp, lsb) +end -return M \ No newline at end of file +-- Write the LM92 T_crit SET POINT +function M.setTcrit(data_wr) + local tmp = data_wr / 0.0625 + if (tmp>=0x1000) then + tmp= tmp-0x2000 --convert the two's complement + end + tmp = bit.lshift(tmp,3) + local lsb = bit.band(tmp, 0x00FF) + tmp = bit.rshift(bit.band(tmp, 0xFF00),8) + write_comp_reg(0x03,tmp, lsb) +end + +-- Write the LM92 Tlow SET POINT +function M.setTlow(data_wr) + local tmp = data_wr / 0.0625 + if (tmp>=0x1000) then + tmp= tmp-0x2000 --convert the two's complement + end + tmp = bit.lshift(tmp,3) + local lsb = bit.band(tmp, 0x00FF) + tmp = bit.rshift(bit.band(tmp, 0xFF00),8) + write_comp_reg(0x04,tmp, lsb) +end + +-- Write the LM92 T_high SET POINT +function M.setThigh(data_wr) + local tmp = data_wr / 0.0625 + if (tmp>=0x1000) then + tmp= tmp-0x2000 --convert the two's complement + end + tmp = bit.lshift(tmp,3) + local lsb = bit.band(tmp, 0x00FF) + tmp = bit.rshift(bit.band(tmp, 0xFF00),8) + write_comp_reg(0x05,tmp, lsb) +end + +-- Read the LM92 Thyst SET POINT +function M.getThyst() + local temperature + local tmp=read_reg(0x02,2) --read 2 bytes from the Hysteresis register + temperature=bit.rshift(tmp[1]*256+tmp[2],3) --lower 3 bits are status bits + if (temperature>=0x1000) then + temperature= temperature-0x2000 --convert the two's complement + end + return temperature * 0.0625 +end + +-- Read the LM92 T_crit SET POINT +function M.getTcrit() + local temperature + local tmp=read_reg(0x03,2) --read 2 bytes from the T Critical register + temperature=bit.rshift(tmp[1]*256+tmp[2],3) --lower 3 bits are status bits + if (temperature>=0x1000) then + temperature= temperature-0x2000 --convert the two's complement + end + return temperature * 0.0625 +end + +-- Read the LM92 Tlow SET POINT +function M.getTlow() + local temperature + local tmp=read_reg(0x04,2) --read 2 bytes from the T Low register + temperature=bit.rshift(tmp[1]*256+tmp[2],3) --lower 3 bits are status bits + if (temperature>=0x1000) then + temperature= temperature-0x2000 --convert the two's complement + end + return temperature * 0.0625 +end + +-- Read the LM92 T_high SET POINT +function M.getThigh() + local temperature + local tmp=read_reg(0x05,2) --read 2 bytes from the T High register + temperature=bit.rshift(tmp[1]*256+tmp[2],3) --lower 3 bits are status bits + if (temperature>=0x1000) then + temperature= temperature-0x2000 --convert the two's complement + end + return temperature * 0.0625 +end + +return M