Minimal STM32 HardwareTimer PlatformIO / Arduino Timer-Interrupt Blink-Beispiel
English
Deutsch
Dies ist ein minimales Beispiel für die Verwendung von Timer-Interrupts auf PlatformIO / Arduino mit HardwareTimer (das Teil der PlatformIO STM32 Arduino-Installation ist – keine Bibliothek muss installiert werden). Getestet auf dem Olimex E407-Board. Es läuft auf fast jedem STM32-Prozessor, aber möglicherweise muss PC13 an den mit der LED verbundenen PIN angepasst werden. Das Beispiel lässt die LED einmal pro Sekunde blinken.
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);
// Configure timer
timer.setPrescaleFactor(2564); // Set prescaler to 2564 => timer frequency = 168MHz/2564 = 65522 Hz (from prediv'd by 1 clocksource of 168 MHz)
timer.setOverflow(32761); // Set overflow to 32761 => timer frequency = 65522 Hz / 32761 = 2 Hz
timer.attachInterrupt(OnTimer1Interrupt);
timer.refresh(); // Make register changes take effect
timer.resume(); // Start
}
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