running simple code turning on pin 2 and turning it off in the loop. Using scope, pin is high for 1.2 used but sometimes 3.2 used. loop time is 5.2 used but sometimes 6.9 us.
Very big problem as need consistent workings, not erratic. What is going on here?
I don't know why it's inconsistent, but that's a lousy way to do timing. I assume there is some stuff running on the processor that you don't know about. As you probably know, the processor isn't running your C/C++ code... It's running the compiled machine language. Maybe it's related to the bootloader "staying alive" in the background?
You might have to use a timer library or write some assembly code to take total-control over the processor. (With assembly/machine language you can look-up the number of processor cycles required for each particular instruction.)
With longer time periods (where the time to perform each instruction doesn't slow you down too much) you can use millis() and micros() for more accurate/consistent timing.
But... Thanks for doing that experiment! I've sometimes wondered how fast you could loop.
Hmm...this is interesting. The reason you're seeing variation in your pulses is because interrupts are firing and the Arduino is servicing their ISRs. One common ISR you'd encounter is the one used for micros/millis. Apparently the Arduino includes that stuff even if your sketch doesn't use it.
You will find this code gives consistent pulses because it doesn't allow the Arduino to initialize any of its own features:
const int statusPin = 2; // status pin
int main()
{
pinMode(statusPin,OUTPUT);
while(1)
{
digitalWrite(statusPin,HIGH);
digitalWrite(statusPin,LOW);
}
return 0;
}
That said, I agree with the other people that this is not the right way to get consistent pulses out of your Arduino.
BigBobby: that explains it. Where do I find docs that describe these nuances not included in the Learning Reference? I can live with the sneaks, just wanted to know about them once I found them.
Dlloyd: Where do I find the notes on manipulating the timer interrupt function? Are the processor registers mapped into the arduino code and where do I find them?
kitch:
BigBobby: that explains it. Where do I find docs that describe these nuances not included in the Learning Reference? I can live with the sneaks, just wanted to know about them once I found them.
Well I've been in the Arduino for a couple months now, and I just learned in your thread that the timer ISR gets created even if you aren't using it
To be fair the Arduino isn't trying to be sneaky, but trying to make it easy for new programmers to do things like measure time and write to the serial port. If you don't want it to do that stuff you can use main() like in my example. You'd then handle things like your timers directly, using the #defines and functions available from avr-libc -> http://www.atmel.com/webdoc/AVRLibcReferenceManual/group__avr__io.html
In the standard installation of the Arduino IDE on Windows 7, you'll find these header files in this directory -> C:\Program Files (x86)\Arduino\hardware\tools\avr\avr\include\avr
kitch:
Dlloyd: Where do I find the notes on manipulating the timer interrupt function? Are the processor registers mapped into the arduino code and where do I find them?
Quite new at this myself ... all source code is provided with the IDE, however I find GitHub quite handy. Also, of course, the datasheet for your board's MCU is needed.