// main.c
#include <stdio.h>
#include <stdint.h>
#include "pico/stdlib.h"
#include "hardware/pio.h"
#include "hardware/irq.h"
#include "hardware/clocks.h"
# include "hardware/sync.h"
void led_blink_non_blocking(void) {
#define LED_PIN 21
static uint32_t last_tick = 0; // 上次状态切换的时间(毫秒)
static bool led_state = false; // 当前 LED 状态(false=灭,true=亮)
static const uint32_t blink_interval = 1000; // 闪烁间隔(1000ms)
if(last_tick==0){
last_tick=1;
gpio_init(LED_PIN);
gpio_set_dir(LED_PIN, GPIO_OUT);
}
// 获取当前系统时间戳(毫秒级,Pico SDK 内置函数)
uint32_t current_tick = time_us_32() / 1000; // us 转 ms
// 检查是否达到闪烁间隔(处理时间溢出:用差值判断,避免 uint32_t 溢出问题)
if ((current_tick - last_tick) >= blink_interval) {
// 更新上次切换时间戳
last_tick = current_tick;
// 切换 LED 状态
led_state = !led_state;
gpio_put(LED_PIN, led_state ? 1 : 0); // 状态反转
}
}
int main() {
// Initialize the SDK
stdio_init_all();
// Blink the LED in a loop
while (true) {
led_blink_non_blocking();
}
return 0;
}