10 pca9555.setup(0, 0x20, 0x0000)
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 可读可写
36 ---------------------------------------------------------------------------------------
37 Config prot 0 register
39 Symbol C0.7 C0.6 C0.5 C0.4 C0.3 C0.2 C0.0
41 ---------------------------------------------------------------------------------------
42 Config prot 1 register
44 Symbol C1.7 C1.6 C1.5 C1.4 C1.3 C1.2 C1.0
46 ---------------------------------------------------------------------------------------
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
57 i2c.setup(0, i2c.FAST)
58 pca9555.setup(0,0x20,0xffff)
60 function pca9555
.setup(i2cid
,addr
,mode
)
65 local tmp
=i2c
.readReg(PCA9555_i2cid
, PCA9555_addr
, 0x02, 2)
67 log.info("PCA9555初始化失败")
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
))
74 local recvData
=i2c
.readReg(PCA9555_i2cid
, PCA9555_addr
,0x00,2)
75 _
,recvData
=pack
.unpack(recvData
,"<H")
76 log.info("pca9555默认为完全输入模式",recvData
)
82 @api pca9555.pin(pin,val)
83 @number pin 引脚 0-7 10-17
85 @return number 如果val未填,则读取pin的输出电平
91 function pca9555
.pin(pin
,val
)
99 local tmp
=i2c
.readReg(PCA9555_i2cid
, PCA9555_addr
,0x00,2)
100 _
,tmp
=pack
.unpack(tmp
,"<H")
101 val
=(tmp
&(1<<pin
))>>pin
107 PCA9555_IOstart
=PCA9555_IOstart
&(~(1<<pin
))
108 i2c
.writeReg(PCA9555_i2cid
,PCA9555_addr
,0x02,pack
.pack("<H",PCA9555_IOstart
))
113 PCA9555_IOstart
=PCA9555_IOstart|
(1<<pin
)
114 i2c
.writeReg(PCA9555_i2cid
,PCA9555_addr
,0x02,pack
.pack("<H",PCA9555_IOstart
))