Răsfoiți Sursa

update: 直接把dht12的读取写到i2c模块得了

Wendal Chen 5 ani în urmă
părinte
comite
d26a099aee
2 a modificat fișierele cu 87 adăugiri și 29 ștergeri
  1. 32 29
      bsp/air302/demo/dht12/main.lua
  2. 55 0
      luat/modules/luat_lib_i2c.c

+ 32 - 29
bsp/air302/demo/dht12/main.lua

@@ -12,39 +12,42 @@ _G.sys = require("sys")
 -- 1. 调换SCL和SDA
 -- 2. 确保SCL和SDA均有上拉到VCC(3.3v), 1k~10k
 
--- 初始化并打开I2C操作DHT12
-local function read_dht12(id)
-    i2c.setup(id, i2c.SLOW)
-
-    local data = i2c.readReg(id, 0x5C, 0, 5)
-    if not data then
-        log.info("i2c", "read reg fail")
-        return
-    end
-
-    i2c.close(id)
-    log.info("DHT12 HEX data: ", data:toHex())
-    -- 分别是湿度整数,湿度小数,温度整数,温度湿度
-    local _, h_H, h_L, t_H, t_L,crc = pack.unpack(data, 'b5')
-    log.info("DHT12 data: ", h_H, h_L, t_H, t_L)
-    -- 计算校验和, 前4位的值相加应该等于最后一位的值
-    if (((h_H + h_L + t_H + t_L) & 0xFF )) ~= crc then
-        log.info("DHT12", "check crc fail")
-        return "0.0", "0.0"
-    end
-    -- 需要考虑温度低于0度的情况, t_L第0位是符号位
-    local t_L2 = tonumber(t_L)
-    if t_L2 > 127 then
-        return h_H .. ".".. h_L, "-" .. t_H .. "." .. tostring(t_L2 - 128)
-    else
-        return h_H .. ".".. h_L, t_H .. "." .. t_L
-    end
-end
+-- -- 初始化并打开I2C操作DHT12
+-- local function read_dht12(id)
+
+--     local data = i2c.readReg(id, 0x5C, 0, 5)
+--     if not data then
+--         log.info("i2c", "read reg fail")
+--         return
+--     end
+
+    
+--     log.info("DHT12 HEX data: ", data:toHex())
+--     -- 分别是湿度整数,湿度小数,温度整数,温度湿度
+--     local _, h_H, h_L, t_H, t_L,crc = pack.unpack(data, 'b5')
+--     log.info("DHT12 data: ", h_H, h_L, t_H, t_L)
+--     -- 计算校验和, 前4位的值相加应该等于最后一位的值
+--     if (((h_H + h_L + t_H + t_L) & 0xFF )) ~= crc then
+--         log.info("DHT12", "check crc fail")
+--         return "0.0", "0.0"
+--     end
+--     -- 需要考虑温度低于0度的情况, t_L第0位是符号位
+--     local t_L2 = tonumber(t_L)
+--     if t_L2 > 127 then
+--         return h_H .. ".".. h_L, "-" .. t_H .. "." .. tostring(t_L2 - 128)
+--     else
+--         return h_H .. ".".. h_L, t_H .. "." .. t_L
+--     end
+-- end
 
 sys.taskInit(function()
+    local id = 0
     while 1 do
         sys.wait(5000) -- 5秒读取一次
-        log.info("dht12", read_dht12(0))
+        i2c.setup(id, i2c.SLOW)
+        --log.info("dht12", read_dht12(0)) -- 传统方式读取,请取消read_dht12方法的注释
+        log.info("dht12", i2c.readDHT12(id))
+        i2c.close(id)
     end
 end)
 

+ 55 - 0
luat/modules/luat_lib_i2c.c

@@ -177,6 +177,58 @@ static int l_i2c_close(lua_State *L) {
     return 0;
 }
 
+/*
+从i2c总线读取DHT12的温湿度数据
+@api i2c.readDHT(id)
+@int 设备id, 例如i2c1的id为1, i2c2的id为2
+@int DHT12的设备地址,默认0x5C
+@return boolean 读取成功返回true,否则返回false
+@return int 湿度值,单位0.1%, 例如 591 代表 59.1%
+@return int 温度值,单位0.1摄氏度, 例如 292 代表 29.2摄氏度
+@usage
+-- 从i2c0读取DHT12
+i2c.setup(0)
+local re, H, T = i2c.readDHT(0)
+if re then
+    log.info("dht12", H, T)
+end
+*/
+static int l_i2c_readDHT12(lua_State *L) {
+    int id = luaL_checkinteger(L, 1);
+    int addr = luaL_optinteger(L, 2, 0x5C);
+    char buff[5] = {0};
+    char temp = 0x00;
+    int result = luat_i2c_send(id, addr, &temp, 1);
+    result = luat_i2c_recv(id, addr, buff, 5);
+    if (result) {
+        lua_pushboolean(L, 0);
+        return 1;
+    }
+    else {
+        if (buff[0] == 0 && buff[1] == 0 && buff[2] == 0 && buff[3] == 0 && buff[4] == 0) {
+            LLOGD("DHT12 DATA emtry");
+            lua_pushboolean(L, 0);
+            return 1;
+        }
+        // 检查crc值
+        LLOGD("DHT12 DATA %02X%02X%02X%02X%02X", buff[0], buff[1], buff[2], buff[3], buff[4]);
+        uint8_t crc_act = (uint8_t)buff[0] + (uint8_t)buff[1] + (uint8_t)buff[2] + (uint8_t)buff [3];
+        uint8_t crc_exp = (uint8_t)buff[4];
+        if (crc_act != crc_exp) {
+            LLOGD("DATA12 DATA crc not ok");
+            lua_pushboolean(L, 0);
+            return 1;
+        }
+        lua_pushboolean(L, 1);
+        lua_pushinteger(L, (uint8_t)buff[0] * 10 + (uint8_t)buff[1]);
+        if (((uint8_t)buff[2]) > 127)
+            lua_pushinteger(L, ((uint8_t)buff[2] - 128) * -10 + (uint8_t)buff[3]);
+        else
+            lua_pushinteger(L, (uint8_t)buff[2] * 10 + (uint8_t)buff[3]);
+        return 3;
+    }
+}
+
 #include "rotable.h"
 static const rotable_Reg reg_i2c[] =
 {
@@ -187,6 +239,9 @@ static const rotable_Reg reg_i2c[] =
     { "writeReg", l_i2c_write_reg, 0},
     { "readReg", l_i2c_read_reg, 0},
     { "close", l_i2c_close, 0},
+
+    { "readDHT12", l_i2c_readDHT12, 0},
+
     { "FAST",  NULL, 1},
     { "SLOW",  NULL, 0},
 	{ NULL, NULL, 0}