Thursday, June 07, 2007

Starting with AVR microcontrollers: Code to burning

Microcontrollers are widely used in the electronics design arena, whatever may be the application we have. With the continuing competition among the microcontroller devices, more and more software and utilities are coming in to picture. Microcontrollers have drastically changed the design approach of an electronics engineer. In today’s world, even a so called complex design can be realized with minimum time, effort and resources. Microcontrollers are loaded with all resources in the form of on chip peripherals, both analog and digital; utilizing them effectively is the only design aspect that a designer has to do.

I have been using 8051 family of microcontrollers starting from 2002. There had been lost of effort, money and search I put to prototype one design using a microcontroller, finding the intended microcontroller in market, more than that getting the programmer, selecting suitable easy development environment etc. I remember when I bought an 8051 microcontroller programmer for 2400/- rupees. Things has been changed drastically nowadays, in the sense, resources are available right at our finger tips with less cost and full documentation. It was in May '06 that I seriously searched and thought about AVR microcontrollers. They really amazing in the sense, blessed with plenty of on chip functions, freely available development/ debug tools, plenty of articles and sample projects etc. A simple example for AVR's power is, most of them come with onchip 10 bit ADCs. They really help you to design your system fast and aviod much of the integration work, so that you can concentarte on the actual design problem. They have In System Programming (ISP) capability too, that really help during the initial stages of development of a design, where you will download the code to the flash many times. Else we have to take the chip out of the circuit and put in an external programmer.

I have woked with ATMEGA8515 and ATMEGA32 AVR microcontrollers. My main design was an Automatic Incubator using ATMEGA32, that used many on chip facilities of AVR. Out of them, the on chip I2C facility was the important one that I utilized much. It helped me to speed up interfacing of thermometer chips with the AVR. The negative side of the game is the device cost. But I think that can be balanced if we look at the utility of the device. If you are going for a uC+ADC design, compared it with a single AVR device with built in ADC.


1. Development Environment:

WinAVR I use C programming with WinAVR for code development. WinAVR contains different tools for code development and debug. Programmers notepad is an easy to use coding environment and it is a continuesly upgrading free software. C compiler used by WinAVR is avr-gcc. It has built in definitions and macros for onchip peripherals and their access. This ease the code development very much, we need to concentrate only on the high level implementation. Currently I am using WinAVR 20070525 release. Latest WinAVR release can be downloaded at Lets go through the code development steps.

I. Download WinAVR to your HDD and install it to a suitable location
II. For every project you develop using WinAVR, you need to keep a Makefile along with it, which will specify or hold the details about the project like, device used, name of C file, frequency of operation etc. It is better to keep the Makefile in the directory where your C code resides. Some of the important fields are

# MCU name
MCU = atmega8535
#Proseccor frequency
F_CPU = 12000000
# Output format. (can be srec, ihex, binary)
FORMAT = ihex
# Target file name (without extension).
TARGET = nokia_3310_lcd
# List C source files here. (C dependencies are automatically generated.)

III. write your C program using Programmers notepad and compile it Use Tools -> Makeall
IV. Output window will show the warning and errors, if any. else it will show that *.hex file has been generated successfully. This file is in intel hex format and this is to be downloaded to avr flash.

2. Programmer/ Programmer software:

Initially I started with PonyProg which used parallel for ISP. Look at for AVR ISP (STK200/300) parallel port interface[ If you plan one, download atleast 2.06C version of Pony programmer software] It is very easy to build with fewer components and easy to use too. It comes with its own programming software. I was really satisfied with it, but I found problems with parallel port when I used in other PCs. It requires some parallel port settings to work it properly, that I couldn't summarize till now. So I thought of looking for an ISP USB programmer for AVR. I could find one design from . Thought of trying once, and believe me or not it took less time to develop this programmer compared to my first parallel port programmer. I used PonyProg to program the ATMEGA8 present in the USB programmer. USB driver and README files are available to download at the above mentioned webpage.

I have incorporated one 28 pin IC socket and one 40pin ZIF socket with a common crystal for both. The above figure shows only 28 pin socket. I have also provided a 6pin male connector on the board which can be utilized to use the programmer for ISP purpose. I didn't provide any separate power supply for the board, else I am directly powering from USB. I couldn't find 68 ohm resistor in my component box so I used two 150 ohm resistors in parallel to form 75ohms. Also, I used 3.3V zener instead of 3.8V zener as prescribed in the above webpage. Other than these connections that are intended for USB, all others are straight forward. Going forward, I am planning to use USB code to utilize for my future projects to give PC access to the microcontroller system through USB. So we can configure settings, transfer data etc between PC and the design board. My practical Programmer circuit is shown in the first figure.

To download the hex file to flash, go to the folder where avrdude is available[…/WinAVR/bin/], run avrdude -c usbasp -p atmega8 -U flash:w:main.hex

3. AVR devices are available at,

Universal Electronics
#4, Mandi Veerappa Lane
Silver Jubilee Park Road
Parallel to Sardar Patrapa(S.P) Road
Bangalore 560 002PH:080-22238686,080-41140023

4. Sample project would suggest to use AVR for your newer designs. Surely that will help you much all through out your design phases.

I am happy to share and discuss designs with you if you like to do... Happy designing!!!


suryakant said...

sir ,
I am pretty beginner in this field can u plz clarify some of my doubts

(1) that what coding of atmega 8 (used in programmer) is to be done?????

(2)If this programmer can be used for atmega 16

(3)What are these JP1 and JP2 etc are??

plz answer ASAP.

Louie said...

Hi, can you please help me. I am doing a final year project using Atmega32 avr microcontroller. But i need a chip programming hardware. Which can flash the microcontroller. So please can you tell me where can i get a cheap flash programming board for Atmega32. Since STK500 is too costly and its out of our project budget.
Since i saw your "USB programmer for Atmel AVR" its too cool and cheap. Where can i get that ready made.

nkz1984 said...


here are the answers,

1) I hope you are asking about what is the code that we need to put inside atmega8.. You can download usbasp.2009-02-28.tar.gz file from and then extract (If you are a windows user, you can extract using 7zip - it is a freeware). You have to use usbasp.2009-02-28\bin\firmware\usbasp.atmega8.2009-02-28.hex file.. And sure you have to download this code with another programmer

NB: This read has full details

2) Yes this can be used for atmega16

3) From above readme,
Setting jumpers:
J1 Power target
Supply target with 5V (USB voltage). Be careful with this option, the
circuit isn't protected against short circuit!
J2 Jumper for firmware upgrade (not self-upgradable)
Set this jumper for flashing the ATMega(4)8 of USBasp with another working
J3 SCK option
If the target clock is lower than 1,5 MHz, you have to set this jumper.
Then SCK is scaled down from 375 kHz to about 8 kHz.

nkz1984 said...


I don't know if it is available ready made. But it is easy to make. If you have any doubts in making it, please ask me