LoginSignup
1
2

More than 3 years have passed since last update.

STM32F103 + Arduino_STM32 で タイマー

Last updated at Posted at 2019-11-04

STM32F103のArduino開発環境として Arduino_STM32 を使っています。
今回はタイマー割り込みを使ってみます。

Arduino_STM32 の元になった Leaflabs Maple に詳しいドキュメントがあります。
http://docs.leaflabs.com/static.leaflabs.com/pub/leaflabs/maple-docs/latest/lang/api/hardwaretimer.html#using-timer-interrupts

ここのサンプルコードはそのままでは動作しないので、少し直して動作確認しました。


// from http://docs.leaflabs.com/static.leaflabs.com/pub/leaflabs/maple-docs/latest/lang/api/hardwaretimer.html#using-timer-interrupts
#define LED_RATE 150000    // in microseconds; each 0.15sec trigger
#define BOARD_LED_PIN PC13
HardwareTimer ledtimer(2);  // timer 2

void setup() {
    pinMode(BOARD_LED_PIN, OUTPUT);
    ledtimer.pause();
    ledtimer.setPeriod(LED_RATE);
    ledtimer.setChannel1Mode(TIMER_OUTPUT_COMPARE);
    ledtimer.setCompare(TIMER_CH1, 1);  // Interrupt 1 count after each update
    ledtimer.attachCompare1Interrupt(handler_led);
    ledtimer.refresh();
    ledtimer.resume();
}

void loop(){
}

void handler_led(void){
    static int LEDstate = 0;
    digitalWrite( BOARD_LED_PIN,LEDstate);
    if ( 0 < LEDstate )
    { LEDstate = 0; } else { LEDstate = 1; }

cf.,「STM32F103 + Arduino_STM32 で Sleep」
https://qiita.com/nanbuwks/items/9abca51134226d5431da

1
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2