Getting an AVR to blink might seem like an incredibly difficult task compared to the usual Arduino blink, but it really isn’t! In this post we will be uploading a basic blink example to an ATtiny2313. This is perfect for projects where using an Arduino would be over the top. So let’s get started!
You will need:
We will be working on a Mac for this example. There are options available for Windows and Linux as well, the gist is the same as described in this post.
Get your ATtiny2313 dev board kit and let’s open it up!
The ATtiny2313 is the main chip in this kit. It is worthwhile to flip through the datasheet and get an overview of how it works. Look at the datasheet here.
Solder in all of the pieces. Be sure to match up the DIP socket with the notch.
Time to add in an LED and a resistor. For this example, we are going to go with pin PB3. This pin is special because it has a 16 bit timer! This makes it quite delightful to use for fading.
Although the pin numbers are “weird” on this board, there are many advantages to this that you will find as you make more projects with AVRs.
Time for the software part! There is a very handy package available that installs everything together called Crosspack. Go and download the latest one here and install it.
To get the code from the computer onto the chip, the procedure is to compile and then upload the code. We can handle this using a makefile. With a short terminal command, it will do everything that we detail it to do. Here is the makefile we used for this project.
The main parts of the makefile that have to change (if you were using a different chip or programmer) are the device, clock, programmer, objects, and fuses.
MAKE SURE THE FUSES ARE CORRECT! The values in the makefile we are using are set for the ATtiny2313. You can actually brick your avr if you mess up the fuses. This is one thing that Arduino protects you from. However if you exercise diligence with regards to the fuses, it should not be an issue. Check out the AVR fuse calculator here.
If you are using a different programmer from the USBTinyISP, then you will have to change the PROGRAMMER variable.
Now let’s test the makefile by opening Terminal (
/Applications/Utilities/Terminal.app) and entering the command
make all. You should see text as seen in the screenshot below. If you do not, be sure to fix this before continuing by checking the installation of Crosspack.
Finally for the interesting part, writing the code to be uploaded to the chip! You can look at our code here.
_BV() is an avr macro to do a left bitshift. To turn on and off the LED, we set
PORTB to 8 or 0 respectively. The
delay_ms function is from here, and the
17500 number comes from the computational time that it takes the loop to run. Cool eh!
make all command into Terminal again since the code has changed since we last compiled. Be sure to fix any errors before proceeding. Plug in the USBtinyISP, and now lets upload the code! Enter the command
make install. You should see some long text appear. The below screenshot shows the end part of the code.
If all was good, you now have a blinking LED using an avr! Totally cool, congrats! If something is not working, go back and check the makefile to see that it is okay.
Now challenge yourself to go further and play around more! There is a most excellent list of resources available here. What will YOU make with your avr? Let us know on Google+ and Twitter, or come show it off on the Robot Party! Happy hacking!