from machine import Pin, SPI
import framebuf
import utime
# LCD size
WIDTH = 84
HEIGHT = 48
# SPI setup (SPI1)
spi = SPI(1, baudrate=2000000, polarity=0, phase=0,
sck=Pin(10), mosi=Pin(11))
# Control pins
dc = Pin(14, Pin.OUT)
cs = Pin(13, Pin.OUT)
rst = Pin(12, Pin.OUT)
# Frame buffer
buffer = bytearray(WIDTH * HEIGHT // 8)
fb = framebuf.FrameBuffer(buffer, WIDTH, HEIGHT, framebuf.MONO_VLSB)
# Send command
def lcd_cmd(cmd):
dc.value(0)
cs.value(0)
spi.write(bytearray([cmd]))
cs.value(1)
# Send data
def lcd_data(data):
dc.value(1)
cs.value(0)
spi.write(data)
cs.value(1)
# Initialize LCD
def lcd_init():
rst.value(0)
utime.sleep_ms(50)
rst.value(1)
lcd_cmd(0x21) # Extended commands
lcd_cmd(0xB1) # Contrast
lcd_cmd(0x04) # Temp coefficient
lcd_cmd(0x14) # Bias mode
lcd_cmd(0x20) # Basic commands
lcd_cmd(0x0C) # Normal display mode
# Update screen
def lcd_update():
lcd_cmd(0x40)
lcd_cmd(0x80)
lcd_data(buffer)
# Clear screen
def lcd_clear():
fb.fill(0)
lcd_update()
# Initialize
lcd_init()
lcd_clear()
counter = 0
while True:
fb.fill(0)
# Title
fb.text("Pico W Demo", 5, 0)
# Counter
fb.text("Count:", 5, 15)
fb.text(str(counter), 50, 15)
# Animated bar
bar_width = (counter % 84)
fb.fill_rect(0, 35, bar_width, 8, 1)
lcd_update()
counter += 1
utime.sleep(0.3)