6135 views|8 replies

1w

Posts

26

Resources
The OP

【MicroPython】Read the HTS221 sensor of the small steel cannon [Copy link]


The Little Steel Cannon development board comes with an HTS221 temperature and humidity sensor. This temperature and humidity sensor is different from the STH22/si7002. It cannot directly read temperature and humidity and needs to be calculated through interpolation. The following is the HTS221 driver I wrote, which can directly read the temperature and humidity constants through functions.
  1. # HTS221 Humidity and temperature micropython drive # Author: shaoziyang # 2016.4 import pyb from pyb import I2C HTS_I2C_ADDR = 0x5F class HTS221(object): def __init__(self, i2cn): self.i2c = I2C(i2cn, I2C.MASTER, baudrate = 100000) # HTS221 Temp Calibration registers self.T0_OUT = self.get2Reg(0x3C) self.T1_OUT = self.get2Reg(0x3E) if self.T0_OUT>=0x8000 : self.T0_OUT -= 65536 if self.T1_OUT>=0x8000 : self.T1_OUT -= 65536 t1 = self.getReg(0x35) self.T0_degC = (self.getReg(0x32) + (t1%4)*256)/8 self.T1_degC = (self.getReg(0x33)+ ((t1%16)/4)*256)/8 # HTS221 Humi Calibration registers self.H0_OUT = self.get2Reg(0x36) self.H1_OUT = self.get2Reg(0x3A) self.H0_rH = self.getReg(0x30)/2 self.H1_rH = self.getReg(0x31)/2 # set av conf: T=4 H=8 self.setReg(0x81, 0x10) # set CTRL_REG1: PD=1 BDU=1 ODR=1 self.setReg(0x85, 0x20) def setReg(self, dat, reg): buf = bytearray(2) buf[0] = reg buf[1] = dat i2c = self.i2c i2c.send(buf, HTS_I2C_ADDR) def getReg(self, reg): i2c = self.i2c i2c.send(reg, HTS_I2C_ADDR) t = i2c.recv(1, HTS_I2C_ADDR) return t[0] def get2Reg(self, reg): a = self.getReg(reg) b = self.getReg(reg + 1) return a + b * 256 def av(self, av=''): i2c = self.i2c if av != '': #buf = bytearray(2) #buf[0] = 0x10; #buf[1] = av; #i2c.send(buf, HTS_I2C_ADDR) self.setReg(av, 0x10) else: #i2c.send(0x10, HTS_I2C_ADDR) #t = i2c.recv(1, HTS_I2C_ADDR) #return t[0] return self.getReg(0x10) def T0_OUT(self): return self.T0_OUT def T1_OUT(self): return self.T1_OUT def T0_degC(self): return self.T0_degC def T1_degC(self): return self.T1_degC # calculate Temperature def getTemp(self): t = self.get2Reg(0x2A) return self.T0_degC + (self.T1_degC - self.T0_degC) * (t - self.T0_OUT) / (self.T1_OUT - self.T0_OUT) def H0_OUT(self): return self.H0_OUT def H1_OUT(self): return self.H1_OUT def H0_rH(self): return self.H0_rH def H1_rH(self): return self.H1_rH # calculate Humidity def getHumi(self): t = self.get2Reg(0x28) return self.H0_rH + (self.H1_rH - self.H0_rH) * (t - self.H0_OUT) / (self.H1_OUT - self.H0_OUT)
复制代码
First copy hts221.py to the PYFLASH disk of the small steel cannon, and then you can use the function to read the temperature and humidity
  1. PYB: sync filesystems PYB: soft reboot MicroPython v1.7 on 2016-04-17; CANNON with STM32F401xE Type "help()" for more information.>>> from hts221 import HTS221 >>> hts=HTS221(1) >>> hts.getTemp() 22.95221 >>> hts.getHumi() 82.62943 >>>
复制代码


Latest reply

@dcexpert, do you have time to make a w5500 spi driver? This will greatly increase the application of micopython boards.  Details Published on 2017-2-15 13:50

1w

Posts

26

Resources
2
This program also demonstrates the use of I2C.

171

Posts

7

Resources
3
Support it

104

Posts

0

Resources
4
Awesome, very fast. I'm looking into how to integrate these drivers into MicroPython.

2767

Posts

8

Resources
5
It turned out to be the hero Shaoziyang...

1

Posts

2

Resources
6
Very good information. I am looking for this information now. Thank you.

5

Posts

0

Resources
7
@dcexpert, do you have time to make a w5500 spi driver? This will greatly increase the application of micopython boards.

Comments

The official driver files actually have w5500 and w5200, but I haven't tried them yet. I don't have the w5500 module, so I can't test it for now. I'll study it when I have a chance.  Details Published on 2017-2-15 15:36
The official driver files actually have w5500 and w5200, but I haven't tried them yet. I don't have the w5500 module, so I can't test it for now. I'll study it when I have a chance.  Details Published on 2017-2-15 15:35

1w

Posts

26

Resources
8
ddfox2009 posted on 2017-2-15 13:50 @dcexpert Do you have time to make a w5500 spi driver? This will greatly increase the application of micopython boards
In the official driver files, there are already w5500 and w5200, but I haven't tried them yet. I don't have a w5500 module here, so I can't test it for now. I'll study it when I have a chance.

1w

Posts

26

Resources
9
ddfox2009 posted on 2017-2-15 13:50 @dcexpert Do you have time to make a spi driver for w5500? This will greatly increase the application of micopython boards
This is the official driver filehttps://github.com/micropython/m ... er/drivers/wiznet5k

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Related articles more>>

    EEWorld
    subscription
    account

    EEWorld
    service
    account

    Automotive
    development
    circle

    Robot
    development
    community

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