Differences between revisions 1 and 40 (spanning 39 versions)
Revision 1 as of 2009-03-14 19:10:33
Size: 8277
Editor: jdd
Comment:
Revision 40 as of 2009-03-28 19:07:57
Size: 8783
Editor: RickMoen
Comment: Adjust style of mailto link to author's original preference.
Deletions are marked like this. Additions are marked like this.
Line 12: Line 12:
If necessary use the [[http://wiki.tldp.org/Avr-Microcontrollers-in-Linux-Howto?action=supplementation|Discussion page]] to give comments. If necessary, use the [[http://wiki.tldp.org/Avr-Microcontrollers-in-Linux-Howto?action=supplementation|Discussion page]] to make comments.
Line 14: Line 14:
== Discussion Page Content if any == == Discussion Page Content, if any ==
Line 19: Line 19:
XXXX HOWTO, copyright (c) 2009 john Doe '''AVR Microcontrollers in Linux HOWTO''', Copyright (C) 2009 Ranjeeth p t
[[mailto:ranjeeth_gecmail@yahoo.com|ranjeeth_gecmail@yahoo.com]]


This HOWTO is for readers wishing to program an AVR microcontroller using a GNU/Linux machine. For burning your code, we will be using a parallel port. You may need other electronics components (like a few resistors, capacitors, parallel port connector, etc.), which can be bought from any electronics shop.
Line 23: Line 27:
= Beginning = = AVR Microcontrollers in Linux HOWTO =
Line 26: Line 30:
      Permission is granted to copy, distribute and/or modify this       Permission is granted to copy, distribute, and/or modify this
Line 28: Line 32:
      Version 1.2 or any later version published by the Free Software
      Foundation; with no Invariant Sections, no Front-Cover Texts and
      Version 1.2, or any later version published by the Free Software
      Foundation; with no Invariant Sections, no Front-Cover Texts, and
Line 34: Line 38:
== HOWTO Content ==
Line 36: Line 39:
''__'''Avr Microcontrollers in Linux Howto'''__'' == What Is a Microcontroller? ==
Line 38: Line 41:
'''1.Introduction''' A microcontroller is a single-chip computer. It has internal RAM, ROM, timers, counters, interrupt circuitry, I/O ports, analog comparators, serial USARTs, analog to digital converters, watchdog timers, and a RISC architecture. When you are using a microprocessor, you cannot program it alone. You need other components, like RAM, ROM, timers, etc. For programming, you should know its architecture thoroughly: You must read the datasheet for your microcontroller.
Line 40: Line 43:
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. == Software Required ==
Line 42: Line 45:
'''1.1 What is a Microcontroller''' '''binutils:''' Tools like the assembler, linker, etc.
Line 44: Line 47:
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 '''gcc-avr:''' The GNU C compiler (cross-compiler for avr).
Line 46: Line 49:
'''2.Software Required''' '''avr-libc:''' Package for the AVR C library, containing many utility functions.
Line 48: Line 51:
binutils. This is a package for tools like the assembler, linker etc. '''uisp:''' A Micro In-System Programmer for Atmel's AVR MCUs (for burning code to MCUs' memory).
Line 50: Line 53:
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
The following Atmel microcontrollers are supported by avr-gcc in Linux:
Line 70: Line 67:
'''Other Avr Devices''' '''Other AVR Devices'''
Line 72: Line 69:
avr2, at90c8534, , at86rf401, avr3, , at43usb320, at43usb355, at76c711, avr4, avr5,, at90can128, at94k, avr1. avr2, at90c8534, at86rf401, avr3, at43usb320, at43usb355, at76c711, avr4, avr5, at90can128, at94k, avr1.
Line 74: Line 71:
'''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 '''binutils:''' Programs 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.
Line 76: Line 73:
'''gcc-avr : '''This is the GNU C compiler, a fairly portable optimizing compiler which supports multiple languages. This package includes support for C. '''gcc-avr:''' The GNU C compiler, a fairly portable optimising compiler that supports multiple languages. This package includes C language support.
Line 78: Line 75:
'''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. '''avr-libc:''' Standard library used for developing C programs for Atmel AVR microcontrollers. This package contains static libraries, as well as needed header files.
Line 80: Line 77:
'''uisp : '''This utility is required to program AVR chips with object code created by the gcc-avr. It supports in-system programming '''uisp:''' Utility to program AVR chips with object code created by gcc-avr. It supports in-system programming.
Line 82: Line 79:
'''3.Hello World''' You download the above packages untar, configure, and install it. If you are using Debian or Ubuntu, these packages are available in your distribution: Install them using apt or synaptic package manager.
Line 84: Line 81:
We are writing hello world for atmega8 microcontroller. Which has a 28 pin,8-bit,risc architecture. == Hello World ==
Line 86: Line 83:
Before going to this to have a look on this manual about __avr-libc__ which will help you for better programing and understanding. We are writing '''hello world''' for the atmega8 microcontroller, which has a 28-pin, 8-bit, RISC architecture.

Before proceeding, have a look at [[http://www.nongnu.org/avr-libc/user-manual/|this manual about __avr-libc__]], which will help you program better, and understand. Also, refer to the datasheets for the various AVR microcontrollers.
Line 91: Line 90:
/* ledblink.c, an led blinking program */
#include<avr/io.h>
#include<util/delay.h>
/* ledblink.c, an LED blinking program */
#include<avr/io.h>
#include<util/delay.h>
Line 99: Line 98:
                _delay_ms(1);/* 1ms delay */                 _delay_ms(1);/* 1 ms delay */
Line 106: Line 105:
        DDRC |=1&lt;&lt;PC2; /* PC2 will be now output pin */         DDRC |=1<<PC2; /* PC2 will now be the output pin */
Line 109: Line 108:
                PORTC &amp;= ~(1&lt;&lt;PC2);/* PC2 LOW */
                sleep(100);/* 100ms delay */
                PORTC &= ~(1<<PC2);/* PC2 LOW */
                sleep(100);/* 100 ms delay */
Line 112: Line 111:
                PORTC |=(1&lt;&lt;PC2); /* PC2 HIGH */
                sleep(100);/* 100ms delay */
                PORTC |=(1<<PC2); /* PC2 HIGH */
                sleep(100);/* 100 ms delay */
Line 116: Line 115:
}}}
Line 117: Line 117:
'''3.1Code Explanation ''' === Code Explanation ===
Line 119: Line 119:
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.
The GNU C compiler for the Atmel family identifies all functional units within the microcontroller with meaningful names.  Thus, writing `PORTC=0xff' will result in the compiler generating machine code that writes 0xff to I/O port C, which will set all port C pins to logic high. Because ports are bidirectional, we must 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's i'th pin will be an output. Otherwise, it will act as an input pin. (Note that pin and bit numbers start at zero.)
To make an LED blink, you have to make a pin high, then low. (Here, we use PORTC's 2^nd^ port. That is, PC2 will be the 25^th^ pin.) There should be a delay between the two.  This is what the rest of the code does.
For the delay, we use built-in function ''_delay_ms(1)'', which causes a 1 ms delay.
Line 123: Line 123:
'''3.2Compilation''' === Compilation ===
Line 125: Line 125:
{{{
Line 126: Line 127:
}}}
which will result in object file ledblink.o. Now, we will covert it to hex file, suitable for burning to the microcontroller's memory.
{{{
avr-objcopy -j .text -j .data -O ihex ledblink.o ledblink.hex
}}}
We are converting it to a hex file because, for burning the code to atmega8, we will use uisp, whose input file must be a .hex file.
Line 127: Line 134:
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.
Notice that you can ''less'' the ledblink.hex file.
{{{
Line 146: Line 147:
}}}
=== Burning the Code ===
Line 147: Line 150:
'''3.3 Burning The Code''' ==== Hardware ====
Line 149: Line 152:
'''3.3.1 Hardware''' We will be using the parallel port for burning. First, we have to develop a burning circuit for it.
Line 151: Line 154:
We will be using the parallel port for burning.First we have to develop a burning circuit for the same
.
avr_html_m7e400112.png
{{attachment:avr_html_m7e400112-2.jpg|atmega8 microcontroller}}
Line 155: Line 156:
This is the circuit for atmega8 microcontroller.Pin9 &amp; pin10 is connected with 4MHZ crystal oscillator which is the external clock.The bottom right is for parallel port. This is the circuit for the atmega8 microcontroller. Pin 9 &amp; pin 10 are connected by a 4 MHz crystal oscillator, which is the external clock. The bottom right connector is for a parallel port.
Line 157: Line 158:
If you are using any other microcontroller as mentioned above,you should change accordingly. If you are using any other microcontroller, as mentioned above, you should change accordingly.
Line 160: Line 161:
You should watch for ''RESET,XTAL1,XTAL2,SCK,MISO,MOSI pins '','' ''and connect.'' '' You should watch for ''RESET,XTAL1,XTAL2,SCK,MISO,MOSI pins'', '' ''and connect.'' ''
Line 162: Line 163:
<<BR>> ==== Software ====
Line 164: Line 165:
Now, we will burn ledblink.hex to the microcontroller.
Line 165: Line 167:
'''3.3.2 Software''' uisp -dprog=dapa -dlpt=0x378
}}}
You should get message ''Atmega8 Found''.
Line 167: Line 171:
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.
''__dprog__'' is the programming method specifier, which in this case is ''dapa'', i.e., Direct AVR Parallel Access. ''__dlpt__'' is for the parallel device setting, which is 0x378, the parallel port's device address.
{{{
Line 175: Line 174:
Will erase the code in the microcontroller. }}}
Will erase the microcontroller's code.
{{{
Line 178: Line 178:
}}}
Line 180: Line 181:
Know you can see the led at pin25 blinking. Notice that you can see the LED at pin 25 blinking.
Line 182: Line 183:
}}}
Line 184: Line 184:
---- == Author ==
Comments to:
 [[mailto:ranjeeth_gecmail@yahoo.com|ranjeeth_gecmail@yahoo.com]]
Line 186: Line 188:
{{{ [[http://ranjeethpt.wordpress.com|Ranjeeth Weblog]]
Line 188: Line 190:
Comments to:
 ''__ [[mailto:ranjeeth_gecmail@yahoo.com|Ranjeeth PT]] __ ''
''ranjeethpt.wordpress.com''
''Govt Engg College ''
''Sreekrishnapuram,Palakkad.''
Govt Engg College Sreekrishnapuram,
Line 194: Line 192:
}}} Palakkad India.
== Last version ==
You may find the last up-to-date version of this HOWTO [[http://wiki.tldp.org/Avr-Microcontrollers-in-Linux-Howto|on the LDP wiki]].

Stable version to be found:

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

If necessary, use the Discussion page to make comments.

Discussion Page Content, if any

HOWTO

AVR Microcontrollers in Linux HOWTO, Copyright (C) 2009 Ranjeeth p t ranjeeth_gecmail@yahoo.com

This HOWTO is for readers wishing to program an AVR microcontroller using a GNU/Linux machine. For burning your code, we will be using a parallel port. You may need other electronics components (like a few resistors, capacitors, parallel port connector, etc.), which can be bought from any electronics shop.

AVR Microcontrollers in Linux HOWTO

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

What Is a Microcontroller?

A microcontroller is a single-chip computer. It has internal RAM, ROM, timers, counters, interrupt circuitry, I/O ports, analog comparators, serial USARTs, analog to digital converters, watchdog timers, and a RISC architecture. When you are using a microprocessor, you cannot program it alone. You need other components, like RAM, ROM, timers, etc. For programming, you should know its architecture thoroughly: You must read the datasheet for your microcontroller.

Software Required

binutils: 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: A Micro In-System Programmer for Atmel's AVR MCUs (for burning code to MCUs' memory).

The following Atmel 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: Programs 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: The GNU C compiler, a fairly portable optimising compiler that supports multiple languages. This package includes C language support.

avr-libc: Standard library used for developing C programs for Atmel AVR microcontrollers. This package contains static libraries, as well as needed header files.

uisp: Utility to program AVR chips with object code created by gcc-avr. It supports in-system programming.

You download the above packages untar, configure, and install it. If you are using Debian or Ubuntu, these packages are available in your distribution: Install them using apt or synaptic package manager.

Hello World

We are writing hello world for the atmega8 microcontroller, which has a 28-pin, 8-bit, RISC architecture.

Before proceeding, have a look at this manual about __avr-libc__, which will help you program better, and understand. Also, refer to the datasheets for the various AVR microcontrollers.

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);/* 1 ms delay */
                millisec--;
        }
}
main()
{

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

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

Code Explanation

The GNU C compiler for the Atmel family identifies all functional units within the microcontroller with meaningful names. Thus, writing `PORTC=0xff' will result in the compiler generating machine code that writes 0xff to I/O port C, which will set all port C pins to logic high. Because ports are bidirectional, we must 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's i'th pin will be an output. Otherwise, it will act as an input pin. (Note that pin and bit numbers start at zero.) To make an LED blink, you have to make a pin high, then low. (Here, we use PORTC's 2nd port. That is, PC2 will be the 25th pin.) There should be a delay between the two. This is what the rest of the code does. For the delay, we use built-in function _delay_ms(1), which causes a 1 ms delay.

Compilation

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

which will result in object file ledblink.o. Now, we will covert it to hex file, suitable for burning to the microcontroller's memory.

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

We are converting it to a hex file because, for burning the code to atmega8, we will use uisp, whose input file must be a .hex file.

Notice that 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 it.

atmega8 microcontroller

This is the circuit for the atmega8 microcontroller. Pin 9 & pin 10 are connected by a 4 MHz crystal oscillator, which is the external clock. The bottom right connector is for a 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 ledblink.hex to the microcontroller.

uisp -dprog=dapa -dlpt=0x378

You should get message Atmega8 Found.

dprog is the programming method specifier, which in this case is dapa, i.e., Direct AVR Parallel Access. dlpt is for the parallel device setting, which is 0x378, the parallel port's device address.

uisp -dprog=dapa -dlpt=0x378 --erase

Will erase the microcontroller's code.

uisp -dprog=dapa -dlpt=0x378 --upload if=ledblink.hex

Will upload the Input File ledblink.hex

Notice that you can see the LED at pin 25 blinking.

Author

Comments to:

Ranjeeth Weblog

Govt Engg College Sreekrishnapuram,

Palakkad India.

Last version

You may find the last up-to-date version of this HOWTO on the LDP wiki.

Avr-Microcontrollers-in-Linux-Howto (last edited 2009-04-20 15:13:07 by jdd)