AVR For PIC People, Part 1: The Basics

The Blown Fuse

As with most microcontrollers, the AVR has various configurable settings relating to options such as system clock speed and source, watchdog timer enable, and similar things.  They’re called “fuses” in Atmel parlance, and are similar in nature to the __CONFIG macros or #pragma config statements in Microchip xc8.

Unfortunately, this is one area where the open-source Atmel toolchain falls short.

While avr-libc provides a very clean mechanism for defining these in your firmware source code, avrdude doesn’t natively support that mechanism.  Instead, you have to figure out the fuse settings for yourself and program them manually.  There is a way to make them work, but it’s beyond the scope of this tutorial; you’ll have to wait for part 2.

Besides, it’s always good to know how to do things like this manually.

With respect to our example circuit, the ATtiny13A ships with settings for a 9.6MHz clock, which is divided by 8 internally, for a 1.2MHz default system clock.  Just as a test, I want to turn off the clock divider and make our trusty demo run at a full 9.6MHz.  To do this, I need to disable the CLKDIV8 fuse.

I’m lazy, so rather than bother with manually assembling everything from the datasheets, I used an online fuse calculator.  Select the ATtiny13, uncheck the CLKDIV8 checkbox, and the new settings are shown in hex form at the bottom:  Low is the same as LFUSE, with a value 0f 0x7A, and High (HFUSE) is 0xFF.

Handing the values off to avrdude is fairly simple; the fuses are simply in another memory type that’s used with the -U option.  We’ll need the fourth parameter field, however, since we want to simply place the value on the command line rather than reading it from a file.  The result looks like this:

$ avrdude -c buspirate -b 115200 -p attiny13 -P /dev/tty.usbserial-AH00MPIC -U lfuse:w:0x7a:m -U hfuse:w:0xff:m

The -U options are fairly self-explanatory; the avrdude manpage will tell you more.  Note that we can happily put any number of -U options on the command line, and avrdude will execute them in order.  Handy for making things simple.

If we execute this without flashing the firmware to an updated version, the lights blink eight times as fast.  Setting it back is easy; just use the fuse calculator with the CLKDIV8 bit turned back on, and substitute the values into the command above.

Note that some AVR microcontrollers also have an efuse, or extended fuse, configuration byte.  And the XMEGA series?  Those are a whole other kettle of fish…

WARNING:  DO NOT MESS WITH THE SPIEN FUSE!  This fuse is what enables you to program the chip, and should be left at the factory default setting unless you really know what you’re doing.  If you clear this bit, you’ll need a high-voltage programmer (which the Bus Pirate and many other low-cost units aren’t) to make your chip programmable again.

The Conclusion

If you followed along, then you’ve successfully built your first AVR-based circuit; you’ve written the code; you’ve flashed the firmware; and you have a working circuit.  This is all very basic, though, isn’t it?  You probably could’ve figured it out on your own (or just used Atmel’s AVR Studio under Windows).

Doesn’t really tell you what you want to know, though, does it?  I mean, how do you actually use these things?

Now that we’ve covered the basics, part 2 will cover the details of real-world programming on AVR.  It’s impossible to cover every peripheral or nuance, but it should give you a good grounding to get you started.

I hope to have Part 2 ready for you next week.

S.

About Steve

When it comes to the desktop, Steve is a former Amiga, Windows, and Linux user, and as of six years ago, a die-hard Mac head (who, for once, isn't thinking of changing platforms again any time soon). When it comes to the server, Linux is pretty much the only game he plays. He also enjoys hardware hacking, and shouldn't be allowed near a keyboard after the sun sets (or for that matter, after it rises. Don't say I didn't warn you).
This entry was posted in AVR, Hardware Hacking. Bookmark the permalink.

2 Responses to AVR For PIC People, Part 1: The Basics

  1. Daniel says:

    Thanks for this! I’ve been using microchip’s PICs a fair amount, and wanted to start trying AVRs to see if they might be more simple. This will be a nice starting point.

  2. Steve says:

    Um. This is a serious cliffhanger – or did Part2 get blown with the wind^Wfuse? Is there still hope?
    Thanks anyway for the nice introduction, as a late adopter I appreciate it a lot. Everyone else is using RasPis now, but AVRs are fun for someone who decades ago had to learn and write (mainframe) assembly code!

Leave a Reply

Your email address will not be published. Required fields are marked *