Hello Everyone the code given below is used for pulse width measurement for pulses as short as 5 to 5 microseconds which I am giving to arduino from function generator. The code makes almost 3 counts every 3 microsecond which is not good since the count should be much more for a 16MHz clock frequency of arduino so if anyone can help me with this in achieving the correct counts every microsecond.
Please use the code tags to properly format your code.
So bascially you say you need ~1 microsecond per count. Let's see what I would expect the while loop to translate to:
reading pin
and operation
compare operation
branch operation
increment two byte integer
jump operation
--> I would not expect much more from this code. I suggest to use avrdump to disassemble the .elf file. Then use the datasheet to count the cycles. Once you understand how many cycles this code takes you can start to tune it.
Thanks for following up but actually I don't need 1 count per microsecond I am already getting 3 counts per microsecond but I need accuracy in my measurements for which it is not enough. What do you think using a external fast counter with following sequence would solve my problem?
providing 12 MHz clock from function generator
providing the pulse to the enable input.
Make an interrupt into the Arduino when the pulse has a negative edge (end of pulse.)
Read and reset the counter from the Arduino.
If I use 8 bit external fast counter with the above sequence?
IMHO you should first specify in more detail what you exactly need. How much precision, how much resolution and what is the maximum expected pulse width. What are you doing?
I am measuring pulse widths between 5 microseconds to 15 microseconds. I am giving these pulses from function generator by setting 1 Hz frequency. The code which I posted in my thread displays 1 microsecond or 0.5 microsecond when I give 1 microsecond width pulse similarly it gives either 10 or 9.5 when I apply 10 microsecond pulse width and so on so if for example when I give 10.3 or 10.2 or 10.1 it keeps on giving me 10 or 10.5 so it is not giving me correct reading for such values and that's what my problem is....
The accuracy of the code is 6/12 micro second which is 0.5 usec, so it measures in units of 0.5 micros. That is the accuracy of the algorithm.
If you have a pulse of 10.1 it will be approached to 9.5, 10.0, 10.5. [because there is no compensation yet for startup effects in the algorithm it can still have a fixed error]