from machine import I2C, Pin
from time import sleep
from pico_i2c_lcd import I2cLcd

# 初始化硬件
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)
lcd = I2cLcd(i2c, i2c.scan()[0], 2, 16)
btn_up = Pin(3, Pin.IN, Pin.PULL_UP)
btn_down = Pin(28, Pin.IN, Pin.PULL_UP)

counter = 0
prev_count = counter
lcd.putstr(f"Count: {counter}")  # 初始显示

while True:
    # 按键消抖与计数
    if btn_up.value() == 0:
        sleep(0.05)  # 消抖延时
        if btn_up.value() == 0:
            counter += 1
    if btn_down.value() == 0:
        sleep(0.05)
        if btn_down.value() == 0:
            counter -= 1
    
    # 更新显示(计数变化时)
    if counter != prev_count:
        lcd.clear()
        lcd.putstr(f"Count: {counter}")
        prev_count = counter
    
    sleep(0.01)  # 主循环延时
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT