MR开发板
SHT20温湿度传感器
由瑞士推出的 SHT20数字温湿度传感器,基于领先世界的CMOSens ? 数字传感技术,具有极高的可靠性和卓越的长期稳定性。全量程标定,两线数字接口,可与单片机直接相连,大大缩短研发时间、简化外围电路并降低费用。此外,体积微小、响应迅速、低能耗、可浸没、抗干扰能力强、温湿一体,兼有露点测量,性价比高,使该产品能够适于多种场合的应用。
1.54寸液晶屏st7789 spi
之前用的是所谓的硬I2C。好处就是简单,坏处就是万一我板子的I2C都被占了,就没法玩了?我们今天试试软I2C~就是普通引脚模拟I2C功能。之前小伙伴私信我说用屏每次都要在主函数文件引入一堆东西很烦,今天咱一起解决。
引脚说明
开发板 | sht20 |
---|
A1 | SCL | A0 | SDA | 3.3v | VCC | GND | GND |
示例代码
main.py 主文件就这些东西了。。其他引用即可。
from SHT20 import SHT20
import pyb
import lcd
WS=SHT20()
lcd = lcd.LCD()
while True:
T = WS.TEM()
H=WS.HUM()
lcd.chars('tem: '+str(round(T,2))+" ",65, 90)
lcd.chars('hum: '+str(round(H,2))+" ",65,120)
pyb.delay(1000)
lcd.py
import time
from pyb import SPI,Pin,delay
from ztst7789class import ST7789
import bauhs93
def color565(r, g, b):
return (r & 0xf8) << 8 | (g & 0xfc) << 3 | b >> 3
def LCD():
RES = Pin('C4')
BLK = Pin('B10')
DC = Pin('C5')
CS = Pin('B11')
SCL = Pin('A5')
TFT_MISO_PIN = Pin('A6')
SDA = Pin('A7')
bl = Pin(BLK, Pin.OUT)
LCD = None
bl.value(1)
spi = SPI(1,SPI.MASTER,baudrate=7800000,polarity=0,phase=0)
LCD = ST7789(spi, cs=Pin(CS), dc=Pin(DC), rst=RES)
delay(1000)
LCD.erase()
LCD.set_font(bauhs93)
LCD.set_color(color565(255,255,0),color565(0,0,0))
return LCD
SHT20.py
from machine import Pin, I2C
from struct import unpack as unp
from time import sleep_ms
SHT20_I2CADDR = 64
TRI_T_MEASURE_NO_HOLD = b'\xf3'
TRI_RH_MEASURE_NO_HOLD = b'\xf5'
READ_USER_REG = b'\xe7'
WRITE_USER_REG = b'\xe6'
SOFT_RESET = b'\xfe'
class SHT20(object):
def __init__(self, scl=Pin.cpu.A1, sda=Pin.cpu.A0, clk_freq=400000):
self._address = SHT20_I2CADDR
pin_c = Pin(scl)
pin_d = Pin(sda)
self._bus = I2C(scl=pin_c, sda=pin_d, freq=clk_freq)
def TEM(self):
self._bus.writeto(self._address, TRI_T_MEASURE_NO_HOLD)
sleep_ms(150)
origin_data = self._bus.readfrom(self._address, 2)
origin_value = unp('>h', origin_data)[0]
value = -46.85 + 175.72 * (origin_value / 65536)
return value
def HUM(self):
self._bus.writeto(self._address, TRI_RH_MEASURE_NO_HOLD)
sleep_ms(150)
origin_data = self._bus.readfrom(self._address, 2)
origin_value = unp('>H', origin_data)[0]
value = -6 + 125 * (origin_value / 65536)
return value
效果
|