‌树莓派Pico‌的timer

pico-sdk-api文档

rp2040-datasheet.pdf

getting-started-with-pico.pdf

树莓派 Pico 之可编程 IO(PIO)

diagram.json

json 复制代码
{
  "version": 1,
  "author": "wang minglie",
  "editor": "wokwi",
  "parts": [
    {
      "type": "wokwi-pi-pico",
      "id": "pico",
      "top": -3.15,
      "left": 3.6,
      "attrs": { "builder": "pico-sdk" }
    },
    { "type": "wokwi-logic-analyzer", "id": "logic1", "top": -143.65, "left": 326.4, "attrs": {} }
  ],
  "connections": [
    [ "pico:GP0", "$serialMonitor:RX", "", [] ],
    [ "pico:GP1", "$serialMonitor:TX", "", [] ],
    [ "pico:GP21", "logic1:D0", "green", [ "h116.4", "v-230.4", "h19.2", "v-38.4" ] ]
  ],
  "dependencies": {}
}

wokwi.toml

xml 复制代码
[wokwi]
version = 1
firmware = "cmake-build-debug-pico/pio_squarewave.uf2"
elf = "cmake-build-debug-pico/pio_squarewave.elf"

main.c

10us 进入一次定时器中断

c 复制代码
#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/timer.h"
#include "hardware/irq.h"

#define ALARM_NUM 0
#define ALARM_IRQ TIMER_IRQ_0

const uint LED_PIN = 21;

// 每 10 微秒触发一次中断
#define PERIOD_US 10+1

// 中断处理函数:每 1µs 翻转 GPIO
static void timer0_irq_handler(void) {
    // 清除 timer0 中断标志
    hw_clear_bits(&timer_hw->intr, 1u << ALARM_NUM);

    // 翻转 LED 电平
    gpio_xor_mask(1u << LED_PIN);

    // 设置下一次中断(当前时间 + 1µs)
    uint32_t next = timer_hw->timerawl + PERIOD_US;
    timer_hw->alarm[ALARM_NUM] = next;
}

static void init_timer0_interrupt(void) {
    // 清中断标志
    hw_clear_bits(&timer_hw->intr, 1u << ALARM_NUM);

    // 使能 timer0 中断
    hw_set_bits(&timer_hw->inte, 1u << ALARM_NUM);

    // 注册中断处理函数
    irq_set_exclusive_handler(ALARM_IRQ, timer0_irq_handler);
    irq_set_enabled(ALARM_IRQ, true);

    // 第一次触发:当前时间 + PERIOD_US
    timer_hw->alarm[ALARM_NUM] = timer_hw->timerawl + PERIOD_US;
}

int main() {
    stdio_init_all();

    gpio_init(LED_PIN);
    gpio_set_dir(LED_PIN, GPIO_OUT);
    gpio_put(LED_PIN, 0);   // 初始为低电平

    init_timer0_interrupt();

    // 主循环不做任何事
    while (true) {
        tight_loop_contents();
    }
}
相关推荐
ebiobiz19 小时前
基于 GD32 Embedded Builder (GEB) 与 Nimmake 的 MCU 工程搭建指南
c++·python·单片机·嵌入式硬件·mcu
fan_0002 天前
国密TLCP协议,LKT4305GMT护航物联网安全通信
单片机·嵌入式硬件·mcu·物联网·加密芯片
李永奉3 天前
中科蓝讯SDK开发-BT897X、BT891X、AB573X、AB572X 系列 触摸按键提示音功能实现
网络·单片机·嵌入式硬件·mcu·物联网
一只小透明啊啊啊啊9 天前
Socket、AIDL、SOME/IP
c++·mcu
youcans_9 天前
【嵌入式软件AI编程】16. Claude Code 与 VS Code 的协同开发
stm32·mcu·嵌入式·ai编程·claude code
橘色的喵10 天前
MCU SD 卡上电加载流程:从寄存器访问到文件系统挂载
mcu·文件系统·加载·sd
ysu_031411 天前
TinyML 模型部署:从 Edge Impulse 到 STM32F103
驱动开发·stm32·嵌入式硬件·mcu·硬件架构·硬件工程·dsp开发
youyou-060612 天前
Classic AUTOSAR 中 CAN 协议栈整体架构
mcu·面试·汽车
意法半导体STM3213 天前
【官方原创】LAT1709STM32C5硬件CRC值的计算话题
stm32·单片机·嵌入式硬件·mcu·crc·微控制器·stm32c5
国科安芯14 天前
低轨卫星姿轨控系统中高可靠嵌入式控制单元的设计与验证
单片机·mcu·低轨卫星·星间链路·卫星姿轨控