2288 views|2 replies

1w

Posts

25

Resources
The OP
 

MicroPython driver porting for STTS751 [Copy link]

 
This post was last edited by dcexpert on 2019-7-17 10:25

Seeing the name of STTS751, it is easy to think of the temperature sensor Si7051. Both are high-precision temperature sensors, but the STTS751 can reach a maximum resolution of 12 bits, while the Si7051 is 14 bits. In terms of power consumption, Si7051 is superior, with an average current of 195nA at a sampling rate of 1Hz. The power supply range of Si7051 is also slightly better, with the lowest being 1.9V. The STTS751 is smaller in package, only 2x2mm. In terms of function, the STTS751 is clearly superior, supporting event and interrupt outputs, and can set upper and lower temperature limits and start the fan when overtemperature occurs. In terms of software, the STTS751 is also more convenient, and can read the temperature directly, while the Si7051 still needs to be converted.

The use of STTS7551 is very simple. After power-on, the default parameters can read the temperature. Unlike some sensors, they are powered off after power-on and need to be set to working mode before running. The temperature value is stored in registers 0 and 2. Register 0 stores the integer part and the sign bit, and register 2 stores the decimal part. If too high precision is not required, just read register 0. The slight inconvenience is that the two register addresses are not continuous.

Register 3 is a configuration register that can set the data resolution (Tres1/Tres0), which is 10 bits by default. RUN/STOP can be set to run and standby mode. In standby mode, if any data is written to the oneshot register (0F), the oneshot function (run once) will be enabled, and the temperature will be automatically switched to standby mode after conversion to reduce power consumption.

The above briefly analyzes the working mode of STTS751. The following is a complete micropython driver: This content is originally created by dcexpert


, a netizen of EEWORLD forum. If you need to reprint or use it for commercial purposes, you must obtain the author's consent and indicate the source

# STTS751 temperature seneor micropython drive
# ver: 1.0
# License: MIT
# Author: shaoziyang (shaoziyang@micropython.org.cn)
# v1.0 2019.7

STTS751_RESOLUTION = (8, 0, 4, 12, 10, 11, 9, 12)
STTS751_REG_STATUS = const(1)
STTS751_REG_CONFIG = const(3)
STTS751_REG_CONRAT = const(4)
STTS751_REG_TEMPVH = const(0)
STTS751_REG_TEMPVL = const(2)
STTS751_REG_TEMPHH = const(5)
STTS751_REG_TEMPHL = const(6)
STTS751_REG_TEMPLH = const(7)
STTS751_REG_TEMPLL = const(8)
STTS751_REG_ONESHOT = const(15)
STTS751_REG_THERM = const(32)
STTS751_REG_THERMHYS = const(33)

class STTS751():
    def __init__(self, i2c, addr = 0x4A):
        self.i2c = i2c
        self.addr = addr
        self.tb = bytearray(1)
        self.rb = bytearray(1)
        self.oneshot = False
        self.mode(False)

    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 resolution(self, res = None):
        self.getreg(STTS751_REG_CONFIG)
        if res is None:
            return STTS751_RESOLUTION[(self.rb[0] & 0x0C)//4 + 4]
        else:
            if res > 12 or res < 9: return
            self.rb[0] = (self.rb[0] & 0xF3) | STTS751_RESOLUTION[res-9]
            self.setreg(STTS751_REG_CONFIG, self.rb[0])

    def mode(self, oneshot=None):
        if oneshot is None:
            return self.oneshot
        else:
            self.getreg(STTS751_REG_CONFIG)
            self.oneshot = oneshot
            if oneshot: self.rb[0] |= 0x40
            else: self.rb[0] &= 0xBF
            self.setreg(STTS751_REG_CONFIG, self.rb[0])

    def ONE_SHOT(self):
        if self.oneshot:
            self.setreg(STTS751_REG_ONESHOT, 1)
            while 1:
                if self.getreg(STTS751_REG_STATUS) < 0x80:
                    return

    def temperature(self):
        try:
            self.ONE_SHOT()
            return self.int16((self.getreg(STTS751_REG_TEMPVH)<<8) + self.getreg(STTS751_REG_TEMPVL))/256
        except MemoryError:
            return self.temperature_irq()
 
    def temperature_irq(self):
        self.ONE_SHOT()
        self.getreg(STTS751_REG_TEMPVH)
        return self.rb[0] if self.rb[0] < 128 else self.rb[0] - 256


1.jpg (27.15 KB, downloads: 0)

1.jpg

2.jpg (23.04 KB, downloads: 0)

2.jpg
This post is from MEMS sensors
 
 

1w

Posts

25

Resources
2
 

Directions:

from machine import I2C
import STTS751

i2c = I2C(1)
stt = STTS751.STTS751(i2c)
stt.temperature()

This post is from MEMS sensors
 
 
 

1w

Posts

25

Resources
3
 

Usage in callback function

from machine import I2C
from pyb import Timer
import STTS751

i2c = I2C(1)
stt = STTS751.STTS751(i2c)

def tim_irq(t):
    print(stt.temperature_irq())

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

Because the callback function does not support floating-point calculations, only the integer part of the temperature can be read in the callback function.

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