1
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.

ESP8266でタイマ割込したい(Arduino + ESP8266TimerInterrupt Library)

Posted at

TL;DR

ESP8266TimerInterrupt Library を使う

経緯

安価でArduinoライクに開発できるWiFi対応モジュール ESP8266 と、それをベースとした ESP-WROOM-02 という便利なモジュールが存在する。これを用いて複雑なデバイスを開発していると、タスクを分けて実行したいと思うことがある。

例:メイン関数からHTTP GETをリクエストしたいが、レスポンスを待っている間もモータやLEDを10msおきに操作し続けたい

このような状況で、呼び出し間隔ごとにタスクを分けて、タイマ割込により定期的に呼び出すということをする。そこで、本記事ではESP8266におけるタイマ割込の実行方法をメモしておく。

導入

ESP8266TimerInterrupt Library という便利なライブラリが存在する。これをインポートする。

(ライブラリの導入方法自体は、Arduino IDE なのか Platform IO なのかでも変わってくる気がするので、省略)

利用

ひとまず、一番簡単な利用方法だけ記載しておく。
(ほかの機能は後日追加)

#include "ESP8266TimerInterrupt.h"

ESP8266Timer ITimer;

// 定期的に呼び出すタスク
// 引数と戻り値は共に void にする
void subtask() {
    // 定期的に呼び出すタスクの内容
}

void setup() {
    // subtask 関数を10msごとに呼び出す例
    // 秒数はマイクロ秒で書く
    ITimer.attachInterruptInterval(10000, subtask);
}

void loop() {
    // subtask のタイマ割込とは別で、ここでメインの処理を行うことができる
}

Todo

グローバル変数経由で別タスクにデータを渡しだすと、データの整合性を保つためなどの理由で、割込禁止を設定したくなる。割込禁止について調べる。

  • そもそもできるのか?
  • できるとして、どのように実装するのか?
1
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
1
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?