Tested on CC3200.
I am trying to see how long a pin is held in one state, the time will be pretty short at most 100us.
So I tried with micros() before and after, but didnt get good results, then I tried timing a simple pinmode and digitalwrite, that gave a negative or very large value...
So I boiled it down to this example
while(1)
{
unsigned long i = micros();
delayMicroseconds(100);
unsigned long p = micros();
unsigned long o = p-i;
Serial.print("Tooki : "); Serial.println(i);
Serial.print("Tookp : "); Serial.println(p);
Serial.print("Tooko : "); Serial.println(o);
i = micros();
delayMicroseconds(1000);
p = micros();
o = p-i;
Serial.print("Tooki : "); Serial.println(i);
Serial.print("Tookp : "); Serial.println(p);
Serial.print("Tooko : "); Serial.println(o);
Serial.println();Serial.println();Serial.println();
delay(1000);
}
I have made a time machine :)
Tooki : 112690699 Tookp : 112690597 Tooko : 4294967194 Tooki : 112693032 Tookp : 112694699 Tooko : 1667
The numbers are stable, the funny part is that i-p is about 100, but p is set to micros() after the sleep of 100micro seconds so it should be lower than i.
If I increase the sleep to 1000(1 mili second if I am not mistaken), then it works correctly
Tooki : 4064995 Tookp : 4065995 Tooko : 1000 Tooki : 4067298 Tookp : 4068995 Tooko : 1697
This is tested in a while loop at the end of the setup function.
I have searched for similar issues but they seemed to relate to using the function in interrupts, if thers another thread on the subject already, please send me in the right direction.