<?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>about:benjie &#187; Source code</title>
	<atom:link href="http://www.benjiegillam.com/tag/source-code/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.benjiegillam.com</link>
	<description>Benjie Gillam&#039;s blog, and home of MythPyWii - probably the best Wii remote (Wiimote) interface to MythTV - see sidebar.</description>
	<lastBuildDate>Tue, 06 Apr 2010 14:14:35 +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>Arduino Pin Speed (Multiplexing)</title>
		<link>http://www.benjiegillam.com/2009/09/arduino-pin-speed-multiplexing/</link>
		<comments>http://www.benjiegillam.com/2009/09/arduino-pin-speed-multiplexing/#comments</comments>
		<pubDate>Tue, 08 Sep 2009 21:13:57 +0000</pubDate>
		<dc:creator>Benjie</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Computers]]></category>
		<category><![CDATA[Hardware]]></category>
		<category><![CDATA[Personal]]></category>
		<category><![CDATA[Arduino]]></category>
		<category><![CDATA[Binary numeral system]]></category>
		<category><![CDATA[Clock signal]]></category>
		<category><![CDATA[LED]]></category>
		<category><![CDATA[Source code]]></category>

		<guid isPermaLink="false">http://www.benjiegillam.com/?p=224</guid>
		<description><![CDATA[I&#8217;m working on a new project, I&#8217;ve got a 8&#215;8 dual colour dot matrix display (£2.50 delivered from Earthshine Design) and I want to power it from the Arduino. One way of making a chip like that (which has 2x8x8 = 128 LEDs) would be to have a common ground and an additional 128 pins [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.earthshinedesign.co.uk/index.php?route=product/product&amp;path=38_43&amp;product_id=82" target="_blank"><img class="alignright" title="8x8 dual colour dot matrix display" src="http://www.earthshinedesign.co.uk/image/cache/1425313_75-250x250.jpg" alt="" width="250" height="250" /></a>I&#8217;m working on a new project, I&#8217;ve got a <a title="8x8 dual colour dot matrix display" href="http://www.earthshinedesign.co.uk/index.php?route=product/product&amp;path=38_43&amp;product_id=82" target="_blank">8&#215;8 dual colour dot matrix display</a> (£2.50 delivered from Earthshine Design) and I want to power it from the <a class="zem_slink" title="Arduino" rel="homepage" href="http://www.arduino.cc" target="_blank">Arduino</a>. One way of making a chip like that (which has 2x8x8 = 128 <a class="zem_slink" title="Light-emitting diode" rel="wikipedia" href="http://en.wikipedia.org/wiki/Light-emitting_diode" target="_blank">LEDs</a>) would be to have a common ground and an additional 128 pins &#8211; one for each LED. This, I think you&#8217;d agree, would be a nightmare, so instead they&#8217;ve basically gone for an 8&#215;16 grid for a total of 24 pins. This raises two main problems:</p>
<ol>
<li>You can&#8217;t turn 2 arbitrary LEDs on at the same time unless they are on the same row/column. (Doing so would actually draw a square of LEDs.)</li>
<li>My Arduino doesn&#8217;t have enough digital input/output pins</li>
</ol>
<p>Point 1 is easily solved &#8211; we simply update just one row at a time, letting <a href="http://en.wikipedia.org/wiki/Persistence_of_vision" target="_blank">Persistance Of Vision</a> (POV) do the hard work for us. Point 2 is the subject of this post &#8211; multiplexing, combining multiple individual signals into just one signal. I will not be using this dot matrix display in this post, instead I will simply be powering normal LEDs. I wanted to find out if the Arduino is fast enough to multiplex the data through just a few pins in order to power this display. The answer (one of my favourite answers!) is: &#8220;Yes, but not without some hacking.&#8221; Read on&#8230;</p>
<p><span id="more-224"></span></p>
<h2>The Hardware</h2>
<p>In order to get a balance between the number of pins we use for this and the speed of the output I&#8217;ve decided to use 3 data pins, each controlling 8 bits, for a total of 24 bits (the same as the number of pins on the LED component &#8211; no coincidence!) The way that I have implemented it, we also need 2 pins for clocks (one fast, one slow) &#8211; read on. The clock pins will be common to all 3 data pins, so for the rest of this post I will only detail one data pin (8 bits).</p>
<p><a href="http://www.benjiegillam.com/wp-content/uploads/2009/09/arduino-project2.JPG"><img src="http://www.benjiegillam.com/wp-content/uploads/2009/09/arduino-project2-300x155.jpg" alt="The Hardware" title="The Hardware" width="300" height="155" class="aligncenter size-medium wp-image-235" /></a></p>
<p>(Sorry about the messiness. Click for a larger picture. There is a Roboduino (Arduino compatible) board on the left, the LEDs and resistors should be fairly obvious from the photo, then there are 3 chips. From left to right they are the Darlington array, the SIPO shift register and the flip flop array (see below for all of these)).</p>
<h3>Fast clock &#8211; Shift Register</h3>
<p>We want to send the data through one pin (data line 1, DL1) to an external Serial-In Parallel-Out (SIPO) chip, also known as a <a class="zem_slink" title="Shift register" rel="wikipedia" href="http://en.wikipedia.org/wiki/Shift_register" target="_blank">shift-register</a> (I&#8217;m using a <a href="http://www.google.co.uk/search?q=M74HC164B1" target="_blank">M74HC164B1</a> octal shift register). This does exactly what it says &#8211; it takes a serial (single) input and converts it to parallel (multiple) outputs. However the chip just receives some high signals and some low signals &#8211; how should it know how many digital 1s there are in a second&#8217;s worth of high signal? This is where the fast clock (FC) comes in. FC just goes from low to high and back again for every bit of data we send through the data lines, and when it changes from low to high the SIPO chip &#8220;reads&#8221; the value of DL1 and outputs it on it&#8217;s first output pin (Pin1). The next time the SIPO chip reads from DL1, it &#8220;shifts&#8221; the value from Pin1 to the second pin (Pin2), and again stores the value of DL1 (on or off) into Pin1. This continues indefinitely, shifting the data along a pin at a time, filling up all 8 of the SIPO chip&#8217;s output pins (and forever overwriting previous data). Here&#8217;s a diagram from the datasheet:</p>
<p><img src="http://www.benjiegillam.com/wp-content/uploads/2009/09/screenshot736.png" alt="Shift Register" title="Shift Register" width="576" height="470" class="aligncenter size-full wp-image-234" /></p>
<p>Time goes from right to left (the left shows an earlier time than the right). The data is the second signal (the third can just remain high always) and the 4th signal is the clock pulse. Signals 5-12 show the state of the output pins at each point in time.</p>
<h3>Slow Clock &#8211; Flip Flop</h3>
<p>We don&#8217;t want to display this shifting data though, so we use a &#8220;<a class="zem_slink" title="Flip-flop (electronics)" rel="wikipedia" href="http://en.wikipedia.org/wiki/Flip-flop_%28electronics%29" target="_blank">flip-flop</a>&#8221; (FF) to store the values. A flip flop can be thought of as a Serial-In Serial-Out (SISO) chip in that it takes it&#8217;s input and uses that as the output. What&#8217;s so special about it then, I hear you ask, isn&#8217;t it just a bit of wire? The &#8220;special&#8221; thing about flip flops is that they &#8220;store&#8221; this value &#8211; recording it when it receives a clock pulse, like the SIPO chip above. We&#8217;re going to use the slow clock (SC) for this purpose, because we only want to store the values once all 8 bits have been output from the SIPO chip. (The SC is triggered once every 8 FC pulses.) Doing so we wait for the SIPO chip to correctly represent the 8 bits it has been sent, and then we store these values whilst the next 8 bits are loaded. I&#8217;m using a <a href="http://www.google.co.uk/search?q=SN74HC273N" target="_blank">SN74HC273N</a> octal flip flop chip which has 8 individual flip flops in it.</p>
<h3>Boosting &#8211; Darlington array</h3>
<p>Unfortunately the flip flop chip does not supply much current &#8211; not enough to power an LED &#8211; so we need to use transistors to boost the current. I&#8217;ve used a Darlington array to boost the current in my circuit as I had one left over from a previous project (it is a <a href="http://www.google.co.uk/search?q=ULN2803A" target="_blank">ULN2803A</a>).</p>
<h3>Display</h3>
<p>I&#8217;ve displayed the output of just one of the data pins using 8 standard LEDs, each with a resistor that&#8217;s too big (didn&#8217;t have one the correct rating).</p>
<h2>The Software</h2>
<p>To test this, I decided to do some binary counting.</p>
<h3>Initial Software</h3>
<p>First I implemented my own solution, and when I turned of the debugging delays and optimized as much as I could it resulted in 2 of the LEDs (the ones signifying the most significant bits) visibly flashing, a sad result meaning that there would be visible flickering on the final display if I used this method, especially as I was outputting only 8 bits and not 24 (doing 24 would make it roughly half as fast due to the extra digitalWrite operations).</p>
<p><code class="source">
<span class="enscript-comment">// The pins I will be using:
</span><span class="enscript-type">int</span> dataPin = 10;
<span class="enscript-type">int</span> fastClockPin = 11;
<span class="enscript-type">int</span> slowClickPin = 12;
<span class="enscript-comment">// The data to display:
</span>byte count=0; 

<span class="enscript-type">void</span> <span class="enscript-function-name">setup</span>() {
  <span class="enscript-comment">// All the pins are output pins
</span>  pinMode(dataPin,OUTPUT);
  pinMode(fastClockPin, OUTPUT);
  pinMode(slowClockPin, OUTPUT);
  <span class="enscript-comment">// Reset all pins low
</span>  digitalWrite(dataPin, LOW);
  digitalWrite(fastClockPin, LOW);
  digitalWrite(slowClockPin, LOW);
}
<span class="enscript-comment">// This will pulse the fast clock pin
</span><span class="enscript-type">void</span> <span class="enscript-function-name">fastClock</span>() {
  digitalWrite(fastClockPin, HIGH);
  digitalWrite(fastClockPin, LOW);
}
<span class="enscript-comment">// This will pulse the slow clock pin
</span><span class="enscript-type">void</span> <span class="enscript-function-name">slowClock</span>() {
  digitalWrite(slowClockPin, HIGH);
  digitalWrite(slowClockPin, LOW);
}
<span class="enscript-type">void</span> <span class="enscript-function-name">loop</span>() {
  <span class="enscript-comment">// We don't want to edit count, so copy it.
</span>  byte v = count;
  <span class="enscript-type">int</span> i;
  <span class="enscript-comment">// There are 8 bits in a byte, do the following for each bit.
</span>  <span class="enscript-keyword">for</span> (i=0; i&lt;8; i++) {
    <span class="enscript-comment">// Set the data: if v modulo 2 is 1, send high, otherwise low. (v modulo 2 is the remainder when you divide v by 2)
</span>    digitalWrite(dataPin, v%2 == 1 ? HIGH : LOW);
    <span class="enscript-comment">// I've written the data, let the SIPO chip know
</span>    fastClock();
    <span class="enscript-comment">// Shift v 1 bit along, dropping the bit we just checked.
</span>    v = v &gt;&gt; 1;
  }
  <span class="enscript-comment">// All 8 bits have been output, tell the flip flops to store their current values:
</span>  slowClock();
  <span class="enscript-comment">// Increase the value of count by one
</span>  ++count;
}
</code>
</p>
<h3>Abstraction Principle</h3>
<p>Looking around at how to optimize this, I soon found that I could use a built in function shiftOut instead of my own method calling digitalWrite for every bit. I updated my code, changing 11 lines of code to just 1. This did speed it up (their <a class="zem_slink" title="Binary numeral system" rel="wikipedia" href="http://en.wikipedia.org/wiki/Binary_numeral_system" target="_blank">binary arithmetic</a> was faster than my method of the time) but not by enough &#8211; 1 LED still flashed &#8211; there would still be visible flicker. How annoying.</p>
<p><code class="source">
<span class="enscript-comment">// The pins I will be using:
</span><span class="enscript-type">int</span> dataPin = 10;
<span class="enscript-type">int</span> fastClockPin = 11;
<span class="enscript-type">int</span> slowClickPin = 12;
<span class="enscript-comment">// The data to display:
</span>byte count=0; 

<span class="enscript-type">void</span> <span class="enscript-function-name">setup</span>() {
  <span class="enscript-comment">// All the pins are output pins
</span>  pinMode(dataPin,OUTPUT);
  pinMode(fastClockPin, OUTPUT);
  pinMode(slowClockPin, OUTPUT);
  <span class="enscript-comment">// Reset all pins low
</span>  digitalWrite(dataPin, LOW);
  digitalWrite(fastClockPin, LOW);
  digitalWrite(slowClockPin, LOW);
}
<span class="enscript-comment">// REMOVED: fastClock(), because shiftOut() handles this for us.
</span>
<span class="enscript-comment">// This will pulse the slow clock pin
</span><span class="enscript-type">void</span> <span class="enscript-function-name">slowClock</span>() {
  digitalWrite(slowClockPin, HIGH);
  digitalWrite(slowClockPin, LOW);
}
<span class="enscript-type">void</span> <span class="enscript-function-name">loop</span>() {
  <span class="enscript-comment">// REMOVED: Most of the code here, shiftOut() does it all for us:
</span>  shiftOut(dataPin, fastClockPin, LSBFIRST, count);
  <span class="enscript-comment">// All 8 bits have been output, tell the flip flops to store their current values:
</span>  slowClock();
  <span class="enscript-comment">// Increase the value of count by one
</span>  ++count;
}
</code>
</p>
<h3>Digging Deeper</h3>
<p>I was a bit disappointed by this, as I knew from the last SoutHACKton meetup that on <a class="zem_slink" title="PICAXE" rel="wikipedia" href="http://en.wikipedia.org/wiki/PICAXE" target="_blank">PICAXE</a> boards you could set multiple pins at the same time. Setting just one pin at a time in Arduino seemed unnecessarily slow. I dug a bit deeper in the <a title="Arduino Extended Reference" href="http://arduino.cc/en/Reference/Extended" target="_blank">extended documentation</a>, and came across <a title="Arduino Port Manipulation" href="http://arduino.cc/en/Reference/PortManipulation" target="_blank">the Port Manipulation page</a>:</p>
<blockquote><p>You may need to be able to turn pins on and off very quickly, meaning within fractions of a microsecond. If you look at the source code in lib/targets/arduino/wiring.c, you will see that digitalRead() and digitalWrite() are each about a dozen or so lines of code, which get compiled into quite a few machine instructions. Each machine instruction requires one clock cycle at 16MHz, which can add up in time-sensitive applications. Direct port access can do the same job in a lot fewer <a class="zem_slink" title="Clock signal" rel="wikipedia" href="http://en.wikipedia.org/wiki/Clock_signal" target="_blank">clock cycles</a>.</p></blockquote>
<p>Huzzah! I can write multiple pins at once, and not only that, I can skip a lot of the clever code that the Arduino libraries come with as I&#8217;m happy making assumptions about my program&#8217;s operation. Excellent!</p>
<p>I had a look at the built in functions, and revised my binary arithmetic so that I could be as optimized as possible, and ran the code&#8230;</p>
<p><code class="source">
<span class="enscript-comment">// REMOVED: the pins (these are now implicit in the binary masks below rather than explicit as before)
</span><span class="enscript-comment">// The data to display:
</span>byte count=0;
<span class="enscript-comment">// Alternatively: unsigned int count = 0;
</span>
<span class="enscript-type">void</span> <span class="enscript-function-name">setup</span>() {
  <span class="enscript-comment">// The pins I am using are output pins: (12,11,10)
</span>  DDRB = B00011100;
  <span class="enscript-comment">// Reset all pins low:
</span>  PORTB = B00000000;
}
<span class="enscript-type">int</span> i;
<span class="enscript-type">void</span> <span class="enscript-function-name">loop</span>() {
  <span class="enscript-comment">// REMOVED: most previous code
</span>  <span class="enscript-comment">// Loop through the bits of count. (if count is an unsigned int, then use this instead:) for (i=0; i &lt; 16; i++) {
</span>  <span class="enscript-keyword">for</span> (i=0; i &lt; 8; i++) {
    <span class="enscript-comment">// Turn all clocks off, and set the data pin
</span>    PORTB = B00000000 | (!!(count &amp; (1 &lt;&lt; i)) &lt;&lt; 2);
    <span class="enscript-comment">// Leave the data pin as it is, turn on the fast clock pin, and if we're on the last bit also turn on the slow clock pin
</span>    PORTB |= B00001000 | (!!(i == 7) &lt;&lt; 4);
    <span class="enscript-comment">// If we're working with an unsigned int, then change the 7 above to a 15.
</span>  }
  <span class="enscript-comment">// Increase the value of count by one
</span>  ++count;
}
</code>
</p>
<p>... All the LEDs appeared on all the time... Either it's working perfectly or something's very wrong. Fortunately by adding a delay into the loop, I could see it was the former, and I celebrated! But just how much faster is it? I couldn't use the Arduinos internal timing, as timing it would cause it to use more processing power and thus slow down... Instead I decided to display the 8 most significant bits of a 16bit number, and see how many of those flickered. All except one flickered (not unexpected) but this meant it was significantly faster than before.</p>
<p>I timed the slowest flashing LED - one full cycle (on to off and back on again) of this 1st bit took an average of 2.2 seconds. This meant that one full cycle of the 9th bit (the one we're bothered about - the slowest of the initial 8 bits) was taking 2.2/256 = 0.0086s, or, in other words, it was running at 116Hz - good enough to not be noticeable by human perception (standard TV is 25-30 Hz). (This also means that the fastest bit is being pulsed at around 15,000Hz.) This is just 8bits, mind, not 24. Adding in the extra bits would not be too taxing on the system though - purely mathematics - as we can set all the pins in the same step. I would expect it to run all 24 bits at around 40-60Hz. </p>
<div class="zemanta-pixie" style="margin-top: 10px; height: 15px;"><a class="zemanta-pixie-a" title="Reblog this post [with Zemanta]" href="http://reblog.zemanta.com/zemified/20a08457-e3af-41d3-a25c-0a4b3401d280/"><img class="zemanta-pixie-img" style="border: medium none; float: right;" src="http://img.zemanta.com/reblog_c.png?x-id=20a08457-e3af-41d3-a25c-0a4b3401d280" alt="Reblog this post [with Zemanta]" /></a><span class="zem-script more-related pretty-attribution"><script src="http://static.zemanta.com/readside/loader.js" type="text/javascript"></script></span></div>
]]></content:encoded>
			<wfw:commentRss>http://www.benjiegillam.com/2009/09/arduino-pin-speed-multiplexing/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>
