• 欢迎大家分享资料!前往留言板评论即可!

SHT20 驱动 IO口模拟IIC

合宙 模组资料网 2年前 (2021-05-15) 323次浏览 0个评论 扫描二维码

— 模块功能:SHT20驱动代码
— @module 模拟IIC  用于传感器导线加长
— @author ZWP0273
— @license MIT
— @copyright openLuat
— @release 2021.3.23
module(, package.seeall)
require “pins”
require “utils”
require “GLV”
require“pm”
pm.wake(“SHT2XTask”)
local ACK = 1
local NACK = 0
local TE = 1
local RH = 0
local sht20_clk_1 = pins.setup(pio.P0_220)
local sht20_data_1 = pins.setup(pio.P0_230)
local get_sht20_data_1 = pins.setup(pio.P0_23)
local sht20_clk_2 = pins.setup(pio.P0_140)
local sht20_data_2 = pins.setup(pio.P0_150)
local get_sht20_data_2 = pins.setup(pio.P0_15)

local function IIc_Start(num)
if num  1 then
sht20_clk_1(1)
sht20_data_1(1)
        sys.wait(1)
sht20_data_1(0)
        sys.wait(1)
sht20_clk_1(0)
        sys.wait(1)
else
sht20_clk_2(1)
sht20_data_2(1)
        sys.wait(1)
sht20_data_2(0)
        sys.wait(1)
sht20_clk_2(0)
        sys.wait(1)
end
end
local function IIc_Stop(num)
if num  
1 then
sht20_clk_1(0)
sht20_data_1(0)
        sys.wait(1)
sht20_clk_1(1)
        sys.wait(1)
sht20_data_1(1)
        sys.wait(1)
else
sht20_clk_2(0)
sht20_data_2(0)
        sys.wait(1)
sht20_clk_2(1)
        sys.wait(1)
sht20_data_2(1)
        sys.wait(1)
end
end
local function IIc_Ack(num)
if num  1 then
sht20_clk_1(0)
sht20_data_1(0)
        sys.wait(1)
sht20_clk_1(1)
        sys.wait(1)
sht20_clk_1(0)
        sys.wait(1)
else
sht20_clk_2(0)
sht20_data_2(0)
        sys.wait(1)
sht20_clk_2(1)
        sys.wait(1)
sht20_clk_2(0)
        sys.wait(1)
end
end
local function IIc_Nack(num)
if num  
1 then
sht20_clk_1(0)
sht20_data_1(1)
        sys.wait(1)
sht20_clk_1(1)
        sys.wait(1)
sht20_clk_1(0)
        sys.wait(1)
else
sht20_clk_2(0)
sht20_data_2(1)
        sys.wait(1)
sht20_clk_2(1)
        sys.wait(1)
sht20_clk_2(0)
        sys.wait(1)

end
end
local function IIc_wait_Ack(num
local err = 0
if num  1 then
sht20_clk_1(1)
sht20_data_1(1)
        sys.wait(1)
while true do
            sys.wait(1)
if get_sht20_data_1()  
true then err = err + 1 else break end
if err > 10 then
IIc_Stop(num)
return false
end
end
return true
else
sht20_clk_2(1)
sht20_data_2(1)
        sys.wait(1)
while true do
            sys.wait(1)
if get_sht20_data_2()  true then err = err + 1 else break end
if err > 10 then
IIc_Stop(num)
return false
end
end
return true
end
end
local function IIc_send_Byte(num,Byte
local i
if num  
1 then
sht20_clk_1(0)
for i = 181 do
if bit.isset(Byte,8 – i)  true then
sht20_data_1(1)
else
sht20_data_1(0)
end
            sys.wait(1)
sht20_clk_1(1)
            sys.wait(1)
sht20_clk_1(0)
            sys.wait(1)
end
IIc_Ack(num)
else
sht20_clk_2(0)
for i = 181 do
if bit.isset(Byte,8 – i)  
true then
sht20_data_2(1)
else
sht20_data_2(0)
end
            sys.wait(1)
sht20_clk_2(1)
            sys.wait(1)
sht20_clk_2(0)
            sys.wait(1)
end
IIc_Ack(num)
end
end
local function IIc_Read_Byte(num,ack)
local i 
local receive = 0
if num  1 then
get_sht20_data_1()
for i = 181 do
sht20_clk_1(0)
            sys.wait(1)
sht20_clk_1(1)
            sys.wait(1)
            receive =bit.lshift(receive,1)
if get_sht20_data_1()  
1 then receive = receive + 1 end
end
else
get_sht20_data_2()
for i = 181 do
sht20_clk_2(0)
            sys.wait(1)
sht20_clk_2(1)
            sys.wait(1)
            receive =bit.lshift(receive,1)
if get_sht20_data_2()  1 then receive = receive + 1 end
end
end
if ack  ACK 
then
IIc_Ack(num)
else
IIc_Nack(num)
end
return receive
end
local function SHT2X_get_TERHnum ,TERH)

local rc = false
local Table = {}
IIc_Start(num)
IIc_send_Byte(num,0x80)   
IIc_send_Byte(num,0xFE— 复位
IIc_Stop(num)
    sys.wait(85)
IIc_Start(num)
IIc_send_Byte(num,0x80)
if TERH  TE then
IIc_send_Byte(num,0xF3— 读取温度
else
IIc_send_Byte(num,0xF5— 读取湿度
end
IIc_Stop(num)
    sys.wait(85)
IIc_Start(num)
IIc_send_Byte(num,0x81)
    Table[1] = IIc_Read_Byte(num,ACK)
    Table[2] = IIc_Read_Byte(num,ACK)
    Table[3] = IIc_Read_Byte(num,NACK)
IIc_Stop(num)
if TERH  TE 
then
local wendu = 175.72  (Table[1256 + Table[2])/65536 –46.85
        log.info(“SHT2X”,“Table”,num,string.format(“%x,%x,%x”,Table[1],Table[2],Table[3]),wendu)
else
local shidu = 125  (Table[1256 + Table[2])/65536 –6
        log.info(“SHT2X”,“Table”,num,string.format(“%x,%x,%x”,Table[1],Table[2],Table[3]),shidu)
end
return rc
end

sys.taskInit(function()
    sys.wait(2000)
while true do

SHT2X_get_TERH(1,TE)
SHT2X_get_TERH(1,RH)
SHT2X_get_TERH(2,TE)
SHT2X_get_TERH(2,RH)
        sys.wait(1000)
end

end)


转载请注明原文链接:SHT20 驱动 IO口模拟IIC
喜欢 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址