Offline
Newbie
Karma: 0
Posts: 16
|
 |
« on: January 29, 2013, 08:36:54 pm » |
Hi, I am trying to measure a time of 1us. I realized the micros() function is only accurate to 4us. I was trying to use the following code to increment a variable every 1us. So far "MicroSecClock is always equal to 0. Is that the right strategy to capture 1us resolution? Thank you uint32_t MicroSecClock = 0;
ISR(TIMER2_OVF_vect) { static uint8_t count; // interrupt counter
if( (++count & 0x01) == 0 ) // bump the interrupt counter MicroSecClock++; // & count uSec every other time.
TCNT2 = 253; // reset counter 2MHz interrupt rate TIFR2 = 0x00; // clear timer overflow flag };
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 16
|
 |
« Reply #1 on: January 29, 2013, 08:56:37 pm » |
Perhaps, I may add that my goal is to make my own "micros()" function that is precise up to 1us rather than 4 us. Since I read that the micros() function is based upon the timers, I figured I had to come up with my own manipulation of the timers to achieve a 1us granularity. I'm just looking for pointers on the best way to accomplish this.
Thank you
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Edison Member
Karma: 114
Posts: 2205
|
 |
« Reply #2 on: January 29, 2013, 09:04:58 pm » |
At 16Mhz, it takes 1us to execute 16 nop instructions.
|
|
|
|
|
Logged
|
|
|
|
|
Left Coast, CA (USA)
Offline
Brattain Member
Karma: 279
Posts: 15316
Measurement changes behavior
|
 |
« Reply #3 on: January 29, 2013, 09:10:42 pm » |
Perhaps, I may add that my goal is to make my own "micros()" function that is precise up to 1us rather than 4 us. Since I read that the micros() function is based upon the timers, I figured I had to come up with my own manipulation of the timers to achieve a 1us granularity. I'm just looking for pointers on the best way to accomplish this.
Thank you
Probably harder then it might first appear depending on if you make it a blocking function or have to have it co-exist in an interruptible environment? Lefty
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Dallas
Online
Shannon Member
Karma: 118
Posts: 10149
|
 |
« Reply #4 on: January 29, 2013, 10:35:12 pm » |
I am trying to measure a time of 1us.
Is that the right strategy to capture 1us resolution? Which? Measure an elapsed time of 1 us or measure something with a resolution of 1 us?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 16
|
 |
« Reply #5 on: January 30, 2013, 09:54:48 am » |
I am trying to measure a time of 1us.
Is that the right strategy to capture 1us resolution? Which? Measure an elapsed time of 1 us or measure something with a resolution of 1 us? Hi, I am trying to measure something with a resolution of 1us. I was thinking that measuring and elapse time of 1us would allow me to do just that. Jean
|
|
|
|
|
Logged
|
|
|
|
|
Poole, Dorset, UK
Offline
God Member
Karma: 8
Posts: 671
|
 |
« Reply #6 on: January 30, 2013, 10:08:10 am » |
I think that you will find that the Arduinos are just to slow to do this.
Mark
|
|
|
|
|
Logged
|
|
|
|
|
United Kingdom
Offline
Faraday Member
Karma: 131
Posts: 4670
|
 |
« Reply #7 on: January 30, 2013, 11:32:52 am » |
I would set up timer 1 to run direct from the system clock, no prescaler. Wait in a tight loop for the input pin to change, using direct port i/o, then read the timer. Wait in another tight loop for the pin to change again, then read the timer again. You should be able to code the loops to run in less than 0.5us (i.e. less than 8 clock cycles), so 1us resolution should be just about achievable.
Alternatively, feed the signal to the input capture pin (or to the analog comparator), and use the input capture facility of timer 1 to record the times of the input transitions. That will give you a resolution of about 0.125us, however the shortest time you can record will be a few us.
|
|
|
|
|
Logged
|
Formal verification of safety-critical software, software development, and electronic design and prototyping. http://www.eschertech.com
|
|
|
|
Offline
God Member
Karma: 9
Posts: 836
|
 |
« Reply #8 on: January 30, 2013, 11:36:21 am » |
If you are trying to measure some small interval, devise a circuit which starts a pulse when the first event occurs and ends the pulse when the second event occurs.
Then use the pulse you created to charge up a capacitor.
And then measure the voltage achieved in the capacitor while it was accumulating current during the pulse.
It is not that easy to measure 1 us, the Arduino is not fast enough to do it using software.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Dallas
Online
Shannon Member
Karma: 118
Posts: 10149
|
 |
« Reply #9 on: January 30, 2013, 02:28:00 pm » |
I think that you will find that the Arduinos are just to slow to do this. That is not correct.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Dallas
Online
Shannon Member
Karma: 118
Posts: 10149
|
 |
« Reply #10 on: January 30, 2013, 02:38:37 pm » |
Alternatively, feed the signal to the input capture pin (or to the analog comparator), and use the input capture facility of timer 1 to record the times of the input transitions. That's what I would do. It works very well. There are examples available on the internet and probably on this forum. That will give you a resolution of about 0.125us, 0.0625us. No prescaler means the clock ticks with the processor. however the shortest time you can record will be a few us. If I remember correctly, the shortest elapsed time I was able to "input capture" was 3 us. The drawback is that you have to use a 16 bit timer (timer 1 on the Uno).
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Dallas
Online
Shannon Member
Karma: 118
Posts: 10149
|
 |
« Reply #11 on: January 30, 2013, 02:39:43 pm » |
It is not that easy to measure 1 us, the Arduino is not fast enough to do it using software. That is not correct.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Edison Member
Karma: 114
Posts: 2205
|
 |
« Reply #12 on: January 30, 2013, 02:46:06 pm » |
Use the capture function.
|
|
|
|
|
Logged
|
|
|
|
|
Pittsburgh, PA, USA
Offline
Faraday Member
Karma: 30
Posts: 2916
I only know some basic electricity....
|
 |
« Reply #13 on: January 30, 2013, 03:00:33 pm » |
If you are trying to measure some small interval, devise a circuit which starts a pulse when the first event occurs and ends the pulse when the second event occurs.
Then use the pulse you created to charge up a capacitor.
And then measure the voltage achieved in the capacitor while it was accumulating current during the pulse.
It is not that easy to measure 1 us, the Arduino is not fast enough to do it using software.
I have charged a cap (I forget the circuit but it included changing a pin from OUTPUT to INPUT to take advantage of the high INPUT impedance -- to me that is like <usec circuit rewiring) and counted loops till it bled out to ground through a resistor. That let me -stretch out- the measure. IIRC I was playing around with capsense at the time.
|
|
|
|
|
Logged
|
Examples can be found at Learning in the Main Site and at the Playground
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 16
|
 |
« Reply #14 on: January 30, 2013, 05:45:19 pm » |
Does anybody have a working code snippet I could use to achieve that?
|
|
|
|
|
Logged
|
|
|
|
|
|