【SAMR21新玩法】25. 使用LPS22H传感器

Adafruit CircuitPython相关
回复
头像
shaoziyang
帖子: 717
注册时间: 2019年 10月 21日 13:48

【SAMR21新玩法】25. 使用LPS22H传感器

#1

帖子 shaoziyang » 2019年 10月 25日 09:20

前面介绍了在MakeCode中使用LPS22气压传感器,下面介绍在CircuitPython中的使用方法。

首先需要注意,CircuitPython中,I2C的读写函数与micropython不同,它只提供了下面两个函数:
  • readfrom_into(address, buffer, *, start=0, end=None)
  • writeto(address, buffer, *, start=0, end=None, stop=True)

因此我们以前编写的MicroPython驱动程序需要做一点修改,将寄存器操作函数替换为CircuitPython的函数。主要修改是:

代码: 全选

    def setreg(self, reg, dat):
        buf = bytearray([reg, dat])
        self.i2c.writeto(self.addr, buf)

    def getreg(self, reg):
        self.tb[0] = reg
        self.i2c.writeto(self.addr, self.tb)
        self.i2c.readfrom_into(self.addr, self.rb)
        return self.rb[0]
修改后,LPS22的驱动就可以使用了。一个完整的程序如下:

代码: 全选

import busio
from board import *

LPS22_CTRL_REG1    = const(0x10)
LPS22_CTRL_REG2    = const(0x11)
LPS22_STATUS       = const(0x27)
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 BDU=1
        self.setreg(LPS22_CTRL_REG1, 0x1A)
        self.oneshot_mode(False)

    def oneshot_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):
        buf = bytearray([reg, dat])
        self.i2c.writeto(self.addr, buf)

    def getreg(self, reg):
        self.tb[0] = reg
        self.i2c.writeto(self.addr, self.tb)
        self.i2c.readfrom_into(self.addr, self.rb)
        return self.rb[0]

    def get2reg(self, reg):
        return self.getreg(reg) + self.getreg(reg+1) * 256

    def ONE_SHOT(self, b):
        if self.oneshot:
            self.setreg(LPS22_CTRL_REG2, self.getreg(LPS22_CTRL_REG2) | 0x01)
            self.getreg(0x28 + b*2)
            while 1:
                if self.getreg(LPS22_STATUS) & b:
                    return

    def temperature(self):
        self.ONE_SHOT(2)
        try:
            return self.int16(self.get2reg(LPS22_TEMP_OUT_L))/100
        except MemoryError:
            return self.temperature_irq()

    def pressure(self):
        self.ONE_SHOT(1)
        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(2)
        return self.int16(self.get2reg(LPS22_TEMP_OUT_L))//100

    def pressure_irq(self):
        self.ONE_SHOT(1)
        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


i2c = busio.I2C(SCL, SDA)
i2c.try_lock()

lps = LPS22(i2c)
lps.get()
lps.altitude()

回复

  • 随机主题
    回复总数
    阅读次数
    最新文章