Programming the ATtiny2313 Using the USBTinyISP AVR Programmer

September 29, 2010 by · Leave a Comment 

In this posting I will show you how to program ATMEL ATtiny2313 and ATtiny85 chips. My objectives are to build a USBTinyISP programmer, and a TV-B-Gone kit. The ATtiny2313 is for the programmer, and the ATtiny85 is for the TV-B-Gone. Unless you bought the TV-B-Gone kit from Adafruit, you don’t have to program the chip as it is already programmed. Instead, I opted to make my own programmer and assembled my own TV-B-Gone kit. Both of the chips can be bought online from Farnell, while the other parts are in abundance in Jalan Pasar. 

Here’s an impasse: I’m building a programmer, and the chips that I bought weren’t programmed, and they need to be programmed before they can be used to program another chip (insert Xzibit meme here). Thankfully, another HSKL member, klks84, already has a programmer. 

[Note: One could have argued that I can just buy the USBTinyISP and TV-B-Gone kit, and don't have to waste my time building them myself. But, in all seriousness, I don't see the economics of that. A TV-B-Gone costs USD19.50 and the USBTinyISP is USD22.00 - that's an ass-whopping USD41.50 (or RM132.80) excluding shipping. However, building them myself is way cheaper - a USBTinyISP programmer with the parts from Jalan Pasar (and the chips from Farnell) costs less than RM20 (or USD6.25). A diy TV-B-Gone kit costs about the same. So you see, it's much cheaper and more economical to build stuff the DIY way. After all, hackerspaces are all about DIY.]

Let’s get started. Before you begin, familiarize yourself with AVR programming. This is a good tutorial on avrdude. avrdude is a command-line utility that you will use to program the chips. Basically, it enables you to upload firmware (you can think of firmware as a set of instructions telling the chip what to do). avrdude is available for Windows, OSX and Linux. I am using the the OSX version, which can be downloaded here. If you’re on Windows, you can use  WinAVR. 

Next, you need a ready-made USBTinyISP programmer. You can get them from Adafruit. klks84′ DIY programmer is a single side board version (this is the one I will be making). In this tutorial, whenever I refer to the programmer, I am referring to the USBTinyISP pictured below. 

usbtinyisp programmer

 

You also need the datasheets for whatever ICs that you are going to program. For ATMEL chips, they can be found from their website

To program the ATtiny2313, you will need:

  1. 1 x 12Mhz Crystal Oscillator
  2. 2 x 22pF ceramic capacitors
To program the ATtiny85, you will need 1 8Mhz ceramic oscillator. You could use a crystal oscillator, but I have a couple of 8MHz ceramic oscillators lying around so I’m gonna use those instead (I’ll cover that in another posting). You will also need jumper cables to connect the programmer to the breadboard. Mine looks like this:
 
jumper cables
 
And finally, you need a breadboard and USB cable. Let’s start.
 

Step 1: Setting Up avrdude


Connect the programmer to your laptop using the USB cable. The red LED should light up. Otherwise, something is wrong. On the command line, type:
 
avrdude -c usbtiny -p t2313 -P usb -C ./avrdude.conf
 

If you haven’t read ladyada’s avrdude tutorial, now is the time to do so :) But anyways let me explain the command-line options. -c tells avrdude the type of programmer. In this case it’s usbtiny. -p tells the part number, in this case it’s ATtiny2313 (meaning that I am going to program an ATtiny2313 chip). -P is the programmer’s port, in this case USB, and -C is the config file (mine’s on the current directory). You should see something like this:

Testing the usbtiny

The initialization failed error is normal, as the programmer cannot find the chip. When you run the command above, the green LED will start blinking. 

Step 2: Connecting the chip to the programmer


Before connecting the chip to the programmer, make sure you know the pin assignments on the programmer and the chip. This is where the datasheets for the ATMEL chips comes in handy. On the programmer, the 6 pins on the right (see picture above) are the ones that you will connect to the chip. The pin assignments for the USBTinyISP are as follows:

1 2 

3 4

5 6

(yeah I know I’m not really following the standards here) where:

1 – MISO

2 – Vcc

3 – SCK

4 – MOSI

5 – RST

6 – GND

If you are using your own programmer, make sure you know the assignments of each pin.

For the ATtiny2313, the relevant pins are:

20 – Vcc

19 – SCK

18 – MISO

17 – MOSI

1 – RST

10 – GND

4 – XTAL2

5 – XTAL1

After you get the pin assignment right, place the chip on the breadboard. Connect the relevant pins from the programmer to the relevant pins on the chip. Basically, you will be connecting Vcc from the programmer to Vcc on the chip, MISO to MISO, SCK to SCK and so on.

Connecting the usbtiny programmer to the ATtiny2313

 

Step 3: Check if the programmer recognizes the chip


Run the same command: avrdude -c usbtiny -p t2313 -P usb -C ./avrdude.conf. You should get this:

Connecting the ATtiny2313 to the usbtinyisp

If you get an error like the one below:
 
wrong part

 

This could mean that your part number (-p option) is wrong. It is also a good idea to check the connections. To find the correct part number for the chip, run:

./avrdude -c usbtiny -p foobar -P usb -C ./avrdude.conf

avrdude will exit with an error and the list of part numbers. 

Step 4: Set the fuse bits


Before flashing the chips, the first thing that you must do is to set the fuse bits. What are fuses? You can think of fuses as a config file for the chip. And as any config file goes, they tell the chip what to use, what to set, etc etc. Here’s a tutorial on fuses

Why do we need to set the fuse, and which fuse bit do we need to set? For both the ATtiny2313 and ATtiny85, you want to to use the external oscillators instead of the internal one. IC chips requires oscillators or resonators to function at a certain ‘beat’. Without the ‘beat’, the chip won’t know how to run codes, or the code may run at undetermined rate. So, in order to use a chip (or in this case, programming it), it must have an oscillator or resonator as part of the circuit components. Go ahead, take a look at some of your circuits that contain IC chips – there’s definitely a crystal oscillators or ceramic resonators there! 

So what are we setting? We basically wants to set the chips to use an external oscillators. In this case, for the ATtiny2313, we will want to use the 12Mhz crystal oscillator, and 8MHz for the ATtiny85. How do we know what Mhz to use? From the datasheet, of course! And how do we know what values to set? You can use fuse calculator. To set the fuse bits on the ATtiny2313, I run the following command:

avrdude -C usbtiny -p t2313 -P usb -C ./avrdude.conf -U hfuse:w:0xdf:m -U -lfuse:w:0xef:m

This command sets the high fuse (hfuse) and low fuse (lfuse) to 0xdf and 0xef respectively.

Setting the fuse bits on the ATtiny2313

 

After setting the fuse, you can then program the chip. But first, you must connect the crystal. 

Step 5: Connecting the crystal to the ATtiny2313


Connecting the capacitors and crystal oscillator to the ATTiny2313 

Here’s the schematics:

Connecting the capacitors and crystal oscillator to the ATTiny2313

 

[NOTE: Compare the schematics with the connections on the breadboard. Something is obviously wrong :) ]

The crystal needs to be connected to the XTAL1 and XTAL2 pins on the chip. Again, the datasheet will tell you what the pins are. For the ATtiny2313, they are pins 4 (XTAL2) and 5 (XTAL1). Since you are using a crystal, you need 2 22pF capacitors. The capacitors act as ‘load capacitors’ to start the crystal. Otherwise, the crystal won’t work.

Always make sure that you set the fuse bits first before connecting the crystal.

Step 6: Flash the chips!


Finally, you can flash the chip. Here’s the command:

avrdude -C avrdude.conf -U flash:w:main.hex -c usbtiny -p t2313 -P usb

Flashing the ATtiny2313

main.hex comes with the USBTinyISP firmware, which you can download here.

And that’s it. You now have a programmed ATtiny2313! In the next posting I will show you how to load the TV-B-Gone firmware onto the ATtiny85 chip. 

Thanks to klks84 for loaning me his USBTinyISP programmer, and temaning me shopping in Jalan Pasar.

Reading Materials/References

 
AVR Tutorial
USBTinyISP – AVR Programmer and SPI Interface
Single Sided USBTinyISP
ATtiny2313 Datasheet
Instructables – Burning USBTinyISP firmware to an ATtiny2313
Fuse Calculator
AVR Tutorial – Fuses
 

 

Share this article

Performance Optimization WordPress Plugins by W3 EDGE

Switch to our mobile site