G
G
etti
etting started w
ng started w
ith C Programming for
ith C Programming for
the ATMEL AVR M
the ATMEL AVR M
icrocontrol
icrocontroller
ler
Version
Version 1.
1.0
0
May 14, 2008 May 14, 2008 URL:
Table of Contents
Table of Contents
1
1.. Introduction Introduction 22
2
2.. Installing Installing tools tools for for C C programming programming 22 3
3.. Using Using AVR AVR Studio Studio for for C C programmprogramm ing ing 33 3.1
3.1 Creating Creating an an AVR AVR Studio Studio project project 33 3.2
3.2 Compiling Compiling C C code code to to HEX HEX file file 55 3.3
3.3 Debugging Debugging C C program program using using the the simulator simulator 66 3.4
3.4 Downloading Downloading and and running running HEX HEX file file on on AVR AVR board board 88
1.
1. II ntroductio
ntroductio n
n
This tutorial provides information on the tools and the basic steps that are involved in using This tutorial provides information on the tools and the basic steps that are involved in using the C programming language for
the C programming language for the Atmel AVR microcontrollers. It is aimed at people whthe Atmel AVR microcontrollers. It is aimed at people wh o areo are new to this family of microcontrollers. The Atmel STK500 development board and the new to this family of microcontrollers. The Atmel STK500 development board and the ATMEGA16 chip are used in this tutorial; however, it is easy to adopt the information given ATMEGA16 chip are used in this tutorial; however, it is easy to adopt the information given here for other AVR chips.
here for other AVR chips.
This tutorial requires the following: This tutorial requires the following:
•
• the AVR Studio produced by Atmel,the AVR Studio produced by Atmel, •
• the WinAVR package by Sourgeforge WinAVR project, andthe WinAVR package by Sourgeforge WinAVR project, and •
• an STK500 development board produced by Atmel.an STK500 development board produced by Atmel.
2.
2. II nstalling tools for C programming
nstalling tools for C programming
To work with the Atmel AVR microcontroller using the C programming language, you will need To work with the Atmel AVR microcontroller using the C programming language, you will need two tools:
two tools: AVR StudioAVR Studio andand WinAVRWinAVR. Both tools are free at the links given below.. Both tools are free at the links given below.
•
• AVR Studio is an integrated development environment that includes an editor, theAVR Studio is an integrated development environment that includes an editor, the
assembler, HEX file downloader and a microcontroller emulator. AVR Studio setup file assembler, HEX file downloader and a microcontroller emulator. AVR Studio setup file and service packs are available at
and service packs are available at
http://www.atmel.com/dyn/products/tools_card.asp?tool_id=2725 http://www.atmel.com/dyn/products/tools_card.asp?tool_id=2725
•
• WinAVR is for a GCC-based compiler for AVR. It appears in AVR Studio as a plug-in.WinAVR is for a GCC-based compiler for AVR. It appears in AVR Studio as a plug-in.
WinAVR also includes a program called Programmer’s Notepad that can be used to WinAVR also includes a program called Programmer’s Notepad that can be used to edit and compile C programs, independently of AVR Studio. WinAVR setup file is edit and compile C programs, independently of AVR Studio. WinAVR setup file is available at
available at
http://winavr.sourceforge.net/ http://winavr.sourceforge.net/
Installing these tools is easy: just download and run the setup files, and accept the default Installing these tools is easy: just download and run the setup files, and accept the default installation options. Remember to install AVR Studio
3.
3. Using A
Using A VR Studio for C
VR Studio for C program
program ming
ming
As an example, we will create a simple C program for the Atmel AVR that allows the user to As an example, we will create a simple C program for the Atmel AVR that allows the user to turn on one of the eight Light Emitting Diodes (LEDs) on the STK500 development board, by turn on one of the eight Light Emitting Diodes (LEDs) on the STK500 development board, by pressing a switch. Next, you will be guided through four major stages:
pressing a switch. Next, you will be guided through four major stages:
•
• creating an AVR Studio project,creating an AVR Studio project, •
• compiling C code to HEX file,compiling C code to HEX file, •
• debugging C program using the simulator,debugging C program using the simulator, •
• downloading HEX file to the STK500 development board and running it.downloading HEX file to the STK500 development board and running it.
3
3.1
.1 Creating an AVR Studio project
Creating an AVR Studio project
Perform the following steps to create a simple AVR Studio project. Perform the following steps to create a simple AVR Studio project.
•
• Start the AVR Studio program by selectingStart the AVR Studio program by selecting StaStart | Prt | P rograms | Atmel AVR Tools |rograms | Atmel AVR Tools |
AVR Studio AVR Studio..
•
• Select menuSelect menu ProjectProject | New | New ProjecProjectt. In the dialog box that appears (see Figure 1),. In the dialog box that appears (see Figure 1),
select AVR GCC as project type, and specify the project name and project location. If select AVR GCC as project type, and specify the project name and project location. If options ‘Create initial file’ and ‘Create folder’ are selected, an empty C file and options ‘Create initial file’ and ‘Create folder’ are selected, an empty C file and containing folder will be created for you. In this case, we create a project called ‘led’. containing folder will be created for you. In this case, we create a project called ‘led’. Click button
Click button NextNext when you are ready.when you are ready.
Figure 7.1:
Figure 7.1: Entering project type, name and location.Entering project type, name and location.
•
• In the ‘Select debug platform and device’ dialog that appears (see Figure 2), chooseIn the ‘Select debug platform and device’ dialog that appears (see Figure 2), choose
‘AVR Simulator’ as the debug platform and ‘ATMEGA16’ as the device. Click button ‘AVR Simulator’ as the debug platform and ‘ATMEGA16’ as the device. Click button
Finish Finish ..
Note
Figure 1:
Figure 1: Selecting debug platform and device.Selecting debug platform and device.
•
• A project file will be created and AVR Studio displays an empty file led.c (see Figure 3).A project file will be created and AVR Studio displays an empty file led.c (see Figure 3).
Enter the C code shown in Figure 4. It is not important to understand the code at this Enter the C code shown in Figure 4. It is not important to understand the code at this stage, but you can do that by reading the C comments.
stage, but you can do that by reading the C comments.
Figure 2:
Figure 2: The AVR Studio with a project file open.The AVR Studio with a project file open.
•
• Click menuClick menu Project | Project | SavSave Pe P rojectroject to save the project file and the C program. AVRto save the project file and the C program. AVR
Studio project files have extension ‘aps’. Studio project files have extension ‘aps’.
List of project files
List of project files
Status messages
Status messages
program code led.c
//
// File: File: led.cled.c //
// Description: Description: Simple Simple C C program program for for the the ATMEL ATMEL AVR AVR uC uC (ATMEGA16 or (ATMEGA16 or ATMEGA8515 ATMEGA8515 chip)chip) // This program lets the user turn on LEDs by pressing the switches on STK500 board // This program lets the user turn on LEDs by pressing the switches on STK500 board // Date modified: 13 May 2008
// Date modified: 13 May 2008 #include <avr/io.h>
#include <avr/io.h> // avr header file for IO ports// avr header file for IO ports int
int mainmain
unsigned
unsigned charchar ii DDRA
DDRA // set PORTA for input// set PORTA for input DDRB
DDRB // set PORTB for output// set PORTB for output PORTB
PORTB // turn ON all LEDs initially// turn ON all LEDs initially
while
while i i
// Send output to PORTB. // Send output to PORTB.
// This port will be connected to the 8 LEDs // This port will be connected to the 8 LEDs
PORTB PORTB
Figure 3
Figure 3: Program code led.c: Program code led.c
3
3.2
.2 Compiling C code to HEX file
Compiling C code to HEX file
•
• Click menuClick menu Build | Rebuild AllBuild | Rebuild All to compile the C code.to compile the C code. •
• If there is no error message, a file called led.hex will be produced (see Figure 5). ThisIf there is no error message, a file called led.hex will be produced (see Figure 5). This
file contains the machine code that is ready to be downloaded to the ATMEGA16 file contains the machine code that is ready to be downloaded to the ATMEGA16 microcontroller.
microcontroller. The file is sThe file is stored in sub-folder ‘\defatored in sub-folder ‘\default’ of your project.ult’ of your project.
•
• If there are error messages, check your C code. Most often, they are caused by If there are error messages, check your C code. Most often, they are caused by somesome
typos or syntax errors. typos or syntax errors.
Figure 4:
Figure 4: Selecting menu Build | Rebuild All to create HEX file.Selecting menu Build | Rebuild All to create HEX file.
void void){){ // temporary variable // temporary variable 0x00 0x00 0xFF 0xFF 0x00 0x00 1 1){){
// Read input from PORTA. // Read input from PORTA.
// This port will be connected to the 8 switches // This port will be connected to the 8 switches
PINA PINA i i return return 11
3
3.3
.3 Debugging C program using the simulator
Debugging C program using the simulator
Debugging is an essential aspect in any type of programming. This section will show you how Debugging is an essential aspect in any type of programming. This section will show you how to debug a C program at source-code level, using AVR Studio. You can execute a C program to debug a C program at source-code level, using AVR Studio. You can execute a C program one line at a time, and observe the effects on the CPU registers, IO ports and memory. This is one line at a time, and observe the effects on the CPU registers, IO ports and memory. This is possible because AVR Studio provides a simulator for many AVR microcontrollers, including the possible because AVR Studio provides a simulator for many AVR microcontrollers, including the ATMEGA16 and ATMEGA8515. Therefore, this debugging does not require the STK500 kit.
ATMEGA16 and ATMEGA8515. Therefore, this debugging does not require the STK500 kit.
We will continue with the example project led.aps created in Section 3.2 of this tutorial. We will continue with the example project led.aps created in Section 3.2 of this tutorial.
•
• AVR Studio lets you examine the contents of CPU registers and IO ports. To enable theseAVR Studio lets you examine the contents of CPU registers and IO ports. To enable these
views, right click on the menu bar
views, right click on the menu bar at the top and select ‘I/O’ and at the top and select ‘I/O’ and ‘Processor’ options. Refer‘Processor’ options. Refer to Figure 6.
to Figure 6.
(a)
(a) Selecting Selecting views views (b) (b) Processor Processor view view (c) (c) IO IO viewview Figure 5:
Figure 5: Debugging views.Debugging views.
•
• Select menuSelect menu Debug | Start DebuggingDebug | Start Debugging. A yellow arrow will appear in the code window. A yellow arrow will appear in the code window
(Figure 7); it indicates the C instruction to be executed next. (Figure 7); it indicates the C instruction to be executed next.
•
• Select menuSelect menu Debug | Step IntoDebug | Step Into (or press hot-key F11) to execute the C instruction at(or press hot-key F11) to execute the C instruction at
the yellow arrow. Figure 6c shows the IO view after the following C instruction is the yellow arrow. Figure 6c shows the IO view after the following C instruction is executed:
executed:
DDRB = 0xFF;
DDRB = 0xFF; // set PORTB for output// set PORTB for output We can see that Port
We can see that Port B Data Direction Register (DDRB) has been changed toB Data Direction Register (DDRB) has been changed to 0xFF0xFF..
click to select tree display
Figure 6:
Figure 6: Stepping through a C program in debugging mode.Stepping through a C program in debugging mode.
•
• While debugging the C program, you can change the contents of a register. For example,While debugging the C program, you can change the contents of a register. For example,
to change Port A Input Pins register (PINA), click on the value column of PINA and enter a to change Port A Input Pins register (PINA), click on the value column of PINA and enter a new value (Figure 8a). This change takes effect immediately. Subsequently, the contents new value (Figure 8a). This change takes effect immediately. Subsequently, the contents of PORTB will be 0x04 (see Figure 8b) after running the two C instructions:
of PORTB will be 0x04 (see Figure 8b) after running the two C instructions:
i = PINA; i = PINA; PORTB = i; PORTB = i;
(a) changing PINA register
(a) changing PINA register (b) effects on PORTB after running(b) effects on PORTB after running i = PINA; PORTB = i; i = PINA; PORTB = i;
•
• To monitor a C variable, select the variable name in the code window and click menuTo monitor a C variable, select the variable name in the code window and click menu
Debug | Quick W
Debug | Quick W atchatch. The variable will be added to a watch window, as in Figure 9.. The variable will be added to a watch window, as in Figure 9.
Figure 8:
Figure 8: Watch window for C variables.Watch window for C variables.
•
• Many other debugging options are available in the Debug menu, such as running up to aMany other debugging options are available in the Debug menu, such as running up to a
break point or stepping over a funct
break point or stepping over a function or a loop. ion or a loop. To view the assembly To view the assembly code along withcode along with the C code, select menu
the C code, select menu View View | Disassembl| Disassemblerer..
3
3.4
.4 Dow
Dow nloading and running HEX fil
nloading and running HEX file on AVR board
e on AVR board
To perform the steps in this section, you will need a STK500 development board from Atmel. To perform the steps in this section, you will need a STK500 development board from Atmel. The STK500 kit includes two
The STK500 kit includes two AVR microcontroller chips: ATMEGA8515 and ATMEGA16.AVR microcontroller chips: ATMEGA8515 and ATMEGA16.
•
• The ATMEGA8515 is installed on the development board by the manufacturer.The ATMEGA8515 is installed on the development board by the manufacturer. •
• The ATMEGA16 is installed on all development boards in SECTE laboratories.The ATMEGA16 is installed on all development boards in SECTE laboratories.
Note Note::
•
• To install the ATmega16 chip, simply use a chip extractor tool to remove the existingTo install the ATmega16 chip, simply use a chip extractor tool to remove the existing
ATMEGA8515 from its socket. Then place the ATMEGA16 in
ATMEGA8515 from its socket. Then place the ATMEGA16 in socket SCsocket SCKT3100AKT3100A 33..
•
• If you use other AVR chips such as ATMEGA128, refer to Table 3.2 AVR Sockets, ‘AVRIf you use other AVR chips such as ATMEGA128, refer to Table 3.2 AVR Sockets, ‘AVR
STK500 User Guide’ for the exact socket. STK500 User Guide’ for the exact socket.
Hardware setup Hardware setup
Refer to Figure 10 when carrying out the following steps for hardware setup. Refer to Figure 10 when carrying out the following steps for hardware setup.
•
• Connect the SPRO3G jumper to the ISP6PIN jumper, using the supplied cable in theConnect the SPRO3G jumper to the ISP6PIN jumper, using the supplied cable in the
STK500 kit. This is needed to program the ATMEGA16 chip. STK500 kit. This is needed to program the ATMEGA16 chip.
•
• Connect the board with the PC using a serial cable. Note that the STK500C has twoConnect the board with the PC using a serial cable. Note that the STK500C has two
RS232 connectors; we use only the connector marked with RS232 CTRL. RS232 connectors; we use only the connector marked with RS232 CTRL.
•
• Connect the SWITCHES jumper to PORTA jumper. This step is needed in our exampleConnect the SWITCHES jumper to PORTA jumper. This step is needed in our example
because we want to connect 8 switches on the development board to port A of the because we want to connect 8 switches on the development board to port A of the microcontroller.
•
• Connect the LEDS jumper to PORTB jumper. This step is needed in our exampleConnect the LEDS jumper to PORTB jumper. This step is needed in our example
because we want to connect 8 LEDs on the development board to port B of the because we want to connect 8 LEDs on the development board to port B of the microcontroller.
microcontroller.
•
• Connect the board with 12V DC power supply and turn the power switch ON.Connect the board with 12V DC power supply and turn the power switch ON.
Figure 9:
Figure 9: Setting up the STK500 for downloading and testing.Setting up the STK500 for downloading and testing.
Downloading and running HEX file Downloading and running HEX file
•
• In AVR Studio, select menuIn AVR Studio, select menu Tools | Program AVR Tools | Program AVR | Connect| Connect.. •
• In the ‘Select AVR Programmer’ dialog box, choose ‘STK500 or AVRISP’ as theIn the ‘Select AVR Programmer’ dialog box, choose ‘STK500 or AVRISP’ as the
platform and ‘Auto’ as Port (see Figure 11). Then click button
platform and ‘Auto’ as Port (see Figure 11). Then click button ConnectConnect..
Figure 10:
Figure 10: Selecting AVR programmer.Selecting AVR programmer.
•
• Depending on the version of your AVR Studio, a message about firmwareDepending on the version of your AVR Studio, a message about firmware maymay
programming mode programming mode PORTA to SWITCHES PORTA to SWITCHES PORTB to LEDs PORTB to LEDs 12-V power supply 12-V power supply to serial port of PC to serial port of PC ATMEGA16 chip ATMEGA16 chip power switch power switch
ATMEGA8515 chip (not used here)
•
• In the ‘STK500’ dialog box that appears, select led.hex as ‘Input Hext In the ‘STK500’ dialog box that appears, select led.hex as ‘Input Hext File’. Then, clickFile’. Then, click
button
button ProgramProgram to download the HEX file to the AVR chip (Figure 12).to download the HEX file to the AVR chip (Figure 12).
Figure 11:
Figure 11: Selecting AVR programmer.Selecting AVR programmer.
•
• The program will now run on the microcontroller. If you press and hold down one of The program will now run on the microcontroller. If you press and hold down one of
the 8 switches on the development board, the corresponding LED will be turned on. the 8 switches on the development board, the corresponding LED will be turned on.
A MPEG-4 video demo of the program is available at A MPEG-4 video demo of the program is available at
http://www.elec.uow
http://www.elec.uow.edu.au/avr/getdoc.edu.au/avr/getdoc.php?doc=ec.php?doc=ecte333/lab07_taste333/lab07_task123.mp4k123.mp4
This is the end of this introductory tutorial. More in-depth information about programming This is the end of this introductory tutorial. More in-depth information about programming Atmel AVR microcontrollers for embedded applications is provided in ECTE333
Atmel AVR microcontrollers for embedded applications is provided in ECTE333 Digital HardwareDigital Hardware course at the School of Electrical, Computer and Telecommunication Engineering, University of course at the School of Electrical, Computer and Telecommunication Engineering, University of Wollongong, and also at our
Wollongong, and also at our website http://www.elec.uow.edu.au/avr.website http://www.elec.uow.edu.au/avr.
By Lam Phung, November 26, 2008. By Lam Phung, November 26, 2008.
*** END *** *** END ***
a) Select HEX file
a) Select HEX file
b) Click to program