Last active
March 17, 2018 10:46
-
-
Save vargham/fda52a17ab4001f50aec245615713cf8 to your computer and use it in GitHub Desktop.
Arduino vs AVR Libc pin change speed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Arduino vs AVR Libc pin change speed | |
// by Mark Peter Vargha | |
// varghamarkpeter.hu | |
// | |
// AVR pin toggle: 0.44 us | |
// | |
// Arduino Polling: 6-11 us | |
// Arduino Interrupt: 11.7 us | |
// AVR Polling: 0.4 - 0.8 us | |
// AVR Interrupt: 1.4 us | |
// IO pins | |
constexpr uint8_t cInputPin = 21; //Port D Pin 0 | |
constexpr uint8_t cOutputPin = 13; //Port B Pin 7 | |
// Configuration | |
constexpr bool cUseInterrupt = true; | |
constexpr bool cUseArduino = false; | |
//Arduino ISR | |
void onChange() { | |
digitalWrite(cOutputPin, digitalRead(cInputPin)); | |
} | |
//AVR Libc ISR | |
ISR(INT0_vect) { | |
PORTB ^= (1 << PB7); //Toggle output pin | |
} | |
void setup() { | |
pinMode(cInputPin, INPUT_PULLUP); | |
pinMode(cOutputPin, OUTPUT); | |
if (cUseInterrupt) { | |
if (cUseArduino) { | |
attachInterrupt(digitalPinToInterrupt(cInputPin), onChange, CHANGE); | |
} | |
else { | |
EICRA |= (1 << ISC00); //Any edge of INT0 generates asynchronously an interrupt request | |
EIMSK |= (1 << INT0); //Enable INT0 | |
} | |
} | |
digitalWrite(cOutputPin, digitalRead(cInputPin)); | |
} | |
void loop() { | |
if (!cUseInterrupt) { | |
if (cUseArduino) { | |
digitalWrite(cOutputPin, digitalRead(cInputPin)); | |
} | |
else { | |
if (PIND & (1 << PD0)) { | |
PORTB |= (1 << PB7); //Set output pin | |
} | |
else { | |
PORTB &= ~(1 << PB7); //Clear output pin | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment