Pages: 1 2 3 [4]   Go Down
Author Topic: Maximum pin toggle speed  (Read 17140 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 3
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.

http://code.google.com/p/arduino/issues/detail?id=140

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

London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6255
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

_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:
#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
« Last Edit: December 24, 2009, 05:45:35 am by mem » Logged

0
Offline Offline
God Member
*****
Karma: 26
Posts: 610
Always making something...
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

@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.
« Last Edit: December 24, 2009, 02:54:11 pm by pjrc » Logged

Pages: 1 2 3 [4]   Go Up
Jump to: