Quote:
Originally Posted by s2man
millis() rolls over about every 9 hours
|
Ok, I think I have microSeconds just about figured out, see below code.
1. on the start of a timed event, save microSeconds()
2. at the end of a timed event, call elapsedMicroSeconds(startMicroSeconds)
It was a lot harder than it looks
It digs into wiring.c and AVR, but it also demonstrates how simply the arduino integrates with them, once you know where to look.
Quote:
Originally Posted by s2man
micros() would be better than millis(), but isn't that why we've got the timers?
|
We now have microseconds, well close enough anyway. I still don't quite get the timer interrupts myself, and the more we do with one timer, the more PWM pins we leave for future expansion. You can time a whole lot of things accurately with microSeconds without knowing anything about timer interrupts or how many timers you have or what else they are connected to.
I'm kind of tempted to say our main loop should be written like delay() with the loop guts in the middle, I used this technique below also, most things come back in 1000ms, a few were in 1001ms, don't know if a timer interrupt every second would actually be any more accurate with the other injector and vss interrupts going on.
Quote:
Originally Posted by s2man
I'm working a river clean up this weekend, so I'll be offline. No coding or testing But I'll be camping and boating
|
No fair, I want to go too!
Code:
/*
* microSeconds, convert timer0 register and the timer 0 overflow
* count (defined in wiring.c) into a microsecond value
* for more accurate timing needs.
*/
//overflow counter used by millis()
extern volatile unsigned long timer0_overflow_count;
//will reset after 71.5 minutes, so don't time anything longer than that
//about 4 microsecond resolution @ 16mhz
unsigned long microSeconds (void){
return ((timer0_overflow_count << 8) + TCNT0) * 4;
}
//return the elapsed microseconds, accounting for overflows
unsigned long elapsedMicroseconds(unsigned long startMicroSeconds ){
unsigned long msec = microSeconds();
if(msec >= startMicroSeconds)
return msec-startMicroSeconds;
return 4294967295 - (startMicroSeconds-msec);
}
void setup (void){
Serial.begin(9600);
}
unsigned long looptime=1000;
void loop (void){
unsigned long loopStart=millis();
unsigned long start=microSeconds();
Serial.print(elapsedMicroseconds(start));
Serial.print(",");
Serial.print(millis());
Serial.print(",");
Serial.print(microSeconds());
Serial.print(",");
Serial.print(elapsedMicroseconds(start));
Serial.print(",");
while (millis()-loopStart < looptime);
//below lines are for testing,
//there should not be anything outside the timed loop
//at runtime
Serial.print(millis()-loopStart); //testing only, messes up actual loop timing out here
Serial.print(",");
Serial.println(elapsedMicroseconds(start));
}
Output:
4,0,2304,7376,1000,1003852
4,1014,1020620,15556,1000,1002320
4,2026,2032344,15556,1000,1003324
4,3039,3045068,15556,1000,1003332
0,4051,4057808,15552,1000,1002288
0,5063,5069500,15556,1000,1002340
4,6075,6081244,15556,1000,1003320
4,7088,7093972,15556,1000,1003316
4,8100,8106692,15556,1000,1002328
4,9112,9118424,15556,1000,1003328
4,10125,10132196,17636,1000,1003348
4,11138,11144948,17636,1000,1003304
4,12150,12157652,17636,1000,1002308
0,13162,13169364,17636,1000,1003352
4,14175,14182124,17636,1000,1003336
4,15187,15194864,17636,1000,1002296
4,16199,16206564,17636,1000,1002308
4,17211,17218276,17636,1000,1003340
4,18224,18231020,17636,1000,1003328
4,19236,19243752,17636,1000,1002296
4,20248,20255452,17636,1000,1003348
4,21261,21268204,17636,1000,1003308
4,22274,22280916,17636,1000,1003348
4,23286,23293668,17636,1000,1002304
4,24298,24305372,17636,1000,1003340
4,25311,25318120,17636,1000,1003336
4,26323,26330860,17636,1000,1002300
4,27335,27342564,17636,1000,1002296
4,28347,28354264,17636,1000,1003328
4,29360,29366996,17636,1000,1003336
4,30372,30379736,17636,1000,1002320
4,31384,31391460,17636,1000,1003336
4,32397,32404200,17636,1000,1003336
4,33410,33416936,17640,1000,1003344
4,34422,34429684,17636,1000,1002288
4,35434,35441376,17636,1000,1003348
4,36447,36454128,17636,1000,1003296
4,37459,37466828,17636,1000,1002336
0,38471,38478564,17636,1000,1002300
4,39483,39490272,17636,1000,1003320
0,40496,40502996,17632,1000,1003332
4,41508,41515736,17636,1000,1002296
4,42520,42527436,17636,1000,1003356
0,43533,43540192,17636,1000,1003348
4,44546,44552944,17640,1000,1003332