LDP/LDP/howto/docbook/Avr-Microcontrollers-in-Lin.../Avr-Microcontrollers-in-Lin...

41 lines
16 KiB
XML

<?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"><article id="Avr-Microcontrollers-in-Linux-Howto"><articleinfo><title>Avr-Microcontrollers-in-Linux-Howto</title><revhistory id="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</authorinitials></revision><revision><revnumber>19</revnumber><date>2009-03-17 16:18:33</date><authorinitials>ranjeeth</authorinitials></revision><revision><revnumber>18</revnumber><date>2009-03-16 19:10:30</date><authorinitials>RickMoen</authorinitials><revremark>Fix hyperlink, fix English diction of new sentence.</revremark></revision><revision><revnumber>17</revnumber><date>2009-03-16 18:50:19</date><authorinitials>ranjeeth</authorinitials></revision><revision><revnumber>16</revnumber><date>2009-03-16 11:36:49</date><authorinitials>RickMoen</authorinitials><revremark>A couple of punctuation nits</revremark></revision><revision><revnumber>15</revnumber><date>2009-03-16 11:21:14</date><authorinitials>RickMoen</authorinitials><revremark>Corrected numerous run-on sentences, punctuation and grammar errors, and questionable idiom.</revremark></revision><revision><revnumber>14</revnumber><date>2009-03-15 10:27:24</date><authorinitials>BordenRhodes</authorinitials><revremark>Cleaned up basic grammar and spelling</revremark></revision><revision><revnumber>13</revnumber><date>2009-03-15 09:52:41</date><authorinitials>jdd</authorinitials></revision><revision><revnumber>12</revnumber><date>2009-03-15 09:51:37</date><authorinitials>jdd</authorinitials><revremark>add link to the manual of avr-libc</revremark></revision><revision><revnumber>11</revnumber><date>2009-03-14 21:47:18</date><authorinitials>jdd</authorinitials><revremark>end of basic conversion</revremark></revision><revision><revnumber>10</revnumber><date>2009-03-14 21:40:18</date><authorinitials>jdd</authorinitials></revision><revision><revnumber>9</revnumber><date>2009-03-14 21:39:26</date><authorinitials>jdd</authorinitials></revision><revision><revnumber>8</revnumber><date>2009-03-14 21:33:04</date><authorinitials>jdd</authorinitials><revremark>add the image</revremark></revision><revision><revnumber>7</revnumber><date>2009-03-14 21:27:59</date><authorinitials>jdd</authorinitials></revision><revision><revnumber>6</revnumber><date>2009-03-14 21:26:48</date><authorinitials>jdd</authorinitials></revision><revision><revnumber>5</revnumber><date>2009-03-14 21:25:13</date><authorinitials>jdd</authorinitials><revremark>Firts step - conversion fro html by jdd and basic format edition</revremark></revision><revision><revnumber>4</revnumber><date>2009-03-14 21:22:37</date><authorinitials>jdd</authorinitials></revision><revision><revnumber>3</revnumber><date>2009-03-14 21:18:25</date><authorinitials>jdd</authorinitials></revision><revision><revnumber>2</revnumber><date>2009-03-14 21:13:09</date><authorinitials>jdd</authorinitials></revision><revision><revnumber>1</revnumber><date>2009-03-14 21:10:33</date><authorinitials>jdd</authorinitials></revision></revhistory></articleinfo><section><title>HOWTO</title><para><emphasis role='strong'>AVR Microcontrollers in Linux HOWTO</emphasis>, Copyright (C) 2009 Ranjeeth p t (ranjeeth_gecmail[at]yahoo[dot]com) </para><para>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. </para></section><section><title>AVR Microcontrollers in Linux HOWTO</title><section><title>Licence</title><screen><![CDATA[ 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".]]></screen><para><ulink url='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><emphasis role='strong'>binutils:</emphasis> Tools like the assembler, linker, etc. </para><para><emphasis role='strong'>gcc-avr:</emphasis> The GNU C compiler (cross-compiler for avr). </para><para><emphasis role='strong'>avr-libc:</emphasis> Package for the AVR C library, containing many utility functions. </para><para><emphasis role='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><emphasis role='strong'>at90s Type Devices</emphasis> </para><para>at90s2313, at90s2323, at90s2333, at90s2343, at90s4414, at90s4433, at90s4434, at90s8515, at90s8515, at90s8515, at90s8535, at90s1200. </para><para><emphasis role='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><emphasis role='strong'>attiny Type Devices</emphasis> </para><para>attiny22, attiny26, attiny26, attiny13, attiny13, attiny13, attiny13, attiny2313, attiny11, attiny12, attiny15, attiny28. </para><para><emphasis role='strong'>Other AVR Devices</emphasis> </para><para>avr2, at90c8534, at86rf401, avr3, at43usb320, at43usb355, at76c711, avr4, avr5, at90can128, at94k, avr1. </para><para><emphasis role='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><emphasis role='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><emphasis role='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><emphasis role='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 <emphasis role='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 <ulink url='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 */
#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 */
}
}]]></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
:1000100024C023C022C021C020C01FC01EC01DC0DC
:100020001CC01BC01AC011241FBECFE5D4E0DEBF28
:10003000CDBF10E0A0E6B0E0EAE8F0E002C0059035
:100040000D92A036B107D9F710E0A0E6B0E001C0EC
:100050001D92A036B107E1F70CC0D2CF282FE4ECF7
:10006000F9E004C0CF010197F1F721502223D1F725
:100070000895CFE5D4E0DEBFCDBFA29AAA9884E66A
:0A008000EDDFAA9A84E6EADFF9CF6B
: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><imagedata fileref='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 &amp;<!--RAW HTML: &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. </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><emphasis role='underline'>dprog</emphasis></emphasis> is the programming method specifier, which in this case is <emphasis>dapa</emphasis>, i.e., Direct AVR Parallel Access. <emphasis><emphasis role='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: <ulink url='mailto:ranjeeth_gecmail[at]yahoo[dot]com'/> </para><para><ulink url='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 <ulink url='http://wiki.tldp.org/Avr-Microcontrollers-in-Linux-Howto'>on the LDP wiki</ulink>. </para></section></section></article>