Stable version to be found:

And other formats as well (may be) on the Stable HOWTOs page

If necessary use the Discussion page to give comments.

Discussion Page Content if any

HOWTO

Avr Microcontrollers in Linux Howto HOWTO, copyright (c) 2009 Ranjeeth p t

This Howto is for readers who want to program an avr microcontrollers using a Gnu/Linux machine.For burning your code we will be using the parallel port, you should require other electronics components(like a few resistors,capacitors,parallel port connector etc..)which can be bought from any electronics shops.

Beginning

Licence

      Permission is granted to copy, distribute and/or modify this
      document under the terms of the GNU Free Documentation License,
      Version 1.2 or any later version published by the Free Software
      Foundation; with no Invariant Sections, no Front-Cover Texts and
      no Back-Cover Texts.  A copy of the license is included in the
      section entitled "GNU Free Documentation License".

GNU Free Documentation License

HOWTO Content

What is a Microcontroller

Microcontrollers are "Single-Chip Computers". It has an internal RAM, ROM, timers, counters, interrupt circuitry, I/O ports,Analog Comparators, Serial USART's, Analog to Digital Converters, Watchdog timers etc... And its have a RISC architecture. When you are using a microprocessor, you cant program it alone?. You require additional components like ram,rom,timers etc.. and for programming you should have a deep knowledge of its architecture.You must read the datasheet of the respective microcontrollers

Software Required

binutils. This is a package for tools like the assembler, linker etc.

gcc-avr. The GNU C compiler (cross compiler for avr).

avr-libc. Package for the AVR C library containing many utility functions.

uisp. It's a Micro In-System Programmer for Atmel's AVR MCUs.(For Burning code to MCUs memory)

The Following Atmel's Microcontrollers are supported by avr-gcc in Linux

at90s Type Devices

at90s2313, at90s2323, at90s2333, at90s2343, at90s4414, at90s4433, at90s4434, at90s8515, at90s8515, at90s8515, at90s8535, at90s1200.

atmega Type Devices

atmega103, atmega603, atmega8, atmega48, atmega88, atmega8515, atmega8535, atmega16, atmega161, atmega162, atmega163, atmega165, atmega168, atmega169, atmega32, atmega323, atmega325, atmega3250, atmega64, atmega645, atmega6450, atmega128.

attiny Type Devices

attiny22, attiny26, attiny26, attiny13, attiny13, attiny13, attiny13, attiny2313, attiny11, attiny12, attiny15, attiny28.

Other Avr Devices

avr2, at90c8534, , at86rf401, avr3, , at43usb320, at43usb355, at76c711, avr4, avr5,, at90can128, at94k, avr1.

binutils : The programs in this package are used to manipulate binary and object files that may have been created for Atmel's AVR architecture. This package is primarily for AVR developers and cross-compilers

gcc-avr : This is the GNU C compiler, a fairly portable optimizing compiler which supports multiple languages. This package includes support for C.

avr-libc : Standard library used to the development of C programs for the Atmel AVR micro controllers. This package contains static libraries as well as the header files needed.

uisp : This utility is required to program AVR chips with object code created by the gcc-avr. It supports in-system programming

Hello World

We are writing hello world for atmega8 microcontroller. Which has a 28 pin,8-bit,risc architecture.

Before going to this to have a look on this manual about avr-libc which will help you for better programing and understanding.

Here is our first program:

/* ledblink.c, an led blinking program */
#include<avr/io.h>
#include<util/delay.h>
void sleep(uint8_t millisec)
{
        while(millisec)

        {
                _delay_ms(1);/* 1ms delay */
                millisec--;
        }
}
main()
{

        DDRC |=1<<PC2;  /* PC2 will be now output pin */
        while(1)
        {
                PORTC &= ~(1<<PC2);/* PC2 LOW */
                sleep(100);/* 100ms delay */

                PORTC |=(1<<PC2); /* PC2 HIGH */
                sleep(100);/* 100ms delay */
        }
}

=== Code Explanation ===

The GNU C compiler for the Atmel family identifies all the  functional units within the microcontroller by meaningful names.Thus, writing `PORTC=0xff' will result in the compiler generating machine code which writes 0xff to I/O Port C - the effect of which will be setting all the pins of Port C to logic high. Because ports are bidirectional, we have to decide whether each pin should act as input or output.If the i'th bit of a register called DDRC (data direction register C) is 1, then the i'th pin of PORTC will be an output, otherwise it will act as an input pin (note that pin and bit numbers start at zero).
To blink an led you have to make a pin high then low(Here we have used PORTC's 2^nd^ port i.e PC2 it will be the 25^th^ pin.) and there should be a delay between the two.This is what the rest of the code.
For delay we are using an in-buid function ''_delay_ms(1) ''which results in a one ms delay.

=== Compilation ===

avr-gcc -mmcu=atmega8 Os ledblink.c o ledblink.o

which will result in an object file ledblink.o.Now we will covert it to hex file for burning the code.

avr-objcopy -j .text -j .data -O ihex  ledblink.o  ledblink.hex

We are coverting it to hex file because for burning the code to atmega8 we will use uisp and the input file for usip should be .hex file.

Know you can ''less ''the ledblink.hex file.

:1000000012C02BC02AC029C028C027C026C025C0C6
:1000100024C023C022C021C020C01FC01EC01DC0DC
:100020001CC01BC01AC011241FBECFE5D4E0DEBF28
:10003000CDBF10E0A0E6B0E0EAE8F0E002C0059035
:100040000D92A036B107D9F710E0A0E6B0E001C0EC
:100050001D92A036B107E1F70CC0D2CF282FE4ECF7
:10006000F9E004C0CF010197F1F721502223D1F725
:100070000895CFE5D4E0DEBFCDBFA29AAA9884E66A

:0A008000EDDFAA9A84E6EADFF9CF6B
:00000001FF

=== Burning The Code ===

==== Hardware ====

We will be using the parallel port for burning.First we have to develop a burning circuit for the same
.
avr_html_m7e400112.png

This is the circuit for atmega8 microcontroller.Pin9 & pin10 is connected with 4MHZ crystal oscillator which is the external clock.The bottom right is for parallel port.

If you are using any other microcontroller as mentioned above,you should change accordingly.

You should watch for RESET,XTAL1,XTAL2,SCK,MISO,MOSI pins , and connect.

==== Software ====

Now we will burn the ledblink.hex file to the microcontroller.

uisp -dprog=dapa -dlpt=0x378
You should get a message Atmega8 Found.

''__dprog__'' is the programming method which is ''dapa'' i.e Direct Avr Parallel Acess and ''__dlpt__'' is for parallel device setting which is 0x378, the parallel port device address.

uisp -dprog=dapa -dlpt=0x378 --erase
Will erase the code in the microcontroller.

uisp -dprog=dapa -dlpt=0x378 --upload if=ledblink.hex
Will ''upload'' the ''I''nput ''F''ile ledblink.hex

Know you can see the led at pin25 blinking.


Author

Comments to:

ranjeethpt.wordpress.com Govt Engg College Sreekrishnapuram,Palakkad.