Hi All,
I need to put a time stamp on data coming into the usb or serial port and wanted to get milliseconds resolution so I came up with this sketch as a test case. Does anyone think this would not work as reliable source for millisecond resolution on the RTC?
This sketch shows the elaspedMillis count value at interrupt time, I notice it over shoots every once and awhile by 1 millisecond, but works pretty well. I guess I'll have to collect some data to see if tracks fine at different temps.
I need to put a time stamp on data coming into the usb or serial port and wanted to get milliseconds resolution so I came up with this sketch as a test case. Does anyone think this would not work as reliable source for millisecond resolution on the RTC?
Code:
/*
append milli seconds to my time stamp using elapsedMillis whenever a USB packet is recieved.
*/
#include <Time.h>
#define LED 13
elapsedMillis rtcMilliSeconds;
void rtc_seconds_isr() {
rtcMilliSeconds = 0;// zero out millisecond counter
digitalWrite(LED, !digitalRead(LED));
}
void setup() {
pinMode(LED, OUTPUT);
Serial.begin(0);
while(!Serial);
delay(100);
setSyncProvider(getTeensy3Time);
}
void loop() {
if ( Serial.available() ) {
String packet;
while ( Serial.available() ) {
packet += (char)Serial.read();
delay(1);
}
String myTime = digitalClockDisplay(rtcMilliSeconds-1);
myTime += " | ";
Serial.print(myTime);// print current time + milliseconds
Serial.print(packet);// print data packet
}
}
String digitalClockDisplay(elapsedMillis millis_t) {
String time;
if (hour() < 10) time += 0;
time += hour() ;//< 10 ? '0' + hour() : hour();
time += ":";
if (minute() < 10) time += 0;
time += minute() ;//< 10 ? '0' + minute() : minute();
time += ":";
if (second() < 10) time += 0;
time += second();
time += ".";
if (millis_t < 100) time += 0;
if (millis_t < 10) time += 0;
time += (int)millis_t;
return time;
}
time_t getTeensy3Time() {
RTC_IER |= 0x10;// set rtc seconds interrupt
rtcMilliSeconds = 0;
NVIC_ENABLE_IRQ(IRQ_RTC_SECOND);// enable interrupt
return Teensy3Clock.get();
}
This sketch shows the elaspedMillis count value at interrupt time, I notice it over shoots every once and awhile by 1 millisecond, but works pretty well. I guess I'll have to collect some data to see if tracks fine at different temps.
Code:
/*
print elapsedMillis at seconds interrupt
*/
#include <Time.h>
#define LED 13
elapsedMillis rtcMilliSeconds;
void rtc_seconds_isr() {
Serial.println(rtcMilliSeconds);
//digitalWrite(LED, !digitalRead(LED));
rtcMilliSeconds = 0;// zero out millisecond counter
}
void setup() {
pinMode(LED, OUTPUT);
Serial.begin(0);
while(!Serial);
delay(100);
setSyncProvider(getTeensy3Time);
}
void loop() {
}
time_t getTeensy3Time() {
RTC_IER |= 0x10;// set rtc seconds interrupt
rtcMilliSeconds = 0;
NVIC_ENABLE_IRQ(IRQ_RTC_SECOND);// enable interrupt
return Teensy3Clock.get();
}
Last edited: