Go Down

Topic: Micros() accurate to 2us using GPS/PPS pin (Read 2892 times) previous topic - next topic

sbright33

Just a few lines of code using millis(), works with micros() also.
Using PPS pin from Adafruit Ultimate GPS connected to pin 3.
Always prints 0, well for hours anyway.
Once you find your value instead of 122, you only need GPS signal in setup.
PPS continues to work for awhile, but you don't need that either.
The Arduino clock stays correct to 1ms for hours.
Or no GPS at all if you only care about relative time.

Code: [Select]

void setup(){
delay(20,000);
while(digitalRead(3)==0); long ms=millis();
}
void loop(){
while(digitalRead(3)==0); long m=millis();
long n=(m/1000-20)*122/1000;
Serial.println((m+n-ms)%1000);
}


sbright33

Noticed that the interrupts in Adafruit_GPS library greatly effect micros().  But no worries you can continue to use PPS even without a GPS signal.  Or useInterrupt(false);

sbright33

It's only a few lines of code, once you have the Adafruit library installed.  Has anyone else tested this?  Is my Uno magic, or does it work for all devices?  Did I make a mistake in my code causing me to be overly optimistic?  Have you tried micros()?

Go Up