【树莓派 001】 RP2040 MicroPython 开发环境配置+GPIO点灯+PIO点灯(Programmable Input/Output)

开发环境配置

在这里插入图片描述
在这里插入图片描述

GPIO点灯

  • RP2 快速参考中给出了灯的位置:
    在这里插入图片描述
  • 需要先改变当前的解释器(右下角),然后将pyton代码保存到单片机上去:
    在这里插入图片描述
  • 程序如下:
from machine import Pin
import time

lamp = Pin(25, Pin.OUT)

while True:
    lamp.value(1)   # 开灯
    time.sleep(1)
    lamp.value(0)   # 关灯
    time.sleep(1)
  • 点击运行即可:

在这里插入图片描述

PIO点灯

  • Python 直接控制 GPIO,延时靠 time.sleep()不精确.如果 Python 在做别的事,就会卡顿,不能做高速或严格时序信号

在这里插入图片描述

# Example using PIO to blink an LED and raise an IRQ at 1Hz.

import time
from machine import Pin
import rp2


@rp2.asm_pio(set_init=rp2.PIO.OUT_LOW)
def blink_1hz():
    # Cycles: 1 + 1 + 6 + 32 * (30 + 1) = 1000
    irq(rel(0))
    set(pins, 1)
    set(x, 31)                  [5]
    label("delay_high")
    nop()                       [29]
    jmp(x_dec, "delay_high")

    # Cycles: 1 + 7 + 32 * (30 + 1) = 1000
    set(pins, 0)
    set(x, 31)                  [6]
    label("delay_low")
    nop()                       [29]
    jmp(x_dec, "delay_low")


# Create the StateMachine with the blink_1hz program, outputting on Pin(25).
sm = rp2.StateMachine(0, blink_1hz, freq=2000, set_base=Pin(25))

# Set the IRQ handler to print the millisecond timestamp.
sm.irq(lambda p: print(time.ticks_ms()))

# Start the StateMachine.
sm.active(1)
  • 完整周期 = 2000 cycles → 1Hz 严格精准
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值