一、目的
这一节我们来学习如何使用合宙ESP32 C3,连接K型 MAX6675K型热电偶模块,使用ST7735屏幕进行显示。下面我们一起来学习一下吧!
二、环境
ESP32 C3开发板(MicroPython v1.19.1 on 2022-06-18)+ K型 MAX6675K型热电偶模块 + 0.96寸TFT ST7735屏幕 + 几根杜邦线 + Win10商业版
ESP32 C3和各模块接线方法:
ESP32 C3连接ST7735屏幕的方法,请参考物联网开发124这个文章。
SCK接GPIO5, SO接GPIO4, CS接GPIO1
三、ST7735屏幕驱动
请查看物联网开发124这个文章获取
四、操作字体文件方法库
请查看物联网开发124这个文章获取
五、MAX6675K型热电偶模块驱动
这个是开源库
# adapted from https://github.com/archemius/MAX6675-Raspberry-pi-python
from machine import Pin
import time
class MAX6675:
# set pin number for communicate with MAX6675
def __init__(self, cs: int, sck: int, so: int) -> None:
self._cs_no = cs
self._cs = Pin(cs, Pin.OUT)
self._cs.on()
self._sck = Pin(sck, Pin.OUT)
self._sck.off()
self._so = Pin(so, Pin.IN)
self._so.on()
def read_temp(self) -> float:
self._cs.off()
time.sleep(0.002)
self._cs.on()
time.sleep(0.22)
self._cs.off()
self._sck.on()
time.sleep(0.001)
self._sck.off()
value = 0
for i in range(11, -1, -1):
self._sck.on()
value = value + (self._so.value() * (2 ** i))
self._sck.off()
self._sck.on()
error_tc = self._so.value()
self._sck.off()
for i in range(2):
self._sck.on()
time.sleep(0.001)
self._sck.off()
self._cs.on()
temp = value * 0.25 # Celsius
if error_tc != 0:
return float(str(self._cs_no))
else:
return float(str(temp))
六、示例代码
使用开源库做个例子哈,如下:
from machine import Pin,SPI,I2C
from st7735 import ST7735,color
from ufont import BMFont
import time
from max6675 import MAX6675
font = BMFont("fonts/unifont-14.0.04.bmf")
spi = SPI(1,baudrate = 60_000_000,polarity = 0,sck = Pin(2),mosi = Pin(3),miso = None)
tft = ST7735(spi,rst = 10,dc = 6,cs = 7,bl = 8, width = 160, height = 80,rotate = 1,rgb = True)
Max = MAX6675(cs = 1,sck = 5,so = 4)
def BMP():
temp = Max.read_temp()
font.text(tft,"温度: %.2f ℃"%(temp)+str(" "),0,40,color(255,255,255),font_size = 16,reverse = False,clear = False,show = True,half_char = True,auto_wrap = False)
def main():
font.text(tft,"Max6675K型热电偶模块",0,0,color(0,255,0),font_size = 16,reverse = False,clear = False,show = True,half_char = True,auto_wrap = False)
tft.hline(0,20,160,color(255,255,0))
tft.hline(0,23,160,color(255,255,0))
tft.show()
while True:
BMP()
pass
if __name__ == "__main__":
main()
七、演示效果
大家可以根据自己的喜好,设置不同的字体颜色哈。。。
八,自建库
我们根据上面开源的库,更改了部分代码,如下:
from machine import Pin,SPI
import time
class MAX6675:
def __init__(self,sck,cs,so):
self.sck = Pin(sck,Pin.OUT)
self.cs = Pin(cs ,Pin.OUT)
self.so = Pin(so,Pin.IN,Pin.PULL_UP)
#读取16位2字节数据
def Max6675_ReadReg(self):
data = 0
self.cs.value(0)
time.sleep_ms(1)
self.sck.value(0)
self.so.value(0)
time.sleep_ms(1)
for i in range(0,16,1):
data <<= 1
self.sck.value(1)
if self.so.value() == 1: #如果数据总线为高进入执行
data |= 0x01 #记录高电平零可忽略空闲位自动补零
self.sck.value(0)
self.cs.value(1) #释放片选线
return data
#判断器件是否存在0存在1断开
def Max6675_Connect(self):
data = self.Max6675_ReadReg() & 0x04
data >>= 2 #D2是器件标志位
return data
#器件存在返回温度,不存在返回-1
def Max6675_temperature(self):
if self.Max6675_Connect() == 0:
data = self.Max6675_ReadReg() << 1 #D15 没有用移除
data = ((((data>>4) & 0x0fff) * 0.25) + 0.5) #右移4位保留12位数据 D14~D3
time.sleep_ms(220) #每次读取间隔不能低于220ms
else:
return -1
return data
九、根据自建库,我们再来写个例子
from machine import Pin,SPI,I2C
from st7735 import ST7735,color
from ufont import BMFont
import time
from Max6675 import MAX6675
font = BMFont("fonts/unifont-14.0.04.bmf")
spi = SPI(1,baudrate = 60_000_000,polarity = 0,sck = Pin(2),mosi = Pin(3),miso = None)
tft = ST7735(spi,rst = 10,dc = 6,cs = 7,bl = 8, width = 160, height = 80,rotate = 1,rgb = True)
Max = MAX6675(cs = 1,sck = 5,so = 4)
def BMP():
if Max.Max6675_Connect() == 0:
temp = Max.Max6675_temperature()
font.text(tft,"温度: %.2f ℃"%(temp)+str(" "),24,40,color(255,255,255),font_size = 16,reverse = False,clear = False,show = True,half_char = True,auto_wrap = False)
else:
font.text(tft,"传感器异常!",24,40,color(255,255,255),font_size = 16,reverse = False,clear = False,show = True,half_char = True,auto_wrap = False)
def main():
font.text(tft,"Max6675K型热电偶模块",0,0,color(0,255,0),font_size = 16,reverse = False,clear = False,show = True,half_char = True,auto_wrap = False)
tft.hline(0,20,160,color(255,255,0))
tft.hline(0,23,160,color(255,255,0))
tft.show()
while True:
BMP()
if __name__ == "__main__":
main()
十、示例三
如果不用max6675库,直接写一个程序可以吗,可以,看下面
from machine import Pin,SPI
from st7735 import ST7735,color
from ufont import BMFont
import time
font = BMFont("fonts/unifont-14.0.04.bmf")
spi = SPI(1,baudrate = 60_000_000,polarity = 0,sck = Pin(2),mosi = Pin(3),miso = None)
tft = ST7735(spi,rst = 10,dc = 6,cs = 7,bl = 8, width = 160, height = 80,rotate = 1,rgb = True)
sck = Pin(5,Pin.OUT)
cs = Pin(1,Pin.OUT)
so = Pin(4,Pin.IN,Pin.PULL_UP)
def Max6675_ReadReg():
dat = 0
cs.value(0)
time.sleep_ms(1)
sck.value(0)
so.value(0)
time.sleep_ms(1)
for i in range(0,16,1):
dat <<= 1
sck.value(1)
if so.value() == 1:
dat |= 0x01
sck.value(0)
cs.value(1)
return dat
def MAX6675_Connect():
dat = Max6675_ReadReg() & 0x0004
dat = dat >> 2
return dat
def MAX6675_Temper():
d = (Max6675_ReadReg() << 1)
dat = (((d>>4) & 0x0fff) * 0.25)
return dat
def BMP():
if MAX6675_Connect() == 0:
temp = MAX6675_Temper()
font.text(tft,"温度: %.2f ℃"%(temp)+str(" "),0,40,color(255,255,255),font_size = 16,reverse = False,clear = False,show = True,half_char = True,auto_wrap = False)
else:
font.text(tft,"传感器异常!",0,40,color(255,255,255),font_size = 16,reverse = False,clear = False,show = True,half_char = True,auto_wrap = False)
def main():
font.text(tft,"Max6675K型热电偶模块",0,0,color(0,255,0),font_size = 16,reverse = False,clear = False,show = True,half_char = True,auto_wrap = False)
tft.hline(0,20,160,color(255,255,0))
tft.hline(0,23,160,color(255,255,0))
tft.show()
while True:
BMP()
if __name__ == "__main__":
main()
十一、模块购买
商品详情https://detail.tmall.com/item.htm?id=659794404075&spm=a1z09.2.0.0.252b2e8dh5plod&_u=ep01rchdcaa
资料下载链接:https://pan.baidu.com/s/1vtWXy_PzRxd8Fo5c2Eu11Q
提取码:8888