<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="bbPress/1.0.2" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title>LeafLabs Garden &#187; Topic: Using a timer for time stamp</title>
		<link>http://forums.leaflabs.com/topic.php?id=74172</link>
		<description>A place to share, learn, and grow...</description>
		<language>en-US</language>
		<pubDate>Fri, 22 Jan 2016 00:17:58 +0000</pubDate>
		<generator>http://bbpress.org/?v=1.0.2</generator>
		<textInput>
			<title><![CDATA[Search]]></title>
			<description><![CDATA[Search all topics from these forums.]]></description>
			<name>q</name>
			<link>http://forums.leaflabs.com/search.php</link>
		</textInput>
		<atom:link href="http://forums.leaflabs.com/rss.php?topic=74172" rel="self" type="application/rss+xml" />

		<item>
			<title>samtal on "Using a timer for time stamp"</title>
			<link>http://forums.leaflabs.com/topic.php?id=74172#post-104990</link>
			<pubDate>Thu, 28 Nov 2013 13:49:27 +0000</pubDate>
			<dc:creator>samtal</dc:creator>
			<guid isPermaLink="false">104990@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Thanks guys.&#60;br /&#62;
I got it!
&#60;/p&#62;</description>
		</item>
		<item>
			<title>gbulmer on "Using a timer for time stamp"</title>
			<link>http://forums.leaflabs.com/topic.php?id=74172#post-104989</link>
			<pubDate>Thu, 28 Nov 2013 13:28:28 +0000</pubDate>
			<dc:creator>gbulmer</dc:creator>
			<guid isPermaLink="false">104989@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;samtal - arithmetic works correctly even when the unsigned number 'rolls-over'&#60;/p&#62;
&#60;p&#62;Try:&#60;br /&#62;
&#60;pre&#62;&#60;code&#62;uint32 curTime = 1;
uint32 prevTime = 0xFFFFFFF0;
uint32 diff = curTime - prevTime;

Serial1.println(diff);&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;The number printed is 17 (1+16)
&#60;/p&#62;</description>
		</item>
		<item>
			<title>mlundinse on "Using a timer for time stamp"</title>
			<link>http://forums.leaflabs.com/topic.php?id=74172#post-104988</link>
			<pubDate>Thu, 28 Nov 2013 13:12:51 +0000</pubDate>
			<dc:creator>mlundinse</dc:creator>
			<guid isPermaLink="false">104988@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;millis rollover is not a problem, shure the starttime is greater than the endtime when the rollover occurs in the intervall, but the difference will become a small positive number, when converted to unit16 it will still give you the correct number of milliseconds. &#60;/p&#62;
&#60;p&#62;Example:&#60;br /&#62;
start time 10 mills before before rollover  t0 = 2^32 - 10&#60;br /&#62;
end time 65 millis after rollover           t1 = 65&#60;br /&#62;
elapsed time t1 - t0 = 65 - (2^32 - 10 ) = 65 + 10 - 2^32&#60;br /&#62;
The 2^32 is truncated when using 32 bit registers, so the result will be 65 + 10 = 75 as it should
&#60;/p&#62;</description>
		</item>
		<item>
			<title>samtal on "Using a timer for time stamp"</title>
			<link>http://forums.leaflabs.com/topic.php?id=74172#post-104987</link>
			<pubDate>Thu, 28 Nov 2013 13:01:05 +0000</pubDate>
			<dc:creator>samtal</dc:creator>
			<guid isPermaLink="false">104987@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Thanks mlundinse.&#60;/p&#62;
&#60;p&#62;Your proposed solution is easy, but, as I wrote, the use of millis() will roll-over at some time (after 49 days from start) and will give erroneous time, while my device is planned to run continuously for years.&#60;br /&#62;
I need a timer with 1 mSec resolution that that will count up to 30 seconds, and will restart for each cycle of my device (this will happen every few hours).&#60;br /&#62;
A solution could be my 1000 mSec timer and a 16 bits counter.&#60;br /&#62;
I therefore try to use one of the ST32 built-in timers.&#60;br /&#62;
I have done this in the past by low level programming (timers registers), and I now try to use the high-level IDE Timer().&#60;br /&#62;
I just don't know how to do it right!
&#60;/p&#62;</description>
		</item>
		<item>
			<title>mlundinse on "Using a timer for time stamp"</title>
			<link>http://forums.leaflabs.com/topic.php?id=74172#post-104986</link>
			<pubDate>Thu, 28 Nov 2013 12:18:03 +0000</pubDate>
			<dc:creator>mlundinse</dc:creator>
			<guid isPermaLink="false">104986@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Your timer doesnt count milliseconds, it counts to 1000 and then restarts.&#60;br /&#62;
Use mills() and a variable uint32 time0 to record the starttime of the current state: &#60;/p&#62;
&#60;p&#62;&#60;code&#62;&#60;br /&#62;
void loop()&#60;br /&#62;
{&#60;br /&#62;
int index = 0;&#60;br /&#62;
uint32 time0;&#60;br /&#62;
do&#60;br /&#62;
{&#60;br /&#62;
state = digitalRead(15);&#60;br /&#62;
time0 = millis();&#60;br /&#62;
while (digitalRead(15)== state) { } //loop wait for state change&#60;br /&#62;
time[index] = (millis()-time0)&#38;amp;0xFFFF; //Use elapsed number of millis to add time stamp to array.&#60;br /&#62;
index++;&#60;br /&#62;
}&#60;br /&#62;
while (digitalRead(16));&#60;br /&#62;
int i;&#60;br /&#62;
for (i=0;i&#38;lt;10;i++) //Print the array (testing)&#60;br /&#62;
Serial1.println(time[i]);&#60;br /&#62;
}&#60;br /&#62;
&#60;/code&#62;
&#60;/p&#62;</description>
		</item>
		<item>
			<title>samtal on "Using a timer for time stamp"</title>
			<link>http://forums.leaflabs.com/topic.php?id=74172#post-104985</link>
			<pubDate>Thu, 28 Nov 2013 11:43:47 +0000</pubDate>
			<dc:creator>samtal</dc:creator>
			<guid isPermaLink="false">104985@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Hi,&#60;br /&#62;
I have a digital input channel that change state every few hundreds millis.&#60;br /&#62;
I need to collect the state change times in an array with 1 mSec resolution.&#60;br /&#62;
I can not use millis() because I need to restart at 0 each time.&#60;br /&#62;
I try to use a Timer, but it gives me a random series of times rather than the real times.&#60;br /&#62;
I am probably doing something wrong.&#60;/p&#62;
&#60;p&#62;Here is a test code:(that does not work as I expect)&#60;/p&#62;
&#60;pre&#62;&#60;code&#62;HardwareTimer timer(1);
uint16 time[20];  //Time stamp array
uint8 state;
uint16 counts;

void setup()
{
  timer.setPeriod(1000);
  Serial1.begin(115200);
}

void loop()
{
  int index = 0;
  timer.setCount(0);   //Zero and reset the timer.
  timer.resume();
  do
  {
    state = digitalRead(15);
    while (digitalRead(15)== state) {   } //loop wait for state change
    time[index] = timer.getCount();       //Add time stamp to array.
    index++;
  }
  while (digitalRead(16));
  int i;
  for (i=0;i&#38;lt;10;i++)                //Print the array (testing)
    Serial1.println(time[i]);
}&#60;/code&#62;&#60;/pre&#62;</description>
		</item>

	</channel>
</rss>
