LPS22HB的驱动其实在几年前就做过,当时用的是LPS22HB,而且micropython也才出来,因此程序显得不够通用。
昨天对比了一下LPS22HH和LPS22HB,发现两者非常相似,无论是功能还是引脚,还有寄存器,基本都是兼容的,只是LPS22HH指标更好,功能更多,而功耗也降低了。除了LPS22HH和LPS22HB外,在ST的网站上还发现了LPS22HD,它是LPS22HH的简化版,没有I3C接口,成本应该会稍低一些。
既然这三个芯片类似,因此就想重新改写驱动,可以同时适合三个型号。以前的驱动不支持oneshot模式,这次也将oneshot模式加上,这样可以有效的降低功耗。另外还增加了高度计算功能,虽然用于计算绝对高度时误差较大,但是计算短时间内相对高度的变化精度还是不错的,这也是很多登山表上使用的方法。
因为气压计算中使用了浮点运算,为了适合在回调函数中使用,所以像HTS221那样,单独为LPS22设置了irq方式的函数,这些函数使用整数运算,会降低数据的精度。
完整的驱动如下:
# LPS22HB/HH pressure seneor micropython drive
# ver: 2.0
# License: MIT
# Author: shaoziyang (shaoziyang@micropython.org.cn)
# v1.0 2016.4
# v2.0 2019.7
LPS22_CTRL_REG1 = const(0x10)
LPS22_CTRL_REG2 = const(0x11)
LPS22_TEMP_OUT_L = const(0x2B)
LPS22_PRESS_OUT_XL = const(0x28)
LPS22_PRESS_OUT_L = const(0x29)
class LPS22():
def __init__(self, i2c, addr = 0x5D):
self.i2c = i2c
self.addr = addr
self.tb = bytearray(1)
self.rb = bytearray(1)
self.oneshot = False
self.irq_v = [0, 0]
# ODR=1 EN_LPFP=1
self.setreg(LPS22_CTRL_REG1, 0x18)
def mode(self, oneshot=None):
if oneshot is None:
return self.oneshot
else:
self.getreg(LPS22_CTRL_REG1)
self.oneshot = oneshot
if oneshot: self.rb[0] &= 0x0F
else: self.rb[0] |= 0x10
self.setreg(LPS22_CTRL_REG1, self.rb[0])
def int16(self, d):
return d if d < 0x8000 else d - 0x10000
def setreg(self, reg, dat):
self.tb[0] = dat
self.i2c.writeto_mem(self.addr, reg, self.tb)
def getreg(self, reg):
self.i2c.readfrom_mem_into(self.addr, reg, self.rb)
return self.rb[0]
def get2reg(self, reg):
return self.getreg(reg) + self.getreg(reg+1) * 256
def ONE_SHOT(self):
if self.oneshot:
self.setreg(LPS22_CTRL_REG2, self.getreg(LPS22_CTRL_REG2) | 0x01)
def temperature(self):
self.ONE_SHOT()
try:
return self.int16(self.get2reg(LPS22_TEMP_OUT_L))/100
except MemoryError:
return self.temperature_irq()
def pressure(self):
self.ONE_SHOT()
try:
return (self.getreg(LPS22_PRESS_OUT_XL) + self.get2reg(LPS22_PRESS_OUT_L) * 256)/4096
except MemoryError:
return self.pressure_irq()
def get(self):
try:
return self.temperature(), self.pressure()
except MemoryError:
return self.get_irq()
def altitude(self):
return (((1013.25 / self.pressure())**(1/5.257)) - 1.0) * (self.temperature() + 273.15) / 0.0065
def temperature_irq(self):
self.ONE_SHOT()
return self.int16(self.get2reg(LPS22_TEMP_OUT_L))//100
def pressure_irq(self):
self.ONE_SHOT()
return self.get2reg(LPS22_PRESS_OUT_L) >> 4
def get_irq(self):
self.irq_v[0] = self.temperature_irq()
self.irq_v[1] = self.pressure_irq()
return self.irq_v
此内容由EEWORLD论坛网友dcexpert原创,如需转载或用于商业用途需征得作者同意并注明出处
demo程序
from machine import I2C
import LPS22
i2c = I2C(1)
lps = LPS22.LPS22(i2c)
lps.get()
回调函数demo
from machine import I2C
import LPS22
from pyb import Timer
i2c = I2C(1)
lps = LPS22.LPS22(i2c)
def tim_irq(t):
print(lps.get_irq())
tim = Timer(1, freq = 1)
tim.callback(tim_irq)
github上的程序链接,以后程序升级也会在此同步更新:
https://github.com/micropython-Chinese-Community/mpy-lib/tree/master/sensor/LPS22