Tecnología

June 8, 2009

MY FIRST VENTURE WITH ATMEGA16 VIA LINUX

After a long struggle with never ending commands and long lists of packages, not forgetting some tedious circuitry, we finally managed to blink an LED using an Atmel ATMEGA16 microcontroller. I’ll explain what we did in some simple steps.
1. Building a programmer (hardware) to program the Microcontroller( MuC )

2. Installation of necessary packages, for compiling and burning the MuC.

3. Burning the MuC

4. Sample program

All the installation and applications are  based on GNU Linux Debian / Ubuntu . Please bear with it.

I. Building a Programmer.
We need a hardware circuit to interface the MuC with a PC via its parallel port so that we can transfer our program from the latter to the former. We have used a circuit compatible with Uisp programming application. The circuit is available here.

atmega16-programmer
The hardware on the bottom right hand side is that for supplying Vcc.
Pin 9 is RESET (active low) and so it is always kept HIGH by connecting it to Vcc via 10k resistor as shown.
Make sure that the GND of Parallel port and that of Power supply are shorted.
Q1 is 4MHz crystal for clock.

II. Packages
Several packages/libraries need to be installed on linux for compiling our program(specifically for MuC) as well as uploading it onto the microcontroller’s (flash) memory using the programmer we discussed above. They are build-essential, avr-libc, gcc-avr, binutils, uisp. To install these packages, type as root user :

#apt-get install  build-essential avr-libc  gcc-avr  binutils uisp

You may skip rest of this section if the above command worked fine!!!

If any error creeps up showing invalid repositories or something similar, we may need to update the repositories file of aptitude (apt). The new repositories to be added for Debian are:

deb http://http.us.debian.org/debian testing main
deb http://www.uk.debian.org/debian stable main contrib non-free

These are to be added/written to the file sources.list in the directory.

/etc/apt/sources.list

( Check whether the current repositories of your apt have the packages  or not, even if your distro is/is not Debian/ Ubuntu. And if needed you may have to find out the necessary ones from the internet )

With our new repositories in place, we need to update our apt with this command:

#apt-get update

If you end up with an error indicating wrong key or something like below

Screenshot:

Reading package lists… Done
W: There is no public key available for the following key IDs:
9AA38DCD55BE302B

try the following commands :

#gpg –keyserver pgpkeys.mit.edu –recv-key your-key

/* in this particular case, ‘your key’ is 9AA38DCD55BE302B */

#apt-key add /root/.gnupg/pubring.gpg

The second command should give a result OK. Else try this one also.

#gpg –export your key | apt-key add –

A possible screen-shot:

#gpg –keyserver pgpkeys.mit.edu –recv-key 9AA38DCD55BE302B

gpg: requesting key 55BE302B from hkp server pgpkeys.mit.edu
gpg: key 55BE302B: public key “Debian Archive Automatic Signing Key (5.0/lenny) <ftpmaster@debian.org>” imported
gpg: no ultimately trusted keys found
gpg: Total number processed: 1
gpg: imported: 1 (RSA: 1)

# apt-key add /root/.gnupg/pubring.gpg
OK

So we are done with updating apt-get. Now the next step is installing the packages. Retype the first command of this section.

The installation part is done!

III. Burning the MuC
Several commands are listed below, which will compile a file ‘a.c’ for running on a MuC, create ‘a.hex’ and burn it onto the MuC. They are as follows

To compile and make a.out :
#avr-gcc -mmcu=atmega16 -Os a.c

To convert it into a.hex
#avr-objcopy -j .text -j .data -O ihex a.out a.hex

To detect MuC

#uisp -dprog=dapa .

To erase any existing program:
#uisp -dprog=dapa -dlpt=0x378 –erase

To load it into MuC
#uisp -dprog=dapa -dlpt=0x378 –upload –verify if=a.hex

Screenshot:
#uisp -dprog=dapa
Atmel AVR ATmega16 is found

#uisp -dprog=dapa -dlpt=0x378 –erase
Atmel AVR ATmega16 is found.
Erasing device …
Reinitializing device
Atmel AVR ATmega16 is found.

# uisp -dprog=dapa -dlpt=0x378 –upload –verify if=a.hex
Atmel AVR ATmega16 is found.
Uploading: flash
Verifying: flash

IV.Sample Program

Here is a sample program for testing the MuC, The LED to be blinked has to be connected as per the diagram given above, on the 19th PIN. Remember, this is just a sample program. So don’t jump into conclusions straight away if you are new to programming a MuC

#include <avr/io.h>
#include<util/delay.h>
#define LED PD5

int main(void)
{
DDRD=(1<<LED);
/ * enable PD5 as output */

while (1) {
PORTD=(1<<LED); /* LED ON */
_delay_ms(100);
PORTD=(0<<LED); /* LED OFF */
_delay_ms(100);
}
return 0;
}

Note : It is two separate ‘-‘ characters where it appears to be one long ‘-‘ eg: ‘–erase’ is actually – – erase without spaces. ( wordpress magic it seems! )

Advertisements

Create a free website or blog at WordPress.com.