树莓派 Pico 代码例程 — MicroPython + C SDK + PIO

知识库
知识库文档
/firmware/开发板/树莓派 Pico (Raspberry Pi Pico)/树莓派 Pico 代码例程 — MicroPython + C SDK + PIO.md

文档

树莓派 Pico 代码例程

Pico 支持 MicroPython(适合快速原型)和 C/C++ SDK(适合性能敏感场景)。


示例 1:MicroPython — LED 闪烁 + 板载 LED

"""Pico MicroPython — 板载 LED (GP25) 闪烁"""
from machine import Pin
import time

led = Pin(25, Pin.OUT)  # Pico 板载 LED 接 GP25

while True:
    led.toggle()
    time.sleep(0.5)

示例 2:MicroPython — ADC 读取 + PWM 输出

"""Pico MicroPython — 电位器控制 LED 亮度"""
from machine import Pin, ADC, PWM
import time

pot = ADC(26)     # GP26 (ADC0)
led = PWM(Pin(15))  # GP15 PWM
led.freq(1000)      # 1kHz

while True:
    val = pot.read_u16()  # 0-65535
    led.duty_u16(val)
    time.sleep(0.05)

示例 3:MicroPython — I²C OLED 显示 (SSD1306)

"""Pico MicroPython — SSD1306 OLED 128x64 I2C"""
from machine import Pin, I2C
import ssd1306

i2c = I2C(0, scl=Pin(1), sda=Pin(0), freq=400000)
oled = ssd1306.SSD1306_I2C(128, 64, i2c)

oled.fill(0)
oled.text("Raspberry Pi", 0, 0)
oled.text("Pico RP2040", 0, 16)
oled.text("Hello World!", 0, 32)
oled.show()

示例 4:PIO — WS2812 灯带驱动(MicroPython)

"""Pico PIO 驱动 WS2812B — 使用 rp2 汇编"""
import rp2
from machine import Pin
import array, time

@rp2.asm_pio(sideset_init=rp2.PIO.OUT_LOW, out_shiftdir=rp2.PIO.SHIFT_LEFT,
             autopull=True, pull_thresh=24)
def ws2812():
    T1 = 2
    T2 = 5
    T3 = 3
    wrap_target()
    label("bitloop")
    out(x, 1)               .side(0)    [T3 - 1]
    jmp(not_x, "do_zero")   .side(1)    [T1 - 1]
    jmp("bitloop")          .side(1)    [T2 - 1]
    label("do_zero")
    nop()                   .side(0)    [T2 - 1]
    wrap()

NUM_LEDS = 8
sm = rp2.StateMachine(0, ws2812, freq=8_000_000, sideset_base=Pin(18))
sm.active(1)

# 彩虹循环
colors = [
    0x00FF00, 0x0000FF, 0xFF0000, 0xFFFF00,
    0x00FFFF, 0xFF00FF, 0xFFFFFF, 0x000000
]

while True:
    for c in colors:
        ar = array.array("I", [c] * NUM_LEDS)
        sm.put(ar, 8)
        time.sleep(0.5)

示例 5:C SDK — GPIO 中断 + 定时器

/*
 * Pico C SDK — 按键中断控制 LED
 * CMakeLists.txt 需包含:
 *   pico_sdk_init()
 *   target_link_libraries(myapp pico_stdlib)
 */
#include "pico/stdlib.h"
#include "pico/time.h"

#define LED_PIN 25
#define BTN_PIN 16

volatile bool led_on = false;

void btn_callback(uint gpio, uint32_t events) {
    led_on = !led_on;
    gpio_put(LED_PIN, led_on);
}

int main() {
    stdio_init_all();

    gpio_init(LED_PIN);
    gpio_set_dir(LED_PIN, GPIO_OUT);

    gpio_init(BTN_PIN);
    gpio_set_dir(BTN_PIN, GPIO_IN);
    gpio_pull_up(BTN_PIN);
    gpio_set_irq_enabled_with_callback(BTN_PIN, GPIO_IRQ_EDGE_FALL,
                                        true, &btn_callback);

    printf("Pico 按键中断示例 (GPIO%d)\n", BTN_PIN);

    while (1) {
        tight_loop_contents();
    }
    return 0;
}

示例 6:C SDK — ADC + DMA 高速采样

/*
 * Pico C SDK — ADC DMA 连续采样 1000 点
 * 需要链接: pico_stdlib hardware_adc hardware_dma
 */
#include "pico/stdlib.h"
#include "hardware/adc.h"
#include "hardware/dma.h"

#define SAMPLE_COUNT 1000
uint16_t capture_buf[SAMPLE_COUNT];

int main() {
    stdio_init_all();
    adc_init();
    adc_gpio_init(26);  // ADC0
    adc_select_input(0);
    adc_fifo_setup(true, false, 1, false, false);

    int dma_chan = dma_claim_unused_channel(true);
    dma_channel_config cfg = dma_channel_get_default_config(dma_chan);
    channel_config_set_transfer_data_size(&cfg, DMA_SIZE_16);
    channel_config_set_read_increment(&cfg, false);
    channel_config_set_write_increment(&cfg, true);
    channel_config_set_dreq(&cfg, DREQ_ADC);
    dma_channel_configure(dma_chan, &cfg,
        capture_buf, &adc_hw->fifo, SAMPLE_COUNT, true);

    adc_run(true);                    // 启动 ADC 自由运行
    dma_channel_wait_for_finish_blocking(dma_chan);
    adc_run(false);

    printf("采样完成 %d 点\n", SAMPLE_COUNT);
    for (int i = 0; i < 10; i++)
        printf("  [%d] = %d\n", i, capture_buf[i]);

    dma_channel_unclaim(dma_chan);
    return 0;
}

⚠️ Pico 编程要点

  1. MicroPython 固件烧录:按住 BOOTSEL 键上电 → 拖入 .uf2 固件 → Thonny 连接。
  2. C SDK 环境:推荐 VS Code + Raspberry Pi Pico 插件,Windows/Linux/Mac 均支持。
  3. PIO 汇编:共 9 条指令,2 个 PIO 块各 4 个状态机,每个状态机 32 字指令。是 Pico 最强大的特性。
  4. 双核使用multicore_launch_core1() 启动第二核心,注意共享数据加锁。
  5. USB 复合设备:可同时模拟 CDC+HID+MSC,TinyUSB 栈内置支持。

信息

路径
/firmware/开发板/树莓派 Pico (Raspberry Pi Pico)/树莓派 Pico 代码例程 — MicroPython + C SDK + PIO.md
更新时间
2026/5/26