<?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; code</title>
	<atom:link href="http://gonium.net/md/tag/code/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>Leopard+Terminal+Spaces workaround</title>
		<link>http://gonium.net/md/2007/11/26/leopardterminalspaces-workaround/</link>
		<comments>http://gonium.net/md/2007/11/26/leopardterminalspaces-workaround/#comments</comments>
		<pubDate>Mon, 26 Nov 2007 13:25:45 +0000</pubDate>
		<dc:creator>md</dc:creator>
				<category><![CDATA[digital]]></category>
		<category><![CDATA[english]]></category>
		<category><![CDATA[nerdism]]></category>
		<category><![CDATA[applescript]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[leopard]]></category>
		<category><![CDATA[mac]]></category>
		<category><![CDATA[spaces]]></category>

		<guid isPermaLink="false">http://gonium.net/md/2007/11/26/leopardterminalspaces-workaround/</guid>
		<description><![CDATA[
Overall, I like the new Apple MacOS 10.5 aka Leopard. I used various virtual desktop solutions with older versions, but the new Spaces provides everything I need &#8211; almost. On a typical workday, I have various terminal windows opened &#8211; but not on one workspace! Spaces changes the screen to one that has a terminal [...]]]></description>
			<content:encoded><![CDATA[<p><img src='http://gonium.net/md/wp-content/uploads/2007/11/leopard.jpg' alt='leopard' /></p>
<p>Overall, I like the new Apple MacOS 10.5 aka Leopard. I used various virtual desktop solutions with older versions, but the new Spaces provides everything I need &#8211; almost. On a typical workday, I have various terminal windows opened &#8211; but not on one workspace! Spaces changes the screen to one that has a terminal currently running, which is a stupid behavior. There is also no way to change this. But a workaround&#8230;</p>
<p><span id="more-57"></span></p>
<p>If you have a terminal on space one and you&#8217;re on space three, clicking the terminal icon moves you to terminal one. What I would like to have is a new terminal on space three. You can do this with the following (trivial) applescript:</p>
<p><code><br />
tell application "Terminal"<br />
    do script ""<br />
end tell<br />
</code></p>
<p>So, basically the Terminal is asked to execute an empty script &#8211; which results in a new window <strong>without</strong> changing the current desktop.</p>
<p>Either you use the script editor to build your own applescript implementation, or you download a precompiled version below. I dropped the script into the dock for easy access. Now I have only two remaining questions:</p>
<ol>
<li>How can I change the icon of the applescript?</li>
<li>How can I assign a keyboard shortcut to it?</li>
</ol>
<p>Hopefully, Spaces allows you to deactivate the switching behavior in future revisions.</p>
<ul>
<li><a href='http://gonium.net/md/wp-content/uploads/2007/11/openterminal.zip' title='openterminal applescript'>openterminal applescript download</a></li>
</ul>
<p>The leopard pic was released under a CC-Attribution-No Derivative Works license by <a href="http://flickr.com/photos/pleeker/130658490/">Matt McGee</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://gonium.net/md/2007/11/26/leopardterminalspaces-workaround/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Tweaking the code</title>
		<link>http://gonium.net/md/2007/04/18/tweaking-the-code/</link>
		<comments>http://gonium.net/md/2007/04/18/tweaking-the-code/#comments</comments>
		<pubDate>Wed, 18 Apr 2007 14:54:47 +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[clock]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[dcf77]]></category>

		<guid isPermaLink="false">http://gonium.net/md/2007/04/18/tweaking-the-code/</guid>
		<description><![CDATA[


Thanks to Lasse Lambrecht, I can release a new version of the DCF77 code &#8211; now you can run it on the ATMega168-based Arduinos.



The ATMega8 differs slightly from the ATMega48/88/168-series: The latter chips have an extended Timer2-hardware and therefore need different initializations. Lasse send me a nice adjustment of the code, basically he uses preprocessor [...]]]></description>
			<content:encoded><![CDATA[<img src='http://gonium.net/md/wp-content/uploads/2007/04/tweaking.png' alt='tweaking.png' />
<br />

<p>Thanks to <a href="http://www.lobl.de">Lasse Lambrecht</a>, I can release a new version of the DCF77 code &#8211; now you can run it on the ATMega168-based Arduinos.</p>

<span id="more-55"></span>

The ATMega8 differs slightly from the ATMega48/88/168-series: The latter chips have an extended Timer2-hardware and therefore need different initializations. Lasse send me a nice adjustment of the code, basically he uses preprocessor flags to figure out which initialization commands to use:


#ifdef ATMEGA168
  TCCR2B |= (1< <CS22);    // turn on CS22 bit
  TCCR2B &#038;= ~((1<<CS21) | (1<<CS20));    // turn off CS21 and CS20 bits   
#else
  TCCR2 |= (1<<CS22);    // turn on CS22 bit
  TCCR2 &#038;= ~((1<<CS21) | (1<<CS20));    // turn off CS21 and CS20 bits   
#endif
  // Use normal mode
#ifdef ATMEGA168
  TCCR2A &#038;= ~((1<<WGM21) | (1<<WGM20));   // turn off WGM21 and WGM20 bits 
  TCCR2B &#038;= ~(1<<WGM22);                  // turn off WGM22
#else
  TCCR2 &#038;= ~((1<<WGM21) | (1<<WGM20));   // turn off WGM21 and WGM20 bits 
#endif
  // Use internal clock - external clock not used in Arduino
  ASSR |= (0<<AS2);
#ifdef ATMEGA168
  TIMSK2 |= (1<<TOIE2) | (0<<OCIE2A);        //Timer2 Overflow Interrupt Enable  
#else
  TIMSK |= (1<<TOIE2) | (0<<OCIE2);        //Timer2 Overflow Interrupt Enable  
#endif


As you can see, the avr-libc just defines other names, e.g. TCCR2A/B for the ATMega168 and TCCR2 for the ATMega8.

You can get the new code here
<ul>
	<li><a href="http://gonium.net/media/arduino-dcf77-clock-0.2.1.tar.bz">arduino-dcf77-clock-0.2.1.tar.bz</a></li>


For the hardware setup, please refer to my <a href="http://gonium.net/md/2006/11/05/arduino-dcf77-radio-clock-receiver/">initial post</a>.

]]></content:encoded>
			<wfw:commentRss>http://gonium.net/md/2007/04/18/tweaking-the-code/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<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>Arduino Timer Interrupt</title>
		<link>http://gonium.net/md/2006/12/23/arduino-timer-interrupt/</link>
		<comments>http://gonium.net/md/2006/12/23/arduino-timer-interrupt/#comments</comments>
		<pubDate>Sat, 23 Dec 2006 22:12:02 +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[microcontroller]]></category>

		<guid isPermaLink="false">http://gonium.net/md/2006/12/23/arduino-timer-interrupt/</guid>
		<description><![CDATA[Continuing my interrupt experiments, I wrote a little sketch to print the seconds since startup to serial. But: Something is wrong&#8230;

I use the Timer2 of the ATMega8. It consists of a 8 bit counter which is automatically increased. When an overflow occurs, the interrupt routine TIMER2_OVF_vect is called.
This is the code:


#include < avr / interrupt.h [...]]]></description>
			<content:encoded><![CDATA[<p>Continuing my interrupt experiments, I wrote a little sketch to print the seconds since startup to serial. But: Something is wrong&#8230;<br />
<span id="more-32"></span><br />
I use the Timer2 of the ATMega8. It consists of a 8 bit counter which is automatically increased. When an overflow occurs, the interrupt routine TIMER2_OVF_vect is called.</p>
<p>This is the code:</p>
<pre>
<code>
#include < avr / interrupt.h >
#include < avr / io.h >

#define INIT_TIMER_COUNT 0
#define RESET_TIMER2 TCNT2 = INIT_TIMER_COUNT

int ledPin = 13;
int int_counter = 0;
volatile int second = 0;
int oldSecond = 0;

// Aruino runs at 16 Mhz, so we have 61 Overflows per second...
// 1/ ((16000000 / 1024) / 256) = 1 / 61
ISR(TIMER2_OVF_vect) {
  int_counter += 1;
  if (int_counter == 61) {
    second+=1;
    int_counter = 0;
  }
};

void setup() {
  Serial.begin(9600);
  Serial.println("Initializing timerinterrupt");
  //Timer2 Settings:  Timer Prescaler /1024
  TCCR2 |= ((1 < < CS22) | (1 << CS21) | (1 << CS20));
  //Timer2 Overflow Interrupt Enable
  TIMSK |= (1 << TOIE2);
  RESET_TIMER2;
  sei();
}

void loop() {
  if (oldSecond != second) {
    Serial.print(second);
    Serial.println(".");
    oldSecond = second;
  }
}
</code>
</code></pre>
<p>Unfortunately, the counter is not increased every second but every three seconds. I need to investigate this. Anyway, it seems to be more reasonable to use the CTC mode (clear-timer-on-compare-match). I need to read the datasheet ;-)</p>
]]></content:encoded>
			<wfw:commentRss>http://gonium.net/md/2006/12/23/arduino-timer-interrupt/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
