# Raspberry Pi Pico 资源总结 ## API、Demo 及网络资源 - [Raspberry Pi Pico and Pico W 官网说明及资源](https://www.raspberrypi.com/documentation/microcontrollers/raspberry-pi-pico.html) - [MicroPython documentation](https://docs.micropython.org/en/latest/index.html):分为 - Python standard libraries and micro-libraries - MicroPython-specific libraries - Port-specific libraries:其中 rp2 为 Raspberry Pi Pico 的 Port-specific library. - [MicroPython 文档(中文版)](http://www.86x.org/en/latet/index-2.html) ## 特定功能代码段 ### 测量 PWM 周期和占空比 ```python from machine import Pin, PWM from rp2 import PIO, StateMachine, asm_pio import time @rp2.asm_pio(set_init=rp2.PIO.IN_LOW, autopush=True, push_thresh=32) def period(): wrap_target() set(x, 0) wait(0, pin, 0) # Wait for pin to go low wait(1, pin, 0) # Low to high transition label('low_high') jmp(x_dec, 'next') [1] # unconditional label('next') jmp(pin, 'low_high') # while pin is high label('low') # pin is low jmp(x_dec, 'nxt') label('nxt') jmp(pin, 'done') # pin has gone high: all done jmp('low') label('done') in_(x, 32) # Auto push: SM stalls if FIFO full wrap() @rp2.asm_pio(set_init=rp2.PIO.IN_LOW, autopush=True, push_thresh=32) def mark(): wrap_target() set(x, 0) wait(0, pin, 0) # Wait for pin to go low wait(1, pin, 0) # Low to high transition label('low_high') jmp(x_dec, 'next') [1] # unconditional label('next') jmp(pin, 'low_high') # while pin is high in_(x, 32) # Auto push: SM stalls if FIFO full wrap() pin16 = Pin(16, Pin.IN, Pin.PULL_UP) sm0 = rp2.StateMachine(0, period, in_base=pin16, jmp_pin=pin16) sm0.active(1) sm1 = rp2.StateMachine(1, mark, in_base=pin16, jmp_pin=pin16) sm1.active(1) pwm = PWM(Pin(17)) pwm.freq(1000) pwm.duty_u16(0xffff // 3) # Clock is 125MHz. 3 cycles per iteration, so unit is 24.0ns def scale(v): return (1 + (v ^ 0xffffffff)) * 24e-6 # Scale to ms while True: period = scale(sm0.get()) mark = scale(sm1.get()) print(period, mark, mark/period) time.sleep(0.2) ```