54
Introduction This is (going to be) a easy tutorial for starting out with basic AVR programming, and also a reference collection so that it sits in one place on the site Comments? Suggestions? Post to the forum ! 1. What is an AVR? 2. Setup for Mac and Windows 3. How programming works 4. Choosing a programmer 5. Using AVRDUDE <- In progress, click on a link to the left Prerequisites This set of notes does have some prerequisites. It requires two important sets of knowledge: 1. You must know how to use the command line for your OS, either Terminal (Mac, Unix) or cmd/DOS (Windows). Here is a tutorial for Macs Here is one for Linux (Ubuntu) Here is one for Windows 2. You must know how to program, preferrably in C. The fact is, most microcontrollers are programmed in C, its a lightweight programming language that is well suited to the restrictive environment. You can also do many of these examples in BASIC but I doubt I'll have time to put up those versions Here is a tutorial I learned C about 12 years ago from a book that came with a CD and had lots

Computer Stuff

Embed Size (px)

Citation preview

Page 1: Computer Stuff

Introduction

This is (going to be) a easy tutorial for starting out with basic AVR programming, and also a reference collection so that it sits in one place on the site

Comments? Suggestions? Post to the forum!

1. What is an AVR? 2. Setup for Mac and Windows3. How programming works 4. Choosing a programmer 5. Using AVRDUDE

<- In progress, click on a link to the left

 

Prerequisites

This set of notes does have some prerequisites. It requires two important sets of knowledge:

1. You must know how to use the command line for your OS, either Terminal (Mac, Unix) or cmd/DOS (Windows). Here is a tutorial for MacsHere is one for Linux (Ubuntu) Here is one for Windows

2. You must know how to program, preferrably in C. The fact is, most microcontrollers are programmed in C, its a lightweight programming language that is well suited to the restrictive environment. You can also do many of these examples in BASIC but I doubt I'll have time to put up those versionsHere is a tutorialI learned C about 12 years ago from a book that came with a CD and had lots of examples, I think it was "Learn C in 21 days" or some nonsense. It doesn't matter how you do it, but this tutorial assumes that you know what a for loop is and how bit-wise arithmetic work, etc.You also need to be able to convert hex, binary and decimal (using a calculator).Microcontrollers are tougher to program because there is limited debugging capability.

Page 2: Computer Stuff

What is a microcontroller?

The best way to explain what a microcontroller is, is to start with your computer. Your desktop computer (or laptop) is comprised of multiple parts, a CPU (such as a Pentium or Celeron), some RAM, a hard disk, a keyboard and mouse and a monitor screen. Programs are stored on the hard disk and run on the CPU, with temporary data stored in RAM. You can run multiple programs at a time by having one 'master program' called an operating system (such as Linux, Windows or Mac OS X) and that master program keeps track of things for you.

The AVR chip has components, too. It has a CPU, some flash storage, some RAM and some EEPROM, all in one little chip!. The CPU is just like the one in a computer, but its much simpler and not nearly as fast (what do you expect for $2.50?) The flash storage is just like the flash storage in your mp3 player or digital camera card, except its used to store programs. Its kinda like the hard disk of the microcontroller, except you can only read from it. The RAM is just like computer RAM. The EEPROM is kinda like flash except you cant run a program from it, but its used as long term storage. The EEPROM doesnt get erased when the chip loses power.

So, to recap: The AVR chip runs whatever program is stored in the flash, uses the RAM for temporary storage and the EEPROM for longer term storage.

Most computers have a 32-bit CPU running at 1GHz, with 1GB of RAM and 100 GB of storage. The kinds of microcontrollers discussed here run at 10MHz, have 1KB of RAM and 10KB of storage. (On the order of) However, their small size, lower power consumption and low cost make them an excellent choice for many projects!

How to do something

OK so now that its clear what microcontrollers do, how do you get it to do something?

1. Well first off you have to write a program (or have someone write one) that tells the chip what to do.

2. Then you have to compile it, that is, turn the program description into machine code.

3. Next you program the chip using a programmer, which will trasfer the machine code to the device

4. Test, debug, repeat!

First step is setting up your computer for programming, so follow the setup steps for Mac or Windows to get that done.

Comments? Suggestions? Post to the forum!

Step-by-step how to install AVR dev tools Introduction

Page 3: Computer Stuff

Here is a 'generic' step-by-step to getting an AVR development platform going on your computer using the free AVR toolchain (avr-gcc, avr-libc and avrdude) Pretty much every project uses this toolset so its a good way to get going

Comments? Suggestions? Post to the forum!

Mac Setup - Paths

The following two methods both place all of the files in the /usr/local/bin directory in the hard driver. Unfortunately that directory is not in the default path. That means that when you type avrdude into the terminal it cant figure out where to look. In this prep-step you'll change the profile of your Terminal to add /usr/local/bin to the path.

Find the Terminal program, you'll be using this to do most of this stuff. Its in the Applications/Utilities folder

In the new Terminal window, type in echo $SHELL and press return

If the output is /bin/bash then type the following command: echo 'PATH=$PATH:/usr/local/bin' >> ~/.bash_profile all on one line. Press return.

If the output is /bin/csh or /bin/tcsh then type the following command: echo 'set path = ($path /usr/local/bin)' >> ~/.cshrcall on one line. Press return.

Page 4: Computer Stuff

Close any Terminal windows and open up a new one. This makes sure the .bash_profile or .cshrc is reloaded. Now type in echo $PATH (for bash) or echo $path (for t/csh) you should get something like the following:

The important thing is that at the end of the line is /usr/local/bin

Option 1. AvrMacPack

This is the suggested method

Download the ready to go nice package from ObDev, I havent tried it but a friendly email'er said its great.

Option 2. OSX-AVRStep 1. Installing XCode

This isnt suggested, and is an older method, but we leave the documentation here in case its handy

You will need make which is included in XCode, as OSX-AVR doesn't come with it (ugh)

Step 1. Download and install the mac developer tools (XCode).

You need to have make installed, but it doesn't come with the OSXAVR package. You can try installing it with fink, which will require a lot less space but the following is guaranteed. If you want to have the latest avr-gcc you may also have to do it the "old way" which guarantees the most recent tools will be installed.

To install XCode you will need the official packages. These are available on your Mac OS X Install CD, or from apple at: apple developer tools. The file is about 900MB so unless you have a fast connection I strongly encourage grabbing it off of the Install CDs that came with your Mac (you do still have those, right?) Basically we need the native Mac OS X compiler tools so that we can generate the AVR compiler tools.

Page 5: Computer Stuff
Page 6: Computer Stuff

 

Step 2. Install OSX-AVR

There's finally a good/fast way of installing all these tools under Mac OS X PPC or i386! First, download the OSX-AVR packge for PPC (older macs) or i386 (Intel macs, latest ones) from sourceforge.

Run the OSX-AVR.mpkg

You're done!

(Images of installation and process are forthcoming but its rather easy so go ahead and try it anyways)

Don't forget you have to install XCode as make doesn't come with this package.

Can't get it working? Dont worry, help is available in the forums!

Option 3. Old style, 'by hand' compilation

Page 7: Computer Stuff

This is the advanced method, for when you need bleeding-edge development and hackability. Not suggested

 

The following steps are essentially the same for MacOS X or Linux, BSD or any other unixy OS. This is the 'old style' of installing avr-gcc, its longer and more tedious but you are guaranteed to have the latest version.

(Note that this doesn't seem to work on Intel Macs for unknown reasons, we're investigating...)

Leah Buchley has an excellent tutorial, and you should follow it. I've reduplicated it here in case the site goes down. (also with a few minor 'improvements' and images

Step 2. Download & install binutils (an essential utility for the C compiler)

Download the current release of binutils from : http://www.gnu.org/software/binutils/ (you can also go straight to the download site here) For these examples, we'll be using binutils-2.17.tar.gz but you should use whatever is most recent. Save it into your home directory, not the desktop.

Decompress the downloaded file and double click on it to decompress it (or use Stuffit Expander, in the Applications folder). You should now have a folder called binutils-2.17 which you should drag into your Home directory

Page 8: Computer Stuff

Open up a Terminal window and navigate to the binutils directory. Type: cd binutils-2.17 <return> (or whatever you downloaded) theb type in ls to verify everythings there

Configure binutils for AVR. type: ./configure --target=avr <return> this will start a long process that will spit out a lot of text.

Page 9: Computer Stuff

Once its done, compile binutils. type: make <return> this will start an even longer compilation process

Once that's done, install binutils. type: sudo make install You will be prompted to enter your password. Only administrators can install software thats why the password is necessary.

Page 10: Computer Stuff

Step 3. Download & install gcc (the C compiler)First, download the current release of gcc from: http://gcc.gnu.org/mirrors.html currently thats gcc 4.2.0

Decompress the downloaded file and put the decompressed folder in your home directory. Open up a new Terminal window in your home directory, type cd gcc-4.2.0 and then ls to verify its all in there.

Page 11: Computer Stuff

Next, c reate another directory to install gcc into. type: cd .. <return> to go back into the home directory, thentype: mkdir avrgcc-4.2 <return> (substituting your gcc version for the 4.2) Navigate to the folder you created. type: cd avrgcc-4.2 <return>  (or whatever you named your folder)Configure gcc for AVR. Type: ../gcc-4.2.0/configure --target=avr --enable-languages=c --disable-libssp   (substituting the name of the folder you decompressed for the gcc-4.2.0)   **thanks to Seth Raphael for the --disable-libssp tip

Page 12: Computer Stuff

Once the configuration is done, compile gcc. type: make CC="cc --no-cpp-precomp" <return>

This will take a long time so go have a sandwich

Page 13: Computer Stuff

When its done and you've washed your plate and silverware, install gcc. type: sudo make install <return>and enter your password when prompted

Step 4. Download and install avr-libc (an essential C library for AVR chips)

Download the current release of avr-libc from : http://savannah.nongnu.org/projects/avr-libc/

Decompress the downloaded file and put the decompressed folder in your home directory

In a new Terminal window, navigagte to the avr-libc directory. from your home directory type: cd avr-libc-1.4.6 <return>  (or whatever you downloaded)

Page 14: Computer Stuff

Configure avr-libc. type: ./configure --host=avr <return>

Compile avr-libc. type: make

Page 15: Computer Stuff

Install avr-libc. type: sudo make install

Step 5. Download and install avrdude (the software that loads programs from your machine onto the chips)

Download the current release of avrdude from : http://download.savannah.gnu.org/releases/avrdude/

Decompress the downloaded file and put the decompressed folder in your home directory

In a Terminal window, navigate to the avrdude directory. From your home directory type: cd avrdude-5.2  (or whatever you downloaded)Configure avrdude. type: ./configureCompile avrdude. type: makeInstall avrdude. type: sudo make install

Page 16: Computer Stuff

Yay all the software is installed!

Can't get it working? Dont worry, help is available in the forums!

Windows Setup

Download the WinAVR package from sourceforge Make sure to download the latest and greatest version

if you grab some older versions (say June 07), avrdude may complain about not being able to find libusb0.dll, do a search on your computer for the file, then copy it to C:/Windows/System32 and try again! This is a known bug, hopefully to be fixed soon...

Install the software

Page 17: Computer Stuff

That's it! WinAVR is very simple to install, now lets test it out to make sure we have it installed.

Page 18: Computer Stuff

Open a command window

Now in the new terminal window type in avrdude you should get this response, which is basically a simple list of what avrdude can do

Page 19: Computer Stuff

Introduction

Here is a 'generic' step-by-step to getting an AVR development platform going on your computer using the free AVR toolchain (avr-gcc, avr-libc and avrdude) Pretty much every project uses this toolset so its a good way to get going.

Every Linux/Unix distribution is slightly different. This particular example is for Ubuntu but should be easily adaptable to your OS.

I use the photos from the old Mac OS X setup but dont let that confuse you.

You can also try this nifty python script, which does it all for you. However, I havent tried it and your mileage may vary.

Please note! This is a very generic tutorial which will work on any Unix type OS. Its long and tedious and will get you the bleeding-edge development environment for advanced hackers. If you have a modern PC with, say, Ubuntu, you can probably get ready made packages for all this stuff! Check your distribution's package library for avr-gcc and avrdude!

Page 20: Computer Stuff

Comments? Suggestions? Post to the forum!

Unix Setup - Paths

99% of Unix distros have /usr/local/bin in the default $PATH. But if it isn't then you can use this technique to add it.

Open up a Terminal or xterm program, you'll be using this to do most of this stuff. In Ubuntu its under "Applications"

In the new Terminal window, type in echo $SHELL and press return

If the output is /bin/bash then type the following command: echo 'PATH=$PATH:/usr/local/bin' >> ~/.bash_profile all on one line. Press return.

If the output is /bin/csh or /bin/tcsh then type the following command: echo 'set path = ($path /usr/local/bin)' >> ~/.cshrcall on one line. Press return.

Close any Terminal windows and open up a new one. This makes sure the .bash_profile or .cshrc is reloaded. Now type in echo $PATH (for bash) or echo $path (for t/csh) you should get something like the following:

The important thing is that somewhere in the line of text you see /usr/local/bin

Unix setup

These instructions are just a mirror of the ones here

Step 1. Download and install the developer tools

You'll need the following packages: flex, byacc, bison, gcc, libusb and libusb-dev (for USB avr programmers)

Page 21: Computer Stuff

In Ubuntu, you can just use the package manager or use the command apt-get install flex byacc bison gcc libusb libusb-dev

gcc is probably already installed, but just in case, get the latest version. Libusb may be in a "universal" repository. If you're not using a usb programmer (such as a AVRISP mk ii or a usbtiny) then its ok if you dont have libusb installed. Otherwise you may have to modify your repository list to include the "universal" repositories. Check out your distro's documentation on how to do this.

For some unfathomable reason, Ubuntu does not include essential headers for libc development so also apt-get install libc6-dev

Step 2. Download & install binutils (an essential utility for the C compiler)

Download the current release of binutils from : http://www.gnu.org/software/binutils/ (you can also go straight to the download site here) For these examples, we'll be using binutils-2.17.tar.gz but you should use whatever is most recent. Save it into your home directory, not the desktop.

Decompress the downloaded file and double click on it to decompress it (Ubuntu has a decompressor built in). You should now have a folder called binutils-2.17 which you should drag into your Home directory (or Desktop)

Page 22: Computer Stuff

Open up a Terminal window and navigate to the binutils directory. Type: cd binutils-2.17 <return> (or whatever you downloaded) then type in ls to verify everythings there

Configure binutils for AVR. type: ./configure --target=avr --program-prefix="avr-" <return> this will start a long process that will spit out a lot of text.

Page 23: Computer Stuff

Once its done, compile binutils. type: make <return> this will start an even longer compilation process

Once that's done, install binutils. type: sudo make install You will be prompted to enter your password. Only administrators can install software thats why the password is necessary.

Page 24: Computer Stuff

Step 3. Download & install gcc (the C compiler)First, download the current release of gcc from: http://gcc.gnu.org/mirrors.html currently thats gcc 4.2.0 (some people argue for the 3.4.x versions that seem to optimize the code to be smaller and faster. But really, whichever you want, just make it recent)

Decompress the downloaded file and put the decompressed folder in your home directory. Open up a new Terminal window in your home directory, type cd gcc-4.2.0 and then ls to verify its all in there.

Page 25: Computer Stuff

Next, c reate another directory to install gcc into. type: cd .. <return> to go back into the home directory, thentype: mkdir avrgcc-4.2 <return> (substituting your gcc version for the 4.2) Navigate to the folder you created. type: cd avrgcc-4.2 <return>  (or whatever you named your folder)Configure gcc for AVR. Type: ../gcc-4.2.0/configure --target=avr --enable-languages=c --disable-libssp   (substituting the name of the folder you decompressed for the gcc-4.2.0)   **thanks to Seth Raphael for the --disable-libssp tip

Page 26: Computer Stuff

Once the configuration is done, compile gcc. type: make CC="cc --no-cpp-precomp" <return>

Note: It seems like the --no-cpp-precomp may not be a good idea, if you are having problems maybe leave it off?

This will take a long time so go have a sandwich

Page 27: Computer Stuff

When its done and you've washed your plate and silverware, install gcc. type: sudo make install <return>and enter your password when prompted

Page 28: Computer Stuff

Step 4. Download and install avr-libc (an essential C library for AVR chips)

Download the current release of avr-libc from : http://savannah.nongnu.org/projects/avr-libc/

Decompress the downloaded file and put the decompressed folder in your home directory

In a new Terminal window, navigagte to the avr-libc directory. from your home directory type: cd avr-libc-1.4.6 <return>  (or whatever you downloaded)

Configure avr-libc. type: ./configure --host=avr <return>

Compile avr-libc. type: make

Page 29: Computer Stuff

Install avr-libc. type: sudo make install

Step 5. Download and install avrdude (the software that loads programs from your machine onto the chips)

Download the current release of avrdude from (v5.5 or higher): http://download.savannah.gnu.org/releases/avrdude/

Uncompress the sources (this screenshot says "usbtiny" but just ignore that)

Page 30: Computer Stuff

Make sure you have libusb and libusb-dev installed! Type in sudo apt-get install libusb libusb-dev

Type in ./configure

Page 31: Computer Stuff

Look for this in the output checking for usb_get_string_simple in -lusb... yes if you don't see that (or it says no) check again, make sure libusb and libusb-dev are installed!

OK now type in make

Page 32: Computer Stuff

Next, its time to install, type in sudo make install

For some unknown reason, to talk to the USB devices, you must be sudo'd. Here is me testing a USB programmer (usbtinyisp) with it attached to a attiny2313 target board.

Page 33: Computer Stuff

If you don't have a target attached you'll get Initialization failed: rc = -1 which means that the programmer is working but it could not find a chip to program!

Yay all the software is installed!

Can't get it working? Dont worry, help is available in the forums!

Introduction So you have this chip, and its programmable. But how do you get your program onto that chip? Comments? Suggestions? Post to the forum!

What is a program? When code is compiled into a windows program, its turned into "binary" which means its not human-readable (which is tough for machines to understand), instead it is machine-readable. Under windows, these programs (often called applications or executables) are often named ending with .exe (for example notepad.exe or winword.exe), on Macs, they are often named ending with .App (although the Finder hides it).

Page 34: Computer Stuff

For microcontrollers, binary files end in .hex (short for Intel Hex Format) There are other formats possible, but this is pretty much standard.Your compiler will generate a .hex file from code, and then all you have to do is transfer that .hex program to the chip!

What is flash?

As discussed in What is it?, the AVR chip has a small amount of flash memory in it. That memory is where the program is stored. When the chip starts up (you give it power) it starts running whatever program is in the flash. So all we have to do is figure out how to write to that flash

A good way to think about flash is to examine it as you probably are familiar with it. Flash storage is also used in MMC/SD cards, which are commonly used in MP3 players or digital cameras. If you look at the bottom of a card, you'll see little gold fingers. These are the interface pads.

Back of Multi Media Card. By Andrzej Barabasz.

If you look up in the MMC card specification, you'll find out that these are what the 7 pins are for. (SD/high speed cards may have extra pads, but that standard was added later because the existing standard was considered too slow for big megapixel cameras)

Pin # Pin Name Pin Function

1 #CS Chip Select (Activate)

2 SI Serial In

3 GND Ground

4 VCC Power

5 SCK Data Clock

6 Not connected Not connected

Page 35: Computer Stuff

7 SO Serial Out

The power and ground pins are used to connect power to the chip. The Chip Select pin is used to tell the chip that it should wake up, Serial In pin is used to send data to the card, Serial Out is how data is sent from the card and Data Clock is how we tell the card how fast we want data to be stored or retrieved. Data is pushed in an out of the chip one bit at a time, so if the clock rate is 100KHz that means that 100,000 bits are transferred per second.

When you want to retreive or store data on a flash card all you have to do is stick the card into a flash card reader. The reader then has a driver on your computer which lets you simply drag files onto a picture on your screen and it does all the hard work of sending the data to the card.

OK, so what does this have to do with a programmer? Well, the microcontroller is like the flash card and the card reader is like the programmer. When you stick the chip into the programmer (or, more likely, the programmer cable into the circuit board that contains the chips) it allows the software on your computer to talk to the chip through the programmer. Usually you can just give a simple command like 'send this program over' and the programmer will do all the hard work of programming and verifying the data.

In-System Programming interface

Every AVR has a set of pins that are the programming pins, you just have to connect the programmer to those pins in the right order and presto you are ready to program it. Unfortunately, nearly every chip has a different pinout so its imperative that you look in the datasheet for the correct pins.

For example, I'm a big fan of the ATtiny2313 chip, so lets open up the 'summary' datasheet (its only a dozen pages long instead of the standard 250. The first page has all sorts of information about what is in the chip.

Quick Quiz : How much EEPROM does the chip have? How about flash storage? RAM?

The second page has pinouts. We will be dealing with the PDIP package (Plastic Dual In-line Package) which is the kind that looks like this:

The pinout is reproduced here

Page 36: Computer Stuff

The chip has 20 pins. Look carefully and you'll notice 6 of these pins are equivalent to the SD/MMC card pins. There is a power pin (VCC) and a ground pin (GND), there is also an SCK pin (its called UCSK for some reason) a Serial Out pin (MISO - Master In Serial Out ) and a Serial In pin (MOSI - Master Out Serial In). The only thing is doesnt have is a /ChipSelect pin. Instead it has a /RESET pin, which acts in a similar way. When the /RESET pin is at a postive voltage (the same voltage as VCC) the chip runs the program, when the /RESET pin is at ground (the same voltage as GND) then the chip stops and listens for programming instructions on the programming pins.

All SD cards have the same fingers in the same place so that when you slide it into the card reader, the right pads are connected. AVR programming works in a similar way. Instead of pads, the chip is often placed in a circuit board which has header pins that the progammer plugs into, the header pinout is standardized so that any programmer can be used once the header is wired up correctly.

There are two standards for AVR in-system programming:

The left is the 6-pin header standard, the right is the 10 pin standard, the headers look like this:

Page 37: Computer Stuff

Here is an example photo showing what it looks like when the chip is wired up with a 6-pin header

Photo courtesy EvilMadScientistLabs

The black mark on the PCB and header indicate pin #1. You can trace the wires to correllate the pins of the chip to the pins of the header and verify for your self that this breadboard is correct.

Page 38: Computer Stuff

Exercise: Verify the pinout for this target board

To learn how to make a 'target board' that lets you program AVRs (in particular, said ATtiny2313) please visit EvilMadScientistLab's tutorial. Its quite easy and will cover some of the same material.

Now that you know how programming works, its time to chose your weapon programmer.

Choosing a programmer Introductions

OK now that you have learned all about how programming works in theory, its time to determine what sort of programmer is going to work best for you

Comments? Suggestions? Post to the forum!

Programmer options

There are easily a dozen programmers out there, I'll cover the 4 major types:

1. Simple/Basic/Bitbang programmers2. ISP smart programmers3. Development boards 4. No-programmer (Bootloader)

Simple/Basic/Bit-bang programmers

Simple programmers are very inexpensive and very simple. They connect to a serial port or parallel port directly and then the computer software twiddles the port to send data to the chips.

A parallel port programmer looks something like this:

Page 39: Computer Stuff

It plugs into the parallel (printer) port of a computer

You can see the silver part plugs into the parallel port, and then theres a cable that plugs into a 10-pin ISP header

A serial port programmer looks something like this:

Page 40: Computer Stuff

Very similar to the parallel port programmer, but plugs into the serial port

Some have a buffer chip to disconnect the programming pins when the chip is running so that it can use the programming pins for other things. Without a buffer you may have to remove the programmer after the chip has been programmed.

Page 41: Computer Stuff

I suggest these for people who are just starting out and are doing very simple stuff but strongly suggest a quick upgrade to a smart ISP programmer or development board.

Pros: Ultra cheap ($10-$20), easy to understand, because the software does everything there's no risk of 'programmer incompatibility or upgrades'Cons: Requires a PC with the ports, Macs can't use these, if you're working on high voltages (more than 5V) you could zap your port, multiple standards for programmer interface.

You can buy such programmers in the adafruit webshop.

Smart ISP programmers

These programmers plug into the serial port or USB port, and have more brains than bitbang programmers. They are also more likely to work, and have some nice features.

The most common programmer is the AVRISP and the AVRISPv2. Both look something like this:

Photo courtesy EvilMadScientistLabs

There's a jack on one end for a cable and the 6-pin and/or 10-pin cable comes out the other. The AVRISP v1 had a serial port jack and the v2 has USB. The v1 has both cables, the v2 has only 6-pin.

AVRISPv2's are available from Digikey.com or Mouser.com for $36 or so

Page 42: Computer Stuff

I really liked the v1 but it was discontinued and the v2 doesn't support 10-pin cables so I designed my own programmer called the USBtinyISP, you can build it from a kit for about $20. Read more about it here.

There's a lot more ISP designs out there, many homemade.

Development boards

There are dozens of development boards out there all slightly different, so I'll cover the two that are the most popular (and fairly inexpensive) and are officially endorsed by Atmel.

The first is the STK500, which for $80 is quite a beast. It supports pretty much every chip Atmel makes with all the various sockets. It has buttons, LEDs, an adjustable clockspeed oscillator, etc. This programmer is supported by AVRStudio, which is Atmel's programming envirionment. Since it is the 'defacto' programmer, the programming method it uses to talk to the computer (Called the STK500 protocol) is also a standard. Its got a bit of a learning curve but lots of people use it. You can buy it from Digikey or Mouser.

Page 43: Computer Stuff

The nice thing about it is you get a huge dev board, it has sockets for all the chips, as well as a programmer. The sad part is that it only has a serial port so you'll need a USB serial converter.

The second is the AVR Dragon which is an emulator as well as a programmer. It is used more like an ISP than a development board, it has connectors for both 6 and 10 pin headers.

I havent had much play-time with it but people say its nice, and its only $50 Note that there are no sockets for the chips.

Bootloaders

In recent years, microcontroller designers have added the capability for the chip to program its own flash (often called self-programming). What this means is that a program burned on a microcontroller can actually change its own program. In most cases this is not a good idea, a program can overwrite itself, damage itself, and/or cause the microcontroller to freak out. However, in certain cases this can be a benefit.

Take, for example, a small program that is burned into the chip that can communicate with a computer through a USB or serial cable and allow the computer to download new firmware without the use of a programmer.When you 'update the firmware' on your MP3 player or cell phone, this is whats happening.

For example, Arduino use a bootloader that is programmed into the chip at the Arduino factory. The bootloader is protected so that it can't overwrite itself. To upload code to the Arduino, you use the Arduino software which is smart enough to recognize when an Arduino is connected.

Bootloaders are great, but you need a ISP programmer to put the bootloader on the chip the first time if you're buying the chip from Digikey or Mouser. Its a bit of a chicken-and-egg problem.

Page 44: Computer Stuff

Bootloaders also don't let you modify the fuses (discussed later) and they take up a portion of the flash memory so uploaded programmers must be smaller.

In general bootloaders are great for starting out with but I find its important to have some way to program the chip so that you can put the bootloader on and maybe modify the bootloader or fuses.

I won't be using bootloaders in these examples, but will assume you have an ISP programmer as they are more universal. Later on, you can always add your own bootloader.

Fuses

Introduction

Fuses are an extremely important part programming a chip, but are rarely explained thoroughly. You only need to set them once, but if you don't do it right, it's a disaster!

Comments? Suggestions? Post to the forum!

What is a fuse?

You know about flash, eeprom and RAM as parts of the chip. What I did not mention is that there are also 3 bytes of permanent (by permanent I mean that they stick around after power goes out, but that you can change them as many times as you'd like) storage called the fuses. The fuses determine how the chip will act, whether it has a bootloader, what speed and voltage it likes to run at, etc. Note that despite being called 'fuses' they are re-settable and dont have anything to do with protection from overpowering (like the fuses in a home).

The fuses are documented in the datasheets, but the best way to examine the fuses is to look at a fuse calculator such as the avr fuse calculator from the palmavr project

Click on that link in a new window and select ATtiny2313 for the fuse calculations

Page 45: Computer Stuff

We'll use the Quick Configuration so use those menus, not the checkboxes

 

Clock selection

The first option is how the chip is clocked. Every CPU uses a clock, The clock keeps track of time for the chip, in general one assembly code instruction is run every clock cycle.The one in your PC has a clock that runs at 1GHz or higher. This little chip runs much slower. If you look at the menu you'll see a huge list of options, but looking carefully you'll see there are two groupings, the Clock Source, the Clock Startup

The Clock Source can be either of the following: External Clock, Internal 8MHz clock, Internal 4MHz clock, Internal 128KHz clock, External Crystal (0.4-0.9 MHz), External Crystal (0.9MHz - 3.0MHz), External Crystal (3.0MHz - 8.0MHz) or External Crystal (8.0MHz +)

Page 46: Computer Stuff

The Clock Startup can be either of the following: 14CK + 0 ms, 14CK + 4 ms, 14CK + 65 ms.

External Clock means that a square wave is being input into the CLOCK-IN pin. This is pretty rare unless you have a clock generating chip. Don't use this unless you're sure you mean toInternal Clock means that theres a little oscillator inside the chip, its not very precise but good for most projects that dont have fine timing issues. The clock varies with temperature and the power supply voltage. You can chose from a 8MHz, 4MHz or 128KHz clock. The 128KHz clock is for very low power applications where running the chip very slowly helps conserve power. Having an internal oscillator means we don't need to wire up a crystal and we can use the clock pins for our own nefarious purposes.External Crystal If you need a special clock rate, like 3.58MHz or 12MHz or a high precision clock that won't drift with the temperature, you'll want an external crystal or oscillator.

Crystals look something like this: Ceramic oscillators look like this:

Crystals come in multiple different pacakges, they may be cylindrical, or larger than the image shown. In both cases the speed is printed on top or on the side, almost always in MHz.

The Startup Time is just how long the clock source needs to calm down from when power is first applied. Always go with the longest setting 14CK + 65ms unless you know for a fact your clock source needs less time and 65ms is too long to wait.

By default, chips that come from the factory have the Internal 8 MHz clock with 14CK + 65ms Startup.

Clock Output

The next option is Clock Out on PortD2 which means, basically, that whatever the clock input is (internal, external, crystal, etc) a square wave of the same frequency will appear on pin D2. This is useful if you're debugging the clock rate, or if you want to use the clock to drive another chip.

By default, chips that come from the factory have this turned off.

Page 47: Computer Stuff

Clock Divide

This fuse causes the chip to divide the clock rate by 8. So if the clock source is set to Internal 8MHz and you have this fuse set, then you'll really be running at 1MHz.

By default, chips that come from the factory have this turned on.

Reset Disable

This fuse turns the Reset pin into a normal pin instead of a special pin. If you turn this fuse on you cant program the chip using ISP anymore. I would suggest you never set this fuse unless you really mean to.

By default, chips that come from the factory have this turned off (that is, Reset is enabled)

Brown-out Detect (BOD)

These fuses set what voltage to turn the Brownout protection circuitry on. A "brown-out" in this case is the same as when the power-grid gets over-taxed by thousands of people running their air conditioners in the summer: the voltage drops and your clock radio maybe stops working. A brownout for a chip means that the power voltage is too low for it to run reliably at the speed of the clock.

For example, the attiny2313 can run as fast at 20MHz but only if the powervoltage is between 4.5V and 5.5V. If the voltage is lower than that, it may behave erratically, erasing or overwriting the RAM and EEPROM. It may also start running random piece of the flash program. To keep it from doing that, set the brownout voltage to 4.3V, then if the voltage dips, the chip will turn off until the voltage returns. It will then reset and start over.

If the chip is meant to run at 5V, set the brown-out to 4.3V. If the chip can run as low as 3.3V you can set the brown-out to 1.8V. If the chip is a 'low voltage compatible' chip such as the attiny2313V (which can run as low as 1.8V if its clocked at 4MHz or less) then you can set the brownout to 1.8V. You can read the speed and voltage grades on the front page of the datasheet.

By default, chips that come from the factory don't have brown-out detect however I suggest you set it if you can. If you have a bootloader or are storing data in the EEPROM you must set the BOD!