From de053cf47966c20c91e1734a6a38de87a9956214 Mon Sep 17 00:00:00 2001 From: Levente Tamas Date: Sun, 17 May 2015 11:57:43 +0300 Subject: [PATCH] Add support for lm92 temperature sensor Added LUA module for the lm92 +-0.33C 12bit+sign temperature sensor. --- lua_modules/lm92/README.md | 98 ++++++++++++++++++++++++++++++++++++++ lua_modules/lm92/lm92.lua | 94 ++++++++++++++++++++++++++++++++++++ 2 files changed, 192 insertions(+) create mode 100644 lua_modules/lm92/README.md create mode 100644 lua_modules/lm92/lm92.lua diff --git a/lua_modules/lm92/README.md b/lua_modules/lm92/README.md new file mode 100644 index 00000000..261e1ebe --- /dev/null +++ b/lua_modules/lm92/README.md @@ -0,0 +1,98 @@ +# 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: +- getting the temperature +- entering the chip's to shutdown mode (350uA -> 5uA power consumption) +- waking up the chip from shutdown + +##Require +```lua +LM92 = require("lm92") +``` +## Release +```lua +LM92 = nil +package.loaded["lm92"]=nil +``` + +##init() +####Description +Setting the i2c pins and address for lm92. + +####Syntax +init(sda, scl, address) + +####Parameters +sda: 1~12, IO index.
+scl: 1~12, IO index.
+address: 0x48~0x4b, i2c address (depends on tha A0~A1 pins) +####Returns +nil + +####Example +```lua +LM92 = require("lm92") +gpio0 = 3 +gpio2 = 4 +sda = gpio0 +scl = gpio2 +addr = 0x48 +LM92.init(sda, scl,addr) +``` +##getTemperature() +####Description +Returns the temperature register's content. + +####Syntax +getTemperature() + +####Parameters +- + +####Returns +Temperature in degree Celsius. + +####Example +```lua +t = LM92.getTemperature() +print("Got temperature: "..t.." C") +``` + +##wakeup() +####Description +Makes the chip exit the low power shutdown mode. + +####Syntax +wakeup() + +####Parameters +- + +####Returns +- + +####Example +```lua +LM92.wakeup() +tmr.delay( 1 * 1000 * 1000 ) +``` + +##shutdown() +####Description +Makes the chip enter the low power shutdown mode. + +####Syntax +shutdown() + +####Parameters +- + +####Returns +- + +####Example +```lua +LM92.shutdown() +``` +#### 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 new file mode 100644 index 00000000..fcec7098 --- /dev/null +++ b/lua_modules/lm92/lm92.lua @@ -0,0 +1,94 @@ +-- ****************************************************** +-- LM92 module for ESP8266 with nodeMCU +-- +-- Written by Levente Tamas +-- +-- GNU LGPL, see https://www.gnu.org/copyleft/lesser.html +-- ****************************************************** + +-- Module Bits +local moduleName = ... +local M = {} +_G[moduleName] = M + +-- Default ID +local id = 0 + +-- Local vars +local address = 0 + +-- read regs for len number of bytes +-- return table with data +local function read_reg(reg_addr, len) + local ret={} + local c + local x + i2c.start(id) + i2c.address(id, address ,i2c.TRANSMITTER) + i2c.write(id,reg_addr) + i2c.stop(id) + i2c.start(id) + i2c.address(id, address,i2c.RECEIVER) + c=i2c.read(id,len) + for x=1,len,1 do + tc=string.byte(c,x) + table.insert(ret,tc) + end + i2c.stop(id) + return ret +end + +--write reg with data table +local function write_reg(reg_addr, data) + i2c.start(id) + i2c.address(id, address, i2c.TRANSMITTER) + i2c.write(id, reg_addr) + i2c.write(id, data) + i2c.stop(id) +end + +-- initialize i2c +-- d: sda +-- c: scl +-- a: i2c addr 0x48|A1<<1|A0 (A0-A1: chip pins) +function M.init(d,c,a) +if (d ~= nil) and (c ~= nil) and (d >= 0) and (d <= 11) and (c >= 0) and ( c <= 11) and (d ~= l) and (a ~= nil) and (a >= 0x48) and (a <= 0x4b ) then + sda = d + scl = c + address = a + i2c.start(id) + res = i2c.address(id, address, i2c.TRANSMITTER) --verify that the address is valid + i2c.stop(id) + if (res == false) then + print("device not found") + return nil + end + else + print("i2c configuration failed") return nil + end + i2c.setup(id,sda,scl,i2c.SLOW) +end + +-- Return the temperature data +function M.getTemperature() + local temperature + local tmp=read_reg(0x00,2) --read 2 bytes from the temperature 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 + +-- Put the LM92 into shutdown mode +function M.shutdown() + write_reg(0x01,0x01) +end + +-- Bring the LM92 out of shutdown mode +function M.wakeup() + write_reg(0x01,0x00) +end + + +return M \ No newline at end of file