<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>gonium.net &#187; interrupt</title>
	<atom:link href="http://gonium.net/md/tag/interrupt/feed/" rel="self" type="application/rss+xml" />
	<link>http://gonium.net/md</link>
	<description>so much time, so little to do.</description>
	<lastBuildDate>Fri, 30 Jul 2010 13:47:27 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>I will think before I code&#8230;</title>
		<link>http://gonium.net/md/2006/12/27/i-will-think-before-i-code/</link>
		<comments>http://gonium.net/md/2006/12/27/i-will-think-before-i-code/#comments</comments>
		<pubDate>Wed, 27 Dec 2006 09:54:04 +0000</pubDate>
		<dc:creator>md</dc:creator>
				<category><![CDATA[arduino]]></category>
		<category><![CDATA[digital]]></category>
		<category><![CDATA[english]]></category>
		<category><![CDATA[ATMega]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[interrupt]]></category>
		<category><![CDATA[microcontroller]]></category>

		<guid isPermaLink="false">http://gonium.net/md/2006/12/27/i-will-think-before-i-code/</guid>
		<description><![CDATA[As CosineKitty pointed out: My Arduino timer interrupt code did not work because the Prescaler bits were not set correctly. 



Applying basic boolean logic helps ;-) This is the working code:


#include < avr / interrupt.h >
#include < avr / io.h >

#define INIT_TIMER_COUNT 6
#define RESET_TIMER2 TCNT2 = INIT_TIMER_COUNT

int ledPin = 13;
int int_counter = 0;
volatile int second [...]]]></description>
			<content:encoded><![CDATA[As <a href="http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1167000525">CosineKitty</a> pointed out: My <a href="http://gonium.net/md/2006/12/23/arduino-timer-interrupt/">Arduino timer interrupt</a> code did not work because the Prescaler bits were not set correctly. 

<span id="more-33"></span>

Applying basic boolean logic helps ;-) This is the working code:


#include < avr / interrupt.h >
#include < avr / io.h >

#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;

// 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 == 1000) {
    second+=1;
    int_counter = 0;
  } 
};

void setup() {
  Serial.begin(9600);
  Serial.println("Initializing timerinterrupt");
  //Timer2 Settings: Timer Prescaler /64, 
  TCCR2 |= (1< <CS22);    
  TCCR2 &#038;= ~((1<<CS21) | (1<<CS20));     
  // Use normal mode
  TCCR2 &#038;= ~((1<<WGM21) | (1<<WGM20));  
  // Use internal clock - external clock not used in Arduino
  ASSR |= (0<<AS2);
  //Timer2 Overflow Interrupt Enable
  TIMSK |= (1<<TOIE2) | (0<<OCIE2);  
  RESET_TIMER2;               
  sei();
  starttime = millis();
}

void loop() {
  if (oldSecond != second) {
    Serial.print(second);
    Serial.print(". ->");
    Serial.print(millis() - starttime);
    Serial.println(".");
    digitalWrite(ledPin, HIGH);
    delay(100);
    digitalWrite(ledPin, LOW);
    oldSecond = second;
  }
}


I will think before I code, I will think before I code, I will think before I code, ...

The timer now works perfectly. I think the CTC mode will be more efficient, but my measurement shows that the clock has a slight skew (3 ms) when the Arduino starts up (lets say, during the first five minutes). Later, the skew is about zero. So the overflow mechanism is quite exact ;-)

Note that the TIMER2 is used for PWM in the Arduino libraries, so this sketch might interfere with PWM applications - I did not test this.]]></content:encoded>
			<wfw:commentRss>http://gonium.net/md/2006/12/27/i-will-think-before-i-code/feed/</wfw:commentRss>
		<slash:comments>24</slash:comments>
		</item>
		<item>
		<title>Handling external Interrupts with Arduino</title>
		<link>http://gonium.net/md/2006/12/20/handling-external-interrupts-with-arduino/</link>
		<comments>http://gonium.net/md/2006/12/20/handling-external-interrupts-with-arduino/#comments</comments>
		<pubDate>Wed, 20 Dec 2006 21:11:53 +0000</pubDate>
		<dc:creator>md</dc:creator>
				<category><![CDATA[arduino]]></category>
		<category><![CDATA[digital]]></category>
		<category><![CDATA[english]]></category>
		<category><![CDATA[nerdism]]></category>
		<category><![CDATA[interrupt]]></category>
		<category><![CDATA[microcontroller]]></category>

		<guid isPermaLink="false">http://gonium.net/md/2006/12/20/handling-external-interrupts-with-arduino/</guid>
		<description><![CDATA[
For my DCF77 clock project, I need an understanding of handling interrupts with the ATMega8 chip &#8211; here&#8217;s my sketch.

The ATMega8 provides two pins (2 and 3) which can trigger software interrupts when the attached digital signal changes. You can use this to be &#8220;notified&#8221; when the external signal changes. Therefore, you do not need [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://gonium.net/md/wp-content/uploads/2006/12//arduino-interrupt.jpg" border="0" height="300" width="400" alt="arduino-interrupt.jpg" align="" /></p>
<p>For my DCF77 clock project, I need an understanding of handling interrupts with the ATMega8 chip &#8211; here&#8217;s my sketch.<br />
<span id="more-31"></span><br />
The ATMega8 provides two pins (2 and 3) which can trigger software interrupts when the attached digital signal changes. You can use this to be &#8220;notified&#8221; when the external signal changes. Therefore, you do not need to poll the pin periodically &#8211; the interrupt routine will be invoked automatically when the specified signal change happens.</p>
<p>In my case, I want to be notified when the DCF77 signal changes, so I need a simple way to exchange the current value of the pin. Here is the sketch:</p>
<pre>
<code>
// Definition of interrupt names
#include < avr/io.h >
// ISR interrupt service routine
#include < avr/interrupt.h >
</code>
<code>
// LED connected to digital pin 13
int ledPin = 13;
// This is the INT0 Pin of the ATMega8
int sensePin = 2;
// We need to declare the data exchange
// variable to be volatile - the value is
// read from memory.
volatile int value = 0;
</code><code>
// Install the interrupt routine.
ISR(INT0_vect) {
  // check the value again - since it takes some time to
  // activate the interrupt routine, we get a clear signal.
  value = digitalRead(sensePin);
}
</code>
<code>
void setup() {
  Serial.begin(9600);
  Serial.println("Initializing ihandler");
  // sets the digital pin as output
  pinMode(ledPin, OUTPUT);
  // read from the sense pin
  pinMode(sensePin, INPUT);
  Serial.println("Processing initialization");
  // Global Enable INT0 interrupt
  GICR |= ( 1 < < INT0);
  // Signal change triggers interrupt
  MCUCR |= ( 1 << ISC00);
  MCUCR |= ( 0 << ISC01);
  Serial.println("Finished initialization");
}
</code></code><code>
void loop() {
  if (value) {
    Serial.println("Value high!");
    digitalWrite(ledPin, HIGH);
  } else {
    Serial.println("Value low!");
    digitalWrite(ledPin, LOW);
  }
  delay(100);
}
</code>
</pre>
<p>In the main loop, the value gets interpreted, but not read. This happens in the interrupt handler routine. The routine is installed with the avrgcc preprocessor macro &#8220;ISR&#8221; &#8211; this way, we do not need to fiddle with the interrupt vector directly. During the setup function, be careful to initialize the ATMega8 correctly &#8211; in this case, we need to enable the INT0 interrupt. With the MCUCR register, we can change the way the interrupt gets triggered &#8211; here, we receive an interrupt when the signal changes in both directions (HIGH -> LOW and LOW -> HIGH).</p>
<p>The wiring for the testing is rather simple: Use a breadboard and power it from the Arduino. Connect a pull-up resistor to positive and the other end to pin 2 of your Arduino. Connect an additional cable from the &#8220;other end&#8221; of the resistor to negative. If you pull the latter cable from negative, the current on pin 2 will go to HIGH, if you reconnect it to negative, it will go to LOW. You could also use a switch, of course ;-)</p>
<p>If you speak german, I highly recommend the &#8220;<a href="http://www.mikrocontroller.net/articles/AVR-GCC-Tutorial">AVR-GCC Tutorial</a>&#8221; of mikrocontroller.net.</p>
]]></content:encoded>
			<wfw:commentRss>http://gonium.net/md/2006/12/20/handling-external-interrupts-with-arduino/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>
