Go Down

Topic: Maximum pin toggle speed (Read 49813 times) previous topic - next topic


Today that probably won't make any difference.  But when (if ever?) my digitalWrite optimization gets included in Arduino, it will make a HUGE difference.  That code is in issue #140.


That code doesn't work for me. What is _digitalWrite?


Dec 24, 2009, 11:43 am Last Edit: Dec 24, 2009, 11:45 am by mem Reason: 1
_digitalWrite would be the current slow function.

If modify the macro as follows you can run the code by calling fastWrite in your sketch.
if the pin and value are both constants, a single op code is used, if pin or port are variables then the exisitng slow digitalWrite  is called.

Code: [Select]
#define fastWrite(P, V) \
       if (__builtin_constant_p(P) && __builtin_constant_p(V)) { \
               if (digitalPinToTimer(P)) \
                       bitClear(*digitalPinToTimer(P), digitalPinToTimerBit(P)); \
               bitWrite(*digitalPinToPortReg(P), digitalPinToBit(P), (V)); \
       } else { \
               digitalWrite((P), (V)); \

I think paul's macro assumed that the implimentation of the current digitalWrite would be renamed to _digitalWrite


Dec 24, 2009, 08:50 pm Last Edit: Dec 24, 2009, 08:54 pm by pjrc Reason: 1
@selfonlypath: that number is TraumaPony's benchmark.  Smaller numbers are better, meaning digitalWrite is running faster.

The number is the elapsed microseconds to perform several different digitalWrites 10000 times.  Just copy-n-paste TraumaPony's sketch and run it on your board, and you'll see a number in the Serial Monitor.  If you have LEDs connected to those pins, they will blink.  If you're using a board with different pinout from the '168 or '328 based Arduino, edit the pin numbers so the two pwm pins are ones with PWM and others aren't.

Also, it looks like I was incorrect about needing "static".  In this case, the complier does seem to be able to treat those numbers as compile time constants.  I have seen it fail to do so in other cases, and honestly I do not fully understand why it would be different.  I tried both ways and even read through disassembly of the compiler generated code, and static does not make any difference in this case.

Go Up