Friday, June 08, 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!!!

Post a Comment