<?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>Jee Labs &#187; AVR</title>
	<atom:link href="http://news.jeelabs.org/category/avr/feed/" rel="self" type="application/rss+xml" />
	<link>http://news.jeelabs.org</link>
	<description>Computing stuff tied to the physical world</description>
	<lastBuildDate>Thu, 09 Sep 2010 22:06:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Sleepy class</title>
		<link>http://news.jeelabs.org/2010/09/04/sleepy-class/</link>
		<comments>http://news.jeelabs.org/2010/09/04/sleepy-class/#comments</comments>
		<pubDate>Fri, 03 Sep 2010 22:01:08 +0000</pubDate>
		<dc:creator>jcw</dc:creator>
				<category><![CDATA[AVR]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[LowPower]]></category>
		<category><![CDATA[Techniques]]></category>

		<guid isPermaLink="false">http://news.jeelabs.org/?p=9256</guid>
		<description><![CDATA[To make it simpler to experiment with the very low-power states of the ATmega on the JeeNode, I&#8217;ve moved some code to the Ports library. It&#8217;s all wrapped up into a new &#8220;Sleepy&#8221; class: See the powerdown_demo.pde and radioBlip.pde sketches for examples of use. This class is documented in the Café: FWIW, I&#8217;m also evaluating [...]]]></description>
			<content:encoded><![CDATA[<p>To make it simpler to experiment with the very low-power states of the ATmega on the JeeNode, I&#8217;ve moved some code to the Ports library.</p>

<p>It&#8217;s all wrapped up into a new &#8220;Sleepy&#8221; class:</p>

<p><img src="http://files.jeelabs.org/2010/09/screen_shot_2010_09_03_at_140754.png" alt="Screen Shot 2010 09 03 at 14.07.54" /></p>

<p>See the <a href="http://code.jeelabs.org/libraries/Ports/examples/powerdown_demo/powerdown_demo.pde">powerdown_demo.pde</a> and <a href="http://code.jeelabs.org/libraries/RF12/examples/radioBlip/radioBlip.pde">radioBlip.pde</a> sketches for examples of use.</p>

<p>This class is documented in the <a href="http://cafe.jeelabs.net/man/class_sleepy/">Café</a>:</p>

<p><img src="http://files.jeelabs.org/2010/09/screen_shot_2010_09_03_at_142339.png" alt="Screen Shot 2010 09 03 at 14.23.39" /></p>

<p>FWIW, I&#8217;m also evaluating the <a href="http://www.redmine.org/">Redmine</a> system as a way to bring the code repository, Café docs, issue tracker, and Talk forums all into one context:</p>

<p><img src="http://files.jeelabs.org/2010/09/screen_shot_2010_09_03_at_142450.png" alt="Screen Shot 2010 09 03 at 14.24.50" /></p>

<p>That site is still experimental, so I&#8217;m not making it public yet. The one missing feature holding me back is that Redmine does not have a good spam prevention mechanism, such as Akismet. At least last time I looked. But all in all, this would be a great way to provide a place to describe projects, fill in the documentation, and track all code changes and bugs collaboratively. If you&#8217;d like to have a sneak preview, or want to have a place to describe your project, or perhaps would like to help with the fairly <em>gigantic</em> task of getting a good documentation section going, please email me.</p>

<p>I&#8217;ve started copying over some content, but it&#8217;s going to take a while before everything has been brought over and adjusted. Both the old and the new system use Markdown, but there are always them pesky little details&#8230;</p>

<p>Anyway, back to the topic at hand &#8211; enjoy the low-power options, and please consider sharing your explorations, findings, and inventions in the <a href="http://talk.jeelabs.net/">Talk</a> discussion forums.</p>
]]></content:encoded>
			<wfw:commentRss>http://news.jeelabs.org/2010/09/04/sleepy-class/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Sending packets without battery</title>
		<link>http://news.jeelabs.org/2010/09/02/sending-packets-without-battery/</link>
		<comments>http://news.jeelabs.org/2010/09/02/sending-packets-without-battery/#comments</comments>
		<pubDate>Wed, 01 Sep 2010 22:01:20 +0000</pubDate>
		<dc:creator>jcw</dc:creator>
				<category><![CDATA[AVR]]></category>
		<category><![CDATA[Hardware]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[JeeNode]]></category>
		<category><![CDATA[LowPower]]></category>

		<guid isPermaLink="false">http://news.jeelabs.org/?p=9211</guid>
		<description><![CDATA[Here&#8217;s a fun experiment&#8230; After yesterday&#8217;s improved power-down current results, I wanted to find out how much power it really takes to send out a bunch of small packets. No acks, just periodically sending out a packet and sleeping. Trouble is, I didn&#8217;t want to wait for a battery to run down, since that would [...]]]></description>
			<content:encoded><![CDATA[<p><em>Here&#8217;s a fun experiment&#8230;</em></p>

<p>After <a href="/2010/09/01/sleep/">yesterday&#8217;s</a> improved power-down current results, I wanted to find out how much power it <em>really</em> takes to send out a bunch of small packets. No acks, just periodically sending out a packet and sleeping.</p>

<p>Trouble is, I didn&#8217;t want to wait for a battery to run down, since that would take months or even years. <em>Bit long to get results for this weblog, eh?</em></p>

<p>So instead, I used this little chap:</p>

<p><img src="http://files.jeelabs.org/2010/08/dsc_1873.jpg" alt="Dsc 1873" /></p>

<p>It&#8217;s a super-capacitor which can handle up to 5.5V and has a capacity of 0.47 <em>Farad!</em> That&#8217;s like putting a thousand 470 µF caps in parallel. Amazing stuff, in an even more amazingly small package.</p>

<p>Here&#8217;s a <a href="http://cafe.jeelabs.net/hw/jeenode/">JeeNode</a>, fitted with this new power source, to give you an idea of just how small this thing is:</p>

<p><img src="http://files.jeelabs.org/2010/08/dsc_1874.jpg" alt="Dsc 1874" /></p>

<p>The next step was to design a small sketch to test this. Here&#8217;s what I came up with:</p>

<p><img src="http://files.jeelabs.org/2010/08/screen_shot_2010_08_29_at_115738.png" alt="Screen Shot 2010 08 29 at 11.57.38" /></p>

<p>Some of the code left out for brevity. Full source code can be found <a href="http://code.jeelabs.org/libraries/RF12/examples/radioBlip/radioBlip.pde">here</a>.</p>

<p>What this demo does is send out a packet with a 2-byte payload, then sleep for approximately 1 second, then send again, etc. Until power runs out.</p>

<p>Sure enough, packets started coming in every second:</p>

<pre><code>    OK 4 0 0
    OK 4 1 0
    OK 4 2 0
    OK 4 3 0
    [...]
</code></pre>

<p>I expected it to send out say 100 packets or so, before the charge in the 0.47 F supercap would run out.</p>

<p><em>Guess how far it went&#8230;</em></p>

<pre><code>    [...]
    OK 4 178 28
    OK 4 179 28
    OK 4 180 28
    OK 4 181 28
</code></pre>

<p>That&#8217;s packet number &#8230; clickety, clickety &#8230; 28 * 256 + 181 = <strong>7349</strong> !!!</p>

<p>In other words, the JeeNode was able to send out well over 7000 packets, i.e. two hours of packets sent once a second.</p>

<p>This is fantastic. I think the secret &#8211; apart from the 3 µA powerdown mode &#8211; is that the RFM12B + RF12 driver require very little time to start up, send off a packet, and go back to sleep again. There is no ACK involved, the RFM12B is hardly ever in reception mode.</p>

<p>With real-world use, I expect power requirements to be considerably higher. First of all, a <a href="http://cafe.jeelabs.net/hw/room_board/">Room Board</a> will draw around 50 µA, due to the on-board PIR sensor. And second, I&#8217;m going to want to use ACKs for at least the motion detector reports, so that the system has a robust mechanism for reporting motion whenever it is detected. This means putting the RFM12B in receive mode for a few milliseconds, waiting for the ACK. And repeating this process a few times if that ACK isn&#8217;t immediately received.</p>

<p><em>But still &#8230; over 7000 packets without a battery!</em></p>

<p><strong>Update</strong> &#8211; with one packet per 5 seconds, the charge lasted 4523 packets, i.e. just over 6:15.</p>

<p><strong>Update #2</strong> &#8211; with one packet per 60 seconds, 771 packets got sent out, that&#8217;s 12:51 hours &#8230; <em>looks like the self-discharge of the supercap is eating up the remaining energy.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://news.jeelabs.org/2010/09/02/sending-packets-without-battery/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Sleep!</title>
		<link>http://news.jeelabs.org/2010/09/01/sleep/</link>
		<comments>http://news.jeelabs.org/2010/09/01/sleep/#comments</comments>
		<pubDate>Tue, 31 Aug 2010 22:01:11 +0000</pubDate>
		<dc:creator>jcw</dc:creator>
				<category><![CDATA[AVR]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[JeeNode]]></category>
		<category><![CDATA[LowPower]]></category>
		<category><![CDATA[Techniques]]></category>

		<guid isPermaLink="false">http://news.jeelabs.org/?p=9192</guid>
		<description><![CDATA[The &#8220;powerdown_demo.pde&#8221; sketch in this recent post draws 20 µA, which surprised me a bit&#8230; A while back, I got it down to a fraction of that, by turning off the brown-out detector (BOD) through a fuse bit on the ATmega. That&#8217;s a little circuit which prevents the ATmega from coming out of reset and [...]]]></description>
			<content:encoded><![CDATA[<p>The &#8220;powerdown_demo.pde&#8221; sketch in this <a href="/2010/08/21/another-lipo-option/">recent post</a> draws 20 µA, which surprised me a bit&#8230;</p>

<p>A while back, I got it down to a <a href="/2009/05/16/power-consumption-more-savings/">fraction</a> of that, by turning off the <em>brown-out detector</em> (BOD) through a fuse bit on the ATmega. That&#8217;s a little circuit which prevents the ATmega from coming out of reset and doing anything if the voltage is too low.</p>

<p>As it turns out, you <em>can</em> turn off the BOD in software, but only for sleep mode. The reasoning being that there&#8217;s no point in protecting the processor from going berserk while it&#8217;s <em>not doing anything</em> in the first place&#8230;</p>

<p>Well, my code to turn off the BOD was wrong. You have to do this <em>right</em> before going to sleep. Here&#8217;s the updated <a href="http://code.jeelabs.org/libraries/Ports/examples/powerdown_demo/powerdown_demo.pde">powerdown_demo.pde</a> sketch:</p>

<p><img src="http://files.jeelabs.org/2010/08/screen_shot_2010_08_28_at_125018.png" alt="Screen Shot 2010 08 28 at 12.50.18" /></p>

<p><em>(correction &#8211; I mixed up my bits: change &#8220;PINB |= &#8230;&#8221; to &#8220;PINB = &#8230;&#8221; or &#8220;PORTD ^= &#8230;&#8221;)</em></p>

<p>The result?</p>

<p><img src="http://files.jeelabs.org/2010/08/dsc_1872.jpg" alt="Dsc 1872" /></p>

<p><em>Now we&#8217;re cookin&#8217; again!</em></p>
]]></content:encoded>
			<wfw:commentRss>http://news.jeelabs.org/2010/09/01/sleep/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Fractional bits?</title>
		<link>http://news.jeelabs.org/2010/08/28/fractional-bits/</link>
		<comments>http://news.jeelabs.org/2010/08/28/fractional-bits/#comments</comments>
		<pubDate>Fri, 27 Aug 2010 22:01:42 +0000</pubDate>
		<dc:creator>jcw</dc:creator>
				<category><![CDATA[AVR]]></category>
		<category><![CDATA[Hardware]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Techniques]]></category>

		<guid isPermaLink="false">http://news.jeelabs.org/?p=9139</guid>
		<description><![CDATA[To continue this little weblog series on bits, I&#8217;m going to go into bit fractions. Yeah, right&#8230; there is no such thing, of course &#8211; unless you&#8217;re into probabilities or fuzzy logic, perhaps. What I actually want to do, is describe &#8220;analog output&#8221; on the ATmega, using the Arduino library&#8217;s analogWrite() function. And throw in [...]]]></description>
			<content:encoded><![CDATA[<p>To continue this little weblog series on <a href="/2010/08/27/flippin’-bits/">bits</a>, I&#8217;m going to go into bit fractions.</p>

<p><em>Yeah, right&#8230; there is no such thing, of course &#8211; unless you&#8217;re into probabilities or fuzzy logic, perhaps.</em></p>

<p>What I actually want to do, is describe &#8220;analog output&#8221; on the ATmega, using the Arduino library&#8217;s <a href="http://www.arduino.cc/en/Reference/AnalogWrite">analogWrite()</a> function. And throw in some bit manipulations along the way, to stay somewhat on topic.</p>

<p>The little secret with analogWrite() is that it doesn&#8217;t do what its name suggests. The ATmega has no way of generating an analog signal, i.e. a voltage level between 0 and VCC.</p>

<p>Instead, a pulse is generated, with a varying duty cycle. I.e. the &#8220;on&#8221; and the &#8220;off&#8221; times of the pulse will be different, the ratio of these times being proprtional to the 0..255 value passed as argument to analogWrite(). With &#8220;0&#8243;, the signal will be 100% off, with &#8220;255&#8243; the signal will be 100% on. With &#8220;128&#8243; the pulse will be on the same amount of time as off. With &#8220;1&#8243;, it will be on <em>very</em> briefly, and then off most of the time, and so on&#8230;</p>

<p>The neat thing is that you connect it to an incandescent lamp, or a motor, then the effect will be that these will light/turn at less than their full power, due to the time it takes for these devices to try and follow the pulse. So the <em>effect</em> is similar to a fractional adjustment: you can dim / slow down these devices by using analogWrite().</p>

<p>It even works with LEDs, although these turn on and off very fast. In this case, the reason is that our eyes can&#8217;t follow such fast changes, and so we perceive the result as dimmed as well. A whole industry was once created around this &#8220;persistence of vision&#8221; property of our eyes &#8211; <em>it&#8217;s called TV&#8230;</em></p>

<p>Here&#8217;s a sketch which uses this pulsed output to control the brightness of a LED connected to DIO3 (i.e. D6):</p>

<p><img src="http://files.jeelabs.org/2010/08/screen_shot_2010_08_27_at_164438.png" alt="Screen Shot 2010 08 27 at 16.44.38" /></p>

<p>Note that I didn&#8217;t have to define pin 6 as an output, analogWrite() does that.</p>

<p>What the above does, is ramp up gradually from 0 to 255, and then repeat:</p>

<p><img src="http://files.jeelabs.org/2010/08/screen_shot_2010_08_27_at_164906.png" alt="Screen Shot 2010 08 27 at 16.49.06" /></p>

<p>Suppose we want it to fade in and out instead:</p>

<p><img src="http://files.jeelabs.org/2010/08/screen_shot_2010_08_27_at_164917.png" alt="Screen Shot 2010 08 27 at 16.49.17" /></p>

<p>Try implementing this yourself.</p>

<p>Note that you&#8217;re going to need at least 9 bits of information to do this: 8 for the brightness level and 1 to keep track of whether you&#8217;re currently in the up ramp or in the down ramp:</p>

<p>Here&#8217;s one way to do it, using some bit trickery:</p>

<p><img src="http://files.jeelabs.org/2010/08/screen_shot_2010_08_27_at_170515.png" alt="Screen Shot 2010 08 27 at 17.05.15" /></p>

<p>A few notes:</p>

<ul>
<li>I&#8217;ve changed the &#8220;level&#8221; variable from an 8-bit <em>byte</em> to a 16-bit <em>word</em></li>
<li>bit 8 toggles from 0 to 1 and back every 256 level counts</li>
<li>it&#8217;ll be 1 when level is 256..511, 768..1023, etc</li>
<li>when it&#8217;s 1, we flip the bits, i.e. 0 becomes 255, 1 becomes 254, etc</li>
<li>the analogWrite() function ignores all upper bits</li>
</ul>

<p>If you think <em>that</em> was an obscure call to analogWrite(), try this one:</p>

<pre><code>    analogWrite(6, level ^ -((level &gt;&gt; 8) &amp; 1));
</code></pre>

<p>Maybe you can decypher it when written slightly differently?</p>

<pre><code>    analogWrite(6, level ^ -bitRead(level, 8));
</code></pre>

<p>(hint: bitRead() always returns either 0 or 1)</p>

<p>It&#8217;s all pretty geeky stuff, and let&#8217;s hope you&#8217;ll never have to deal with code such as this again, but the point of this story is that there&#8217;s no magic. You just have to know what each operator does, and how to translate an integer from decimal to binary notation and back.</p>

<p>I&#8217;ll summarize my intuitive interpretation of bit operators below:</p>

<ul>
<li>&#8220;<strong>X | Y</strong>&#8221; = take X and copy all the 1&#8242;s of Y into it</li>
<li>&#8220;<strong>X &amp; Y</strong>&#8221; = take X and copy all the 0&#8242;s of Y into it</li>
<li>&#8220;<strong>X ^ Y</strong>&#8221; = take X and flip all the bits where Y has 1&#8242;s</li>
<li>&#8220;<strong>~ X</strong>&#8221; = flip all the bits of X</li>
<li>&#8220;<strong>- X</strong>&#8221; = arithmetic minus (same as &#8220;(~X) + 1&#8243; !)</li>
<li>&#8220;<strong>! X</strong>&#8221; = 1 if X is zero, 0 otherwise</li>
<li>&#8220;<strong>X &lt;&lt; N</strong>&#8221; = multiply X by 2, N times</li>
<li>&#8220;<strong>X >> N</strong>&#8221; = divide X by 2, N times</li>
</ul>

<p>Some tricks based on this:</p>

<ul>
<li>&#8220;<strong>~ 0</strong>&#8221; = all bits set to 1 (same as &#8220;-1&#8243; !)</li>
<li>&#8220;<strong>~ 0 &lt;&lt; N</strong>&#8221; = all bits 1, but N lowest bits set to 0</li>
<li>&#8220;<strong>bit(N) &#8211; 1</strong>&#8221; = a constant with N lowest bits set to 1</li>
<li>&#8220;<strong>X &amp; (bit(N) &#8211; 1)</strong>&#8221; = the N lowest bits of X, the rest is 0</li>
<li>&#8220;<strong>X &amp; ~ (bit(N) &#8211; 1)</strong>&#8221; = X, but with the N lowest bits set to 0</li>
<li>&#8220;<strong>!! X</strong>&#8221; = 0 if X is zero, 1 otherwise</li>
</ul>

<p>An useful rule when writing logical expressions is: <em>when in doubt, parenthesize!</em> &#8211; see <a href="http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B">C operator precedence</a>.</p>

<p>Sooo&#8230; use bit(), bitRead(), bitWrite(), bitSet(), and bitClear() wherever you can, since it usually makes the code easier to read. But there&#8217;s no need to get lost if you see ^&amp;|~!&#8217;s in your expression &#8211; just <em>slow down</em> and decode such expressions step by step!</p>
]]></content:encoded>
			<wfw:commentRss>http://news.jeelabs.org/2010/08/28/fractional-bits/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Flippin’ bits</title>
		<link>http://news.jeelabs.org/2010/08/27/flippin%e2%80%99-bits/</link>
		<comments>http://news.jeelabs.org/2010/08/27/flippin%e2%80%99-bits/#comments</comments>
		<pubDate>Thu, 26 Aug 2010 22:01:12 +0000</pubDate>
		<dc:creator>jcw</dc:creator>
				<category><![CDATA[AVR]]></category>
		<category><![CDATA[Hardware]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Techniques]]></category>

		<guid isPermaLink="false">http://news.jeelabs.org/?p=9123</guid>
		<description><![CDATA[After yesterday&#8217;s post about setting and clearing bits, let&#8217;s explore reversing bits, i.e. changing them from 0 to 1 and back. And let&#8217;s do it by blinking an LED attached to DIO of port 1 &#8211; i.e. Arduino digital pin 4: The &#8220;if (onOff = 0)&#8221; etc is the logic that toggles onOff between 0 [...]]]></description>
			<content:encoded><![CDATA[<p>After <a href="/2010/08/26/bit-manipulation/">yesterday&#8217;s post</a> about setting and clearing bits, let&#8217;s explore <em>reversing</em> bits, i.e. changing them from 0 to 1 and back. <em>And let&#8217;s do it by blinking an LED attached to DIO of port 1</em> &#8211; i.e. Arduino digital pin 4:</p>

<p><img src="http://files.jeelabs.org/2010/08/screen_shot_2010_08_26_at_103126.png" alt="Screen Shot 2010 08 26 at 10.31.26" /></p>

<p>The &#8220;if (onOff = 0)&#8221; etc is the logic that toggles onOff between 0 and 1 on each pass through the loop:</p>

<pre><code>    if (onOff == 0) onOff = 1; else onOff = 0;
</code></pre>

<p>But there are <em>lots of</em> ways to do the same thing, coded differently:</p>

<pre><code>    if (onOff == 0) onOff = 1; else onOff = 0;
    if (onOff == 0) onOff = bit(0); else onOff = 0;
    if (onOff == 0) bitSet(onOff, 0); else bitClear(onOff, 0);
    onOff = onOff ? 0 : 1;
    onOff = (~ onOff) &amp; 1;
    onOff = (onOff + 1) &amp; 1;
    onOff = ! onOff;
    onOff = 1 - onOff;
    onOff = onOff ^ 1;
    onOff ^= 1;
</code></pre>

<p>See if you can figure out all of these.</p>

<p>Take your pick. Those last two use C&#8217;s <em>XOR</em> operator. I tend to prefer shorter source code, so I&#8217;d use that last notation (note that the resulting <em>compiled</em> code is not necessarily shorter than the other examples).</p>

<p>Now suppose you have a byte value &#8220;X&#8221; and you want to flip the 4th bit in it, while not changing anything else. That&#8217;s a bit more work. It could be done like this, for example:</p>

<pre><code>    if (bitRead(X, 4) == 0) bitSet(X, 4); else bitClear(X, 4);
</code></pre>

<p>Or like either of these:</p>

<pre><code>    X = X ^ bit(4);
    X ^= bit(4);
</code></pre>

<p>This shows clearly that the &#8220;^&#8221; XOR operator does exactly what we need: flip bits.</p>

<p>Back to blinking an actual LED, as done with the above sketch. Here&#8217;s a little mind bender &#8211; another sketch, <em>doing the same</em> using raw ports and the XOR operator:</p>

<p><img src="http://files.jeelabs.org/2010/08/screen_shot_2010_08_26_at_105810.png" alt="Screen Shot 2010 08 26 at 10.58.10" /></p>

<p>The first example was doing things &#8220;the Arduino way&#8221;, using pinMode() and digitalWrite(). It compiles to 890 bytes of code. This second example goes straight to the hardware and uses 554 bytes of code:</p>

<ul>
<li>Arduino digital pin 4 is bit 4 on the &#8220;D port&#8221; of an ATmega</li>
<li>&#8220;DDRD&#8221; is the &#8220;Data Direction Register&#8221;, where we set up pin 4 as an output</li>
<li>&#8220;PORTD&#8221; is the out &#8220;Port Register&#8221;, which controls the actual output signal</li>
</ul>

<p>You can see the XOR in action in that last example. It takes all the output bits of port D (Arduino pins 0 .. 7), and flips just a single bit, i.e. bit 4.</p>

<p>Just for kicks, I&#8217;ll show you one more way to blink the LED:</p>

<p><img src="http://files.jeelabs.org/2010/08/screen_shot_2010_08_26_at_110344.png" alt="Screen Shot 2010 08 26 at 11.03.44" /></p>

<p>This uses a relatively little-known feature of the hardware, which actually has &#8220;bit flipping&#8221; built-in. The &#8220;PIND&#8221; register is normally used for input, i.e. for reading the state of a pin as an input signal. But you can also write to that register. When you do, it will be used to <em>flip</em> output pins, <em>but only for the bits which were set to 1</em>. It&#8217;s essentially a built-in XOR.</p>

<p>That last example uses 550 bytes of code, most of which is overhead from the Arduino run-time library (setting up the milliseconds timer, etc). So what&#8217;s in a measly 4 bytes, right? Wrong. There is a minute, but sometimes important difference: the other approaches all had to read the register value first, flip the bit, and then write the value back. This last version only writes a (constant) value to a register. With interrupts, that can be <em>very</em> important: this last version can&#8217;t ever go wrong, it will always flip the requested bit. The other version <em>could</em> have an interrupt occur between the read and the write. It&#8217;s a <a href="http://code.google.com/p/arduino/issues/detail?id=146">known issue</a> for the Arduino Mega. It can lead to code which runs for a week, and then fails mysteriously. Bugs like these are fiendishly hard to properly diagnose.</p>

<p>Bit-flipping can be quite useful for physical computing. Not only does it let you easily toggle specific bits, and change the state of some output pins, it can also be a way to <em>clear</em> a bit. Let&#8217;s say you need to generate a (very) quick pulse. Here are four ways to accomplish the same thing:</p>

<pre><code>    bitSet(PORTD, 4); bitClear(PORTD, 4);
    PORTD |= bit(4); PORTD ^= bit(4);
    PORTD |= bit(4); PIND = bit(4);
    PIND = bit(4); PIND = bit(4);
</code></pre>

<p>That second one based on XOR works, because bit 4 is <em>known</em> to be one, so setting it to zero is always the same as flipping it. That&#8217;s also why the third PORTD/PIND example works, with PIND doing the XOR in hardware. Lastly, the fourth approach will only work if bit 4 was initially zero. It&#8217;s the fastest one, and does not suffer from the interrupt race condition mentioned above.</p>

<p><em>Ok, that&#8217;s enough flippin&#8217; for one day!</em></p>

<p>Tomorrow, I&#8217;m going to go into, ehm&#8230; &#8220;fractional bits&#8221; (haha!) ;)</p>

<p><strong>Update</strong> &#8211; see comment below on why &#8220;bitSet(PORTD, 4); bitClear(PORTD, 4);&#8221; are also interrupt-safe (mostly &#8211; but not on <em>every</em> pin of an Arduino Mega!).</p>
]]></content:encoded>
			<wfw:commentRss>http://news.jeelabs.org/2010/08/27/flippin%e2%80%99-bits/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Bit manipulation</title>
		<link>http://news.jeelabs.org/2010/08/26/bit-manipulation/</link>
		<comments>http://news.jeelabs.org/2010/08/26/bit-manipulation/#comments</comments>
		<pubDate>Wed, 25 Aug 2010 22:01:13 +0000</pubDate>
		<dc:creator>jcw</dc:creator>
				<category><![CDATA[AVR]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Techniques]]></category>

		<guid isPermaLink="false">http://news.jeelabs.org/?p=9096</guid>
		<description><![CDATA[Today I&#8217;d like to go into bit manipulation, ehm, a bit&#8230; You need bit manipulation when you&#8217;re dealing with the individual bits in a byte, such as on the I/O ports of an ATmega, for example. First the easy approach &#8211; use these predefined macros from the Arduino library: bit(N) returns an integer with the [...]]]></description>
			<content:encoded><![CDATA[<p>Today I&#8217;d like to go into bit manipulation, ehm, <em>a bit</em>&#8230;</p>

<p>You need bit manipulation when you&#8217;re dealing with the individual bits in a byte, such as on the I/O ports of an ATmega, for example.</p>

<p>First the <em>easy</em> approach &#8211; use these predefined macros from the <a href="http://arduino.cc/en/Reference/Bit">Arduino library</a>:</p>

<ul>
<li><strong>bit(N)</strong> returns an integer with the N&#8217;th bit set to 1</li>
<li><strong>bitRead(X,N)</strong> &#8211; returns the N-th bit of X as 0 or 1</li>
<li><strong>bitWrite(X,N,B)</strong> &#8211; sets Nth bit of X to B (0 or 1)</li>
<li><strong>bitSet(X,N)</strong> &#8211; sets the Nth bit of X to 1</li>
<li><strong>bitClear(X,N)</strong> &#8211; sets the Nth bit of X to 0</li>
</ul>

<p>This is why you might see code such as the following:</p>

<pre><code>    bitSet(WDTCSR, WDIE);
</code></pre>

<p>This means: &#8220;set the Watchdog Interrupt Enable to 1 in the Watchdog Timer Control Register&#8221;. The WDTCSR and WDIE terms are predefined constants. WDIE is 6, for example.</p>

<p>Note that some of these routines can be written in terms of the others, i.e.</p>

<ul>
<li>bitSet(X,N) <em>is the same as</em> bitWrite(X,N,1)</li>
<li>bitClear(X,N) <em>is the same as</em> bitWrite(X,N,0)</li>
</ul>

<p><em>But what does it all mean?</em></p>

<p>Well, let&#8217;s dive in. First make sure that you are comfortable with &#8220;bit shifting&#8221;. The expression &#8220;bit(3)&#8221; is the same as &#8220;1 &lt;&lt; 3&#8243;, which in turn is the same as doubling the value 1 three times, i.e. the value <em>eight</em>. So &#8220;bit(0)&#8221; is 1 doubled zero times (i.e. 1) and &#8220;bit(7)&#8221; is 1 doubled 7 times, i.e. 128. Bytes have 8 bits numbered 0 to 7, so all you need for (byte-sized) hardware registers is to remember that bits 0..7 map to (specific!) integers with values 1 to 128.</p>

<p>Setting a bit, is like <em>OR-ing</em> the bit with the rest of the value. The following statements are all identical:</p>

<pre><code>    WDTCSR = WDTCSR | bit(WDIE);
    WDTCSR = WDTCSR | (1 &lt;&lt; WDIE);
    WDTCSR = WDTCSR | (1 &lt;&lt; 6);
    WDTCSR = WDTCSR | 0b1000000;
    WDTCSR = WDTCSR | 0x40;
    WDTCSR = WDTCSR | 64;
</code></pre>

<p>This, in turn, can be abbreviated in C as:</p>

<pre><code>    WDTCSR |= bit(WDIE);
    WDTCSR |= (1 &lt;&lt; WDIE);
    WDTCSR |= (1 &lt;&lt; 6);
    etc...
</code></pre>

<p>Or you could write:</p>

<pre><code>    bitSet(WDTCSR, WDIE);
</code></pre>

<p>It&#8217;s all the same. So <em>OR-ing</em> is about <em>setting</em> bits (to 1).</p>

<p>Likewise, <em>AND-ing</em> is about <em>not clearing</em> bits (to 0). Whoa, that&#8217;s confusing. This expression returns a value which is what X was, but <em>only</em> for bit N:</p>

<pre><code>    X &amp; bit(N);
</code></pre>

<p>So this will change X to a value with all bits <em>except</em> bit N set to zero:</p>

<pre><code>    X = X &amp; bit(N);
</code></pre>

<p>To put it differently: X will lose its original bits, except bit N, which will be left alone. All the bits are set to zero, <em>except</em> bit N.</p>

<p>Usually, you want the opposite, setting <em>only</em> bit N to zero. That too is accomplished with <em>AND-ing</em>, but you have to flip all the 0&#8242;s to 1 and all the 1&#8242;s to 0 first. Hang in there, it&#8217;s a slightly longer story. This sets bit N to zero:</p>

<pre><code>    X = X &amp; ~ bit(N);
</code></pre>

<p>Let&#8217;s examine what&#8217;s going on here. First, &#8220;bit(N)&#8221; is a value with <em>only</em> the Nth bit set. Now, &#8220;~ bit(N)&#8221; is a value with all the bits flipped around (&#8220;~&#8221; is called the <em>complement</em> operator in C), so that&#8217;s a value with all <em>but</em> the Nth bit set. Everything is 1, but bit N is 0.</p>

<p>Now we can tackle the expression &#8220;X &amp; ~ bit(N)&#8221;. Since <em>AND-ing</em> is about &#8220;not clearing bits&#8221;, that means that the result of this expression is all the bits of X unchanged where &#8220;~ bit(N)&#8221; was one, which is almost everywhere. The only bit that differs is bit N &#8211; it is zero in &#8220;~ bit(N)&#8221;, therefore that particular bit will &#8220;not not clear &#8230;&#8221; (a double negation!): it <em>will</em> be cleared (to 0) in the result.</p>

<p>Finally, we replace X by that result. So X will change in precisely one bit: bit N. That bit will be cleared to zero, the rest is not affected. In short: we&#8217;ve cleared bit N.</p>

<p><em>Confused?</em></p>

<p>Well, that&#8217;s why the bit/bitSet/etc macro definitions were introduced. These expressions are all identical:</p>

<pre><code>    X = X &amp; ~ bit(N);
    X = X &amp; ~ (1 &lt;&lt; N);
    if (X &amp; bit(N)) X = X - bit(N);
    bitClear(X, N);
</code></pre>

<p>That last one is clearest by far, because it conveys the actual operation with a well-chosen name: clear bit N, leave the rest alone.</p>

<p><em>So why would anyone ever choose to use anything but the bit/bitRead/etc routines?</em></p>

<p>Many reasons. Habit, perhaps. Coming from another environment which doesn&#8217;t have these macros. Being so used to this bit-manipulation that the use of words doesn&#8217;t really look any clearer. Whatever&#8230;</p>

<p>But another more important reason is that you can&#8217;t do everything with these bit/bitSet/bitClear routines. Sometimes you just have to go to the raw code. Such as when you need to set multiple bits at once, or flip bits. That&#8217;s why the ATmega datasheet has examples like these:</p>

<pre><code>    WDTCSR |= (1&lt;&lt;WDCE) | (1&lt;&lt;WDE);
</code></pre>

<p>By now, you should be able to decode such a statement. It&#8217;s the same as:</p>

<pre><code>    WDTCSR |= bit(WDCE) | bit(WDE);
</code></pre>

<p>In other words:</p>

<pre><code>    WDTCSR = WDTCSR | bit(WDCE) | bit(WDE);
</code></pre>

<p>Which in turn is <em>almost</em> the same as these two statements together:</p>

<pre><code>    WDTCSR |= bit(WDCE);
    WDTCSR |= bit(WDE);
</code></pre>

<p>That in turn, can be written as these two lines:</p>

<pre><code>    bitSet(WDTCSR, WDCE);
    bitSet(WDTCSR, WDE);
</code></pre>

<p>Much clearer, right? Except&#8230; <em>it&#8217;s not 100% identical!</em></p>

<p>The problem is a hardware issue: timing. The above two statements will set both bits, but <em>not at the same time!</em> For hardware register settings, that difference can be important. There is a fraction of a microsecond between the WDCE bit being set and the WDE bit being set. Unfortunately, in some cases that causes real problems &#8211; your code won&#8217;t work as expected.</p>

<p>Tomorrow, I&#8217;ll continue on this topic, but it&#8217;ll be a bit more fun, because there will be <em>LEDs</em> involved!</p>

<p><img src="http://files.jeelabs.org/2010/08/dsc_1385_2.jpg" alt="Dsc 1385 2" /></p>

<p><em>(Please ignore the cable on the left, I snatched the above picture from <a href="http://news.jeelabs.org/2010/05/06/low-level-development/">this post</a>)</em></p>

<p><strong>Update</strong> &#8211; see this excellent <a href="http://en.wikipedia.org/wiki/Bitwise_operation">Wikipedia article</a> for more details about bitwise operations.</p>
]]></content:encoded>
			<wfw:commentRss>http://news.jeelabs.org/2010/08/26/bit-manipulation/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Simplified button interface</title>
		<link>http://news.jeelabs.org/2010/08/24/simplified-button-interface/</link>
		<comments>http://news.jeelabs.org/2010/08/24/simplified-button-interface/#comments</comments>
		<pubDate>Mon, 23 Aug 2010 22:01:01 +0000</pubDate>
		<dc:creator>jcw</dc:creator>
				<category><![CDATA[AVR]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[JeePlug]]></category>
		<category><![CDATA[Techniques]]></category>

		<guid isPermaLink="false">http://news.jeelabs.org/?p=9083</guid>
		<description><![CDATA[The Blink Plug has two pushbuttons and two LEDs. The buttons are simple miniature switches, but nothing is ever simple in microcontroller-land: reading out the state of a pushbutton reliably can be deceptively hard, due to mechanical bounce issues. The Ports library has had a BlinkPlug class for some time now, including a &#8220;pushed()&#8221; function [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://cafe.jeelabs.net/hw/blink_plug/">Blink Plug</a> has two pushbuttons and two LEDs. The buttons are simple miniature switches, but nothing is ever simple in microcontroller-land: reading out the state of a pushbutton <em>reliably</em> can be deceptively hard, due to mechanical bounce issues.</p>

<p>The <a href="http://cafe.jeelabs.net/sw/library_-_ports/">Ports library</a> has had a <em>BlinkPlug</em> class for some time now, including a &#8220;pushed()&#8221; function to do all the debouncing. Unfortunately, that function turned out to be a bit harder to use than I originally intended.</p>

<p><em>Time to add some more code to the BlinkPlug class!</em></p>

<p>I&#8217;ve added a new &#8220;buttonCheck()&#8221; member, which returns <em>events</em> instead of <em>state</em>. That makes it a lot easier to detect when people <em>press</em> a button, which is usually all you&#8217;re after anyway.</p>

<p>Here&#8217;s a new <a href="http://code.jeelabs.org/libraries/Ports/examples/button_demo/button_demo.pde">button_demo.pde</a> example sketch, which illustrates the new functionality:</p>

<p><img src="http://files.jeelabs.org/2010/08/screen_shot_2010_08_23_at_174421.png" alt="Screen Shot 2010 08 23 at 17.44.21" /></p>

<p>Sample output:</p>

<p><img src="http://files.jeelabs.org/2010/08/screen_shot_2010_08_23_at_174439.png" alt="Screen Shot 2010 08 23 at 17.44.39" /></p>

<p>As you can see, it&#8217;s now a lot simpler to detect when people press or release one of the two buttons on a Blink Plug. Each time you call buttonCheck(), you&#8217;ll get one of the following events:</p>

<pre><code>ALL_OFF, ON1, OFF1, ON2, OFF2, SOME_ON.
</code></pre>

<p>You have to keep calling &#8220;buttonCheck()&#8221; reasonably often, at least 10 times per second, if you don&#8217;t want to miss any events. Calling it all the time in the main loop is fine. Keep in mind that ON1, etc. will be returned only <em>once</em> for each actual button press.</p>

<p>You can still call &#8220;state()&#8221; whenever you want, to check the position of either button. But when you use buttonCheck(), you should <em>not</em> call the old &#8211; <em>now deprecated</em> &#8211; &#8220;pushed()&#8221; function, as these two will interfere with each other.</p>

<p>This code is now part of the Ports library (subversion and ZIP). Gory details are in <a href="http://code.jeelabs.org/libraries/Ports/Ports.cpp">Ports.cpp</a>, near line 230.</p>
]]></content:encoded>
			<wfw:commentRss>http://news.jeelabs.org/2010/08/24/simplified-button-interface/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Assembling the JeeSMD, part 2</title>
		<link>http://news.jeelabs.org/2010/07/09/assembling-the-jeesmd-part-2/</link>
		<comments>http://news.jeelabs.org/2010/07/09/assembling-the-jeesmd-part-2/#comments</comments>
		<pubDate>Thu, 08 Jul 2010 22:01:51 +0000</pubDate>
		<dc:creator>jcw</dc:creator>
				<category><![CDATA[AVR]]></category>
		<category><![CDATA[Hardware]]></category>
		<category><![CDATA[ISP]]></category>
		<category><![CDATA[JeeSMD]]></category>
		<category><![CDATA[Techniques]]></category>

		<guid isPermaLink="false">http://news.jeelabs.org/?p=8887</guid>
		<description><![CDATA[Yesterday&#8217;s post was about assembling all the SMD components of the JeeSMD kit. The last step is to program a sketch into the ATmega. This isn&#8217;t as straightforwards as with a JeeNode, because there&#8217;s no on-board FTDI or USB serial port hookup. It&#8217;s fairly easy to create an FTDI connection, but even if you do, [...]]]></description>
			<content:encoded><![CDATA[<p>Yesterday&#8217;s <a href="/2010/07/08/assembling-the-jeesmd/">post</a> was about assembling all the SMD components of the JeeSMD kit.</p>

<p>The last step is to program a sketch into the ATmega. This isn&#8217;t as straightforwards as with a JeeNode, because there&#8217;s no on-board FTDI or USB serial port hookup.</p>

<p>It&#8217;s fairly easy to create an FTDI connection, but even if you do, you&#8217;ll still need an ISP programmer to install a boot loader (see this <a href="/2010/07/04/uploading-isp-ftdi-huh/">recent post</a> for some background).</p>

<p>So let&#8217;s hook up an ISP programmer first:</p>

<p><img src="http://files.jeelabs.org/2010/07/dsc_1787.jpg" alt="Dsc 1787" /></p>

<p>I&#8217;m using a somewhat weird setup: first of all, my cable connector was attached the wrong way around, so I always have to use this one in that weird <em>folded-over</em> position.</p>

<p>But a more important issue is that the ISP connection needs to use pins 1..6 of the 2&#215;4-pin SPI/ISP connector on the JeeSMD. That doesn&#8217;t work with normal flat cable connectors, which assume 2&#215;3 pins and are too wide to fit in a 2&#215;4-pin header. My solution is to insert wire-wrap pins the wrong way around into the cable header. This effectively <em>extends</em> the connector, but now it won&#8217;t be as wide and it&#8217;ll fit just fine. Another solution would be to only solder 2&#215;3 pins in the SPI/ISP position &#8211; you can always add two more pins later.</p>

<p>Once you&#8217;ve passed that hurdle, you can use any ISP programmer you like. There have been several posts about this on the weblog, as listed <a href="/tag/isp/">here</a>.</p>

<p>Now, <em>if</em> you want to use FTDI, then presumably you just uploaded a bootloader into the ATmega, with all the proper fuse settings, etc. The next step then, is to somehow connect to a 6-pin FDTI header.</p>

<p>There are several ways to do this. The one I use nowadays, is through a <a href="http://cafe.jeelabs.net/hw/carrier_board/">Carrier Board</a>, which includes the 6-pin FTDI connector:</p>

<p><img src="http://files.jeelabs.org/2010/07/dsc_1786.jpg" alt="Dsc 1786" /></p>

<p>The point about the FTDI connector, is that it&#8217;s <em>almost</em> trivial. All you need is 4 wires to GND, PWR, TX, and RX &#8211; <em>plus</em> a way to reset the board from the RTS signal. The clever way to generate a reset is to insert a 0.1 µF capacitor <em>between</em> the serial side RTS and the ATmega&#8217;s reset pin. Tiny trick, huge implications (does the name &#8220;Arduino&#8221; ring a bell?).</p>

<p>So how does the Carrier Board implement FTDI? Easy: it adds the capacitor. And you can easily do that yourself without a Carrier Board. Here&#8217;s how:</p>

<p><img src="http://files.jeelabs.org/2010/07/screen_shot_2010_07_08_at_232052.png" alt="Screen Shot 2010 07 08 at 23.20.52" /></p>

<p>Note that what FTDI calls &#8220;RX&#8221; is connected to what the ATmega calls &#8220;TXD&#8221;, and vice versa. <em>It&#8217;s all a matter of perspective&#8230;</em> Once you have the FTDI connection set up, you can upload sketches using the Arduino IDE just as with any other board. All you need is a <a href="http://www.moderndevice.com/products/usb-bub">USB-BUB</a> or some other equivalent USB-to-FTDI interface.</p>

<p><em>Congratulations: that&#8217;s all it takes to build and use the Arduino-compatible JeeSMD!</em></p>
]]></content:encoded>
			<wfw:commentRss>http://news.jeelabs.org/2010/07/09/assembling-the-jeesmd-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Assembling the JeeSMD</title>
		<link>http://news.jeelabs.org/2010/07/08/assembling-the-jeesmd/</link>
		<comments>http://news.jeelabs.org/2010/07/08/assembling-the-jeesmd/#comments</comments>
		<pubDate>Wed, 07 Jul 2010 22:01:27 +0000</pubDate>
		<dc:creator>jcw</dc:creator>
				<category><![CDATA[AVR]]></category>
		<category><![CDATA[Hardware]]></category>
		<category><![CDATA[JeeSMD]]></category>
		<category><![CDATA[Techniques]]></category>

		<guid isPermaLink="false">http://news.jeelabs.org/?p=8850</guid>
		<description><![CDATA[The JeeSMD is a kit with tiny &#8220;Surface Mounted Device&#8221; (SMD) components. SMD was designed for automated assembly with Pick &#38; Place machines, but with a bit of patience it&#8217;s fairly easy to assemble a board by hand &#8211; see this post for an overview of what you will need. You won&#8217;t be able to [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://cafe.jeelabs.net/hw/jeesmd/">JeeSMD</a> is a kit with tiny &#8220;Surface Mounted Device&#8221; (SMD) components. SMD was designed for automated assembly with Pick &amp; Place machines, but with a bit of patience it&#8217;s fairly easy to assemble a board by hand &#8211; see this <a href="/2010/06/08/smd-hand-soldering-tools/">post</a> for an overview of what you will need.</p>

<p>You won&#8217;t be able to do this without at least a fine-tipped (0.4..0.6mm) soldering iron plus the following tools:</p>

<p><img src="http://files.jeelabs.org/2010/07/dsc_17731.jpg" alt="Dsc 1773" /></p>

<p>A magnifier lamp also helps, I <em>know</em> I couldn&#8217;t do this without one anymore!</p>

<p>This is a step-by-step guide on how to assemble the SMD kit, which consists of these parts:</p>

<p><img src="http://files.jeelabs.org/2010/07/dsc_17721.jpg" alt="Dsc 1772" /></p>

<p>The tiny ones (don&#8217;t sneeze!) are hardest to tell apart:</p>

<p><img src="http://files.jeelabs.org/2010/07/jee_smd_closeup.png" alt="Jee smd Closeup" /></p>

<p><em>(thanks to Steve E for the macro shot &#8211; I just added some labels)</em></p>

<p>There are 2 10 kΩ resistors in there, although you only need one. That lets you get started without having to worry too much &#8211; if you mess up completely, just remove it and start over with the other one.</p>

<p>For a fantastic resource with detailed videos about hand-soldering SMD, see this <a href="http://www.curiousinventor.com/guides/Surface_Mount_Soldering">Curious Inventor page</a>.</p>

<p>So let&#8217;s get started. First thing to do is to apply flux wherever you&#8217;re going to solder things. The flux is <em>essential</em> because the flux in your solder wire will have evaporated longe before you can move your tip from the wire to the part.</p>

<p>I&#8217;m right-handed, so that&#8217;s how I hold my soldering iron. That leaves only my left hand for the tweezers &#8211; and no hand at all for the soldering wire:</p>

<p><img src="http://files.jeelabs.org/2010/07/dsc_1774.jpg" alt="Dsc 1774" /></p>

<p>Use your tweezers for all these parts, and don&#8217;t let go &#8211; once a 0603 part flies off or drops on the floor, your chances of finding it again are next to zero. Best is to work on a clean flat surface with <em>everything</em> around you removed.</p>

<p>The trick is to place the part and then push it down while you touch it with your iron with solder already on it. The moment a part is soldered down on at least one pin, it becomes a lot easier:</p>

<p><img src="http://files.jeelabs.org/2010/07/dsc_1775.jpg" alt="Dsc 1775" /></p>

<p>The matte &#8220;stain&#8221; you see around these pads is the flux, which has dried up but is still active.</p>

<p><em>(Remainder <a href="/2010/07/08/assembling-the-jeesmd/">continued</a> after the break&#8230;)</em></p>

<p><span id="more-8850"></span>
Ok, apply solder and add the other three 0603-sized caps. Don&#8217;t forget to add solder to both sides, of course:</p>

<p><img src="http://files.jeelabs.org/2010/07/dsc_1776.jpg" alt="Dsc 1776" /></p>

<p><em>It&#8217;s not hard, it just takes some patience.</em></p>

<p>Try to get better at it, and don&#8217;t touch the parts longer than necessary. These ones will take a lot of beating, but the next ones are less happy with too much heat.</p>

<p>Next is the voltage regulator, it&#8217;s in a 3-pin SOT-23 package. Again, once the first pin is soldered on, the rest is relatively easy:</p>

<p><img src="http://files.jeelabs.org/2010/07/dsc_1777.jpg" alt="Dsc 1777" /></p>

<p>Now a tricky one: the 16 MHz resonator. This is a three-pad package. I tend to put it slightly off-center, and always start with the middle pad:</p>

<p><img src="http://files.jeelabs.org/2010/07/dsc_1778.jpg" alt="Dsc 1778" /></p>

<p>Make sure it&#8217;s properly aligned, and make sure it&#8217;s still flush against the PCB. Otherwise, re-heat from the side until everything is right. Be patient &#8211; you can fix it with just one pad soldered, you can&#8217;t fix it later.</p>

<p>I use the middle pad, because it&#8217;s easy to check whether the connection is good: just try to lift the part with the tweezers. If well-attached, the board will lift as well.</p>

<p>Now carefully apply a <em>little</em> solder to the other two pads. The top is a metal can, you don&#8217;t want any of the solder joints to connect to that:</p>

<p><img src="http://files.jeelabs.org/2010/07/dsc_1779.jpg" alt="Dsc 1779" /></p>

<p>Now comes the <em>pièce de résistance</em> &#8211; the ATmega328 as a 32-TQFP package. Start by soldering <em>only</em> 2 pins on opposite sides of the chip:</p>

<p><img src="http://files.jeelabs.org/2010/07/dsc_1780.jpg" alt="Dsc 1780" /></p>

<p><em>Whoops &#8211; I used too much solder and created a bridge. Help!</em></p>

<p>No problem. Ignore it. This happens all the time. Just make sure the chip is well-aligned on the pads, and don&#8217;t proceed before it is. Fix the problem now, because there will be no way to do so later. Once you&#8217;re satisfied, do all the remaining pins:</p>

<p><img src="http://files.jeelabs.org/2010/07/dsc_1781.jpg" alt="Dsc 1781" /></p>

<p>Yep, two more solder bridges. Again, no problem.</p>

<p>Once all the pins have been soldered, make sure <em>each one</em> of them really has a solder joint to the pad. If not: add flux, and touch the pin with a drop of solder again. All that matters here is that every single pin is connected to its pad.</p>

<p>Now we&#8217;re going to fix the solder bridges. That&#8217;s where the solder wick comes in. Solder wick is nothing but thin copper braid, which clings to solder like crazy once hot enough. So the idea is to put a <em>fresh &amp; unused</em> bit of wick on top of the pins with too much solder, and then heat the whole sandwich with the iron. At one point, the wick wil start to soak itself with solder, removing it from the pins and pads below.</p>

<p>This process will make the pads look completely different, almost as if there is no longer any solder, even:</p>

<p><img src="http://files.jeelabs.org/2010/07/dsc_1782.jpg" alt="Dsc 1782" /></p>

<p>Not to worry. If the ATmega pins are flat and flush against the pads, then there wail always remain a bit of solder between them (due to capillary forces) &#8211; exactly where it matters.</p>

<p>Every time you use solder wick, you have to cut off the bit soaked in solder, to have a new piece ready for the next repair. Note that copper is a very good conductor &#8211; also of heat &#8211; so the wick can get very hot, everywhere.</p>

<p>Ok, if you&#8217;ve got this far: you&#8217;re done! Congratulations. Your board will now look something like this:</p>

<p><img src="http://files.jeelabs.org/2010/07/dsc_1783.jpg" alt="Dsc 1783" /></p>

<p><em>Tomorrow, the easy final part: adding headers, programming it, and trying it out.</em></p>

<p>(Reminder: the Jee Labs <a href="http://shop.jeelabs.com">shop</a> will be closed starting July 14th. All orders before today will be sent out before then &#8211; <em>I hope!</em> &#8211; the rest as well, but I <em>might</em> run out of stock on certain items. Please get in touch to check if you don&#8217;t want to risk having to wait until the end of August)</p>
]]></content:encoded>
			<wfw:commentRss>http://news.jeelabs.org/2010/07/08/assembling-the-jeesmd/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Uploading? ISP? FTDI? Huh?</title>
		<link>http://news.jeelabs.org/2010/07/04/uploading-isp-ftdi-huh/</link>
		<comments>http://news.jeelabs.org/2010/07/04/uploading-isp-ftdi-huh/#comments</comments>
		<pubDate>Sat, 03 Jul 2010 22:01:08 +0000</pubDate>
		<dc:creator>jcw</dc:creator>
				<category><![CDATA[AVR]]></category>
		<category><![CDATA[Hardware]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Arduino]]></category>
		<category><![CDATA[ISP]]></category>
		<category><![CDATA[JeeNode]]></category>

		<guid isPermaLink="false">http://news.jeelabs.org/?p=8686</guid>
		<description><![CDATA[There seems to be a fair bit of confusion in- and outside the Arduino world, and it&#8217;s spilling over to JeeNodes &#8230; I&#8217;d like to go through some terms and buzzwords to try and clarify how to get your Arduino or JeeNode to do the thing you want it to do. I&#8217;m going to assume [...]]]></description>
			<content:encoded><![CDATA[<p><em>There seems to be a fair bit of confusion in- and outside the Arduino world, and it&#8217;s spilling over to JeeNodes &#8230;</em></p>

<p>I&#8217;d like to go through some terms and buzzwords to try and clarify how to get <em>your</em> Arduino or JeeNode to do the thing <em>you</em> want it to do. I&#8217;m going to assume that you are familiar with the process of writing software (&#8220;code&#8221;), compiling it, and running it &#8211; at least on a Windows, Mac, or Linux computer (let&#8217;s call them all PC&#8217;s for now). With software for the ATmega, there are two approaches, depending on whether you use the Arduino IDE or not:</p>

<p><img src="http://files.jeelabs.org/2010/06/screen_shot_2010_06_28_at_235022.png" alt="Screen Shot 2010 06 28 at 23.50.22" /></p>

<p>Both lead to the same result: a &#8220;hex&#8221; file with code that needs to be transferred from the PC to the ATmega.</p>

<p>The step which can be puzzling when starting out with physical computing and embedded hardware is how to get things across from your PC to that little Arduino or Arduino-like system you&#8217;re holding in your hand. And vice versa, since we often want to get results back or see some confirmation that things are working properly.</p>

<p>The confusion comes from the different conceptual levels involved, and things like:</p>

<ul>
<li>do you mean how to connnect? &#8211; <em>plug in a USB cable</em></li>
<li>do you mean how does the ATmega change its own code? &#8211; <em>through a boot loader</em></li>
<li>do you mean via ISP (In System Programming)? &#8211; <em>no, that&#8217;s normally not needed</em></li>
<li>do you mean via FTDI? &#8211; <em>yes, that&#8217;s the name of the chip which is hooked up to USB</em></li>
<li>isn&#8217;t FTDI a connector? &#8211; <em>yeah, that too, sort of&#8230; i.e. a 6-pin convention</em></li>
<li>isn&#8217;t a power + serial cable enough? &#8211; <em>no, resets also play a key role</em></li>
<li>do I have to use the Arduino IDE? &#8211; <em>no, you can also use a program called &#8220;avrdude&#8221;</em></li>
<li>what&#8217;s avrdude? &#8211; <em>a program which can upload to a boot loader or an ISP programmer</em></li>
<li>do I need an ISP progranmmer? &#8211; <em>nope, the boot loader does essentially the same thing</em></li>
<li>so why not just get rid of ISP? &#8211; <em>because you need ISP to install the boot loader</em></li>
</ul>

<p><em>Confused? Welcome to the club&#8230;</em></p>

<p>In case you&#8217;re wondering&#8230; the process is called &#8220;uploading&#8221; because the PC initiates this as <em>outbound</em> transfer:</p>

<p><img src="http://files.jeelabs.org/2010/06/screen_shot_2010_06_29_at_015150.png" alt="Screen Shot 2010 06 29 at 01.51.50" /></p>

<p>To get your code (&#8220;sketch&#8221; in Arduino-speak) into an ATmega, you need three things, working together to make uploading happen under all possible conditions:</p>

<ol>
<li><p><strong>An electrical connection</strong> &#8211; to power the ATmega, to communicate with it, and to restart the ATmega when it is stuck or busy doing something else.</p></li>
<li><p><strong>A common language / protocol</strong> &#8211; the communication must be understood by both sides, i.e. PC and ATmega.</p></li>
<li><p><strong>Software on both sides of the connection</strong> &#8211; Sending something when the other side isn&#8217;t listening, or listening while no-one is sending will not have the desired outcome.</p></li>
</ol>

<p>Let&#8217;s go through each of these separately.</p>

<h2>1. An electrical connection</h2>

<p>There are several ways to make the connection. With an Arduino, or any similar board which has a USB connector, you can simply plug in the USB cable:</p>

<p><img src="http://files.jeelabs.org/2010/06/screen_shot_2010_06_28_at_235241.png" alt="Screen Shot 2010 06 28 at 23.52.41" /></p>

<p>Some boards use a separate USB interface (&#8220;FTDI adapter&#8221;), allowing reuse of that interface for multiple boards:</p>

<p><img src="http://files.jeelabs.org/2010/06/screen_shot_2010_06_28_at_235408.png" alt="Screen Shot 2010 06 28 at 23.54.08" /></p>

<p>The end effect is the same: a connection which powers the ATmega and allows communicating with it using a simple serial protocol. There&#8217;s also a trick in this hookup to let the PC reset the ATmega whenever it wants.</p>

<h2>2. A common language / protocol</h2>

<p>Ah, now it gets interesting. First thing to note is that <em>there is no single common language / protocol!</em></p>

<p>That&#8217;s right. It all depends on what you want to do. Here we want to upload code from the PC to the ATmega. That requires exchanging &#8220;ISP commands&#8221; over the connection. But once uploading is done, we really want to <em>reuse</em> the connection as a regular two-way serial link.</p>

<p>The way it works is that the PC will reset the ATmega just before uploading new code. This activates a &#8220;boot loader&#8221; on the ATmega. Now both sides will be in sync (briefly) so they can exchange the necessary information to make an upload happen. IOW, right after reset the protocol is &#8220;ISP commands&#8221;. Once the upload is done, the connection can be reused for any protocol you like &#8211; as determined by the code that was uploaded.</p>

<h2>3. Software on both sides of the connection</h2>

<p>Back to the software now. We need to send ISP commands over the connection.</p>

<p>As you may have guessed, that&#8217;s exactly what the Arduino boot loader on the ATmega
understands. When reset, the boot loader in the ATmega gets control. It waits and listens for incoming STK500 ISP commands. If none come in within a second or so, it relinquishes control to whatever was previously uploaded to the ATmega.</p>

<p>On the PC side, we need software which resets the ATmega and then immediately sends all the ISP commands needed to transfer and program the contents of a hex file.</p>

<p>This is what &#8220;avrdude&#8221; does. You can either use it implicitly from the Arduino IDE by starting an &#8220;Upload&#8221; from the menu, or you can launch it manually from the command line &#8211; avrdude needs a few options to tell it where the USB port is, what baudrate to use, the type of ATmega, the protocol to use (i.e. STK500), etc.</p>

<h2>There&#8217;s more&#8230;</h2>

<p>The above describes the different pieces and concepts involved in getting code into an ATmega. The beauty of it is that once it works, it really works well. Supplying power, uploading, communication, control, debugging &#8230; <em>all with one simple USB cable.</em> You only need to go back a few years to appreciate just how much this approach simplifies embedded development.</p>

<p>But there&#8217;s one important detail: the ATmega has to have a functioning boot loader. Placing a boot loader into an ATmega is a bit more complicated (and involves other things such as &#8220;fuses&#8221;). It&#8217;s a chicken-and-egg problem.</p>

<p>This is where the ISP programmer comes in. An ISP programmer is a fairly simple piece of hardware. In fact, you can make your own, as I&#8217;ve described in <a href="http://news.jeelabs.org/tag/isp/">several posts</a> on this weblog. See <a href="http://news.jeelabs.org/2010/07/02/fixing-a-faulty-atmega-arduino/">this</a> and <a href="http://news.jeelabs.org/2010/06/09/repairing-a-faulty-atmega/">this</a> post for some quick solutions which require nothing more than a working Arduino or JeeNode.</p>

<p>The good news, is that you usually don&#8217;t have to worry about installing the boot loader &#8211; it&#8217;s all done for you. Once. For the mechanism described above, you&#8217;ll never need an ISP programmer.</p>

<p>Some people actually prefer to use the ISP technique for uploading their sketches. In fact, sometimes you have no choice, i.e. when you need the serial port at all times, or when you can&#8217;t spare the 1..4 Kb required by the boot loader code, or when working with ATtiny chips which don&#8217;t support bootloaders.</p>

<p>In thoses cases, you&#8217;ll need a setup with an ISP programmer. But for most people doodling around with ATmega&#8217;s and the Arduino IDE, the above boot loader mechanism is usually very convenient and the easiest to get going.</p>

<p><em>Either way, it helps to understand the process. I hope the above was helpful in that direction.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://news.jeelabs.org/2010/07/04/uploading-isp-ftdi-huh/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
