ハイブリッド静的/動的 Arduino Mega2560 シングルチャンネル短パルスジェネレータ

このパルスジェネレータファームウェアは、Arduino Uno での短パルス生成 パート4: NOP for ループ などで議論された方法を使用して、動的(NOP 命令の for ループ)と静的(テンプレートベースの自動展開)の両方を組み合わせて生成し、高分解能でほぼ任意のパルス幅を選択できます。

パルス幅のジャンプがまだいくつかあるため、100% 動作するわけではありません。それでも、多くのマイクロ秒 5V パルス生成アプリケーションに役立ち、最適化された整数テンプレートループ展開アプローチを使用したハイブリッド静的/動的パルサーの有用な技術デモにもなる可能性があります。

パルス幅を制御するには、シリアルインターフェースを開き、+ を入力してパルス幅を増やすか、- を入力して減らします。

これは Arduino Uno でも動作するかもしれませんが、私の Uno はシリアルインターフェースチップが壊れていたため、ATMega2560 ボードを使用しました。

pulse_generator.cpp
// License: CC0 1.0 Universal
// By Uli Köhler (techoverflow.net)
#include <Arduino.h>
#include <avr/io.h>
#include <ArduinoJson.h>

#define PORT13 PORTB
#define PIN13 7
#define PIN13_MASK (1 << PIN13)

#define PORT11 PORTB
#define PIN11 5
#define PIN11_MASK (1 << PIN11)

int pulseLength = 1;

using PulseFunc = void (*)(int);

// void(int) への関数ポインタ
PulseFunc pulser = nullptr;
int pulserParam = 0; // パルサー関数用に事前計算されたパラメータ

void DoNothingPulser(int _) {
}

/**
 * 出力ピンをパルス駆動。非常に短いパルス用。
 * 静的(最適化された)NOP のみを実行する。
 * 動的(低速な)NOP サイクルは実行しない
 *
 * したがって、この関数には for ループのオーバーヘッドがない。
 */
template<int nNOPs>
void PulseStatic(int _) {
    cli();
    PORT11 |= PIN11_MASK;
    // 静的 for ループ(最適化により消去 - テンプレート駆動)
    // ループのアンローリングを強制
    // 注意: コンパイラは n < 8 の場合のみアンローリングする
    for (int i = 0; i < min(nNOPs, 6); i++) {
        _NOP();
    }
    if(nNOPs > 6) {
        for (int i = 0; i < nNOPs - 6; i++) {
            _NOP();
        }
    }
    PORT11 &= ~PIN11_MASK;
    sei();
}

/**
 * 出力ピンをパルス駆動。非常に短いパルス用。
 * 静的(最適化された)NOP と
 * 動的(低速な)NOP サイクルの両方を実行する
 */
template<int nNOPs>
void PulseDynamic(int dynamicNOPs) {
    cli();
    PORT11 |= PIN11_MASK;
    // 動的 for ループ(最適化により消去されない - テンプレート駆動)
    for (int i = 0; i < dynamicNOPs; i++)
    {
        _NOP();
    }
    // 静的 for ループ(最適化により消去 - テンプレート駆動)
    // ループのアンローリングを強制
    // 注意: コンパイラは n < 8 の場合のみアンローリングする
    for (int i = 0; i < min(nNOPs, 6); i++) {
        _NOP();
    }
    if(nNOPs > 6) {
        for (int i = 0; i < nNOPs - 6; i++) {
            _NOP();
        }
    }
    PORT11 &= ~PIN11_MASK;
    sei();
}

PulseFunc staticPulsers[] = {
    DoNothingPulser,
    &PulseStatic<0>, // 0 NOP
    &PulseStatic<1>, // 1 NOP ...
    &PulseStatic<2>,
    &PulseStatic<3>,
    &PulseStatic<4>,
    &PulseStatic<5>,
    &PulseStatic<6>,
    &PulseStatic<7>,
    &PulseStatic<8>,
    &PulseStatic<9>,
    &PulseStatic<10>,
    &PulseStatic<11>
};

PulseFunc dynamicPulsers[] = {
    &PulseDynamic<0>,
    &PulseDynamic<1>,
    &PulseDynamic<2>,
    &PulseDynamic<3>,
    &PulseDynamic<4>,
    &PulseDynamic<5>,
    &PulseDynamic<6>,
    &PulseDynamic<7>,
    &PulseDynamic<8>,
    &PulseDynamic<9>,
    &PulseDynamic<10>,
    &PulseDynamic<11>
};

constexpr int A = 7;
constexpr int B = 6;

void ReconfigurePulse() {
    // 非常に短いパルスは静的のみで実行される
    if(pulseLength < A) {
        pulser = staticPulsers[pulseLength];
    } else {
        pulser = dynamicPulsers[(pulseLength - A) % B];
        pulserParam = (pulseLength - A) / B;
    }
}

void setup()
{
    Serial.begin(115200);
    Serial.setTimeout(25);

    ReconfigurePulse(); // デフォルトの pulseLength で

    pinMode(11, OUTPUT);
    pinMode(LED_BUILTIN, OUTPUT);
}

void loop()
{
    pulser(pulserParam);
    Serial.println(pulseLength);
    //ProcessSerialInput();
    // 開始から 50ms 経過するまで待つ
    while(Serial.available() > 0) {
        int c = Serial.read();
        if(c == '+') {
            pulseLength++;
            ReconfigurePulse();
        } else if(c == '-') {
            pulseLength--;
            if(pulseLength < 0) {pulseLength = 0;}
            ReconfigurePulse();
        }
    }
    delay(50);
}

同じカテゴリの類似記事をチェック: Arduino C/C++