Sunday, February 9, 2014

Programming AVR processors

I'm going to to through the process of getting the simplest possible program working on a simple AVR chip. When done, the chip will blink an LED.  From there we can move on to other stuff.

I'm going to use the Atmel ATTiny85 chip as an example, because I've used it for my last few projects.

Before you start, check out this link for equipment.  You'll want a breadboard and jumper wires, whatever chip you decide to use, and a programmer that is compatible with that chip.

Since we're using a bare chip, you'll need to wire things to it.  The best way is to use a solderless breadboard, as shown in the link above.

You'll also need some way to plug the programming plug into it. I have hand built a little 6 pin breakout board that I just hand wire into my boards, but this is a great alternative:
I found this on ebay. There are different ones for different chips. This is very handy for prototyping and saves 5 minutes of wiring every time you want to breadboard a circuit, and saves having to make a little header breakout board.  If you just use this in-circuit, the ISP header will provide +5V power for your circuit as well so you can throw together quick circuits quite fast.

You'll need a programmer - USBASP is available on ebay for almost nothing (see above link again)

You need a programming environment - WinAVR or AVR Studio (I use the latter- use studio 4.  The newer versions are a ridiculous amount of overkill unless you're developing large systems using advanced chips)  Install WinAVR first, then AVR Studio.  WinAVR contains the compiler, AVR studio gives you a nice environment to use it in.

For the first program, we're just going to cause an LED to blink with an ATTiny85 chip.  Plug it into the board above and plug that into the breadboard, or plug the chip straight in and wire in an ISP socket, whichever you want.


The LED and a 150 ohm resistor will be wired between pins 4 and 5 (ground and output 0) on the ATTiny85 as shown.

Open AVR Studio 4, create a new project called "helloworld" specifying AVR Simulator 2 and ATTiny85 as your chip.  Create "helloworld.c" and put this into it:

#define F_CPU 2000000 // 8 MHz clock

#include <avr/io.h>
#include <util/delay.h>

main()
{
// set port B bit 0 to output
DDRB = 1;
while (1)
{
PORTB = 1; // turn LED on
_delay_ms(100);
PORTB = 0;
_delay_ms(100);
}
}

Click on Build/Build.  Hopefully it will compile without errors.

Go out to the DOS prompt and switch to the directory where you saved the project.  There should now be a "helloworld.hex" file which is the compiled version of your little program.  This needs to be sent to the chip using the programmer.  With the programmer plugged into the chip, run this command from the command line:
avrdude -p t85 -c usbasp -U flash:w:helloworld.hex

avrdude should write the code to the chip, and the LED should immediately start flashing.

No comments:

New printer: JGAurora A3

This week I decided I'd had it with all the other printers in my stable.  The CTC is stable and decent but it just bugs me (can't st...