最小 STM32 HardwareTimer PlatformIO / Arduino 定时器中断闪烁示例
这是一个在 PlatformIO / Arduino 上使用 HardwareTimer(它是 PlatformIO STM32 Arduino 安装的一部分 - 无需安装库)使用定时器中断的最小示例。在 Olimex E407 板上测试过。它几乎可以在任何 STM32 处理器上运行,但你可能需要将 PC13 调整为连接到 LED 的引脚。该示例将每秒闪烁一次 LED。
timer_interrupt_blink.cpp
#include <Arduino.h>
HardwareTimer timer(TIM1);
bool ledOn = false;
void OnTimer1Interrupt() {
ledOn = !ledOn;
digitalWrite(PC13, ledOn ? HIGH : LOW);
}
void setup() {
pinMode(PC13, OUTPUT);
// 配置定时器
timer.setPrescaleFactor(2564); // 设置预分频器为 2564 => 定时器频率 = 168MHz/2564 = 65522 Hz(来自 168 MHz 时钟源预分频为 1)
timer.setOverflow(32761); // 设置溢出为 32761 => 定时器频率 = 65522 Hz / 32761 = 2 Hz
timer.attachInterrupt(OnTimer1Interrupt);
timer.refresh(); // 使寄存器更改生效
timer.resume(); // 启动
}
void loop() {
}Check out similar posts by category:
PlatformIO, STM32
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow