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

4 Comments »

  1. good description…
    really helped.

    Comment by nk.ramakrishnan — July 4, 2009 @ 6:56 pm

  2. Hi! The AVCC must be connected to VCC.

    Comment by killbill — July 24, 2009 @ 3:11 pm

  3. hey there and thank you for your info – I’ve definitely picked up something new from right here. I did however expertise a few technical points using this website, since I experienced to reload the website many times previous to I could get it to load correctly. I had been wondering if your web host is OK? Not that I am complaining, but slow loading instances times will sometimes affect your placement in google and can damage your quality score if advertising and marketing with Adwords. Well I am adding this RSS to my e-mail and could look out for much more of your respective intriguing content. Make sure you update this again very soon.

    Comment by tecnologia — February 22, 2013 @ 4:52 am

  4. Vale, no estoy рlenamente deacuerdo con la forma ԁe decirlo, sin embargo est correсto el fondo.Un saludo!

    Comment by Consuelo — December 20, 2013 @ 10:44 am


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: