add: 添加新的阿里云物联网库-aliyun2库,及搭配的demo, 逐步完善中
[LuatOS.git] / script / libs / pca9555.lua
blobad1a1372c9cb967014da6b0dc966ff03d3b0d8c6
1 --[[
3 @module pca9555
4 @summary pca9555 IO扩展
5 @version 1.0
6 @date 2024/1/15
7 @author 小牛
8 @usage
9 i2c.setup(0, i2c.FAST)
10 pca9555.setup(0, 0x20, 0x0000)
11 for i=0,7 do
12 pca9555.pin(i,0)
13 end
16 local pca9555 = {}
18 local PCA9555_i2cid=0
19 local PCA9555_addr=0x20
20 local PCA9555_mode=0x0000 --IO全为输出
21 local PCA9555_IOstart=0xffff
23 -- pca9555 registers define
24 local PCA9555_REG_IN0 = 0x00 --输入端口寄存器0 默认值由外部逻辑决定 只可读不可写
25 local PCA9555_REG_IN1 = 0x01 --输入端口寄存器1 默认值由外部逻辑决定 只可读不可写
26 local PCA9555_REG_OUT0 = 0x02 --输出端口寄存器0 默认值为1 可读可写
27 local PCA9555_REG_OUT1 = 0x03 --输入端口寄存器1 默认值为1 可读可写
28 local PCA9555_REG_POL0 = 0x04 --极性反转寄存器0 默认值为0 可读可写
29 local PCA9555_REG_POL1 = 0x05 --极性反转寄存器1 默认值为0 可读可写
30 local PCA9555_REG0_CFG0 = 0x06 --配置端口寄存器0 默认为1 可读可写
31 local PCA9555_REG1_CFG1 = 0x07 --配置端口寄存器1 默认为1 可读可写
33 --[[
34 配置寄存器说明 1为输入模式 0为输出模式
35 Config registers
36 ---------------------------------------------------------------------------------------
37 Config prot 0 register
38 Bit 7 6 5 4 3 2 1
39 Symbol C0.7 C0.6 C0.5 C0.4 C0.3 C0.2 C0.0
40 Default 1 1 1 1 1 1 1
41 ---------------------------------------------------------------------------------------
42 Config prot 1 register
43 Bit 7 6 5 4 3 2 1
44 Symbol C1.7 C1.6 C1.5 C1.4 C1.3 C1.2 C1.0
45 Default 1 1 1 1 1 1 1
46 ---------------------------------------------------------------------------------------
49 --[[
50 pca9555初始化
51 @api pca955.setup(i2cid, addr,mode)
52 @number i2cid 所在的i2c总线
53 @number addr pca9555设备的i2c地址
54 @number mode 配置输入输出模式 --0xffff 高八位配置IO 17-10 低八位配置IO 7-0
55 @return 初始化失败,返回nil
56 @usage
57 i2c.setup(0, i2c.FAST)
58 pca9555.setup(0,0x20,0xffff)
60 function pca9555.setup(i2cid,addr,mode)
61 PCA9555_i2cid=i2cid
62 PCA9555_addr=addr
63 PCA9555_mode=mode
64 readEncoder()
65 local tmp=i2c.readReg(PCA9555_i2cid, PCA9555_addr, 0x02, 2)
66 if tmp==nil then
67 log.info("PCA9555初始化失败")
68 return nil
69 end
70 if PCA9555_mode~=nil then
71 i2c.writeReg(PCA9555_i2cid,PCA9555_addr,0x06,pack.pack("<H",PCA9555_mode))
72 i2c.writeReg(PCA9555_i2cid,PCA9555_addr,0x02,pack.pack("<H",PCA9555_IOstart))
73 else
74 local recvData=i2c.readReg(PCA9555_i2cid, PCA9555_addr,0x00,2)
75 _,recvData=pack.unpack(recvData,"<H")
76 log.info("pca9555默认为完全输入模式",recvData)
77 end
78 end
80 --[[
81 pca9555 pin控制
82 @api pca9555.pin(pin,val)
83 @number pin 引脚 0-7 10-17
84 @number val 高/低电平 1/0
85 @return number 如果val未填,则读取pin的输出电平
86 @usage
87 for i=0,7 do
88 pca9555.pin(i,0)
89 end
91 function pca9555.pin(pin,val)
92 if pin==nil then
93 log.info("请选择引脚")
94 elseif pin then
95 if val==nil then
96 if pin>9 then
97 pin=pin-2
98 end
99 local tmp=i2c.readReg(PCA9555_i2cid, PCA9555_addr,0x00,2)
100 _,tmp=pack.unpack(tmp,"<H")
101 val=(tmp&(1<<pin))>>pin
102 return val
103 elseif val==0 then
104 if pin>9 then
105 pin=pin-2
107 PCA9555_IOstart=PCA9555_IOstart&(~(1<<pin))
108 i2c.writeReg(PCA9555_i2cid,PCA9555_addr,0x02,pack.pack("<H",PCA9555_IOstart))
109 else
110 if pin>9 then
111 pin=pin-2
113 PCA9555_IOstart=PCA9555_IOstart|(1<<pin)
114 i2c.writeReg(PCA9555_i2cid,PCA9555_addr,0x02,pack.pack("<H",PCA9555_IOstart))
119 return pca9555