Arduino


一般的なLEDのon/off

void loop() {
// put your main code here, to run repeatedly:

  digitalWrite(SIG1, LOW );
  digitalWrite(SIG1, HIGH );
}



1周期 6.8us, 147kHz


ビット出力を高速化したLEDのon/off

void loop() {
// put your main code here, to run repeatedly:

  PORTB &= ~_BV(5);
  PORTB |= _BV(5);
}



1周期 496ns, 2.02MHz
ただしあきらかにデューティーが50%ではない。


ビット出力を反転しデューティーを合わせたLEDのon/off

void loop() {
// put your main code here, to run repeatedly:

  PORTB ^= _BV(5);
}



1周期 880ns, 1.14MHz


ビット出力を反転の方法を変更すたLEDのon/off

void loop() {
// put your main code here, to run repeatedly:

  PINB = _BV(5); // avr特有ビット反転命令
}



1周期 628ns, 1.59MHz


さらにループを高速化したLEDのon/off

void loop() {
// put your main code here, to run repeatedly:

  while(1){
    PINB = _BV(5); // avr特有ビット反転命令
  }
}



1周期 376ns, 2.66MHz


ArduinoのCPUやポートの違いを吸収する部分をのぞくと、かなり高速化できるが、
ボードによる差によりプログラムは動かなくなる可能性がある。



標準プログラム         1周期 6.8us, 147kHz
高速化したプログラム     1周期 376ns, 2.66MHz

このプログラムの場合 単純計算で18倍早くなった。


一般的なLEDのon/off

void loop() {
// put your main code here, to run repeatedly:

  digitalWrite(SIG1, LOW );
  digitalWrite(SIG1, HIGH );
}



1周期 6.4us, 156kHz


ループを高速化したLEDのon/off

void loop() {
// put your main code here, to run repeatedly:

  while(1){
    digitalWrite(SIG1, LOW );         // 229kHz 4.36us
    digitalWrite(SIG1, HIGH );
  }
}




1周期 4.36us, 229kHz


ビット出力を高速化したLEDのon/off

void loop() {
// put your main code here, to run repeatedly:

  REG_PIOB_SODR |= (0x01 << 27); // Turn on the LED // 455kHz 2.20us
  REG_PIOB_CODR |= (0x01 << 27); // Turn off the LED using the CODR
}



1周期 2.20us, 455kHz


さらにループを高速化したLEDのon/off

void loop() {
// put your main code here, to run repeatedly:

  while(1){
    REG_PIOB_SODR |= (0x01 << 27);   // Turn on the LED      // 4.24MHz 236ns
    REG_PIOB_CODR |= (0x01 << 27);   // Turn off the LED using the CODR
  }
}



1周期 236ns, 4.24MHz



標準プログラム         1周期 6.4us, 156kHz
高速化したプログラム     1周期 236ns, 4.24MHz

このプログラムの場合 単純計算で27倍早くなった。