Arduino下IO速度的测试

Arduino版本arduino-1.6.7,测试型号Arduino Nano,测试工具Saleae Logic 1.1.15
测试代码:

void setup() {
  pinMode(13, OUTPUT);
}

void loop() {
  digitalWrite(13, HIGH);
  digitalWrite(13, LOW);
  digitalWrite(13, HIGH);
  digitalWrite(13, LOW);
}

测试结果:
01.png

双路测试:
02.png

尝试寄存器操作:

void setup() {
  pinMode(13, OUTPUT);
  //pinMode(11, OUTPUT);
  DDRB |= (1<<PB3);
}

void loop() {
  digitalWrite(13, HIGH);
  digitalWrite(13, LOW);
  digitalWrite(13, HIGH);
  digitalWrite(13, LOW);
  //PB3
  digitalWrite(11, HIGH);
  digitalWrite(11, LOW);
  PORTB |= (1<<PB3);
  PORTB &= ~(1<<PB3);
  PORTB |= (1<<PB3);
  PORTB &= ~(1<<PB3);
}

结果:
03.png

速度竟然提高了40倍,1<<PB3,_BV(3),bit(3),0b00001000效果一样

04.png

标签: none

添加新评论