Last active
February 7, 2017 08:06
-
-
Save Tamakichi/662cf1fd8bfa890d1750f19b83934b92 to your computer and use it in GitHub Desktop.
Arduino STM32 ワンショットタイマー割り込み
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Arduino STM32 ワンショットタイマー割り込み | |
* 2017/02/07 by たま吉さん | |
* | |
* | |
* |<----------->| 4sec Timer stop | |
* |<->| 1sec LED on | |
* |<----->| 2sec LED off | |
* | |
* ____|~~~|______ | |
* | |
* 1)タイマースタート | |
* 2)タイマースタートから1秒後にLED点灯 | |
* 3)タイマースタートから2秒後にLED消灯 | |
* 4)タイマー終了 | |
* | |
* LED点灯、LED消灯のタイミングで割り込みを実行して、LEDを制御 | |
* 再度、ワンショット割り込み実行は、設定なしで呼び出し可能 | |
* | |
* ・HardwareTimer | |
* http://docs.leaflabs.com/static.leaflabs.com/pub/leaflabs/maple-docs/latest/lang/api/hardwaretimer.html | |
* ・APIs timer.h | |
* http://docs.leaflabs.com/static.leaflabs.com/pub/leaflabs/maple-docs/latest/libmaple/api/timer.html | |
* | |
*/ | |
#include <libmaple/timer.h> | |
#define LED_PIN PC13 | |
void ledOn() { | |
digitalWrite(LED_PIN, LOW); | |
} | |
void ledOff() { | |
digitalWrite(LED_PIN, HIGH); | |
} | |
void setup() { | |
pinMode(LED_PIN, OUTPUT); | |
ledOff(); | |
// 初期化 4秒でリロード | |
timer_pause(TIMER2); | |
timer_init(TIMER2); | |
timer_set_prescaler(TIMER2, 10000); | |
timer_set_reload(TIMER2, 7200*4); | |
// LEDを1秒後に点灯する割り込みハンドラ登録 | |
timer_set_mode(TIMER2,1, TIMER_OUTPUT_COMPARE); | |
timer_set_compare(TIMER2,1,7200*1); | |
timer_attach_interrupt(TIMER2, TIMER_CC1_INTERRUPT, ledOn); | |
timer_enable_irq(TIMER2, 1); | |
// LEDを2秒後に消灯する割り込みハンドラ登録 | |
timer_set_mode(TIMER2,2, TIMER_OUTPUT_COMPARE); | |
timer_set_compare(TIMER2,2,7200*2); | |
timer_attach_interrupt(TIMER2, TIMER_CC2_INTERRUPT, ledOff); | |
timer_enable_irq(TIMER2, 2); | |
// 割り込み実行 | |
TIMER2->regs.adv->CR1 |= TIMER_CR1_OPM; // 繰り返しなし指定 | |
timer_generate_update(TIMER2); | |
timer_resume(TIMER2); | |
} | |
void loop() { | |
// 5秒毎にワンショットタイマー割り込みを実行する | |
delay(5000); | |
timer_resume(TIMER2); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment