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

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

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

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

Micropython ESP频道

micropython esp32 驱动ST7302屏幕


ST7302

arduino版本见 https://github.com/zhcong/ST7302-for-arduino
工作为ST7302液晶屏,而MCU是Raspberry Pi pico。ST7302具有复杂的显示转换映射和存储器集命令,在此链接中查看更多内容。并且此驱动程序具有部分更新功能。



main.py

from machine import Pin, SPI
import framebuf
import Screen

spi=SPI(id=1, baudrate=10_000_000, polarity=0, phase=0, sck=Pin(10, Pin.OUT), mosi=Pin(11,Pin.OUT))

dc_pin=Pin(0,Pin.OUT)
rest_pin=Pin(1,Pin.OUT,Pin.PULL_UP)

screen = Screen.ST7032(spi, dc_pin, rest_pin, 250, 122)
screen.text('hello word', 5, 5)
screen.fill_rect(5, 20, 10, 20, 1)
screen.text('hello2 word2', 150, 50)
screen.line(5,50,220,100,1)
screen.flush_buffer()

Screen.py

import time, math, framebuf, utime, gc

class ST7302(framebuf.FrameBuffer):
    # TODO 电压不稳
    # TODO 简化代码逻辑

    def __init__(self, spi, dc_pin, rest_pin, width, height):
        self.spi = spi
        self.rest_pin = rest_pin
        self.dc_pin = dc_pin
        # buffer max 7.5kb
        self.buffer = bytearray(math.ceil(height/8)*(width if width%2==0 else width+1))
        # for judgment change
        self.old_buffer = bytearray(math.ceil(height/8)*(width if width%2==0 else width+1))
        self.width = width
        self.height = height
        self.bit_column_fill = [ 0x00 for _ in range(3-math.ceil(height%12/4))] # once param must 3 bit
        super().__init__(self.buffer, width, height, framebuf.MONO_VLSB)
        self.init_lcd()

    def __sleep_waith_command(self, sleep_time=100):
        time.sleep_ms(sleep_time)
    
    def get_buffer(self):
        return self.buffer

    def inv_off(self):
        self.send_command(0x20)
        self.__sleep_waith_command()
    
    def inv_on(self):
        self.send_command(0x21)
        self.__sleep_waith_command()
    
    def hard_rest(self):
        self.rest_pin.value(0)
        self.__sleep_waith_command(200)
        self.rest_pin.value(1)
    
    def clear(self):
        self.fill(0)
        self.old_buffer[:] = self.buffer
        self.send_command(0xb9)  #enable CLR RAM
        self.send_param(0xe3)
        self.__sleep_waith_command(1000)
        self.send_command(0xb9)  #disable CLR RAM
        self.send_param(0x23)

    def send_command(self, command):
        self.dc_pin.value(0)
        data = bytearray(1)
        data[0]=command
        self.spi.write(data)
        self.dc_pin.value(1)

    def send_param(self, param):
        data = bytearray(1)
        data[0]=param
        self.spi.write(data)

    def __judgment_change_column(self, buffer, old_buffer):
        change_list=set()
        for i, bits in enumerate(buffer):
            if not old_buffer[i] == bits:
                # update two line together
                change_list.add(i%self.width if i%self.width%2 == 0 else i%self.width - 1)
        return change_list


    def __shift_buffer(self, new_pos, cache):
        self.send_command(0x2a) # Row set
        self.send_param(0x19) # 0x19 is access memory start
        self.send_param(0x19+self.width)
        self.send_command(0x2b) # column set
        self.send_param(new_pos)
        self.send_param(self.height)
        self.send_command(0x2c) # write memory
        for item in cache:
            self.send_param(item)

    def flush_buffer(self):
        column_list = [[] for _ in range(self.width)]
        change_column_index_list = self.__judgment_change_column(self.buffer, self.old_buffer)

        for i,bit in enumerate(self.buffer):
            column_list[i%self.width].append(bit)

        for column_i in change_column_index_list:
            two_column_param=[]
            for bit_i, bit in enumerate(column_list[column_i]):
                bit1 = self.__mix_bit(bit, column_list[column_i+1][bit_i])
                two_column_param.append(bit1)
                bit2 = self.__mix_bit(bit>>4, column_list[column_i+1][bit_i]>>4)
                two_column_param.append(bit2)
            self.__shift_buffer(column_i//2, two_column_param + self.bit_column_fill)
        if not len(change_column_index_list) == 0:
            self.old_buffer[:] = self.buffer
        gc.collect()

    def __mix_bit(self, bit1:int, bit2:int):
        mix_bit=0x00
        mix_bit=mix_bit|((bit1&0x01)<<7)
        mix_bit=mix_bit|((bit2&0x01)<<6)
        mix_bit=mix_bit|((bit1&0x02)<<4)
        mix_bit=mix_bit|((bit2&0x02)<<3)
        mix_bit=mix_bit|((bit1&0x04)<<1)
        mix_bit=mix_bit|((bit2&0x04)<>2)
        mix_bit=mix_bit|((bit2&0x08)>>3)
        return mix_bit

    def init_lcd(self):
        self.spi.init()
        self.hard_rest()
        self.send_command(0xeb) # Enable OTP
        self.send_param(0x02)
        self.send_command(0xd7) # OTP Load Control
        self.send_param(0x68)
        self.send_command(0xb4)  #GateEQSettingHPMEQLPMEQ
        self.send_param(0xa5)
        self.send_param(0x66)
        self.send_param(0x01)
        self.send_param(0x00)
        self.send_param(0x00)
        self.send_param(0x40)
        self.send_param(0x01)
        self.send_param(0x00)
        self.send_param(0x00)
        self.send_param(0x40)
        self.send_command(0x11) # sleep out
        self.__sleep_waith_command()
        self.send_command(0x36) # Memory Data Access Control
        self.send_param(0x00)
        self.send_command(0x39) # Low power
        self.send_command(0x3a) # DataFormatSelect4writefor24bit
        self.send_param(0x11)
        self.send_command(0xb0)  # Duty setting
        self.send_param(0x64)  # 250duty/4=63
        self.send_command(0xb8)  # Panel Setting Frame inversion
        self.send_param(0x09)  # 250duty/4=63
        self.send_command(0x29) # Display on

        self.clear()


开源地址:https://github.com/zhcong/ST7302-for-micropython



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

Copyright © 2014 ESP56.com All Rights Reserved

执行时间: 0.0074491500854492 seconds