2191 views|3 replies

1w

Posts

25

Resources
The OP
 

HTS221 driver porting for MicroPython [Copy link]

 

The MicroPython driver for HTS221 was actually developed a few years ago ( https://en.eeworld.com/bbs/thread-488307-1-1.html ), but now it seems that the program was not well written and not universal enough, so we took the opportunity of this event to modify the driver program to make it more convenient to use.

# HTS221 Humidity and temperature micropython drive
# ver: 2.0
# License: MIT
# Author: shaoziyang (shaoziyang@micropython.org.cn)
# v1.0 2016.4
# v2.0 2019.7

from machine import I2C

HTS_I2C_ADDR = const(0x5F)

class HTS221(object):
    def __init__(self, i2c):
        self.i2c = i2c
        # data buffer
        self.tb = bytearray(1)
        self.rb = bytearray(1)
        self.irq_v = [0, 0]
        # HTS221 Temp Calibration registers
        self.T0_OUT = self.int16(self.get2reg(0x3C))
        self.T1_OUT = self.int16(self.get2reg(0x3E))
        t = self.getreg(0x35) % 16
        self.T0_degC = self.getreg(0x32) + (t%4) * 256
        self.T1_degC = self.getreg(0x33) + (t//4) * 256
        # HTS221 Humi Calibration registers
        self.H0_OUT = self.int16(self.get2reg(0x36))
        self.H1_OUT = self.int16(self.get2reg(0x3A))
        self.H0_rH = self.getreg(0x30) * 5
        self.H1_rH = self.getreg(0x31) * 5
        self.K1 = (self.T1_degC - self.T0_degC) / (self.T1_OUT - self.T0_OUT)
        self.K2 = (self.H1_rH - self.H0_rH) / (self.H1_OUT - self.H0_OUT)
        # set av conf: T=4 H=8
        self.setreg(0x10, 0x81)
        # set CTRL_REG1: PD=1 BDU=1 ODR=1
        self.setreg(0x20, 0x85)
   
    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(HTS_I2C_ADDR, reg, self.tb)

    def getreg(self, reg):
        self.i2c.readfrom_mem_into(HTS_I2C_ADDR, reg, self.rb)
        return self.rb[0]
   
    def get2reg(self, reg):
        return self.getreg(reg) + self.getreg(reg+1) * 256

    # calculate Temperature
    def temperature(self):
        try:
            return (self.T0_degC + (self.int16(self.get2reg(0x2A)) - self.T0_OUT) * self.K1)/8
        except MemoryError:
            print('##')
            return self.temperature_irq()

    # calculate Humidity
    def humidity(self):
        try:
            return (self.H0_rH + (self.int16(self.get2reg(0x28)) - self.H0_OUT) * self.K2)/10
        except MemoryError:
            print('##')
            return self.humidity_irq()

    def get(self):
        try:
            return self.temperature(), self.humidity()
        except MemoryError:
            print('##')
            return self.get_irq()

    def temperature_irq(self):
        return (self.T0_degC + (self.int16(self.get2reg(0x2A)) - self.T0_OUT) * (self.T1_degC - self.T0_degC) // (self.T1_OUT - self.T0_OUT)) >> 3

    def humidity_irq(self):
        return (self.H0_rH + (self.int16(self.get2reg(0x28)) - self.H0_OUT) * (self.H1_rH - self.H0_rH) // (self.H1_OUT - self.H0_OUT))//10

    def get_irq(self):
        self.irq_v[0] = self.temperature_irq()
        self.irq_v[1] = self.humidity_irq()
        return self.irq_v




This content is originally created by dcexpert , a user of EEWORLD forum. If you want to reprint or use it for commercial purposes, you must obtain the author's consent and indicate the source

This post is from MEMS sensors
 
 

1w

Posts

25

Resources
2
 

Directions:

from machine import I2C
import HTS221

i2c = I2C(1)
hts = HTS221.HTS221(i2c)
hts.get()

How to use it in the callback function (interrupt):

from machine import I2C
import HTS221
from pyb import Timer

i2c = I2C(1)
hts = HTS221.HTS221(i2c)

def tim_irq(t):
    print(hts.get_irq())

tim = Timer(1, freq = 1)
tim.callback(tim_irq)

This post is from MEMS sensors
 
 
 

1w

Posts

25

Resources
3
 
This post was last edited by dcexpert on 2019-7-15 09:35

The main improvements of the new driver are:

  • Use the machine library for better versatility
  • Added get() function to get temperature and humidity data at once
  • Use writeto_mem()/readfrom_mem_into() method to set and read registers, which can be called in interrupt
  • Add temperature_irq()/humidity_irq()/get_irq() functions for easy calling in interrupts
  • Optimized code efficiency

Because in MicroPython, callback functions are not allowed to allocate memory in the heap, so floating-point operations cannot be performed. In order to make the driver compatible with callback functions, temperature_irq()/humidity_irq()/get_irq() functions are specially set up so that the driver can be used in callback functions. However, in order to avoid memory allocation problems, floating-point calculations are changed to integer calculations, at the cost of reducing the data precision in the callback function.

This post is from MEMS sensors

Comments

If you want higher precision in the callback function, you can multiply the data such as self.T0_OUT and self.T0_OUT by 10N, so that the returned data will be enlarged in the same way. Finally, divide the result by 10N to get better precision. However, you need to pay attention that N cannot be too large to avoid super large integer calculations.  Details Published on 2019-7-15 09:41
 
 
 

1w

Posts

25

Resources
4
 
dcexpert posted on 2019-7-15 09:26 The main improvements of the new driver are: Use the machine library, which has better versatility. Add the get() function to get temperature and humidity data at one time...

If you want higher precision in the callback function, you can multiply self.T0_OUT, self.T0_OUT and other data by 10N, so that the returned data will be enlarged in the same way, and finally divide the result by 10N to get better precision. However, you need to pay attention that N cannot be too large to avoid super large integer calculations, which will also cause memory allocation errors.

In addition, HTS221 itself also uses interpolation calculation, so the original accuracy of the sensor is not particularly high. Therefore, adding too many decimal places is not very meaningful. It is better to sample multiple times and use oversampling to improve accuracy.

This post is from MEMS sensors
 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list