atmega-tutorial.pdf

(1014 KB) Pobierz
AnIntroductiontoprogramminganAtmega
microcontroller
BenjaminReh
July30,2012
Contents
1.Preface
3
2.Compilationandtransfer
3
2.1.
Preparation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3
2.1.1.
Basis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3
2.1.2.
First steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3
2.1.3.
Fuse-bits
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4
3.Thebasics
4
3.1.
Pins and Ports
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5
4.Firststeps
5
4.1.
Controlling a LED
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5
4.1.1.
Plugging in the LED . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5
4.1.2.
Switching it on . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
6
4.1.3.
Making the LED blink . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7
4.1.4.
Making the LED blink (advanced)
. . . . . . . . . . . . . . . . . . . . .
8
4.2.
Reading an input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
8
4.2.1.
Detecting an edge
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
9
4.2.2.
Providing a clean signal
. . . . . . . . . . . . . . . . . . . . . . . . . . .
10
5.Timer/Counter
11
5.1.
Hardware timer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
11
5.2.
Hardware timer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
11
5.3.
Servo control
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
11
5.3.1.
About servos
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
11
5.3.2.
Servos in basis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
12
5.4.
Timer for software interrupt . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
12
6.Analogue-Digital-Converter(ADC)
13
6.1.
ADC in basis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
13
1
1007749056.006.png
Version 1.1
IWR - robotics lab
7.Serialconnection(UART)
13
7.1.
UART in basis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
14
7.1.1.
Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
14
7.2.
The terminal
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
15
A.Appendix
15
A.1. Algebra with bits
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
15
A.1.1. Shifting
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
15
A.1.2. Bit-wise logic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
16
A.1.3. Setting a bit
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
16
A.1.4. Clearing a bit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
17
A.2. Pin out of the Atmega 168
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
17
B.References
18
2
1007749056.007.png
Version 1.1
IWR - robotics lab
1.Preface
This document an introduction into the programming of an Atmega microcontroller. It is
separated into the rst part guiding like a tutorial for beginners and a second part which is a
reference book to the functions provided in the basis .
The examples and explanations provided are neither exhaustive nor complete. The only aim
of this document is to lower the burden of getting started.
Only a basic knowledge in C is
required.
2.Compilationandtransfer
2.1.Preparation
Before starting to write own programs, it is advisable to rst familiarize with the tool-chain
which will be used.
Prerequisites
To compile your programs and transfer them to the microcontroller, a few tools have to be
installed. If you are working on a PC in the Lab, then you can skip this section.
First, I assume you are working with Linux, so I also assume you already have your favorite
editor. If you do not, I recommend geany , which is a light, but powerful editor using gnome.
For the Atmega you need the avr port of the gcc, a program for uploading the code to the
device ( avrdude ) and make for running Makeles.
Everything can be installed on Debian/Ubuntu systems with the following command
sudoapt-getinstallmakeavr-libcavrdudebinutils-avrgcc-avrgdb-avr
For other distributions please take a look inside your documentation how to install new packages
and how to nd their names.
2.1.1.Basis
For a clean start you should get a fresh copy of the basic programming environment we call
basis. You can either check it out from the svn-repository with
svncheckouthttp://ornella.iwr.uni-heidelberg.de/svn/basis
or download it from http://roboter.uni-hd.de .
After downloading it, you will need to
extract the les from the archive of course.
Change the directory ( cdbasis ) and start with your rst steps.
2.1.2.Firststeps
Just type
make
to compile and get an output similar to this
3
 
Version 1.1
IWR - robotics lab
Compilingmain.c...
Compilingadc.c...
Compilingtimer.c...
Compilingpwm.c...
Compilinguart.c...
Compilingservo.c...
Linkingbasis.elf...
Creatingbasis.hex...
Creatingbasis.eep...
avr-objcopy:--change-section-lma.eeprom=0x0000000000000000neverused
If you encounter any problems or errors with this, you should verify your installation of the
toolchain.
The make command produces a binary output le called basis.elf which now needs to be
transferred to the microcontroller.
You should supply power to the board and plug in the
programming device.
By typing
makeprog
a program called avrdude will be started to initiate the transfer.
Since there is more than just one possible programmer, maybe the Makefile has to be adjusted
to the one you are using.
. . .
#selectprogrammer
prog:prog-usb
#prog:prog-ser
#prog:prog-funk
. . .
In this example the usb-programmer is chosen because it is the only line that does not start
with a # .
If you encounter problems that look like a problem with permissions you can try executing
the command as root with for example sudomakeprog . For being able to program as a
normal unprivileged user, you can modify your udev-rules accordingly.
In the lab this has
already been done. This problem should therefor not occur.
2.1.3.Fuse-bits
A really essential part of initializing a new controller is setting the so called fuse-bits. You
should take a look at the board's documentation how to do that in detail or visit http:
From this point of view it is sucient to just call
makefuse
once every time you put a new controller chip in your board. If your are in doubt whether
your controller has already been fused, just it again. Fusing more than once doesn't hurt.
3.Thebasics
In this chapter you learn what registers and ports are and how to set a pin on a certain port.
4
1007749056.008.png
 
Version 1.1
IWR - robotics lab
3.1.PinsandPorts
Pins
The word pin can be referred to as the metal connectors on the package of the controller chip.
While some of those pins can be freely used within your programs, some have xed purposes
for example to supply voltage. There are also some pins, which are both: They normally have
a distinctive purpose, but in some applications can also be used freely. 1
The electric connector pins that are not congurable cannot be accessed from within the
software.
So whenever the word pin is used in the following text, the ones that are accessible from inside
the software are meant.
Generally every (software) pin can be used as a digital input or output. This means that it
can either "listen" to the signal that is supplied to the pin or provide a signal itself. These two
behaviors are called the Data Direction.
Ports
As the reader may recall from lectures in computer engineering 2 , a processor has a set of
registers which contain data or control the peripheral devices. A microcontroller typically is
nothing more than a microprocessor with memory, some special hardware (counter, ADCs,. . . )
and direct access to IO.
The Atmega series we are using has a 8 bit architecture. It can calculate values of the size of
1 byte at normally one clock cycle.
For this reason 8 pins are combined to 1 port, which simply is represented by one register in
the CPU.
Ports are enumerated with capital letters (A,B,C,. . . ), pins with number from 0 ::: 7.
For
example PIND6 represents the pin no. 6 on port D
Each bit in a register for IO represents a (physical) pin. This makes it necessary to know how
to manipulate just several bits at a time while leaving the others untouched.
If your are not familiar with bit shifting and bit algebra in expressions like a|=(1<<5); you
can take a look at A.1 to learn the basics.
4.Firststeps
This chapter elaborates about setting an output, reading an input and programming simple
time depending tasks. If you are not familiar with the bit expressions used in this section feel
free to take a look at A.1.
4.1.ControllingaLED
4.1.1.PluggingintheLED
In the lab we provide simple LEDs with integrated resistors soldered on a connector to be
directly plugged onto the board.
1 AnexampleistheresetpinwhichcanalsobeusedfornormalIO.Thistutorialkeepsontheusagethatis
recommendedbyanunchangedboardwithunchangedsoftware.
2 TechnischeInformatik,typicallyInformatikII
5
1007749056.001.png 1007749056.002.png 1007749056.003.png 1007749056.004.png 1007749056.005.png
 
Zgłoś jeśli naruszono regulamin