<?xml version="1.0" encoding="UTF-8"?><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"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: I will think before I code&#8230;</title>
	<atom:link href="http://gonium.net/md/2006/12/27/i-will-think-before-i-code/feed/" rel="self" type="application/rss+xml" />
	<link>http://gonium.net/md/2006/12/27/i-will-think-before-i-code/</link>
	<description>so much time, so little to do.</description>
	<lastBuildDate>Sun, 07 Mar 2010 18:56:13 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Arduino: Interrupción en Timer2 &#171; Anrusal&#39;s Blog</title>
		<link>http://gonium.net/md/2006/12/27/i-will-think-before-i-code/comment-page-1/#comment-2453</link>
		<dc:creator>Arduino: Interrupción en Timer2 &#171; Anrusal&#39;s Blog</dc:creator>
		<pubDate>Thu, 29 Oct 2009 23:11:01 +0000</pubDate>
		<guid isPermaLink="false">http://gonium.net/md/2006/12/27/i-will-think-before-i-code/#comment-2453</guid>
		<description>[...] http://gonium.net/md/2006/12/27/i-will-think-before-i-code/  Dejar un comentario   Aún no hay comentarios por mucho  Deja un comentario   Canal RSS de los comentarios de la entrada. URI para TrackBack.     Deja un comentario Haz clic para cancelar la respuesta. Línea y párrafo se rompe automáticamente, direcciones email nunca se muestran, [...]</description>
		<content:encoded><![CDATA[<p>[...] <a href="http://gonium.net/md/2006/12/27/i-will-think-before-i-code/" rel="nofollow">http://gonium.net/md/2006/12/27/i-will-think-before-i-code/</a>  Dejar un comentario   Aún no hay comentarios por mucho  Deja un comentario   Canal RSS de los comentarios de la entrada. URI para TrackBack.     Deja un comentario Haz clic para cancelar la respuesta. Línea y párrafo se rompe automáticamente, direcciones email nunca se muestran, [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: pee panty</title>
		<link>http://gonium.net/md/2006/12/27/i-will-think-before-i-code/comment-page-1/#comment-2425</link>
		<dc:creator>pee panty</dc:creator>
		<pubDate>Sat, 18 Jul 2009 20:37:59 +0000</pubDate>
		<guid isPermaLink="false">http://gonium.net/md/2006/12/27/i-will-think-before-i-code/#comment-2425</guid>
		<description>emm. informative )</description>
		<content:encoded><![CDATA[<p>emm. informative )</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: basbrun.com &#187; Arduino Timer interrupt</title>
		<link>http://gonium.net/md/2006/12/27/i-will-think-before-i-code/comment-page-1/#comment-2424</link>
		<dc:creator>basbrun.com &#187; Arduino Timer interrupt</dc:creator>
		<pubDate>Fri, 10 Jul 2009 13:50:52 +0000</pubDate>
		<guid isPermaLink="false">http://gonium.net/md/2006/12/27/i-will-think-before-i-code/#comment-2424</guid>
		<description>[...] I found the following code and adapted it to the ATMEGA 328. http://gonium.net/md/2006/12/27/i-will-think-before-i-code/ [...]</description>
		<content:encoded><![CDATA[<p>[...] I found the following code and adapted it to the ATMEGA 328. <a href="http://gonium.net/md/2006/12/27/i-will-think-before-i-code/" rel="nofollow">http://gonium.net/md/2006/12/27/i-will-think-before-i-code/</a> [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Roger Haxby</title>
		<link>http://gonium.net/md/2006/12/27/i-will-think-before-i-code/comment-page-1/#comment-2409</link>
		<dc:creator>Roger Haxby</dc:creator>
		<pubDate>Mon, 16 Mar 2009 20:56:21 +0000</pubDate>
		<guid isPermaLink="false">http://gonium.net/md/2006/12/27/i-will-think-before-i-code/#comment-2409</guid>
		<description>I borrowed your interrupt code, trying to do a serial read and write driven by the interrupt timings.  Thank you.</description>
		<content:encoded><![CDATA[<p>I borrowed your interrupt code, trying to do a serial read and write driven by the interrupt timings.  Thank you.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Donal</title>
		<link>http://gonium.net/md/2006/12/27/i-will-think-before-i-code/comment-page-1/#comment-2406</link>
		<dc:creator>Donal</dc:creator>
		<pubDate>Thu, 19 Feb 2009 17:40:51 +0000</pubDate>
		<guid isPermaLink="false">http://gonium.net/md/2006/12/27/i-will-think-before-i-code/#comment-2406</guid>
		<description>Hi Mathias,

We ended up just counting the number of reads in a specified time and found that they averaged out as they should.

Many thanks for your help,

Donal.</description>
		<content:encoded><![CDATA[<p>Hi Mathias,</p>
<p>We ended up just counting the number of reads in a specified time and found that they averaged out as they should.</p>
<p>Many thanks for your help,</p>
<p>Donal.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: md</title>
		<link>http://gonium.net/md/2006/12/27/i-will-think-before-i-code/comment-page-1/#comment-2403</link>
		<dc:creator>md</dc:creator>
		<pubDate>Wed, 04 Feb 2009 02:04:01 +0000</pubDate>
		<guid isPermaLink="false">http://gonium.net/md/2006/12/27/i-will-think-before-i-code/#comment-2403</guid>
		<description>Hi Donal,

sorry, I was confused - of course you&#039;re right, the code uses interrupts (I somehow thought of my first version of the sketch from which I remembered this behaviour, sorry). The interrupts itself are highly accurate. I would guess that the variations in the timing are a result of the calls to the Serial.println(...) method which is pretty heavyweight. This would explain the milliseconds variation. I don&#039;t have an Arduino here, but you can test this simply by precomputing the values to be printed before the Serial.println calls. I&#039;m thinking of something like

if (oldsecond != second) {
  long difference=millis() - starttime;
  Serial.print(second);  
  Serial.print(&quot;. -&gt;&quot;);   
  Serial.print(difference);  
  Serial.println(&quot;.&quot;);
}  

Please let me know whether this explains the behaviour. If not you can also adjust the timing settings of the interrupt. Please note that Timer2 is used here which has an 8-bit resolution. You could also try to use Timer1 instead which is a 16-bit timer. Depending on your problem you should also read the ATMega168 datasheet - for example, you can built a simple hardware frequency generator based on the timers. Atmel has also a nice introduction to the timer hardware, google for &quot;AVR130: Setup and Use the AVR Timers&quot;.

HTH
-Mathias</description>
		<content:encoded><![CDATA[<p>Hi Donal,</p>
<p>sorry, I was confused &#8211; of course you&#8217;re right, the code uses interrupts (I somehow thought of my first version of the sketch from which I remembered this behaviour, sorry). The interrupts itself are highly accurate. I would guess that the variations in the timing are a result of the calls to the Serial.println(&#8230;) method which is pretty heavyweight. This would explain the milliseconds variation. I don&#8217;t have an Arduino here, but you can test this simply by precomputing the values to be printed before the Serial.println calls. I&#8217;m thinking of something like</p>
<p>if (oldsecond != second) {<br />
  long difference=millis() &#8211; starttime;<br />
  Serial.print(second);<br />
  Serial.print(&#8220;. ->&#8221;);<br />
  Serial.print(difference);<br />
  Serial.println(&#8220;.&#8221;);<br />
}  </p>
<p>Please let me know whether this explains the behaviour. If not you can also adjust the timing settings of the interrupt. Please note that Timer2 is used here which has an 8-bit resolution. You could also try to use Timer1 instead which is a 16-bit timer. Depending on your problem you should also read the ATMega168 datasheet &#8211; for example, you can built a simple hardware frequency generator based on the timers. Atmel has also a nice introduction to the timer hardware, google for &#8220;AVR130: Setup and Use the AVR Timers&#8221;.</p>
<p>HTH<br />
-Mathias</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Donal</title>
		<link>http://gonium.net/md/2006/12/27/i-will-think-before-i-code/comment-page-1/#comment-2402</link>
		<dc:creator>Donal</dc:creator>
		<pubDate>Tue, 03 Feb 2009 20:19:38 +0000</pubDate>
		<guid isPermaLink="false">http://gonium.net/md/2006/12/27/i-will-think-before-i-code/#comment-2402</guid>
		<description>Hi again,

many thanks for your reply... I must apologise in advance as I&#039;m a bit of a newbie here... but I was under the impression that this code does use &#039;timer interrupts&#039;. I thought it was working off an internal timer that overflows every 256 clock cycles and that when it does the ISR would be triggered. The code I&#039;m using is actually adapted for the atmega168 chip and I have included it below.

My main question now is... is there a more appropriate &#039;type&#039; of interrupt to be using here?

Many thanks,

Donal.

#include   
 #include   
   
 #define INIT_TIMER_COUNT 6  
 #define RESET_TIMER2 TCNT2 = INIT_TIMER_COUNT  
   
 int ledPin = 13;  
 int int_counter = 0;  
 volatile int second = 0;  
 int oldSecond = 0;  
 long starttime = 0;  
 //unsigned long m = millis();
   
 // Aruino runs at 16 Mhz, so we have 1000 Overflows per second...  
 // 1/ ((16000000 / 64) / 256) = 1 / 1000  
 ISR(TIMER2_OVF_vect) {  
   RESET_TIMER2;  
   int_counter += 1;  
   if (int_counter == 4) {  
     second+=1;  
     int_counter = 0;  
   }   
 };  
   
 void setup() {  
   Serial.begin(9600);  
   Serial.println(&quot;Initializing timerinterrupt&quot;);  
   //so we just hard code this for now.
  //Timer2 Settings: Timer Prescaler /64,
  TCCR2A &#124;= (1&lt;&lt;CS22);
  TCCR2A &amp;= ~((1&lt;&lt;CS21) &#124; (1&lt;&lt;CS20));
  // Use normal mode
  TCCR2A &amp;= ~((1&lt;&lt;WGM21) &#124; (1&lt;&lt;WGM20));
  // Use internal clock - external clock not used in Arduino
  ASSR &#124;= (0&lt;&lt;AS2);
  //Timer2 Overflow Interrupt Enable
  TIMSK2 &#124;= (1&lt;&lt;TOIE2) &#124; (0&lt;&lt;OCIE2A);

  sei();
  //Timer2 Overflow Interrupt Enable
  TIMSK2 = 1&lt;&quot;);  
     Serial.println(millis() - starttime, DEC);  
     //Serial.println(&quot;.&quot;);  
     //digitalWrite(ledPin, HIGH);  
     //delay(100);  
     //digitalWrite(ledPin, LOW);  
     oldSecond = second;
     starttime = millis();  
   }  
 }</description>
		<content:encoded><![CDATA[<p>Hi again,</p>
<p>many thanks for your reply&#8230; I must apologise in advance as I&#8217;m a bit of a newbie here&#8230; but I was under the impression that this code does use &#8216;timer interrupts&#8217;. I thought it was working off an internal timer that overflows every 256 clock cycles and that when it does the ISR would be triggered. The code I&#8217;m using is actually adapted for the atmega168 chip and I have included it below.</p>
<p>My main question now is&#8230; is there a more appropriate &#8216;type&#8217; of interrupt to be using here?</p>
<p>Many thanks,</p>
<p>Donal.</p>
<p>#include<br />
 #include   </p>
<p> #define INIT_TIMER_COUNT 6<br />
 #define RESET_TIMER2 TCNT2 = INIT_TIMER_COUNT  </p>
<p> int ledPin = 13;<br />
 int int_counter = 0;<br />
 volatile int second = 0;<br />
 int oldSecond = 0;<br />
 long starttime = 0;<br />
 //unsigned long m = millis();</p>
<p> // Aruino runs at 16 Mhz, so we have 1000 Overflows per second&#8230;<br />
 // 1/ ((16000000 / 64) / 256) = 1 / 1000<br />
 ISR(TIMER2_OVF_vect) {<br />
   RESET_TIMER2;<br />
   int_counter += 1;<br />
   if (int_counter == 4) {<br />
     second+=1;<br />
     int_counter = 0;<br />
   }<br />
 };  </p>
<p> void setup() {<br />
   Serial.begin(9600);<br />
   Serial.println(&#8220;Initializing timerinterrupt&#8221;);<br />
   //so we just hard code this for now.<br />
  //Timer2 Settings: Timer Prescaler /64,<br />
  TCCR2A |= (1&lt;&lt;CS22);<br />
  TCCR2A &amp;= ~((1&lt;&lt;CS21) | (1&lt;&lt;CS20));<br />
  // Use normal mode<br />
  TCCR2A &amp;= ~((1&lt;&lt;WGM21) | (1&lt;&lt;WGM20));<br />
  // Use internal clock &#8211; external clock not used in Arduino<br />
  ASSR |= (0&lt;&lt;AS2);<br />
  //Timer2 Overflow Interrupt Enable<br />
  TIMSK2 |= (1&lt;&lt;TOIE2) | (0&lt;&lt;OCIE2A);</p>
<p>  sei();<br />
  //Timer2 Overflow Interrupt Enable<br />
  TIMSK2 = 1&lt;&#8221;);<br />
     Serial.println(millis() &#8211; starttime, DEC);<br />
     //Serial.println(&#8220;.&#8221;);<br />
     //digitalWrite(ledPin, HIGH);<br />
     //delay(100);<br />
     //digitalWrite(ledPin, LOW);<br />
     oldSecond = second;<br />
     starttime = millis();<br />
   }<br />
 }</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: md</title>
		<link>http://gonium.net/md/2006/12/27/i-will-think-before-i-code/comment-page-1/#comment-2401</link>
		<dc:creator>md</dc:creator>
		<pubDate>Tue, 03 Feb 2009 19:05:33 +0000</pubDate>
		<guid isPermaLink="false">http://gonium.net/md/2006/12/27/i-will-think-before-i-code/#comment-2401</guid>
		<description>Hi Donal,

I don&#039;t think this code is appropriate for measuring time in the millisecond range. Basically it uses busy waiting in order to detect the time interval between two DCF77 signals. If you want to measure time exactly, I suggest you take a look at interrupts. The ATMega168 chip provides three programmable timer interrupts which can be used to sample something with high accuracy.

HTH,
-Mathias</description>
		<content:encoded><![CDATA[<p>Hi Donal,</p>
<p>I don&#8217;t think this code is appropriate for measuring time in the millisecond range. Basically it uses busy waiting in order to detect the time interval between two DCF77 signals. If you want to measure time exactly, I suggest you take a look at interrupts. The ATMega168 chip provides three programmable timer interrupts which can be used to sample something with high accuracy.</p>
<p>HTH,<br />
-Mathias</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Donal</title>
		<link>http://gonium.net/md/2006/12/27/i-will-think-before-i-code/comment-page-1/#comment-2400</link>
		<dc:creator>Donal</dc:creator>
		<pubDate>Tue, 03 Feb 2009 11:35:42 +0000</pubDate>
		<guid isPermaLink="false">http://gonium.net/md/2006/12/27/i-will-think-before-i-code/#comment-2400</guid>
		<description>Hello,

when i run this code on my arduino the ms print out has a variation of about 2 - 3 ms, i.e. i see

1 - 1001
2 - 2004
3 - 3002
etc...

is this expected? I am looking for something that can guarantee a consistent 4 ms sampling rate. Is that possible do you think?</description>
		<content:encoded><![CDATA[<p>Hello,</p>
<p>when i run this code on my arduino the ms print out has a variation of about 2 &#8211; 3 ms, i.e. i see</p>
<p>1 &#8211; 1001<br />
2 &#8211; 2004<br />
3 &#8211; 3002<br />
etc&#8230;</p>
<p>is this expected? I am looking for something that can guarantee a consistent 4 ms sampling rate. Is that possible do you think?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: md</title>
		<link>http://gonium.net/md/2006/12/27/i-will-think-before-i-code/comment-page-1/#comment-2382</link>
		<dc:creator>md</dc:creator>
		<pubDate>Mon, 06 Oct 2008 06:48:36 +0000</pubDate>
		<guid isPermaLink="false">http://gonium.net/md/2006/12/27/i-will-think-before-i-code/#comment-2382</guid>
		<description>Hi Stijn,

I haven&#039;t tested the code with recent Arduino versions - but your problem has something to do with the naming of control registers. Do you use an ATMega168-based Arduino? There are some differences in register naming.

For example, for my DCF77 library I had to define things differently, see

http://gonium.net/md/2007/04/18/tweaking-the-code/

Try to adjust the code accordingly.

HTH,
-Mathias</description>
		<content:encoded><![CDATA[<p>Hi Stijn,</p>
<p>I haven&#8217;t tested the code with recent Arduino versions &#8211; but your problem has something to do with the naming of control registers. Do you use an ATMega168-based Arduino? There are some differences in register naming.</p>
<p>For example, for my DCF77 library I had to define things differently, see</p>
<p><a href="http://gonium.net/md/2007/04/18/tweaking-the-code/" rel="nofollow">http://gonium.net/md/2007/04/18/tweaking-the-code/</a></p>
<p>Try to adjust the code accordingly.</p>
<p>HTH,<br />
-Mathias</p>
]]></content:encoded>
	</item>
</channel>
</rss>
