晨旭的博客
你想干什么!
晨旭的博客~
距离/光线传感器SLI3108的使用

最近一个项目用到了SLI3108这个芯片,用起来其实也蛮简单的,文章这里稍微记录一下以免忘记
主控为Air268模块

SLI3108是一款可以测距离/光线强度的芯片,具体datasheet可以从此处查看:SLI3108 datasheet-Rev0

项目里用到的就是简单的测距功能,当距离靠,距离在设置阈值内,INT引脚就会触发一个低电平,重新对寄存器进行设置后,INT可以回到高电平等待下一次触发

引脚图如下:
https://source.papapoi.com/wp-content/uploads/2018/12/31a2a12d8284db67788a625dbf52d895.png?imageView2/2/w/800|watermark/1/image/aHR0cDovL3NvdXJjZS5wYXBhcG9pLmNvbS93cC1jb250ZW50L3VwbG9hZHMvMjAxNi8wOC9sb2dvcGljLnBuZw==/dissolve/100/gravity/SouthEast/dx/10/dy/10

硬件连接:
https://source.papapoi.com/wp-content/uploads/2018/12/6387167643a6d8f357c3c7f280aa21c3.png?imageView2/2/w/800|watermark/1/image/aHR0cDovL3NvdXJjZS5wYXBhcG9pLmNvbS93cC1jb250ZW50L3VwbG9hZHMvMjAxNi8wOC9sb2dvcGljLnBuZw==/dissolve/100/gravity/SouthEast/dx/10/dy/10

读取也是简单的几步

依照手册,设备地址为0x44,由于lua脚本接口的设置地址为7bit地址,所以右移一位为0x22

local i2cid = 2
local i2cslaveaddr = 0x44
if i2c.setup(i2cid,i2c.FAST,i2cslaveaddr) ~= i2c.FAST then
    print("SLI3107.init i2c fail")
end

然后依据手册,开启距离检测,设定检测周期为200ms一次,寄存器表如下:
https://source.papapoi.com/wp-content/uploads/2018/12/d38e2f4183efaf35f9500741281db9bb.png?imageView2/2/w/800|watermark/1/image/aHR0cDovL3NvdXJjZS5wYXBhcG9pLmNvbS93cC1jb250ZW50L3VwbG9hZHMvMjAxNi8wOC9sb2dvcGljLnBuZw==/dissolve/100/gravity/SouthEast/dx/10/dy/10

代码如下:

i2c.write(i2cid,0x01,0xa8)  -- enable proximity check, cycle is 200 ms

接下来设定阈值,经过从0x08地址直接读出数值进行测试,发现距离远近,数值越大,所以设置阈值最大值为10,最小值为0(大于最大值/小于最小值会触发)
具体相关寄存器表如下:
https://source.papapoi.com/wp-content/uploads/2018/12/04ae109fa6606bd4d3cdd6ed0a8e7408.png?imageView2/2/w/800|watermark/1/image/aHR0cDovL3NvdXJjZS5wYXBhcG9pLmNvbS93cC1jb250ZW50L3VwbG9hZHMvMjAxNi8wOC9sb2dvcGljLnBuZw==/dissolve/100/gravity/SouthEast/dx/10/dy/10
https://source.papapoi.com/wp-content/uploads/2018/12/e1971b7c5b62798052c897b1a024a4a5.png?imageView2/2/w/800|watermark/1/image/aHR0cDovL3NvdXJjZS5wYXBhcG9pLmNvbS93cC1jb250ZW50L3VwbG9hZHMvMjAxNi8wOC9sb2dvcGljLnBuZw==/dissolve/100/gravity/SouthEast/dx/10/dy/10

初始化代码如下:

i2c.write(i2cid,0x03,0)     -- proximity low threshold is 0
i2c.write(i2cid,0x04,10)    -- proximity high threshold is 10

接下来就是设置触发条件了,经测试,触发条件最终设置为了1次即触发,具体寄存器表如下:
https://source.papapoi.com/wp-content/uploads/2018/12/2a71d3bcfca7a56f4b207fb3c3c6b3e0.png?imageView2/2/w/800|watermark/1/image/aHR0cDovL3NvdXJjZS5wYXBhcG9pLmNvbS93cC1jb250ZW50L3VwbG9hZHMvMjAxNi8wOC9sb2dvcGljLnBuZw==/dissolve/100/gravity/SouthEast/dx/10/dy/10

因为触发中断后要重新配置该寄存器才能进行下次中断,所以把这句初始化语句写为一个函数:

--初始化中断设置
function resetInterrupt()
    i2c.write(i2cid,0x02,0xa0) -- enable proximity interrupt, 1 consecutive proximity values out of range
end
resetInterrupt()

最后配置一下中断接收引脚,就完成了

function gpio4IntFnc(msg)
    log.info("testGpioSingle.gpio4IntFnc",msg,getGpio4Fnc())
    --下降沿中断
    if msg~=cpu.INT_GPIO_POSEDGE then
        lcd.wake(10000)--亮屏10秒
        sys.timerStart(resetInterrupt,1000)
    end
end
--GPIO4配置为中断,可通过getGpio4Fnc()获取输入电平,产生中断时,自动执行gpio4IntFnc函数
getGpio4Fnc = pins.setup(pio.P0_4,gpio4IntFnc)
赞赏
如非特殊说明,本站所有文章均采用知识共享 署名-非商业性使用-相同方式共享4.0国际许可协议(CC BY-NC-SA 4.0)进行许可。

发表评论

textsms
account_circle
email

晨旭的博客~

距离/光线传感器SLI3108的使用
最近一个项目用到了SLI3108这个芯片,用起来其实也蛮简单的,文章这里稍微记录一下以免忘记 主控为Air268模块 SLI3108是一款可以测距离/光线强度的芯片,具体datasheet可以从此处查看…
扫描二维码继续阅读
2018-12-28