5 可以用8种通道方式,4种差分,4种in0-in3
12 sys.taskInit(function ()
13 i2c.setup(i2cid, i2c_speed)
14 ads1115plus.Setup(i2cid) -- 一次初始化
17 log.info("ads1115:",ads1115plus.task_read(5))
27 _G
.sys
= require ("sys")
34 ads1115plus
= {} --定义存放程序的TABLE 全局变量
37 local i2cslaveaddr
= 0x48
42 @api ads1115plus.Setup(i2c_id)
46 i2c.setup(0, i2c_speed)
50 function ads1115plus
.Setup(i2c_id
)
52 i2c
.writeReg(i2cid
, i2cslaveaddr
, 0x02, string.char(0x00, 0x00)) --给低阈值寄存器写进去值 A1高八位 A2 低八位
53 i2c
.writeReg(i2cid
, i2cslaveaddr
, 0x03, string.char(0xff, 0xff)) --给高阈值寄存器写进去值 A1高八位 A2 低八位
54 log.info("ADS1115", "init_ok")
62 @api ads1115plus.task_recv(MU1,FSR)
63 @number MUX:通道选择 0-7 MUX=0 AIN0 AIN1 MUX=7 AIN3 GND
65 | | 000 : AINP = AIN0 and AINN = AIN1 (default)
66 | | 001 : AINP = AIN0 and AINN = AIN3
67 | | 010 : AINP = AIN1 and AINN = AIN3
68 | | 011 : AINP = AIN2 and AINN = AIN3
69 | | 100 : AINP = AIN0 and AINN = GND
70 | | 101 : AINP = AIN1 and AINN = GND
71 | | 110 : AINP = AIN2 and AINN = GND
72 | | 111 : AINP = AIN3 and AINN = GND
75 @number FSR:量程选择 0-7 2 2.048v
78 | | 000 : FSR = В±6.144 V
79 | | 001 : FSR = В±4.096 V
80 | | 010 : FSR = В±2.048 V (默认)
81 | | 011 : FSR = В±1.024 V
82 | | 100 : FSR = В±0.512 V
83 | | 101 : FSR = В±0.256 V
84 | | 110 : FSR = В±0.256 V
85 | | 111 : FSR = В±0.256 V
87 @return number ADC数据,若读取失败会返回nil
89 local ads1115_data = ads1115plus.task_recv(0,2)
90 log.info("ads1115", ads1115_data)
92 function ads1115plus
.task_recv( MU1
,FSR
)
93 a1
= 0x80 + (MU1
* 16) + (FSR
* 2) + 1 --a1为配置寄存器高八位 a2为低八位
94 if FSR
> 0x05 then --FSR大于5时,切换速率减少噪声,提高采样数据精确度
95 a2
= (0x3 * 32) + 3 --量程超过5的时候,速率配置为64sps
96 i2c
.writeReg(i2cid
, i2cslaveaddr
, 0x01, string.char(a1
, a2
)) --给配置寄存器写进去值 A1高八位 A2 低八位
99 a2
= (0x4 * 32) + 3 --其他情况下速率为128sps
100 i2c
.writeReg(i2cid
, i2cslaveaddr
, 0x01, string.char(a1
, a2
)) --给配置寄存器写进去值 A1高八位 A2 低八位
104 -- 从转换就绪引脚读取转换好的采集数据2个字节(四个十六进制的数值)的数据
105 local data
= i2c
.readReg(i2cid
, i2cslaveaddr
,0x00 , 2)
107 local _
,val_data_raw
= pack
.unpack(data
, ">h")
109 if not val_data_raw
then
112 if val_data_raw
>=0x8000 then
113 return ((0xffff-val_data_raw
))
115 return (val_data_raw
)
122 @api ads1115plus.task_read(MU1)
124 @return number ADC的mv数据,若读取失败会返回nil
126 log.info("ads1115plus:",ads1115plus.task_read(5))
130 function ads1115plus
.task_read(MU1
)
131 local ads
= ads1115plus
.task_recv(MU1
,0) --先用大量程测量正负6.144去测量 逼近
133 return nil --判断采样数值是否非空
142 if ads_ADC
> 10922 then
143 if ads_ADC
>20000 then
146 ads
= ads1115plus
.task_recv(MU1
,1)
150 elseif ads_ADC
> 5461 then
151 ads
= ads1115plus
.task_recv(MU1
,2)
153 elseif ads_ADC
> 2730 then
154 ads
= ads1115plus
.task_recv(MU1
,3)
156 elseif ads_ADC
> 1365 then
157 ads
= ads1115plus
.task_recv(MU1
,4)
160 ads
= ads1115plus
.task_recv(MU1
,5)
163 return mv
--返回采集到的电压值 单位(mV)有正有负