Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Arduino vs AVR Libc pin change speed
// 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
You can’t perform that action at this time.