After some slight optimizations ordering the code a bit, I found a discussion on the Arduino forum that handles this topic.
In this discussion the digitalWrite and shiftOut are optimized. The regular shiftOut takes 110us to output a byte, but with the optimizations it performs 6 times better. Now thats a nice gain. That definitely should solve the flicker...
But wait, there's more! The compile optimization can really make a difference, when the compiler knows all values.
Quote:
The compiler will produce code that will do port I/O in a single instruction (65 nanoseconds) if it knows the port, pin and state at compile time.
So, when you define a hardcoded pin in your sketch, there's a real difference in the way you define it. A regular integer variable containing the pin number.
int OUTPUT_PIN = 5;
#define OUTPUT_PIN 5
From now on, I'll code my pins using a #define statement, instead of regular variables!!!
Well, actually you can also use the following:
const int OUTPUT_PIN = 5;
So let us look at the following code:
void f(int); void g() { f(OUTPUT_PIN); }
#define OUTPUT_PIN 5
" or "const int OUTPUT_PIN = 5;
", the resulting instructions are in both cases: push 5 call f add esp, 4
Okay, to summarize... use the "const" keyword for defining your pins and every other value that you know will not change.
From my rusty C++ guidelines I vaguely remember: "Use const wherever possible."
Extra:
- Nice discussion on using const
- Disassembled code copied from here
- Thanks to all people on the Arduino forum!
No comments:
Post a Comment