<?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>Keith&#039;s Electronics Blog &#187; AVR</title>
	<atom:link href="http://www.neufeld.newton.ks.us/electronics/?cat=31&#038;feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://www.neufeld.newton.ks.us/electronics</link>
	<description></description>
	<lastBuildDate>Fri, 18 Apr 2025 00:10:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>OS X ATtiny25 Programming Walkthrough</title>
		<link>http://www.neufeld.newton.ks.us/electronics/?p=1539</link>
		<comments>http://www.neufeld.newton.ks.us/electronics/?p=1539#comments</comments>
		<pubDate>Sun, 08 Jul 2012 01:18:53 +0000</pubDate>
		<dc:creator>Keith Neufeld</dc:creator>
				<category><![CDATA[AVR]]></category>

		<guid isPermaLink="false">http://www.neufeld.newton.ks.us/electronics/?p=1539</guid>
		<description><![CDATA[A while back I bought a batch of ATtiny25 microcontrollers and today I finally put one to use and got it programmed. MIT&#8217;s High-Low Tech group has put together a really nice package for programming ATtiny45s and ATtiny85s from the Arduino IDE, but it doesn&#8217;t support ATtiny25s, presumably because the limited memory doesn&#8217;t leave enough [...]]]></description>
			<content:encoded><![CDATA[<p>A while back I bought a batch of ATtiny25 microcontrollers and today I finally put one to use and got it programmed.  MIT&#8217;s High-Low Tech group has put together a <a href="http://hlt.media.mit.edu/?p=1695">really nice package for programming ATtiny45s and ATtiny85s from the Arduino IDE</a>, but it doesn&#8217;t support ATtiny25s, presumably because the limited memory doesn&#8217;t leave enough space after the bootloader is installed.  I had a dickens of a time trying to find instructions to walk through programming an ATtiny25, much less on OS X; but it turned out to be pretty easy after pulling together a few separate resources.  Here&#8217;s the path to blinkemness.</p>
<p><a href="http://www2.neufeld.newton.ks.us/images/electronics/2012/07/07/IMG_1982.jpg"><img src="http://www2.neufeld.newton.ks.us/images/electronics/2012/07/07/IMG_1982_mid.jpg" alt="ATtiny25 board with USBtinyISP and Saleae Logic, overhead view" /></a></p>
<p><span id="more-1539"></span></p>
<h3>Install CrossPack Development Environment</h3>
<p>First, install <a href="http://www.obdev.at/products/crosspack/index.html">CrossPack</a>, which provides &#8220;the GNU compiler suite, a C library for the AVR, the AVRDUDE uploader and several other useful tools.&#8221;  Make sure you completely close your Terminal program (if you had it running) and restart it after the installation, to pick up the location it adds to your command search path.</p>
<h3>Test the Programmer and Connectivity</h3>
<p>Once installed, connect your programmer (I have a couple of USBtinyISPs, so that&#8217;s what I used) to your computer and target board and test that the programmer is working and sees your ATtiny:</p>
<p><code>$ avrdude -c usbtiny -p attiny25<br />
avrdude: AVR device initialized and ready to accept instructions</p>
<p>Reading | ################################################## | 100% 0.01s</p>
<p>avrdude: Device signature = 0x1e9108</p>
<p>avrdude: safemode: Fuses OK</p>
<p>avrdude done.  Thank you.<br />
</code></p>
<p>If you get any other response, check the troubleshooting notes at the bottom of <a href="http://mightyohm.com/blog/tutorials/avr-toolchain-installation/mac-os-x/">Jeff Keyzer&#8217;s AVR toolchain installation page</a>.</p>
<h3>Create Blink Code</h3>
<p>The <a href="http://www.obdev.at/products/crosspack/index.html">CrossPack web page</a> includes a blinky demo project, but it&#8217;s written for a different microcontroller and requires minor adaptation to work on the ATtiny25.  Follow along both here and there for greatest insight.</p>
<p>In your Terminal window,</p>
<p><code>$ avr-project blink<br />
Using template: /usr/local/CrossPack-AVR-20120217/etc/templates/TemplateProject<br />
$ cd blink/firmware<br />
$ ls<br />
Makefile	main.c<br />
</code></p>
<p>The Crosspack example is written for an ATmega8, which has more I/O ports than the ATtiny.  The ATtiny has a single I/O port, and it&#8217;s port B (mystery of the universe why not A).  So adapting the example code, use your favorite text editor to insert into <code>main.c</code> to create the following (picking whichever pin you&#8217;d like to use for your LED):</p>
<pre>
#include &lt;avr/io.h&gt;
#include &lt;util/delay.h&gt;

#define LEDPIN (3)

int main(void) {
    DDRB = 1 &lt;&lt; LEDPIN;  //  make only the LED pin an output
    while (1) {
        char i;
        for (i = 0; i &lt; 10; ++i) {
            _delay_ms(50);
        }
        PORTB ^= 1 &lt;&lt; LEDPIN;
    }
    return 0;   /* never reached */
}
</pre>
<h3>Calculate Fuse Values and Edit the Makefile</h3>
<p>Use the <a href="http://www.engbedded.com/fusecalc/">Engbedded AVR fuse calculator</a> to calculate the command-line values to set all the ATtiny behavior flags.  Use <code>AVR part name: ATtiny25</code>, uncheck (for this example) <code>Divide clock by 8 internally</code>, and scroll down to <code>Current settings</code>.  You should see something like</p>
<p><code>-U lfuse:w:0xe2:m -U hfuse:w:0xdf:m<br />
-U efuse:w:0xff:m<br />
</code></p>
<p>Back in Terminal, edit <code>Makefile</code> .  Update the following lines:</p>
<p><code>DEVICE     = attiny25<br />
PROGRAMMER = -c usbtiny<br />
FUSES      = -U lfuse:w:0xe2:m -U hfuse:w:0xdf:m -U efuse:w:0xff:m<br />
</code></p>
<p>(using whatever programmer you have and any additional flags it needs) and save and exit the <code>Makefile</code> .</p>
<h3>Compile and Load the Program</h3>
<p>Run</p>
<p><code>$ make<br />
avr-gcc -Wall -Os -DF_CPU=8000000 -mmcu=attiny25 -c main.c -o main.o<br />
avr-gcc -Wall -Os -DF_CPU=8000000 -mmcu=attiny25 -o main.elf main.o<br />
rm -f main.hex<br />
avr-objcopy -j .text -j .data -O ihex main.elf main.hex<br />
avr-size --format=avr --mcu=attiny25 main.elf<br />
AVR Memory Usage<br />
----------------<br />
Device: attiny25</p>
<p>Program:      92 bytes (4.5% Full)<br />
(.text + .data + .bootloader)</p>
<p>Data:          0 bytes (0.0% Full)<br />
(.data + .bss + .noinit)<br />
</code></p>
<p>to build the program.  If you get errors instead, double-check <code>main.c</code> around the line the compiler indicates.</p>
<p>Run</p>
<p><code>$ make flash<br />
avrdude -c usbtiny -p attiny25 -U flash:w:main.hex:i</p>
<p>avrdude: AVR device initialized and ready to accept instructions</p>
<p>Reading | ################################################## | 100% 0.01s</p>
<p>avrdude: Device signature = 0x1e9108<br />
avrdude: NOTE: FLASH memory has been specified, an erase cycle will be performed<br />
         To disable this feature, specify the -D option.<br />
avrdude: erasing chip<br />
avrdude: reading input file "main.hex"<br />
avrdude: writing flash (92 bytes):</p>
<p>Writing | ################################################## | 100% 0.08s</p>
<p>avrdude: 92 bytes of flash written<br />
avrdude: verifying flash memory against main.hex:<br />
avrdude: load data flash data from input file main.hex:<br />
avrdude: input file main.hex contains 92 bytes<br />
avrdude: reading on-chip flash data:</p>
<p>Reading | ################################################## | 100% 0.05s</p>
<p>avrdude: verifying ...<br />
avrdude: 92 bytes of flash verified</p>
<p>avrdude: safemode: Fuses OK</p>
<p>avrdude done.  Thank you.<br />
</code></p>
<p>to program the IC.  Run</p>
<p><code>$ make fuse<br />
avrdude -c usbtiny -p attiny25 -U lfuse:w:0xe2:m -U hfuse:w:0xdf:m -U efuse:w:0xff:m </p>
<p>avrdude: AVR device initialized and ready to accept instructions</p>
<p>Reading | ################################################## | 100% 0.01s</p>
<p>avrdude: Device signature = 0x1e9108<br />
avrdude: reading input file "0xe2"<br />
avrdude: writing lfuse (1 bytes):</p>
<p>Writing | ################################################## | 100% 0.00s</p>
<p>avrdude: 1 bytes of lfuse written<br />
avrdude: verifying lfuse memory against 0xe2:<br />
avrdude: load data lfuse data from input file 0xe2:<br />
avrdude: input file 0xe2 contains 1 bytes<br />
avrdude: reading on-chip lfuse data:</p>
<p>Reading | ################################################## | 100% 0.00s</p>
<p>avrdude: verifying ...<br />
avrdude: 1 bytes of lfuse verified<br />
avrdude: reading input file "0xdf"<br />
avrdude: writing hfuse (1 bytes):</p>
<p>Writing | ################################################## | 100% 0.00s</p>
<p>avrdude: 1 bytes of hfuse written<br />
avrdude: verifying hfuse memory against 0xdf:<br />
avrdude: load data hfuse data from input file 0xdf:<br />
avrdude: input file 0xdf contains 1 bytes<br />
avrdude: reading on-chip hfuse data:</p>
<p>Reading | ################################################## | 100% 0.00s</p>
<p>avrdude: verifying ...<br />
avrdude: 1 bytes of hfuse verified<br />
avrdude: reading input file "0xff"<br />
avrdude: writing efuse (1 bytes):</p>
<p>Writing | ################################################## | 100% 0.00s</p>
<p>avrdude: 1 bytes of efuse written<br />
avrdude: verifying efuse memory against 0xff:<br />
avrdude: load data efuse data from input file 0xff:<br />
avrdude: input file 0xff contains 1 bytes<br />
avrdude: reading on-chip efuse data:</p>
<p>Reading | ################################################## | 100% 0.00s</p>
<p>avrdude: verifying ...<br />
avrdude: 1 bytes of efuse verified</p>
<p>avrdude: safemode: Fuses OK</p>
<p>avrdude done.  Thank you.<br />
</code></p>
<p>to burn the fuse bits.</p>
<h3>Behold Blinky Goodness</h3>
<p><a href="http://www2.neufeld.newton.ks.us/images/electronics/2012/07/07/IMG_1989.jpg"><img src="http://www2.neufeld.newton.ks.us/images/electronics/2012/07/07/IMG_1989_mid.jpg" alt="ATtiny25 board with USBtinyISP and Saleae Logic, angle" /></a></p>
<p>The board I&#8217;m developing doesn&#8217;t have an LED on it, so I clipped my logic analyzer to ground and I/O pin B3 (IC pin 2) to test for blinking.</p>
<p><a href="http://www2.neufeld.newton.ks.us/images/electronics/2012/07/07/blink-capture.png"><img src="http://www2.neufeld.newton.ks.us/images/electronics/2012/07/07/blink-capture_mid.png" alt="1 Hz square wave from ATtiny25 IC" /></a></p>
<p>1 Hz blinking on my I/O pin.  Fundamental programming accomplished.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.neufeld.newton.ks.us/electronics/?feed=rss2&#038;p=1539</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
