2
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?

Wio BG770Aで割り込み処理を実装する方法

Last updated at Posted at 2025-04-06

はじめに

今回は、Wio BG770Aで割り込み処理を試してみます。

使用するデバイス

  • Wio BG770A v1.0
  • Grove Button

ポイント

ポイントは以下になります。

  • Grove Buttonは、GroveコネクタのDigitalポートに接続
  • 割り込みハンドラの作成
  • 割り込みの初期化
  • 割り込み関数内でフラグを制御
  • 主な処理は、loop関数内で処理を実装

ソースコード

ソースコードは以下になります。
サンプルスケッチの「grove-button.ino」をベースにして、ボタンによる割り込み処理を実装しています。割り込み関数以内でいろいろな処理を行うのはあまり望ましくないので、割り込み関数内ではフラグの制御のみを行い、主な処理はloop関数内で処理をしています。

/*
 * grove-button.ino
 * Copyright (C) Seeed K.K.
 * MIT License
 */

#include <Adafruit_TinyUSB.h>
#include <WioCellular.h>

#define BUTTON_PIN (D30)  // Grove - Digital (P1)
#define INTERVAL (100)

// 割り込みフラグ
volatile bool buttonPressed = false;

// 割り込みハンドラ
void handleButtonPress() {
  buttonPressed = true;
}

void setup(void) {
  Serial.begin(115200);
  {
    const auto start = millis();
    while (!Serial && millis() - start < 5000) {
      delay(2);
    }
  }
  Serial.println();
  Serial.println();

  WioCellular.begin();
  digitalWrite(PIN_VGROVE_ENABLE, LOW);

  pinMode(BUTTON_PIN, INPUT);

  // 割り込みの設定
  attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), handleButtonPress, RISING);

}

void loop(void) {
  static int count = 0;

  if (buttonPressed) {
    // ボタンが押されたときの処理
    Serial.println("Button pressed!");
    digitalWrite(LED_BUILTIN, HIGH);
    delay(500);
    digitalWrite(LED_BUILTIN, LOW);

    buttonPressed = false;
  }

/*
  int buttonState = digitalRead(BUTTON_PIN);
  Serial.print(buttonState ? '*' : '.');

  if (++count >= 10) {
    Serial.println();
    count = 0;
  }
*/

  delay(INTERVAL);
}

今後の展望

今回は、Wio BG770Aで割り込み処理の実装を行いました。割り込みの延長線での処理は、シリアル出力とLED点滅のみを実装していますが、今後はセルラー通信なども試してみたいと思います。

2
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
2
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?