一、目的

        这一节我们学习如何使用我们的ESP32开发板来学习LCD12864液晶屏和AHT10温度传感器的操作。

 二、环境

        ESP32 + Thonny + LCD12864液晶屏 + AHT10温度传感器 + 几根杜邦线 + Win10

接线方法:

 

 三、屏幕代码

 

from machine import Pin
import time

cs  = Pin(12,Pin.OUT)
sid = Pin(18,Pin.OUT)
clk = Pin(19,Pin.OUT)
psb = Pin(13,Pin.OUT)

'''
# 方法1
def Lcd12864_Write_Byte(byte):
    
    cs.value(1)
    
    for i in range(0,8,1):
        
        if byte & 0x80: #1000 0000 0x1111 1000
            sid.value(1)
        else:
            sid.value(0)
        
        clk.value(0)
        clk.value(1)
        
        byte <<= 1
        
    cs.value(0)
'''
# 方法2
#写一字节函数
def Lcd12864_Write_Byte(byte):
    cs.value(1) 
    for i in range(0,8,1):  
        sid.value(1 if(byte<<i)&0x80 else 0)
        clk.value(0)
        clk.value(1)
    cs.value(0)
    
# 写指令数据函数 0xf8指令 0xfa数据
def Lcd12864_Write(cmd,data):  # 0xf8 #0xfa 
    Lcd12864_Write_Byte(cmd)
    Lcd12864_Write_Byte(data&0xf0)
    Lcd12864_Write_Byte((data<<4)&0xf0)

#液晶初始化函数
def Lcd12864_Init():
    psb.value(0) #串行4线
    
    Lcd12864_Write(0xf8,0x30)
    Lcd12864_Write(0xf8,0x06)
    Lcd12864_Write(0xf8,0x0c)
    Lcd12864_Write(0xf8,0x01)
    Lcd12864_Write(0xf8,0x80)

# 写显示Txet
def Lcd12864_Write_Text(addr,data):  
    Lcd12864_Write(0xf8,addr)  # 0x80 0x88 0x90 0x98 16个字符 "123456789"
    
    for i in range(len(data)):   
        Lcd12864_Write(0xfa,ord(data[i]))  # utf-8

def main():
    Lcd12864_Init()
    
    Lcd12864_Write_Text(0x80,str("Welcome China!"))
    Lcd12864_Write_Text(0x90,str("----------------"))
    Lcd12864_Write_Text(0x98,str("中".encode("utf-8")))  # 编码
    
    print("中".encode("utf-8"))
    print(b'\xe4\xb8\xad'.decode("utf-8"))
     
    while True:
        for i in range(1000):
            Lcd12864_Write_Text(0x88,str("Num = [ %.4d ]" %i))
            time.sleep_ms(500)


if __name__ == "__main__":
    main()

 四、演示效果

五、温度显示代码

AHT10温度传感器驱动代码:

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)

示例代码:

from machine import Pin,I2C
import time
import aht10

cs  = Pin(12,Pin.OUT)
sid = Pin(18,Pin.OUT)
clk = Pin(19,Pin.OUT)
psb = Pin(13,Pin.OUT)

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

'''
# 方法1
def Lcd12864_Write_Byte(byte):
    
    cs.value(1)
    
    for i in range(0,8,1):
        
        if byte & 0x80: #1000 0000 0x1111 1000
            sid.value(1)
        else:
            sid.value(0)
        
        clk.value(0)
        clk.value(1)
        
        byte <<= 1
        
    cs.value(0)
'''

# 方法2
# 写一字节函数
def Lcd12864_Write_Byte(byte):
    cs.value(1)
    for i in range(0,8,1):
        sid.value(1 if(byte<<i)&0x80 else 0)
        clk.value(0)
        clk.value(1)
    cs.value(0)
    
# 写指令数据函数 0xf8指令 0xfa数据
def Lcd12864_Write(cmd,data):  # 0xf8 # 0xfa 
    Lcd12864_Write_Byte(cmd)
    Lcd12864_Write_Byte(data&0xf0)
    Lcd12864_Write_Byte((data<<4)&0xf0)

# 液晶初始化函数
def Lcd12864_Init():
    psb.value(0)  # 串行4线
    
    Lcd12864_Write(0xf8,0x30)
    Lcd12864_Write(0xf8,0x06)
    Lcd12864_Write(0xf8,0x0c)
    Lcd12864_Write(0xf8,0x01)
    Lcd12864_Write(0xf8,0x80)

# 写显示Txet
def Lcd12864_Write_Text(addr,data):
    Lcd12864_Write(0xf8,addr)  # 0x80 0x88 0x90 0x98 16个字符 "123456789"
    for i in range(len(data)):
        Lcd12864_Write(0xfa,ord(data[i]))  # utf-8

def AHT10_Read_Data():
    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))
    time.sleep_ms(100)
    
def main():
    Lcd12864_Init()
    Lcd12864_Write_Text(0x81,str("I2C - AHT10"))
    Lcd12864_Write_Text(0x90,str("----------------"))
    
    while True:
        AHT10_Read_Data()


if __name__ == "__main__":
    main()

六、演示效果

七、购买链接:

LCD12864液晶购买地址:

https://item.taobao.com/item.htm?spm=a1z09.2.0.0.51922e8d2uqrYB&id=623290143528&_u=1p01rchf810icon-default.png?t=N176https://item.taobao.com/item.htm?spm=a1z09.2.0.0.51922e8d2uqrYB&id=623290143528&_u=1p01rchf810      宝贝详情图片:

 

 我收到的屏幕和宝贝详情里的不一样,是下面这样的,不过不影响使用:

排针是我自己焊接的,有点丑哈。。

 

AHT10购买地址:

https://item.taobao.com/item.htm?spm=a1z09.2.0.0.51922e8d2uqrYB&id=630894804483&_u=1p01rch844eicon-default.png?t=N176https://item.taobao.com/item.htm?spm=a1z09.2.0.0.51922e8d2uqrYB&id=630894804483&_u=1p01rch844e

 

更多推荐