… “But hasn’t this been done before?” YES! And I didn’t do it. I didn’t do it this time either 😛
I was reading Bill Porter‘s article about how many cycles it takes to do a digitalWrite as fast as you can in Arduino. He compared it to the speeds attained by writing directly to the port, and he found that a digitalWrite operation takes 56 clock cycles to perform, whereas a direct port write takes 2 clock cycles. He referred to a library that rewrote digitalWrite in terms of a macro (something I was thinking of doing, but I’m glad I didn’t, cause John Rain? with the assistance of Paul Stoffregen and Bill Westfield worked out the details of doing it).
The problem with that library, is that the last update was in 2010, and after trying, it didn’t work with Arduino 1.0. The original file was provided as a library for arduino, but it included a ‘bonus’ section that had the macroized digitalWrite, digitalRead, and pinMode. I’ve updated just those files for Arduino 1.0, and I have it hosted here. I have a few readme’s in there, the .txt readme is one I created, which tells you where to put the files. But I have arranged it so that after you extract it, you can copy the lib and hardware folders to your arduino installation, and copy and replace your existing files. The trick to their modifications is only using the original digitalWrite routine if the pin number or level specified changes. For me, most of my digitalWrite and reads are static, same pin number, everytime.
As always, this comes with no warranties. When comparing the Blink sketch, the size went from 1026 bytes to 674. For my controller sketch, it went from 16708 to 16642 bytes. I hope everyone enjoys this, and feel free to ask me any questions.