Hi all,
I am using Teensy 3.6 for my research project where I need timing features with accuracy down to 0.5 microsecond. The built-in micro() can only go to 4 microsecond resolution (same with arduino I guess) so I tried using another library called eRCaGuy_timer_counter (see link: http://www.electricrcaircraftguy.com/2014/02/Timer2Counter-more-precise-Arduino-micros-function.html) , which works fine with Arduino but cannot compile with teensy.
Here is the example code:
The error message (see attached file) stated that the timer is overflowed.
Would you please let me know if there’s a way to go around this? Or if there’s another function in Teensy that can go to this time accuracy? Thank you very much for your help.
Regards,
Minh
I am using Teensy 3.6 for my research project where I need timing features with accuracy down to 0.5 microsecond. The built-in micro() can only go to 4 microsecond resolution (same with arduino I guess) so I tried using another library called eRCaGuy_timer_counter (see link: http://www.electricrcaircraftguy.com/2014/02/Timer2Counter-more-precise-Arduino-micros-function.html) , which works fine with Arduino but cannot compile with teensy.
Here is the example code:
PHP:
//include the library
#include <eRCaGuy_Timer2_Counter.h>
[ATTACH]11524._xfImport[/ATTACH]
//Note: an object of this class was already pre-instantiated in the .cpp file of this library, so you can simply access its methods (functions)
// directly now through the object name "timer2"
//eRCaGuy_Timer2_Counter timer2; //this is what the pre-instantiation line from the .cpp file looks like
void setup() {
//configure Timer2
timer2.setup(); //this MUST be done before the other Timer2_Counter functions work; Note: since this messes up PWM outputs on pins 3 & 11, as well as
//interferes with the tone() library (http://arduino.cc/en/reference/tone), you can always revert Timer2 back to normal by calling
//timer2.unsetup()
//prepare serial
Serial.begin(115200);
//Output a header of info:
Serial.println(F("Notes:"));
Serial.println(F("micros() has a precision of 4us"));
Serial.println(F("get_count() with unsigned long final data type has a final precision of 1us, and is fast"));
Serial.println(F("get_count() with float final data type has a final precision of 0.5us, and is not quite as fast"));
Serial.println(F("get_micros() has a precision of 0.5us, and is slower than the above 2 methods, so one of the above 2 methods is preferred"));
Serial.println(F("=============================================="));
}
void loop() {
//declare local variables
static unsigned long t_start = timer2.get_count(); //units of 0.5us; the count accumulated by Timer2_Counter
//acquire time stamps
unsigned long t_micros = micros();
unsigned long t_T2_count = timer2.get_count(); //units of 0.5us; the count accumulated by Timer2_Counter
float t_T2_micros = timer2.get_micros(); //us; the time value accumulated by Timer2_Counter (this is the exact same as doing timer2.get_count()/2.0;)
//See if 1.000003 seconds has elapsed. If so, print out the time stamps. Note: I am using this elapsed time because I want it to NOT be divisible by 4, so that
//you can hopefully see the extra precision provided by the Timer2_Counter library, which the default Arduino micros() function does not have
if ((t_T2_count - t_start)/2 >= 1000003) //if 1.000003 seconds has elapsed
{
t_start = t_T2_count; //update start time
//Print times using micros(), get_count(), and get_micros()
//Demonstrate several ways of using these functions
Serial.print("micros() = "); Serial.print(t_micros); Serial.println(" us");
Serial.print("get_count() = "); Serial.print(t_T2_count); Serial.println(" (units of 0.5 us per count)");
Serial.print("get_count()/2 = "); Serial.print(t_T2_count/2); Serial.println(" us");
Serial.print("get_count()/2.0 = "); Serial.print(t_T2_count/2.0); Serial.println(" us");
Serial.print("get_micros() = "); Serial.print(t_T2_micros); Serial.println(" us");
Serial.println(""); //add an extra line space
}
} //end of loop()
The error message (see attached file) stated that the timer is overflowed.
Would you please let me know if there’s a way to go around this? Or if there’s another function in Teensy that can go to this time accuracy? Thank you very much for your help.
Regards,
Minh