2024年寒假练 - 基于搭配带屏12指神探的传感器扩展板实现恒温控制系统
该项目使用了MicroPython语言与带屏幕的十二指神探,实现了恒温控制系统的设计,它的主要功能为:用按键控制目标温度并显示目标温度及实时温度。
标签
嵌入式系统
数字逻辑
开发板
接口
USB
QVQ
更新2024-04-17
北京理工大学
45

项目总结报告

一.项目介绍

1.项目名称

恒温或恒湿控制系统

2.任务要求

a.至少使用一种自控算法控制,如PID等

b.可用按键或拨轮控制目标温度

c.可在LCD屏上显示目标温度及实时温度

3.项目功能介绍

通过两个按钮升高或降低设定温度,oled屏幕上会显示当前的温湿度和设定的温度,同时增加了中文字库,让使用更加方便,功能更加完整。

4.硬件介绍

扩展板搭载了几款常见传感器和功能模块,包括为初学者准备的麦克风、蜂鸣器、、红外收发、霍尔效应开关、加热电阻,为进阶操作准备的温湿度传感器、六轴传感器、接近/环境光/IR传感器、颜色传感器。其中温湿度传感器、六轴传感器、接近传感器、颜色传感器可拆卸为单个模块,通过杜邦线等连接线延伸其使用的空间范围。带屏版的12指神探,在原板基础上,配备了一块240*240分辨率的LCD彩屏以及两个可程控按键和一个拨轮,丰富了人机交互功能,方便信息观察、界面切换等使用方式。

最值得介绍的结构是树莓派Pico核心芯片RP2040它的具体介绍如下:

  • 双Arm Cortex M0+内核,可以运行到133MHz
  • 264KBSRAM,板卡上外扩2MBFlash
  • 性能强大、高度灵活的可编程IO(PIO)可用于高速数字接口
  • 拥有2个UART、2个SPI、2个I2C、16个PWM通道以及4路12位精度ADC
  • 支持MicroPython、C、C++编程
  • 拖拽UF2固件至U盘的方式烧录固件,方便快捷

5.电路图示意

二.项目功能

  • 随着目标温度的变化,PID算法控制加热电阻随之使用/禁用
  • 左键控制升高温度,右键控制降低温度
  • 在屏幕显示目标温度以及当前温度
  • 具体使用详见视频

三.部分重要代码展示

1.配置LCD和SPI接口:

初始化SPI接口,并将其用于驱动ST7789型号的液晶显示屏。通过指定SPI接口的参数和显示屏的参数,实现了显示屏的初始化配置,设定初始温度,屏幕颜色。

spi0=SPI(0,baudrate=4000000, phase=1, polarity=1, sck=Pin(2), mosi=Pin(3))
display = st.ST7789(spi0, 240, 240,
reset=machine.Pin(0, machine.Pin.OUT),
dc=machine.Pin(1, machine.Pin.OUT),
xstart=0, ystart=0, rotation=0)

buttonL = Pin(7,Pin.IN, Pin.PULL_UP)
buttonPRESS = Pin(8,Pin.IN, Pin.PULL_UP)
buttonR = Pin(9,Pin.IN, Pin.PULL_UP)

heat_r = Pin(22,Pin.OUT, value=0)
hpwm = PWM(heat_r)
hpwm.freq(2000)# 500us
hpwm.duty_u16(0)
nsh = I2C(0, scl=Pin(21), sda=Pin(20))
display.fill(st.BLACK)

WHITE = st.color565(255, 255, 255)
BLACK = st.color565(0, 0, 0)
RED = st.color565(0, 255, 0)
GREEN = st.color565(0, 0, 255)
BLUE = st.color565(255, 0, 0)
YELLOW = st.color565(0, 255, 255)

target_tem_t = 25
target_tem = 25

2.温度传感器读取:

从温度传感器读取温度和湿度数据,通过I2C接口与传感器通信,读取相应的寄存器数据,并进行计算得到温度值和湿度值。

def nsht30_get(i2c):
dat = i2c.readfrom(0x44, 6)
tem = (dat[0]<<8) | dat[1]
hum = (dat[3]<<8) | dat[4]
pout0 = 175.0*float(tem)/65535.0-45.0
pout1 = 100.0*float(hum)/65535.0
return round(pout0,1),round(pout1,1)

3.加热电阻控制(PID控制算法)

根据当前温度与设定温度的差值,计算PWM的占空比,并设置给加热电阻的PWM对象,从而控制加热电阻的加热程度。

def heat_res(hpwm, nsh, adj):
val = nsht30_get(nsh)
if(val[0]<adj):
num = int((adj-val[0])*45000)
if(num>65535):
num = 65535
hpwm.duty_u16(num)
else:
hpwm.duty_u16(0)
return round(val[0], 1), 0

4.LCD屏幕展示

通过控制LCD屏幕上像素点的颜色和位置来实现图像和文本的显示包括了绘制矩形、填充矩形、绘制线条和绘制文本等。

    def fill_rect(self, x, y, width, height, color):
"""
Draw a rectangle at the given location, size and filled with color.

Args:
x (int): Top left corner x coordinate
y (int): Top left corner y coordinate
width (int): Width in pixels
height (int): Height in pixels
color (int): 565 encoded color
"""
self.set_window(x, y, x + width - 1, y + height - 1)
chunks, rest = divmod(width * height, _BUFFER_SIZE)
pixel = _encode_pixel(color)
self.dc.on()
if chunks:
data = pixel * _BUFFER_SIZE
for _ in range(chunks):
self.write(None, data)
if rest:
self.write(None, pixel * rest)

def line(self, x0, y0, x1, y1, color):
"""
Draw a single pixel wide line starting at x0, y0 and ending at x1, y1.

Args:
x0 (int): Start point x coordinate
y0 (int): Start point y coordinate
x1 (int): End point x coordinate
y1 (int): End point y coordinate
color (int): 565 encoded color
"""
steep = abs(y1 - y0) > abs(x1 - x0)
if steep:
x0, y0 = y0, x0
x1, y1 = y1, x1
if x0 > x1:
x0, x1 = x1, x0
y0, y1 = y1, y0
dx = x1 - x0
dy = abs(y1 - y0)
err = dx // 2
if y0 < y1:
ystep = 1
else:
ystep = -1
while x0 <= x1:
if steep:
self.pixel(y0, x0, color)
else:
self.pixel(x0, y0, color)
err -= dy
if err < 0:
y0 += ystep
err += dx
x0 += 1

def text(self, font, text, x0, y0, color=WHITE, background=BLACK):
"""
Draw text on display in specified font and colors. 8 and 16 bit wide
fonts are supported.

Args:
font (module): font module to use.
text (str): text to write
x0 (int): column to start drawing at
y0 (int): row to start drawing at
color (int): 565 encoded color to use for characters
background (int): 565 encoded color to use for background
"""
if font.flag == 'gb16':
self._text_gb16(font,size, text, x0, y0, color, background)
elif font.WIDTH == 8:
self._text8(font, text, x0, y0, color, background)
else:
self._text16(font, text, x0, y0, color, background)

5.字库设置

通过这种方式,不断定义多种字体,来实现中文字库以及数字符号展示的目的。

WIDTH = 16
HEIGHT = 16
flag = 'gb16'
FIRST = 0x20
LAST = 0x7f

四.算法流程图

五.总结与反思

这个项目让我对于嵌入式程序有了更深的了解,激发了我接下来学习的热情,并且我也完成了实验相关的要求目标,成功的调节温度与目标温度匹配,达到了实验目的。在实验过程中我也遇见了相关的问题,比如E盘总是自动弹出,U盘插入后无法运行,这让我更加的理解实战的重要性,即使在技术支持的情况下,也会出现一些非技术原因的突发情况影响实验的进程,因此这次实践活动让我受益颇多。同时我的实验目标主要是温度提升,而加热电阻无法调节温度降低,在未来将尝试更多技术手段达成这一目标。


附件下载
main.py
主函数
st7789.py
显示屏设置
font_gb_16x16.py
字体设置
vga1_16x32.py
字体设置
vga2_8x8.py
字体设置
团队介绍
秦子棋 北京理工大学
评论
0 / 100
查看更多
目录
硬禾服务号
关注最新动态
0512-67862536
info@eetree.cn
江苏省苏州市苏州工业园区新平街388号腾飞创新园A2幢815室
苏州硬禾信息科技有限公司
Copyright © 2023 苏州硬禾信息科技有限公司 All Rights Reserved 苏ICP备19040198号