0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ArduinoのFlextimer2について

Posted at

概要

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だと
image.png
で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); 

変更したところ、
image.png
10ms間隔に改善された。

結論

FlexiTimer2::set(times, resolution,#function)

この指定を使用する場合は、resolutionを細かく、timesを大きめにとったほうが正しい割り込みになり、timesが1だと、だいぶ大雑把な時間間隔になることに注意してほしい

0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?