What are the fastest alternatives to calling the Arduino functions digitalread()
and digitalwrite()
? AVR-specific or chip-specific solutions are acceptable.
Answer
Access the digital ports directly!
The 3 methods I tested were
- digitalWrite(pin, LOW); digitalWrite(pin, HIGH);
- CLR(PORTB, 0) ; SET(PORTB, 0);
- PORTB |= _BV(0); PORTB &= ~(_BV(0));
[...]
As you can see, digitalWrite takes around 56 cycles to complete, while direct Port addressing takes 2 cycles. That’s a big difference in time for programs that have lot’s of IO operations!
No comments:
Post a Comment