<?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>David Beck&#039;s Blog &#187; Electronics</title>
	<atom:link href="http://davidbeckblog.com/category/electronics/feed/" rel="self" type="application/rss+xml" />
	<link>http://davidbeckblog.com</link>
	<description>The ramblings of an obsessed programmer</description>
	<lastBuildDate>Thu, 22 Jul 2010 18:01:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>AVR and Xcode</title>
		<link>http://davidbeckblog.com/2009/10/avr-and-xcode/</link>
		<comments>http://davidbeckblog.com/2009/10/avr-and-xcode/#comments</comments>
		<pubDate>Sun, 11 Oct 2009 23:05:31 +0000</pubDate>
		<dc:creator>David Beck</dc:creator>
				<category><![CDATA[Electronics]]></category>
		<category><![CDATA[AVR]]></category>
		<category><![CDATA[Microcontroller]]></category>
		<category><![CDATA[Xcode]]></category>

		<guid isPermaLink="false">http://davidbeckblog.com/?p=130</guid>
		<description><![CDATA[I recently purchased the beginning embedded electronics kit from SparkFun Electronics. It is a kit that include everything to start programming AVR microcontrollers. A microcontroller lets you control the most simple, physical things, like an LED or a motor. Given my lust for control, you can see why this has quickly become my latest obsessive [...]]]></description>
			<content:encoded><![CDATA[<p>I recently purchased the <a href="http://www.sparkfun.com/commerce/tutorial_info.php?tutorials_id=57" target="_blank" onclick="pageTracker._trackPageview('/outgoing/www.sparkfun.com/commerce/tutorial_info.php?tutorials_id=57&amp;referer=');">beginning embedded electronics</a> kit from <a href="http://sparkfun.com" target="_blank" onclick="pageTracker._trackPageview('/outgoing/sparkfun.com?referer=');">SparkFun Electronics</a>. It is a kit that include everything to start programming AVR microcontrollers. A microcontroller lets you control the most simple, physical things, like an LED or a motor. Given my lust for control, you can see why this has quickly become my latest obsessive hobby.</p>
<p>The problem is that I want to be able to program the micro with my Mac, which presented a few problems. Here is how I got it working with Xcode on my Mac.<br />
<span id="more-130"></span><br />
The first step was getting the programmer to work.</p>
<p>The only problem? The kit screams Windows. By default it includes a <a href="http://www.sparkfun.com/commerce/product_info.php?products_id=13" onclick="pageTracker._trackPageview('/outgoing/www.sparkfun.com/commerce/product_info.php?products_id=13&amp;referer=');">parallel programer</a>. The last time I had a computer with a parallel port was&#8230; well it was when I had a PC. Not only that but SparkFun only carries Windows compatible <a href="http://www.sparkfun.com/commerce/product_info.php?products_id=8702" onclick="pageTracker._trackPageview('/outgoing/www.sparkfun.com/commerce/product_info.php?products_id=8702&amp;referer=');">USB programmers</a> (a programmer in this case is a device that transfers your code to the chip, not the skinny kid from high school with a Redbull IV in him). In fact, there are almost no AVR programmers that are Mac compatible.</p>
<p>Not knowing the days of frustration that I was getting myself into, I bought the <a href="http://www.sparkfun.com/commerce/product_info.php?products_id=8702" onclick="pageTracker._trackPageview('/outgoing/www.sparkfun.com/commerce/product_info.php?products_id=8702&amp;referer=');">STK500 USB programmer</a>. Other options would be the now discontinued <a href="http://www.pololu.com/catalog/product/740" onclick="pageTracker._trackPageview('/outgoing/www.pololu.com/catalog/product/740?referer=');">Pololu programmer</a>, or build it yourself.</p>
<p>Luckily, <a href="http://www.olimex.com/" onclick="pageTracker._trackPageview('/outgoing/www.olimex.com/?referer=');">Olimex</a>, the company that made the STK500 USB programmer, has released an <a href="http://www.olimex.com/dev/soft/avr/AVR-ISP500/AvrIspFwUpdate_1_0_0_8.zip" onclick="pageTracker._trackPageview('/outgoing/www.olimex.com/dev/soft/avr/AVR-ISP500/AvrIspFwUpdate_1_0_0_8.zip?referer=');">update</a> that makes it Mac compatible. You can find info and instructions on how to install it on <a href="http://www.olimex.com/dev/avr-isp500.html" onclick="pageTracker._trackPageview('/outgoing/www.olimex.com/dev/avr-isp500.html?referer=');">their product page</a>. Be sure to follow the instructions <em>exactly</em>. You will need a Windows computer to do the upgrade. It worked fine in <a href="http://www.amazon.com/gp/product/B002Q72JB8?ie=UTF8&amp;tag=dabesbl-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=B002Q72JB8" onclick="pageTracker._trackPageview('/outgoing/www.amazon.com/gp/product/B002Q72JB8?ie=UTF8_amp_tag=dabesbl-20_amp_linkCode=as2_amp_camp=1789_amp_creative=390957_amp_creativeASIN=B002Q72JB8&amp;referer=');">VMWare Fusion</a>.</p>
<p>The next step was to get the build tools woking in Mac OS X. These are the programs that will compile your code and transfer it to the microcontroller.</p>
<p>The easiest way to get the tools needed is to get <a href="http://www.obdev.at/products/crosspack/index.html" onclick="pageTracker._trackPageview('/outgoing/www.obdev.at/products/crosspack/index.html?referer=');">CrossPack</a>. CrossPack includes all the tools necessary to build and upload your projects. This also includes some tools to generate Xcode templates.</p>
<p>Basically, the Xcode templates create a project with an external build target. So in the background all that is happening is make is being called. I found two problems with the default templates. One is that you have to use the command line to generate your templates. That isn&#8217;t really necessary since Xcode has a built in template architecture. Basically place any templates you wan to show up in the new file window in &#8220;/Developer/Library/Xcode/Project Templates&#8221;. Just make sure to create a sub folder there to create the section. I called mine AVR.</p>
<p>The second problem I had was that there is no way to upload the code to the chip. Luckily there is an easy fix. Right click on executables and select &#8220;Add: New Custom Executable&#8221;. Use /usr/bin/make as the path. Under arguments, add &#8220;flash&#8221;. This will call make, just like the target does, but it will make for flash instead of compile. Now you can select Build and Go and it will upload the program to the connected microcontroller.</p>
<p>To get going, you need to edit the makefile in the project. Select your microcontroller, for DEVICE. Input the correct value for CLOCK. I also had to add a variable for the location of the tools. I called it TOOLS</p>
<pre>DEVICE     = atmega168
CLOCK      = 8000000
PROGRAMMER = -c stk500v2 -P /dev/tty.usbmodem*
OBJECTS    = main.o
FUSES      = -U hfuse:w:0xd9:m -U lfuse:w:0x24:m

TOOLS      = /usr/local/CrossPack-AVR/bin/

AVRDUDE = $(TOOLS)avrdude $(PROGRAMMER) -p $(DEVICE)
COMPILE = $(TOOLS)avr-gcc -std=c99 -Wall -Os -DF_CPU=$(CLOCK) -mmcu=$(DEVICE)</pre>
<p>In the PROGRAMMER line you will notice I used an asterisk (*) to select any connected programmer. I did this because the number changed on me a few times and I didn&#8217;t want to have to edit the makefile every time I plugged it in.</p>
<p>You should be good to go now. You can download <a title="Xcode AVR template" href="/wp-content/uploads/2009/10/Firmware.zip">my template</a>, but you will still need the CrossPack tools.</p>
<p>Update: <a rel="external nofollow" href="http://www.instepmusic.com/" target="_blank" onclick="pageTracker._trackPageview('/outgoing/www.instepmusic.com/?referer=');">Jonathan</a> mentioned in the comments that you can use <a title="Engbedded Atmel AVR Fuse Calculator" href="http://www.engbedded.com/fusecalc/" target="_blank" onclick="pageTracker._trackPageview('/outgoing/www.engbedded.com/fusecalc/?referer=');">http://www.engbedded.com/fusecalc/</a> to calculate the fuse bits.</p>
]]></content:encoded>
			<wfw:commentRss>http://davidbeckblog.com/2009/10/avr-and-xcode/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Working on &#8220;Robot Building for Beginners&#8221;</title>
		<link>http://davidbeckblog.com/2009/04/working-on-robot-building-for-beginners/</link>
		<comments>http://davidbeckblog.com/2009/04/working-on-robot-building-for-beginners/#comments</comments>
		<pubDate>Sat, 04 Apr 2009 17:49:21 +0000</pubDate>
		<dc:creator>David Beck</dc:creator>
				<category><![CDATA[Electronics]]></category>
		<category><![CDATA[Books]]></category>
		<category><![CDATA[Robots]]></category>

		<guid isPermaLink="false">http://davidbeckblog.com/?p=24</guid>
		<description><![CDATA[Wednesday I picked up a copy of &#8220;Robot Building for Beginners&#8221; from the library and am now almost done with it. I have read several other books on robots and electronics, but this is the first that isn&#8217;t a complete waste of paper. I have built the circuit on a solderless breadboard without the motors [...]]]></description>
			<content:encoded><![CDATA[<p>Wednesday I picked up a copy of &#8220;Robot Building for Beginners&#8221; from the library and am now almost done with it. I have read several other books on robots and electronics, but this is the first that isn&#8217;t a complete waste of paper.<br />
<span id="more-24"></span><br />
I have built the circuit on a solderless breadboard without the motors and using a wall wart instead of a 9V battery. It works quite nicely.</p>
<p>There is however a wall I have hit. Motors cost like $50 for gear heads. So I suppose the robot will be put on hold until then.</p>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="425" height="344" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.youtube.com/v/Y3m8lvIL9dc&amp;hl=en&amp;fs=1" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="425" height="344" src="http://www.youtube.com/v/Y3m8lvIL9dc&amp;hl=en&amp;fs=1" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
]]></content:encoded>
			<wfw:commentRss>http://davidbeckblog.com/2009/04/working-on-robot-building-for-beginners/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Finally getting somewhere with electronics</title>
		<link>http://davidbeckblog.com/2009/03/finally-getting-somewhere-with-electronics/</link>
		<comments>http://davidbeckblog.com/2009/03/finally-getting-somewhere-with-electronics/#comments</comments>
		<pubDate>Sat, 28 Mar 2009 22:00:22 +0000</pubDate>
		<dc:creator>David Beck</dc:creator>
				<category><![CDATA[Electronics]]></category>

		<guid isPermaLink="false">http://davidbeckblog.com/?p=16</guid>
		<description><![CDATA[I feel like I am finally getting somewhere with this electronics thing. A month ago I bought an ATMega168 microcontroller from Spark Fun Electronics. Having programed for several years now I felt like venturing into microcontrollers. So far I have made an LED blink, read the value of a potentiometer and now I have a tri color LED fading [...]]]></description>
			<content:encoded><![CDATA[<p>I feel like I am finally getting somewhere with this electronics thing.</p>
<p>A month ago I bought an ATMega168 microcontroller from <a href="http://www.sparkfun.com" onclick="pageTracker._trackPageview('/outgoing/www.sparkfun.com?referer=');">Spark Fun Electronics</a>. Having programed for several years now I felt like venturing into microcontrollers.<br />
<span id="more-16"></span> So far I have made an LED blink, read the value of a potentiometer and now I have a tri color LED fading between all the different colors.</p>
<p>My next challenge is to control the speed of a motor, which is proving to be a bit difficult.</p>
<p>This is all hopefully leading to something useful. I have been thinking about an RC helicopter with an attached camera for arial photography but that is a little ways down the road to say the least.</p>
<p>To get the tri color led to work I used PWM (Pulse Width Modulation). as long as you keep the micro running fast enough it will not look like it&#8217;s blinking.</p>
<p>The key was that the led has 1 positive pin and 3 ground pins that control the individual colors. So when the voltage is lower, it is actually brighter.</p>
<p>I wired the positive lead (the longest) to VCC and each of the three grounds to an output pin on the ATMega168 through an appropriate resistor. the colors are a little uneven because I didn&#8217;t have precise enough resistors (red takes a different current than green and blue).</p>
<p>Source Code:</p>
<pre>#include &lt;avr/io.h&gt;

#define STEPS 255

//change these to the pins you have connected to the led
#define RED 5
#define BLUE 4
#define GREEN 3

void ioinit(void);      //Initializes IO
void fadePin(int pin, int destination);
//gentally fades a pin to destination. assumes starting at oposite value.

int main (void) {
	ioinit(); //Setup IO pins and defaults

	//zero is fully on because output pins are connected to ground pins

	PORTC |= (1 &lt;&lt; RED);
	PORTC |= (1 &lt;&lt; GREEN);
	PORTC |= (1 &lt;&lt; BLUE);
	//start with red
	fadePin(RED, 0);

	//loop forever
	while(1) {
		fadePin(BLUE, 0);	//purple

		fadePin(RED, 1);	//blue

		fadePin(GREEN, 0);	//turquist

		fadePin(BLUE, 1);	//green

		fadePin(RED, 0);	//yellow

		fadePin(GREEN, 1);	//red
	}

    return(0);
}

void fadePin(int pin, int destination) {
	if(destination == 0)
		destination = -1;

	//i controls the PWM
	int i=0;
	for(float j=0.0f; j&lt;1.0f; j+=0.0001f) {
		i++;
		if(i&gt;STEPS)
			i=0;

		//destination makes everything negative if it is zero (before we change it)
		if(i * destination &lt; STEPS * j * destination) {
			//turn pin on
			PORTC |= (1 &lt;&lt; pin);
		} else {
			//turn pin off
			PORTC &amp;= ~(1 &lt;&lt; pin);
		}
	}

	//make sure to end with the desired value
	if(destination == 1) {
		//turn pin on
		PORTC |= (1 &lt;&lt; pin);
	} else {
		//turn pin off
		PORTC &amp;= ~(1 &lt;&lt; pin);
	}
}

void ioinit (void) {
    //1 = output, 0 = input
    DDRC = 0b11111111; //All outputs on Port C
}</pre>
<p style="text-align:center;"><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="425" height="344" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.youtube.com/v/CzGkVcvBU1M&amp;hl=en&amp;fs=1" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="425" height="344" src="http://www.youtube.com/v/CzGkVcvBU1M&amp;hl=en&amp;fs=1" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
]]></content:encoded>
			<wfw:commentRss>http://davidbeckblog.com/2009/03/finally-getting-somewhere-with-electronics/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
