Thứ Bảy, 29 tháng 1, 2022

Kết nối màn hình LCD 162 LCM1602A-14 DS-162-3V3-SPI2C V6 với ESP8266 ESP32 bằng I2C trên Micropython

 Màn hình LCM1602A-14 DS-162-3V3-SPI2C V6 có ưu điểm là có cả 2 giao thức I2C và SPI, đồng thời dùng điện áp 3.3V, phù hợp cho ESP8266 / ESP32 hoặc Pico.



Kết nối VSS -> GND, VDD ->3.3V 
ESP8266:sda=12(D6), scl=14(D5)hoặc sda=12(D6), scl=13(D7)
ESP32GPIO21(SDA), GPIO22(SCL)
 

Bài này, hướng dẫn kết nối màn hình với ESP8266 và ESP32

Bước 1: Tải thư viện về máy và lưu vào ESP8266 / ESP32 với tên LCD_I2C.py

# Use lib for ESP8266 and ESP32 with LCD both I2C and SPI model LCM1602A-14 DS-162-3V3-SPI2C V6

import machine

import utime



COLUMNS = 16

ROWS    = 2



#Instruction set

CLEARDISPLAY         = 0x01



ENTRYMODESET         = 0x04

ENTRYLEFT            = 0x02

ENTRYRIGHT           = 0x00

ENTRYSHIFTINCREMENT  = 0x01

ENTRYSHIFTDECREMENT  = 0x00



DISPLAYCONTROL       = 0x08

DISPLAYON            = 0x04

DISPLAYOFF           = 0x00

CURSORON             = 0x02

CURSOROFF            = 0x00

BLINKON              = 0x01

BLINKOFF             = 0x00



FUNCTIONSET          = 0x20

_5x10DOTS            = 0x04

_5x8DOTS             = 0x00

_1LINE               = 0x00

_2LINE               = 0x08

_8BITMODE            = 0x10

_4BITMODE            = 0x00



class LCD:

    def __init__(self, sda, scl):

       

        self.column = 0

        self.row = 0

       

        self.address = 62 #0x3e

       

        self.command = bytearray(2)



        _sda = machine.Pin(sda)

        _scl = machine.Pin(scl)

       

        if (sda==12 and scl==13) or (sda==12 and scl==14) :

            self.i2c=machine.I2C(sda=_sda, scl=_scl, freq=400000)

        else:

            self.i2c=machine.SoftI2C(sda=_sda, scl=_scl, freq=400000)

       

        utime.sleep_ms(50)

       

        for i in range(3):

            self._command(FUNCTIONSET | _2LINE )

            utime.sleep_ms(10)

       

        self.on()

        self.clear()

       

        self._command(ENTRYMODESET | ENTRYLEFT | ENTRYSHIFTDECREMENT)

       

        self.set_cursor(0, 0)

       

    def on(self, cursor=False, blink=False):

        if cursor == False and blink == False:

            self._command(DISPLAYCONTROL | DISPLAYON | CURSOROFF | BLINKOFF)

        elif cursor == False and blink == True:

            self._command(DISPLAYCONTROL | DISPLAYON | CURSOROFF | BLINKON)

        elif cursor == True and blink == False:

            self._command(DISPLAYCONTROL | DISPLAYON | CURSORON | BLINKOFF)

        elif cursor == True and blink == True:

            self._command(DISPLAYCONTROL | DISPLAYON | CURSORON | BLINKON)        

   

    def off(self):

        self._command(DISPLAYCONTROL | DISPLAYOFF | CURSOROFF | BLINKOFF)

       

    def clear(self):

        self._command(CLEARDISPLAY)

        self.set_cursor(0, 0)

   

   

    def set_cursor(self, column, row):

        column = column % COLUMNS

        row = row % ROWS

        if row == 0:

            command = column | 0x80

        else:

            command = column | 0xC0

        self.row = row

        self.column = column

        self._command(command)

   

    def write(self, s):

        for i in range(len(s)):

            utime.sleep_ms(10)

            self.i2c.writeto(self.address, b'\x40'+s[i])

            self.column = self.column + 1

            if self.column >= COLUMNS:

                self.set_cursor(0, self.row+1)

       

    def _command(self, value):

        self.command[0] = 0x80

        self.command[1] = value

        self.i2c.writeto(self.address, self.command)

        utime.sleep_ms(1)


Bước 2: Tải file Code này về và test thử

# /*******************************************************************************

#  * Sử dụng màn hình LCD loại LCM1602A-14 DS-162-3V3-SPI2C V6 (adress: 0x3e = 62)có cả giao thức I2C và SPI Trên ESP8266

#  ********************************************************************************

#  * Connect ESP8266:

#  * option1: sda=12(D6), scl=14(D5)

#  * option2: sda=12(D6), scl=13(D7)

#  *

#  * Connect ESP32

#  * The ESP32 has two I2C channels and any pin can be set as SDA or SCL. When using the ESP32 with the Arduino IDE, the default I2C pins are

#  * GPIO21(SDA), GPIO22(SCL)

#  *******************************************************************************/



from LCD_I2C import *    #import LCD_I2C library

from machine import Pin

from time import sleep

led2 = Pin(2, Pin.OUT);



#ESP8266

lcd = LCD(sda=12, scl=14)



#ESP32

#lcd = LCD(sda=21, scl=22)



lcd.set_cursor(0,0)          # Set the cursor at first column, first row

lcd.write("Hello World")     # Write string to the LCD  

sleep(1)               # Delay for 1 second



lcd.off()                    # Off the lcd display without clearing the data

sleep(1)



lcd.on()                     # On LCD display without cursor and blink - cursor=False, blink=False - by default

sleep(1)



lcd.clear()

lcd.on(cursor=False, blink=True) # On LCD display without cursor and with blink

lcd.write("Without cursor and with blink")

sleep(3)



lcd.clear()

lcd.on(cursor=True, blink=False) # On LCD display with cursor and without blink

lcd.write("With cursor and without blink")

sleep(3)



lcd.clear()

lcd.on(cursor=True, blink=True)  # On LCD display with cursor and with blink

lcd.write("With cursor and blink")

sleep(3)



lcd.on(cursor = False, blink = False)

lcd.clear()

lcd.set_cursor(0,0)

lcd.write("Temp:")

lcd.write("30")

lcd.set_cursor(0,1)

lcd.write("RH:")

lcd.write("89%")

sleep(3)



lcd.clear()                 # Clear the data in display

i = 0



while True:

    while (i < 20):

        lcd.set_cursor(6,0) #Colum 6 and rows first

        lcd.write(str(i*1000))

        utime.sleep(0.1)

        i = i + 1

        lcd.clear()

   

    if (led2.value()==0):

        led2.value(1)

        lcd.clear()

        lcd.write("Led is: OFF")

        print("Led is: OFF")

    else:

        led2.value(0)

        lcd.clear()

        lcd.write("Led is: ON ")

        print("Led is: ON")

    sleep(1)


Không có nhận xét nào:

Đăng nhận xét