Hi,
The following code should print on each line an incremental number and after that line a number of micros(). For some reason the incremental number are fine, I mean, they are showing right but the micros is sometimes showing values smaller than before!
long x = 0;
void setup() {
Serial.begin(115200);
attachInterrupt(1,registrar_rf,CHANGE);
}
void loop() {
}
void registrar_rf() {
x++;
Serial.println(x);
Serial.println(micros());
}
My output is something like this:
100
23900
101
22450
102
22195
103
24877
104
...
If you take a close look the micros() number is getting smaller sometimes. HOw is it possible?
Serial.print fucks up if you try to use it in an ISR. Read the documentation in the ref section!
Mark
If you take a close look the micros() number is getting smaller sometimes. HOw is it possible?
Simple you have faked the output.
There is no way that that program can give that output
And you still have not read the documentation of interrupts in the ref section.
Mark
@gilperon: Mind your language.
Yes I would expect exactly that output. Then I would expect it to hang. By doing serial prints inside an ISR, which you were advised not to do, the timer interrupt may not fire from time to time. micros() consists of data from the timer (a number from 0 to 255) added onto the count of timer overflows. So if you miss a timer overflow, the timer counter could be lower.
An analogy would be if you froze the hour hand on your clock, and then complained that after a while the time "went backwards" because the minute hand wrapped around and went from (say) 11 back to 1.
The timer ISR is the one that increments the hour hand, so to speak.
The short answer is that if you do Serial prints inside an ISR the results will be unpredictable.
Ah well, you know best, then.
You ask for help and the discard the answers.
Timer 0 is an 8-bit timer. It operates (in this case) with a prescaler of 64, so therefore it overflows every 1024 µS (not every hour). It is this overflow I am talking about.
gilperon:
@Nick Gammon thank you but micros returns a long number not a byte (0-255) as you said. So it would only happen an overflow once in an hour (at least). I still see no reason why a predictable output (as mine) is considered "unpredictable". The incremental number is shown in a perfect order, so it's not unpredtictable.
It's not smart to argue with Nick. He knows more about Arduino internals than most of us ever will. If you dig into how micros() is really implemented, you'll find he is absolutely right, and you are absolutely wrong....
Regards,
Ray L.
long x = 0;
Since you are altering that in an ISR, it should be declared volatile.
I tried your code with nothing on pin 3, and I got no output at all, which was pretty much what I expected (it hung in the ISR).
The only reason you got output at all was you must have been activating the interrupt quite slowly, giving it time to flush the serial buffer.
I, and others, said you cannot use serial prints inside the ISR. You can use micros(). However, if you use serial prints, it is likely to stuff things up, and micros() returning the wrong value is an example of that.