Skip to content

Instantly share code, notes, and snippets.

@indraastra
Last active September 14, 2015 23:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save indraastra/782d836f0ab5f5575a74 to your computer and use it in GitHub Desktop.
Save indraastra/782d836f0ab5f5575a74 to your computer and use it in GitHub Desktop.
micros counter accuracy
void print_deltas(uint32_t t0_millis, uint32_t t0_micros) {
uint32_t t_m = millis();
uint32_t t_u = micros();
Serial.print(t_u - t0_micros);
Serial.print(", ");
Serial.println(t_m - t0_millis);
}
void setup() {
pinMode(D4, OUTPUT);
Serial.begin(9600);
delay(2000);
uint32_t t0_m = millis();
uint32_t t0_u = micros();
print_deltas(t0_m, t0_u); // Relative time 0.
__disable_irq();
digitalWrite(D4, HIGH);
delayMicroseconds(5000);
digitalWrite(D4, LOW);
print_deltas(t0_m, t0_u); // Relative time 5000.
__enable_irq();
print_deltas(t0_m, t0_u); // Relative time still 5000.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment