<?xml version='1.0' encoding='UTF-8'?><!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN" "http://www.docbook.org/xml/4.2/docbookx.dtd"><articleid="Avr-Microcontrollers-in-Linux-Howto"><articleinfo><title>Avr-Microcontrollers-in-Linux-Howto</title><revhistoryid="revhistory"><revision><revnumber>44</revnumber><date>2009-04-20 17:13:07</date><authorinitials>jdd</authorinitials><revremark>obfuscate e-mail at the author demand</revremark></revision><revision><revnumber>43</revnumber><date>2009-03-29 20:49:12</date><authorinitials>ranjeeth</authorinitials></revision><revision><revnumber>42</revnumber><date>2009-03-29 20:45:09</date><authorinitials>jdd</authorinitials><revremark>reverting after publication</revremark></revision><revision><revnumber>41</revnumber><date>2009-03-29 20:41:59</date><authorinitials>jdd</authorinitials><revremark>edit to export docbook</revremark></revision><revision><revnumber>40</revnumber><date>2009-03-28 21:07:57</date><authorinitials>RickMoen</authorinitials><revremark>Adjust style of mailto link to author's original preference.</revremark></revision><revision><revnumber>39</revnumber><date>2009-03-28 21:05:45</date><authorinitials>RickMoen</authorinitials><revremark>Revert e-mail obfuscation, supply some missing punctuation, [re-]fix capitalisation, fix and clarify new run-on sentence.</revremark></revision><revision><revnumber>38</revnumber><date>2009-03-23 18:21:24</date><authorinitials>ranjeeth</authorinitials></revision><revision><revnumber>37</revnumber><date>2009-03-23 18:19:51</date><authorinitials>ranjeeth</authorinitials></revision><revision><revnumber>36</revnumber><date>2009-03-23 17:20:32</date><authorinitials>ranjeeth</authorinitials></revision><revision><revnumber>35</revnumber><date>2009-03-23 17:19:47</date><authorinitials>ranjeeth</authorinitials></revision><revision><revnumber>34</revnumber><date>2009-03-23 10:26:00</date><authorinitials>jdd</authorinitials><revremark>publishing tests</revremark></revision><revision><revnumber>33</revnumber><date>2009-03-23 10:25:06</date><authorinitials>jdd</authorinitials></revision><revision><revnumber>32</revnumber><date>2009-03-23 10:24:24</date><authorinitials>jdd</authorinitials><revremark>use of macro to obfuscate e-mail</revremark></revision><revision><revnumber>31</revnumber><date>2009-03-23 10:11:47</date><authorinitials>jdd</authorinitials><revremark>change was only to export docbook without admonitions</revremark></revision><revision><revnumber>30</revnumber><date>2009-03-23 10:09:15</date><authorinitials>jdd</authorinitials></revision><revision><revnumber>29</revnumber><date>2009-03-23 10:05:07</date><authorinitials>jdd</authorinitials></revision><revision><revnumber>28</revnumber><date>2009-03-23 09:58:40</date><authorinitials>jdd</authorinitials><revremark>adding the wiki as link</revremark></revision><revision><revnumber>27</revnumber><date>2009-03-23 09:53:28</date><authorinitials>RickMoen</authorinitials><revremark>Insert needed space characters into "pin9", "pin10", "pin25" constructs</revremark></revision><revision><revnumber>26</revnumber><date>2009-03-23 09:49:05</date><authorinitials>RickMoen</authorinitials><revremark>Make markup of all the software items mentioned be consistent</revremark></revision><revision><revnumber>25</revnumber><date>2009-03-23 09:43:40</date><authorinitials>RickMoen</authorinitials><revremark>Remove a couple of stray commas.</revremark></revision><revision><revnumber>24</revnumber><date>2009-03-23 09:41:30</date><authorinitials>RickMoen</authorinitials><revremark>Polishing up a few last bits of punctuation</revremark></revision><revision><revnumber>23</revnumber><date>2009-03-23 04:11:31</date><authorinitials>ranjeeth</authorinitials></revision><revision><revnumber>22</revnumber><date>2009-03-23 04:10:34</date><authorinitials>ranjeeth</authorinitials></revision><revision><revnumber>21</revnumber><date>2009-03-23 04:09:07</date><authorinitials>ranjeeth</authorinitials></revision><revision><revnumber>20</revnumber><date>2009-03-17 16:20:27</date><authorinitials>ranjeeth</authorinitia
section entitled "GNU Free Documentation License".]]></screen><para><ulinkurl='http://wiki.tldp.org/LdpWikiDefaultLicence#GNUFreeDocumentationLicense'>GNU Free Documentation License</ulink></para></section><section><title>What Is a Microcontroller?</title><para>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. </para></section><section><title>Software Required</title><para><emphasisrole='strong'>binutils:</emphasis> Tools like the assembler, linker, etc. </para><para><emphasisrole='strong'>gcc-avr:</emphasis> The GNU C compiler (cross-compiler for avr). </para><para><emphasisrole='strong'>avr-libc:</emphasis> Package for the AVR C library, containing many utility functions. </para><para><emphasisrole='strong'>uisp:</emphasis> A Micro In-System Programmer for Atmel's AVR MCUs (for burning code to MCUs' memory). </para><para>The following Atmel microcontrollers are supported by avr-gcc in Linux: </para><para><emphasisrole='strong'>at90s Type Devices</emphasis></para><para>at90s2313, at90s2323, at90s2333, at90s2343, at90s4414, at90s4433, at90s4434, at90s8515, at90s8515, at90s8515, at90s8535, at90s1200. </para><para><emphasisrole='strong'>atmega Type Devices</emphasis></para><para>atmega103, atmega603, atmega8, atmega48, atmega88, atmega8515, atmega8535, atmega16, atmega161, atmega162, atmega163, atmega165, atmega168, atmega169, atmega32, atmega323, atmega325, atmega3250, atmega64, atmega645, atmega6450, atmega128. </para><para><emphasisrole='strong'>attiny Type Devices</emphasis></para><para>attiny22, attiny26, attiny26, attiny13, attiny13, attiny13, attiny13, attiny2313, attiny11, attiny12, attiny15, attiny28. </para><para><emphasisrole='strong'>Other AVR Devices</emphasis></para><para>avr2, at90c8534, at86rf401, avr3, at43usb320, at43usb355, at76c711, avr4, avr5, at90can128, at94k, avr1. </para><para><emphasisrole='strong'>binutils:</emphasis> 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. </para><para><emphasisrole='strong'>gcc-avr:</emphasis> The GNU C compiler, a fairly portable optimising compiler that supports multiple languages. This package includes C language support. </para><para><emphasisrole='strong'>avr-libc:</emphasis> Standard library used for developing C programs for Atmel AVR microcontrollers. This package contains static libraries, as well as needed header files. </para><para><emphasisrole='strong'>uisp:</emphasis> Utility to program AVR chips with object code created by gcc-avr. It supports in-system programming. </para><para>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. </para></section><section><title>Hello World</title><para>We are writing <emphasisrole='strong'>hello world</emphasis> for the atmega8 microcontroller, which has a 28-pin, 8-bit, RISC architecture. </para><para>Before proceeding, have a look at <ulinkurl='http://www.nongnu.org/avr-libc/user-manual/'>this manual about __avr-libc__</ulink>, which will help you program better, and understand. Also, refer to the datasheets for the various AVR microcontrollers. </para><para>Here is our first program: </para><screen><![CDATA[/* ledblink.c, an LED blinking program */
}]]></screen><section><title>Code Explanation</title><para>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<superscript>nd</superscript> port. That is, PC2 will be the 25<superscript>th</superscript> 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 <emphasis>_delay_ms(1)</emphasis>, which causes a 1 ms delay. </para></section><section><title>Compilation</title><screen><![CDATA[avr-gcc -mmcu=atmega8 Os ledblink.c o ledblink.o]]></screen><para>which will result in object file ledblink.o. Now, we will covert it to hex file, suitable for burning to the microcontroller's memory. </para><screen><![CDATA[avr-objcopy -j .text -j .data -O ihex ledblink.o ledblink.hex]]></screen><para>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. </para><para>Notice that you can <emphasis>less</emphasis> the ledblink.hex file. </para><screen><![CDATA[:1000000012C02BC02AC029C028C027C026C025C0C6
:00000001FF]]></screen></section><section><title>Burning the Code</title><section><title>Hardware</title><para>We will be using the parallel port for burning. First, we have to develop a burning circuit for it. </para><para><inlinemediaobject><imageobject><imagedatafileref='avr_html_m7e400112-2.jpg'/></imageobject><textobject><phrase>atmega8 microcontroller</phrase></textobject></inlinemediaobject></para><para>This is the circuit for the atmega8 microcontroller. Pin 9 &<!--RAW HTML: &--> pin 10 are connected by a 4 MHz crystal oscillator, which is the external clock. The bottom right connector is for a parallel port. </para><para>If you are using any other microcontroller, as mentioned above, you should change accordingly. }}} </para><para>You should watch for <emphasis>RESET,XTAL1,XTAL2,SCK,MISO,MOSI pins</emphasis>, <emphasis></emphasis>and connect.<emphasis></emphasis></para></section><section><title>Software</title><para>Now, we will burn ledblink.hex to the microcontroller. </para><screen><![CDATA[uisp -dprog=dapa -dlpt=0x378]]></screen><para>You should get message <emphasis>Atmega8 Found</emphasis>. </para><para><emphasis><emphasisrole='underline'>dprog</emphasis></emphasis> is the programming method specifier, which in this case is <emphasis>dapa</emphasis>, i.e., Direct AVR Parallel Access. <emphasis><emphasisrole='underline'>dlpt</emphasis></emphasis> is for the parallel device setting, which is 0x378, the parallel port's device address. </para><screen><![CDATA[uisp -dprog=dapa -dlpt=0x378 --erase]]></screen><para>Will erase the microcontroller's code. </para><screen><![CDATA[uisp -dprog=dapa -dlpt=0x378 --upload if=ledblink.hex]]></screen><para>Will <emphasis>upload</emphasis> the <emphasis>I</emphasis>nput <emphasis>F</emphasis>ile ledblink.hex </para><para>Notice that you can see the LED at pin 25 blinking. </para></section></section></section><section><title>Author</title><para>Comments to: <ulinkurl='mailto:ranjeeth_gecmail[at]yahoo[dot]com'/></para><para><ulinkurl='http://ranjeethpt.wordpress.com'>Ranjeeth Weblog</ulink></para><para>Govt Engg College Sreekrishnapuram, </para><para>Palakkad,Kerala India. </para></section><section><title>Last version</title><para>You may find the last up-to-date version of this HOWTO <ulinkurl='http://wiki.tldp.org/Avr-Microcontrollers-in-Linux-Howto'>on the LDP wiki</ulink>. </para></section></section></article>