概要
Flextimer2の割り込み間隔に対し疑問が発生したので実験しました。
FlexiTimer2::set(times, resolution,#function)
このタイプの場合、1secondに100回というのが公式ドキュメントぽいですが。
FlexiTimer2::set(unsigned long units, double resolution, void (*f)())
this function sets a time on units time the resolution for the overflow. Each overflow, "f" will be called. "f" has to be declared void with no parameters. E.g. units=1, resolution = 1.0/3000 will call f 3000 times per second, whereas it would be called only 1500 times per second when units=2.
# include <FlexiTimer2.h>
volatile boolean flag = true;
void control() {
digitalWrite(A0,flag);
flag = !flag;
}
void setup() {
pinMode(A0,OUTPUT);
FlexiTimer2::set(1, 1.0 / 100, control);
FlexiTimer2::start();
}
void loop() {
// put your main code here, to run repeatedly:
}
これで100回ごとにON,OFFさせてみると
Arduino_Nanoだと
で0.784msごとになる。
やはり、狙い通りになっていない。
ちなみに
FlexiTimer2::set(unsigned long ms, void (*f)())
との違いは、指定時間の精度によって使い分ける。具体的にいうとms指定の方は0.1ms間隔等の指定はできない、というより自動的に1msに繰り上げられる(四捨五入かも)。こういう制度の違いである。
# include <FlexiTimer2.h>
volatile boolean flag = true;
void control() {
digitalWrite(A0,flag);
flag = !flag;
}
void setup() {
pinMode(A0,OUTPUT);
FlexiTimer2::set(10, 1.0 / 1000, control);
FlexiTimer2::start();
}
void loop() {
// put your main code here, to run repeatedly:
}
に
FlexiTimer2::set(10, 1.0 / 1000, control);
結論
FlexiTimer2::set(times, resolution,#function)
この指定を使用する場合は、resolutionを細かく、timesを大きめにとったほうが正しい割り込みになり、timesが1だと、だいぶ大雑把な時間間隔になることに注意してほしい