You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Raspberry Pi Pico W连接Waveshare 1.3寸LCD无法显示文本求助

问题描述

使用2022款Raspberry Pi Pico W,背部连接1.3寸Waveshare Pico LCD显示屏,屏幕亮度可调、按键功能正常,但无法显示任何文本。相关代码如下:

显示屏驱动代码

from machine import Pin,SPI,PWM
import framebuf
import utime
import os
import math
# ============ Start of Drive Code ================
BL = 13  # 显示屏使用的引脚
DC = 8
RST = 12
MOSI = 11
SCK = 10
CS = 9

class LCD_1inch3(framebuf.FrameBuffer):
    def __init__(self):
        self.width = 240
        self.height = 240
        
        self.cs = Pin(CS,Pin.OUT)
        self.rst = Pin(RST,Pin.OUT)
        
        self.cs(1)
        self.spi = SPI(1)
        self.spi = SPI(1,1000_000)
        self.spi = SPI(1,100000_000,polarity=0, phase=0,sck=Pin(SCK),mosi=Pin(MOSI),miso=None)
        self.dc = Pin(DC,Pin.OUT)
        self.dc(1)
        self.buffer = bytearray(self.height * self.width * 2)
        super().__init__(self.buffer, self.width, self.height, framebuf.RGB565)
        self.init_display()
        
        self.red   =   0x07E0 # 预定义颜色
        self.green =   0x001f # 建议使用下方定义的colour(r,g,b)函数
        self.blue  =   0xf800
        self.white =   0xffff
        
    def write_cmd(self, cmd):
        self.cs(1)
        self.dc(0)
        self.cs(0)
        self.spi.write(bytearray([cmd]))
        self.cs(1)  

    def write_data(self, buf):
        self.cs(1)
        self.dc(1)
        self.cs(0)
        self.spi.write(bytearray([buf]))
        self.cs(1)

    def init_display(self):
        """初始化显示屏"""  
        self.rst(1)
        self.rst(0)
        self.rst(1)
        
        self.write_cmd(0x36)
        self.write_data(0x70)

        self.write_cmd(0x3A) 
        self.write_data(0x05)

        self.write_cmd(0xB2)
        self.write_data(0x0C)
        self.write_data(0x0C)
        self.write_data(0x00)
        self.write_data(0x33)
        self.write_data(0x33)

        self.write_cmd(0xB7)
        self.write_data(0x35) 

        self.write_cmd(0xBB)
        self.write_data(0x19)

        self.write_cmd(0xC0)
        self.write_data(0x2C)

        self.write_cmd(0xC2)
        self.write_data(0x01)

        self.write_cmd(0xC3)
        self.write_data(0x12)   

        self.write_cmd(0xC4)
        self.write_data(0x20)

        self.write_cmd(0xC6)
        self.write_data(0x0F) 

        self.write_cmd(0xD0)
        self.write_data(0xA4)
        self.write_data(0xA1)

        self.write_cmd(0xE0)
        self.write_data(0xD0)
        self.write_data(0x04)
        self.write_data(0x0D)
        self.write_data(0x11)
        self.write_data(0x13)
        self.write_data(0x2B)
        self.write_data(0x3F)
        self.write_data(0x54)
        self.write_data(0x4C)
        self.write_data(0x18)
        self.write_data(0x0D)
        self.write_data(0x0B)
        self.write_data(0x1F)
        self.write_data(0x23)

        self.write_cmd(0xE1)
        self.write_data(0xD0)
        self.write_data(0x04)
        self.write_data(0x0C)
        self.write_data(0x11)
        self.write_data(0x13)
        self.write_data(0x2C)
        self.write_data(0x3F)
        self.write_data(0x44)
        self.write_data(0x51)
        self.write_data(0x2F)
        self.write_data(0x1F)
        self.write_data(0x1F)
        self.write_data(0x20)
        self.write_data(0x23)
        
        self.write_cmd(0x21)

        self.write_cmd(0x11)

        self.write_cmd(0x29)

    def show(self):
        self.write_cmd(0x2A)
        self.write_data(0x00)
        self.write_data(0x00)
        self.write_data(0x00)
        self.write_data(0xef)
        
        self.write_cmd(0x2B)
        self.write_data(0x00)
        self.write_data(0x00)
        self.write_data(0x00)
        self.write_data(0xEF)
        
        self.write_cmd(0x2C)
        
        self.cs(1)
        self.dc(1)
        self.cs(0)
        self.spi.write(self.buffer, buf_len=self.width*self.height*2)
        self.cs(1)
# ========= End of Driver ===========

def colour(R,G,B):
# 获取红色值
    rp = int(R*31/255) # 范围0-31
    if rp < 0: rp = 0
    r = rp *8
# 获取绿色值 - 稍复杂
    gp = int(G*63/255) # 范围0-63
    if gp < 0: gp = 0
    g = 0
    if gp & 1:  g = g + 8192
    if gp & 2:  g = g + 16384
    if gp & 4:  g = g + 32768
    if gp & 8:  g = g + 1
    if gp & 16: g = g + 2
    if gp & 32: g = g + 4
# 获取蓝色值       
    bp =int(B*31/255) # 范围0-31
    if bp < 0: bp = 0
    b = bp *256
    colour = r+g+b
    return colour

主程序代码

pwm = PWM(Pin(BL)) # Screen Brightness
pwm.freq(1000)
pwm.duty_u16(65534) # max 65535 - mid value

LCD = LCD_1inch3()
while True:
    # Background colour - dark grey
    LCD.fill(colour(0,0,0))

    # Define pins for buttons and Joystick
    keyA = Pin(15,Pin.IN,Pin.PULL_UP) # Normally 1 but 0 if pressed
    keyB = Pin(17,Pin.IN,Pin.PULL_UP)
    keyX = Pin(19,Pin.IN,Pin.PULL_UP)
    keyY= Pin(21,Pin.IN,Pin.PULL_UP)

    up = Pin(2,Pin.IN,Pin.PULL_UP)
    down = Pin(18,Pin.IN,Pin.PULL_UP)
    left = Pin(16,Pin.IN,Pin.PULL_UP)
    right = Pin(20,Pin.IN,Pin.PULL_UP)
    ctrl = Pin(3,Pin.IN,Pin.PULL_UP)
    LCD.text('Hi', 120, 120, colour(255, 255, 255))
问题解决

核心问题

主程序中从未调用LCD.show()方法,所有绘制操作(fill、text)都只修改了内存中的帧缓冲区,没有将内容同步到物理屏幕上,这是屏幕无显示的直接原因。

额外优化点

  1. 按键引脚初始化重复:原代码在循环内每次都重新初始化按键引脚,属于无效操作,应移到循环外执行一次。
  2. SPI初始化冗余:驱动中SPI被重复赋值三次,可简化为一行代码。

修改后的主程序代码

pwm = PWM(Pin(BL)) # 屏幕亮度控制
pwm.freq(1000)
pwm.duty_u16(65534) # 最大值为65535,此处设为接近最大值

LCD = LCD_1inch3()

# 按键和摇杆引脚定义移至循环外,避免重复初始化
keyA = Pin(15, Pin.IN, Pin.PULL_UP) # 默认高电平,按下时为低电平
keyB = Pin(17, Pin.IN, Pin.PULL_UP)
keyX = Pin(19, Pin.IN, Pin.PULL_UP)
keyY = Pin(21, Pin.IN, Pin.PULL_UP)

up = Pin(2, Pin.IN, Pin.PULL_UP)
down = Pin(18, Pin.IN, Pin.PULL_UP)
left = Pin(16, Pin.IN, Pin.PULL_UP)
right = Pin(20, Pin.IN, Pin.PULL_UP)
ctrl = Pin(3, Pin.IN, Pin.PULL_UP)

while True:
    # 填充背景为黑色
    LCD.fill(colour(0, 0, 0))
    # 在屏幕中央绘制白色文本
    LCD.text('Hi', 120, 120, colour(255, 255, 255))
    # 关键:调用show()将缓冲区内容刷新到屏幕
    LCD.show()

可选:简化驱动中的SPI初始化

将驱动中三次SPI赋值替换为一行:

self.spi = SPI(1, baudrate=100000000, polarity=0, phase=0, sck=Pin(SCK), mosi=Pin(MOSI), miso=None)

内容的提问来源于stack exchange,提问作者Minion123

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 14:59:59