Data Transceiver Practice: hardware Configuration, MicroPython Programming
This experiment realizes UART send & receive on Fengya No.1 board, with bytes decoding, LED prompt and verification using XCOM.
【Preliminary Note】The original hardware example in this article was written based on the RP2040. The actual hardware used in this hands-on demonstration features the W55RP20 as the main controller chip. The circuit logic and UF2 flashing operation principles are universally applicable, with only the main controller model differing. The original chip model mentioned in the circuit descriptions below is provided for reference purposes only.
Tweet
MiniUSB cable to connect the Fengya No. 1 Board - Universal Compatible Expansion Board 's UART0 to the USB port on it with the USB port on the computer:UART) data transmission and reception.elegance-devkit v1\Demo\07 UART_ReadWrite:# Python env : MicroPython v1.23.0
# -*- coding: utf-8 -*-
# @Time : 2024/6/23 下午9:16
# @Author : 李清水
# @File : main.py
# @Description : UART类实验,实现简单的串口数据收发
# ======================================== 导入相关模块 =========================================
# 硬件相关的模块
from machine import UART, Pin
# 时间相关的模块
import time
# ======================================== 全局变量 ============================================
# 串口接收计数变量
recv_count: int = 0
# ======================================== 功能函数 ============================================
def LED_Blink(led_obj: Pin, delay_time: int) -> None:
"""
控制LED灯闪烁。
进行LED灯的子系统闪烁控制,指示程序停止运行。
Args:
led_obj (Pin): LED对象,用于控制LED灯的GPIO控制器
delay_time (int): 延时时间,单位为毫秒
Returns:
None
"""
while True:
# 点亮LED
led_obj.on()
# 延时 delay_time ms
time.sleep_ms(delay_time)
# 熄灭LED灯
led_obj.off()
# 再次延时 delay_time ms
time.sleep_ms(delay_time)
# ======================================== 自定义类 ============================================
# ======================================== 初始化配置 ==========================================
# 上电延时3s
time.sleep(3)
# 打印调试信息
print("FreakStudio : UART Read and Write Experiment")
# 创建串口对象,设置波特率为115200
uart: UART = UART(0, 115200)
# 初始化uart对象,波特率为115200,数据位为8,无校验位,停止位为1
# 设置串口超时时间为100ms
uart.init(baudrate = 115200,
bits = 8,
parity = None,
stop = 1,
tx = 0,
rx = 1,
timeout = 100)
# 设置GPIO 25为LED输出引脚,下拉电阻使能
LED: Pin = Pin(25, Pin.OUT, Pin.PULL_DOWN)
# ======================================== 主程序 ===========================================
# 循环执行
while True:
# 打印调试信息,输出串口收发次数
print("UART0 RECV COUNT: ", recv_count)
# 延时100ms
time.sleep_ms(100)
# 判断串口接收计数变量是否大于等于10
if recv_count >= 10:
# 串口接收计数变量归零
recv_count = 0
break
# 向串口发送数据
uart.write("Hello, UART!")
# 发送调试信息
print("UART0 SEND DATA")
# 检查是否有数据可读
if uart.any():
# 打印可读取的字符数
print("number of characters that can be read:", uart.any())
# 点亮LED灯,做指示作用
LED.on()
# 读取一行数据
data = uart.readline()
# 接收到数据为 b'Hello, UART!\r\n',字符串是bytes类型
print("UART0 RECV DATA: ", data)
# 对接收到数据data进行解码
print("DECODE UART0 RECV DATA: ", data.decode('utf-8'))
# 延时300ms
time.sleep_ms(300)
# 关闭LED灯
LED.off()
# 延时300ms
time.sleep_ms(300)
# 串口接收计数变量递增
recv_count = recv_count + 1
# 收发10次后,跳出循环,LED灯闪烁
print("Stop Program Running")
LED_Blink(LED, 100)while loop to implement serial data transmission and reception, send the string Hello, UART! each time and try to read data, decode and print the received data, meanwhile control the LED to turn on and off to indicate the receiving operationLED_Blink function to make the LED blink continuously# 对接收到数据data进行解码
print("DECODE UART0 RECV DATA: ", data.decode('utf-8'))XCOM software can normally receive the data sent by the Raspberry Pi Pico.Hello, Pico! to the Raspberry Pi Pico:XCOM software, the Raspberry Pi Pico 's onboard LED will blink once. After ten consecutive transmissions, the Raspberry Pi Pico stops receiving serial data, and its onboard LED keeps blinking continuously:Fengya No. 1 Board - Universal Compatible Expansion Board the serial port transmit-receive indicator on it LED3 will light up intermittently: