Micropython学习交流群 学习QQ群:786510434 提供多种固件下载和学习交流。

Micropython-扇贝物联 QQ群:31324057 扇贝物联是一个让你与智能设备沟通更方便的物联网云平台

Micropython学习交流群 学习QQ群:468985481 学习交流ESP8266、ESP32、ESP8285、wifi模块开发交流、物联网。

Micropython老哥俩的IT农场分享QQ群:929132891 为喜欢科创制作的小白们分享一些自制的计算机软硬件免费公益课程,由两位多年从事IT研发的中年大叔发起。

Micropython ESP频道

micropython esp32s3 2.8寸4线SPI串口TFT液晶显示屏模块ILI9341驱动LCD触摸屏 240*320 显示中文


微信图片_20230703225532.jpg


"""ILI9341 demo (fonts 8x8)."""
from time import sleep
from ili9341 import Display, color565
from machine import Pin, SPI  # type: ignore
from xglcd_font import XglcdFont
from machine import Pin,I2C
import time
import aht10
from cnfont import Chine

"""Test code."""
spi = SPI(1, baudrate=20000000, sck=Pin(4), mosi=Pin(5))
display = Display(spi, dc=Pin(16), cs=Pin(18), rst=Pin(15), rotation=180)


addr = 0x38  # aht10 I2C地址
i2c = I2C(scl = Pin(11),sda = Pin(12),freq = 1_000_000)
# addr = hex(i2c.scan()[0]) 
aht = aht10.AHT10(i2c,0,addr)

espresso_dolce = XglcdFont('fonts/EspressoDolce18x24.c', 18, 24)
  
display.clear()


#16*32字体函数
def ByteOpera16x32(num,dat):
  byte= [0x01,0x02,0x04,0x8,0x10,0x20,0x40,0x80]
  if dat&byte[num]:
    return 1
  else:
    return 0
 
def LcdShowCh_16x32(n, x_axis, y_axis):
  for i in range(4): 
    for a in range(16): 
      for b in range(8):  # 8个汉字
        if(ByteOpera16x32(b,Chine.chine[n*64+i*16+a])): 
          display.draw_pixel(x_axis+a,y_axis+i*8+b,color565(255,0,255))
        else:
          display.draw_pixel(x_axis+a,y_axis+i*8+b,color565(0,0,0))


# 显示汉字
for i in range(8): # 8指得是8个汉字
    LcdShowCh_16x32(i,(i*16)+50,10)
        
def test():
    humi = aht.humidity()   # 湿度
    temp = aht.temperature()  # 温度
    
    #Lcd12864_Write_Text(0x88,str("humi = %.2f"%humi))
    #Lcd12864_Write_Text(0x98,str("temp = %.2f"%temp))
     
    print("湿度 = %.2f \t 温度 = %.2f" %(humi,temp))
    

    
    display.draw_text(0, 70, "WD = %.2f"%temp, espresso_dolce,color565(0, 255, 255))
    display.draw_text(0, 110, "SD = %.2f"%humi, espresso_dolce,color565(0, 255, 255))
    

    #display.draw_text8x8(0, 0, '=====================================', color565(255, 0, 255))
    display.draw_text8x8(0, 55, '====================================', color565(255, 255, 0))
    display.draw_text8x8(0, 310, '=====================================', color565(255, 255, 0))
    time.sleep_ms(1000)
    #display.clear()


    #sleep(15)
    #display.cleanup()

while True:
    test()


字库文件 cnfont.py

class Chine:
    chine = [
                0x00,0x40,0x40,0x40,0x40,0x40,0x40,0x7C,0x40,0x40,0x40,0x40,0x40,0x40,0x60,0x00,
                0x00,0x00,0x00,0x00,0xFF,0x22,0x22,0x22,0x22,0x22,0x22,0x5E,0x00,0x00,0x00,0x00,
                0x00,0x00,0xFF,0x02,0x02,0xF2,0x12,0x12,0x12,0x12,0xF2,0x02,0x02,0xFF,0x00,0x00,
                0x00,0x00,0x7F,0x00,0x00,0x07,0x02,0x02,0x02,0x02,0x07,0x10,0x20,0x7F,0x00,0x00,#高#0*/

                0x00,0x40,0x80,0xF4,0x08,0x80,0x40,0x40,0x40,0x40,0xFC,0x40,0x40,0x60,0x40,0x00,
                0x00,0x20,0x27,0xFF,0x20,0x33,0x20,0x40,0x44,0x44,0x7F,0x44,0x44,0x46,0x60,0x00,
                0x00,0x80,0x78,0xFF,0x01,0x0E,0x00,0x00,0xFE,0x12,0x12,0x12,0x12,0xFF,0x00,0x00,
                0x00,0x03,0x00,0x7F,0x00,0x00,0x00,0x00,0x7F,0x01,0x01,0x21,0x61,0x3F,0x00,0x00,#精#1*/

                0x00,0x00,0xE0,0x40,0x40,0x40,0xC0,0x44,0x78,0x40,0xC0,0x40,0x40,0x60,0x40,0x00,
                0x00,0x00,0xFF,0x00,0x08,0x08,0xFF,0x09,0x08,0x08,0x08,0xFF,0x08,0x0C,0x00,0x00,
                0x00,0x00,0xFF,0x00,0x00,0x08,0x1B,0xE9,0x09,0x09,0xC9,0x39,0x08,0x00,0x00,0x00,
                0x00,0x3C,0x03,0x40,0x40,0x20,0x10,0x18,0x0D,0x07,0x18,0x18,0x30,0x70,0x00,0x00,#度#2*/

                0x00,0x00,0x08,0x70,0x00,0x00,0xF0,0x10,0x10,0x10,0x10,0x10,0xF0,0x00,0x00,0x00,
                0x00,0x08,0x70,0x80,0x7F,0x00,0xFF,0x42,0x42,0x42,0x42,0x42,0xFF,0x00,0x00,0x00,
                0x00,0x40,0xE0,0x1F,0x00,0xFC,0x08,0x08,0xF8,0x08,0xF8,0x08,0x08,0xFC,0x00,0x00,
                0x00,0x00,0x3F,0x20,0x20,0x3F,0x20,0x20,0x3F,0x20,0x3F,0x20,0x20,0x3F,0x38,0x00,#温#3*/

                0x00,0x00,0x18,0xF0,0x00,0x00,0xF0,0x10,0x10,0x10,0x10,0x10,0x10,0xF0,0x00,0x00,
                0x00,0x0C,0x78,0xC0,0x3E,0x00,0xFF,0x22,0xA2,0x22,0xA2,0x22,0x22,0x7F,0x00,0x00,
                0x00,0x20,0xF0,0x0F,0x00,0x18,0xE0,0x00,0xFF,0x00,0xFF,0x01,0xE0,0x18,0x04,0x00,
                0x00,0x00,0x1F,0x00,0x20,0x20,0x23,0x20,0x3F,0x20,0x3F,0x21,0x20,0x20,0x30,0x00,#湿#4*/

                0x00,0x00,0xE0,0x40,0x40,0x40,0xC0,0x44,0x78,0x40,0xC0,0x40,0x40,0x60,0x40,0x00,
                0x00,0x00,0xFF,0x00,0x08,0x08,0xFF,0x09,0x08,0x08,0x08,0xFF,0x08,0x0C,0x00,0x00,
                0x00,0x00,0xFF,0x00,0x00,0x08,0x1B,0xE9,0x09,0x09,0xC9,0x39,0x08,0x00,0x00,0x00,
                0x00,0x3C,0x03,0x40,0x40,0x20,0x10,0x18,0x0D,0x07,0x18,0x18,0x30,0x70,0x00,0x00,#度#5*/

                0x00,0x00,0x00,0xFC,0x00,0x00,0x00,0x00,0xC0,0x3C,0x60,0x80,0x00,0x00,0x00,0x00,
                0x00,0x02,0x02,0xFF,0x62,0xC2,0x32,0x4C,0x43,0x40,0x40,0x43,0x4E,0x18,0x18,0x00,
                0x00,0x70,0x0F,0xFF,0x00,0x03,0x08,0xF0,0x00,0x1E,0xE0,0x80,0x7F,0x02,0x00,0x00,
                0x00,0x00,0x00,0x7F,0x00,0x00,0x20,0x23,0x20,0x20,0x20,0x1F,0x20,0x10,0x10,0x00,#检#6*/

                0x00,0x00,0x18,0xF0,0x00,0xF0,0x10,0x10,0x10,0xF8,0x00,0x80,0x80,0xFC,0x08,0x00,
                0x00,0x0C,0x38,0xC0,0x3C,0xFF,0x00,0xFE,0x00,0xFF,0x00,0xFF,0x00,0xFF,0x00,0x00,
                0x00,0x20,0xE0,0x1F,0x00,0x7F,0xC0,0x3F,0x00,0x3F,0x00,0xFF,0x00,0xFF,0x00,0x00,
                0x00,0x00,0x1F,0x40,0x30,0x0C,0x03,0x00,0x03,0x3C,0x00,0x10,0x30,0x7F,0x00,0x00,#测#7*/
                
        ]


aht10.py

import time
from math import log
 
# AHT10 Library for MicroPython on ESP32
# Author: Sean Yong
# Date: 23rd December, 2019
# Version 1.0
 
#CONSTANTS
AHT10_ADDRESS = 0x38 # 0111000 (7bit address)
AHT10_READ_DELAY_MS = 75 # Time it takes for AHT to collect data
AHT_TEMPERATURE_CONST = 200
AHT_TEMPERATURE_OFFSET = 50
KILOBYTE_CONST = 1048576
CMD_INITIALIZE = bytearray([0xE1, 0x08, 0x00])
CMD_MEASURE = bytearray([0xAC, 0x33, 0x00])
FARENHEIT_MULTIPLIER = 9/5
FARENHEIT_OFFSET = 32
 
class AHT10:
    def __init__(self, i2c, mode=0, address=AHT10_ADDRESS):
        if i2c is None:
            raise ValueError('I2C object required.')
        if mode is not (0 and 1):
            raise ValueError('Mode must be either 0 for Celsius or 1 Farenheit')
        self.i2c = i2c
        self.address = address
        self.i2c.writeto(address, CMD_INITIALIZE)
        self.readings_raw = bytearray(8)
        self.results_parsed = [0, 0]
        self.mode = mode # 0 for Celsius, 1 for Farenheit
 
    def read_raw(self):
        self.i2c.writeto(self.address, CMD_MEASURE)
        time.sleep_ms(AHT10_READ_DELAY_MS)
        self.readings_raw = self.i2c.readfrom(AHT10_ADDRESS, 6)
        self.results_parsed[0] = self.readings_raw[1] << 12 | self.readings_raw[2] << 4 | self.readings_raw[3] >> 4
        self.results_parsed[1] = (self.readings_raw[3] & 0x0F) << 16 | self.readings_raw[4] << 8 | self.readings_raw[5]
 
    def humidity(self):
        self.read_raw()
        return (self.results_parsed[0] / KILOBYTE_CONST) * 100 
 
    def temperature(self):
        self.read_raw()
        if self.mode is 0:
            return (self.results_parsed[1] / KILOBYTE_CONST) * AHT_TEMPERATURE_CONST - AHT_TEMPERATURE_OFFSET
        else:
            return ((self.results_parsed[1] / KILOBYTE_CONST) * AHT_TEMPERATURE_CONST - AHT_TEMPERATURE_OFFSET) * FARENHEIT_MULTIPLIER + FARENHEIT_OFFSET
 
    def set_mode(self, mode):
        if mode is not (0 or 1):
            raise ValueError('Mode must be either 0 for Celsius or 1 Farenheit')
        self.mode = mode
 
    def print(self):
        print("Temperature: " + str(self.temperature()) + ("C","F")[self.mode] + ", Humidity: " + str(self.humidity()))
 
    def dew_point(self):
        h = self.humidity()
        prev_mode = self.mode
        self.mode = 0
        h = (log(h, 10) - 2) / 0.4343 + (17.62 * t) / (243.12 + t)
        return 243.12 * h / (17.62 - h)



完整源码:链接: https://pan.baidu.com/s/1DU0g1bAIRQo8l_Xv9smliQ?pwd=1gjj

提取码: 1gjj 复制这段内容后打开百度网盘手机App,操作更方便哦


淘宝链接:https://item.taobao.com/item.htm?spm=a1z10.5-c.w4002-17584875593.11.5a4e49deQMWcmI&id=654657725650


取模软件PCtoLCD2002链接: https://pan.baidu.com/s/1sntqfzPIlxcbHhgCn9ai6g?pwd=2sd8



推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 ESP56.com All Rights Reserved

执行时间: 0.0091540813446045 seconds