from machine import UART, Pin
import time
# 初始化 UART,使用默认引脚 (TX: GPIO1, RX: GPIO3),2025.6.9 仿真平台要有串口1
uart = UART(1, baudrate=115200, tx=1, rx=3)
#uart = UART(2, baudrate=115200, tx=17, rx=16)
# 初始化 LED 引脚
led = Pin(15,Pin.OUT)
# 运行前提示信息
uart.write("\rPlease enter 1 or 0,1 is open led,0 is close led:\n")
while True:
uart.write("\r") #将光标移动到当前行的最左边,确保每次输入的数据都从最左边开始显示。
# 读取串口数据
if uart.any():
data = uart.read()
print("Received:", data) # 在 REPL 中打印接收到的数据
# 如果收到 '1',点亮 LED 并输出 1(靠最左边输出)
if data == b'1':
led.value(1) # 点亮 LED
uart.write("\rinput 1, LED is ON.\n") # 在串口监视器中靠最左边输出输出状态
# 如果收到 '0',熄灭 LED 并输出 0(靠最左边输出)
elif data == b'0':
led.value(0) # 熄灭 LED
uart.write("\rinput 0, LED is OFF.\n") # 在串口监视器中靠最左边输出输出状态
# 如果输入无效,提示重新输入
else:
uart.write("\rInvalid input! Please enter 1 or 0.\n")