I'm measuring pulse widths using micros()
It looks like the smallest increment is 112 units. Eg it goes from 3136 to 3248 in one step. I need a more accurate measurement. Any suggestions ?
while(analogRead(5) < 200) {} // Wait for rising edge
RPMStartTime=micros();
while(analogRead(5) >= 200) {} // wait till falling edge
while(analogRead(5) < 200) {} // Wait for rising edge
RPMEndTime=micros();
RPMDuration=RPMEndTime-RPMStartTime;
The analog read is just the trigger, the micro() should still measure micros.
Your problem is that analogRead is not instantaneus. It needs 14 ADC cycles of 64 machine cycles to sample and measure input voltage. This adds up to 56 microseconds. In contrast, a digital read is immediate (a single machine cycle + function call overhead).
The following minimum resolutions would apply when sampling pulse duration:
Using analogRead: 56us * 2
Using digitalRead: about 2us * 2
Using direct port I/O: sub microsecond
The absolute minimum resolution for the AtMega microcontroller is a single machine cycle (62.5 nano seconds at 16Mhz).
My day job has interfered with my Ardrino programming.
Your problem is that analogRead is not instantaneus. It needs 14 ADC cycles of 64 machine cycles to sample and measure input voltage. This adds up to 56 microseconds. In contrast, a digital read is immediate (a single machine cycle + function call overhead).
I'm not sure I understand this properly
I have two rising edges, not related to each other at all. I understand that the Analog read takes 56 microseconds to read the data. So from the sample to result is 112 micros(). If I use 2 analog reads, the sample timing will be the same for both but why is the period between the two samples always multiples of 112.
Does the processor logic process the program from top to bottom or as in some PLC's, scan all the ladder logic and the only write the changes at the end of the scan.