Writing code for your AVR Micro-controller


Ok, now that you’ve made the programmer, I am sure you will be raring to go and program the microcontroller with it.

Before you begin to do anything, check this :

1. Check the continuity(using the multimeter) of various pins of the parallel port with the cable connected.
2. Make sure none of the pins are shorted.
3. Make sure the right pins of the parallel port go to the right pins of the micro-controller.

you will need the following to proceed :

1. WinAVR :
It is software that is used to program most of the AVR micro-controllers.
You can download the latest version here.

2. Micro-controller :
This is a crucial part. While choosing a micro-controller, you must take into consideration the memory,speed and compatibility of the mcu with software. The ATMEGA series of AVR micro-controllers is pretty useful for most beginners and intermediate projects.
The ATMEGA16 or ATMEGA32 were recommended to me as being handy.I bought an ATMEGA32L and later realised that I was stuck with a “problem child”.
The chips having L at the end are almost the same,except that they have speed limitations.
The problem in my case was that ATMEGA32L was not among the list of supported devices.
I eventually found a way to overcome this, but I’ll come to that later…

Now to begin :

Once you’ve connected the microcontroller to the port using the cable and installed WinAVR :

1. You need to write the code for the program that your micro-controller will run.
For this , open “Programmers’ Notepad” in the “WinAVR” folder.

2. Type the code in programmers’ notepad. For my first project I used Bibin John’s code for blinking LEDs on all ports. You can find that and other useful stuff here.

3. Now you have to make a “makefile” which contains all the information about your micro-controller and other hardware.

In the application that opens,

you must change the following parameters :

1. Main file name : Enter the name of c file that you have made, without the .c extension
2. MCU Type : Select the microcontroller you are using.
3. Programmer : BSD
4. Port : lpt1

You need not bother with anything else, unless you are experienced enough.

Ok, now you need to save the file : save it in the same folder as your source-code and don’t change its name.

Now, you’re done with the program and just need to load it to memory.

Again, open Programmers’ Notepad and click on Tools — > Make All

If there are no problems with the code, the making should be successful and a lot of files will appear in your program folder. I haven’t faced any such errors in my program, but will try and put them here as soon as I encounter them.

Ok, now you need to load the finished program to the mcu. Just turn on the external power supply to the programmer and click on Tools — > Make program.

If everything’s perfect, the compiler will take some 2 or 3 seconds to flash the program to chip and will get a “Process Exit Code: 0 ” as the output.

However, in quite a few cases you will get an error with different messages . Here are the ones I know about and their causes :

1. Connection problems :

avrdude: AVR device not responding
avrdude: initialization failed, rc=-1
Double check connections and try again, or use -F to override
this check.
avrdude done. Thank you.
make.exe: *** [program] Error 1

Here, they mean exactly what they say. Check your connections. Also check that the external power is turned on.

2. Port access :

I don’t remember the exact error code for this one, but the problem here is that Windows XP often blocks access to ports that will prevent your PC from communicating with the micro-controller.To overcome this problem, run “install_giveio.bat” in your WinAVR directory.

3. Device Mismatch

This was the most problematic error for me, and I spent two days racking my brains over it.
This error is caused when you try to use a micro-controller other than the one that you have mentioned in the makefile.

However, in my case, I had no option because the ATMEGA32L I was using was not in the WinAVR list of supported devices. I tried downloading some 5 additional software which might be compatible with my mcu, but that was not to be. None worked. I was stuck with the chip and had no option but to somehow get it to work.
Then it hit me. I figured that since the ATMEGA32 and the ATMEGA32L were the same except for their processing speeds (and some other features I was not aware of 😉 ) , the program should work fine.
So, I decide to use the ‘-F’ switch that allows me to over-ride the device signature error.
Finding how to focibly flash it was a bit difficult, but I finally found it.
Here’s how to go about it :

1. Copy all the files in your source code folder to the ‘bin’ directory where WinAVR is installed.
2. Run DOS prompt and enter the bin directory again in DOS.
3. Now, when you try to click on “Make Program” in WinAVR and get the error, check the output window. In the very beginning there will be a line like this :

avrdude -p atmega32 -P lpt1 -c bsd -U flash:w:blink.hex

When you type this in DOS and press enter you will get the same signature error.
4. Now, to bypass the error, just add -F at the end of the command, like this :

avrdude -p atmega32 -P lpt1 -c bsd -U flash:w:blink.hex -F

Here, the computer will encounter the error, but will ignore it and continue flashing it. This should do the trick. You will get a success message at the end.


I have tried to make this as easy and comprehensive as possible, but suggestions are most welcome.

I will try putting in more errors and solutions if and when I encounter them.