立即注册

扫一扫,访问微社区

micropython-microbit 中文社区

查看: 636|回复: 0

microbit的MPL115A1 驱动

[复制链接]
  • TA的每日心情
    开心
    2019-5-12 11:06
  • 签到天数: 8 天

    [LV.3]偶尔看看II

    发表于 2017-8-4 21:16:48 | 显示全部楼层 |阅读模式

    马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

    您需要 登录 才可以下载或查看,没有帐号?立即注册

    x
    MPL115A1 是一个SPI接口的气压温度传感器。

                                   
    登录/注册后可看大图


    下面是国外网友写的驱动。

    连接
    MPL115A1 pinmicro:bit pin
    SCLK13
    MISO14
    MOSI15
    CS16
    VDD3V
    GNDGND


    1. from microbit import *

    2. MY_ALTITUDE = 9.0  # metres above mean sea level (AMSL)

    3. SCROLL_SPEED = 80
    4. READ_INTERVAL = 5000  # milliseconds


    5. class MPL115A1:
    6.     """Read pressure and temperature from MPL115A1 SPI sensor.
    7.     Adapted from https://github.com/FaBoPlatform/FaBoBarometer-MPL115-Python
    8.     Sources:
    9.     http://www.nxp.com/assets/documents/data/en/data-sheets/MPL115A1.pdf
    10.     https://learn.adafruit.com/micropython-hardware-spi-devices/spi-master.
    11.     As shown in section 3.6 of the datasheet, each 1-byte command is followed
    12.     by reading a 1-byte value (during which the master sends a dummy 0x00).
    13.     After a sequence of commands and reads, an extra 0x00 byte is sent.
    14.     The whole sequence is contained between CS enable and CS disable.
    15.     Constants have been defined for the sequence of commands and dummy
    16.     bytes to read coefficients (CMD_COEFS), start measurement (CMD_START)
    17.     and read pressure and temperature data (CMD_DATA).
    18.     """

    19.     CMD_COEFS = bytearray(b'\x88\x00\x8A\x00\x8C\x00\x8E\x00\x90\x00\x92\x00\x94\x00\x96\x00\x00')
    20.     CMD_START = bytearray(b'\x24\x00')
    21.     CMD_DATA = bytearray(b'\x80\x00\x82\x00\x84\x00\x86\x00\x00')

    22.     def __init__(self, sclk=pin13, miso=pin14, mosi=pin15, cs=pin16,
    23.                  baudrate=1000000, mode=0, altitude=0.0):
    24.         self.cs = cs
    25.         self._spi_enable(False)
    26.         spi.init(baudrate=baudrate, mode=mode, sclk=sclk, miso=miso, mosi=mosi)
    27.         self._get_coefficients()
    28.         self.P = 0  # measured pressure at observer's altitude
    29.         self.P0 = 0  # calculated pressure at mean sea level
    30.         self.T = 0  # measured temperature
    31.         self.altitude = altitude

    32.     def _spi_enable(self, b=True):
    33.         self.cs.write_digital(0) if b else self.cs.write_digital(1)

    34.     def _spi_disable(self):
    35.         self._spi_enable(False)

    36.     def _convert_data(self, lsb, msb):
    37.         value = lsb | (msb << 8)
    38.         if (value & (1 << 16 - 1)):
    39.             value -= (1 << 16)
    40.         return value

    41.     def _get_coefficients(self):
    42.         data = bytearray(len(self.CMD_COEFS))
    43.         self._spi_enable(True)
    44.         spi.write_readinto(self.CMD_COEFS, data)
    45.         self._spi_enable(False)
    46.         self.a0 = self._convert_data(data[3], data[1])
    47.         self.b1 = self._convert_data(data[7], data[5])
    48.         self.b2 = self._convert_data(data[11], data[9])
    49.         self.c12 = self._convert_data(data[15], data[13])
    50.         self.a0 = float(self.a0) / (1 << 3)
    51.         self.b1 = float(self.b1) / (1 << 13)
    52.         self.b2 = float(self.b2) / (1 << 14)
    53.         self.c12 = float(self.c12) / (1 << 24)

    54.     def take_readings(self):
    55.         self._spi_enable(True)
    56.         spi.write(self.CMD_START)
    57.         self._spi_enable(False)
    58.         sleep(3)
    59.         data = bytearray(len(self.CMD_DATA))
    60.         self._spi_enable(True)
    61.         spi.write_readinto(self.CMD_DATA, data)
    62.         self._spi_enable(False)
    63.         padc = ((data[1] << 8) | data[3]) >> 6
    64.         tadc = ((data[5] << 8) | data[7]) >> 6
    65.         pcomp = self.a0 + (self.b1 + self.c12 * tadc) * padc + self.b2 * tadc
    66.         self.P = pcomp * ((1150.0 - 500.0) / 1023.0) + 500.0
    67.         self.P0 = self.P / pow(1.0 - (self.altitude / 44330.0), 5.255)
    68.         self.T = 25.0 - (tadc - 512.0) / 5.35


    69. barometer = MPL115A1(altitude=MY_ALTITUDE)

    70. while True:
    71.     barometer.take_readings()
    72.     display.scroll('T: {t:.1f}C  P: {p:.0f}kPa  P0: {p0:.0f}kPa'.
    73.                    format(t=barometer.T, p=barometer.P, p0=barometer.P0),
    74.                    SCROLL_SPEED)
    75.     sleep(READ_INTERVAL)
    复制代码



    https://github.com/hackscribble/microbit-MPL115A1-barometer





    手机扫码浏览

    本帖被以下淘专辑推荐:

    回复

    使用道具 举报

    游客
    回复
    您需要登录后才可以回帖 登录 | 立即注册

    Archiver|手机版|小黑屋|micropython-microbit 中文社区

    GMT+8, 2019-7-18 10:52 , Processed in 0.150933 second(s), 22 queries .

    Powered by Discuz! X3.4

    © 2001-2013 Comsenz Inc.

    快速回复 返回顶部 返回列表