Using Simulator in AVR Studio
Using Simulator in AVR Studio
By Lam Phung Version 1.0 Created on May 14, 2008. Last updated January 15, 2010. Latest version of this document is available at: http://www.elec.uow.edu.au/avr
Table of Contents
1. 2. 3. Introduction Installing tools for C programming Using AVR Studio for C programming Creating an AVR Studio project Compiling C code to HEX file Debugging C program using the simulator Downloading and running HEX file on AVR board 2 2 3 3 5 6 8
1. Introduction
This tutorial provides information on the tools and the basic steps that are involved in using the C programming language for the Atmel AVR microcontrollers. It is aimed at people who are 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 here for other AVR chips. This tutorial requires the following: the AVR Studio produced by Atmel, the WinAVR package by Sourgeforge WinAVR project, and an STK500 development board produced by Atmel.
Figure 1: Entering project type, name and location. In 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 Finish. Note: If you want to use other AVR chips such as ATMAGE8515, select it at this step. In this tutorial, we will use ATMEGA16 for both software simulation and hardware testing.
Figure 2: Selecting debug platform and device. 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 stage, but you can do that by reading the C comments.
Status messages
Figure 3: The AVR Studio with a project file open. Click menu Project | Save Project to save the project file and the C program. AVR Studio project files have extension aps.
// // // //
File: led.c Description: Simple C program for the ATMEL AVR uC (ATMEGA16 or ATMEGA8515 chip) This program lets the user turn on LEDs by pressing the switches on STK500 board Date modified: 13 May 2008
#include <avr/io.h> // avr header file for IO ports int main(void){ unsigned char i; // temporary variable DDRA = 0x00; DDRB = 0xFF; PORTB = 0x00; // set PORTA for input // set PORTB for output // turn ON all LEDs initially
while(1){ // Read input from PORTA. // This port will be connected to the 8 switches i = PINA; // Send output to PORTB. // This port will be connected to the 8 LEDs PORTB = i; } return 1; }
(c) IO view
Select menu Debug | Start Debugging. A yellow arrow will appear in the code window (Figure 7); it indicates the C instruction to be executed next. Select menu Debug | Step Into (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 executed: DDRB = 0xFF; // set PORTB for output We can see that Port B Data Direction Register (DDRB) has been changed to 0xFF.
Figure 7: Stepping through a C program in debugging mode. 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 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:
i = PINA; PORTB = i;
To monitor a C variable, select the variable name in the code window and click menu Debug | Quick Watch. The variable will be added to a watch window, as in Figure 9.
Figure 9: Watch window for C variables. Many other debugging options are available in the Debug menu, such as running up to a break point or stepping over a function or a loop. To view the assembly code along with the C code, select menu View | Disassembler.
Connect 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 microcontroller.
power switch Connect the board with 12V DC power supply and turn the power switch ON. 12-V power supply PORTA to SWITCHES ATMEGA16 chip to serial port of PC
programming mode
PORTB to LEDs
Figure 10: Setting up the STK500 for downloading and testing. Downloading and running HEX file In AVR Studio, select menu Tools | Program AVR | Connect. In the Select AVR Programmer dialog box, choose STK500 or AVRISP as the platform and Auto as Port (see Figure 11). Then click button Connect.
Figure 11: Selecting AVR programmer. Depending on the version of your AVR Studio, a message about firmware may appear. For now, this message can be discarded by clicking button Cancel. In the future, you may want to read this message carefully and perform the steps described there to perform firmware update. 9
In the STK500 dialog box that appears, select led.hex as Input Hext File. Then, click button Program to download the HEX file to the AVR chip (Figure 12).
b) Click to program
Figure 12: Selecting AVR programmer. 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. A MPEG-4 video demo of the program is available at
http://www.elec.uow.edu.au/avr/getdoc.php?doc=ecte333/lab07_task123.mp4
This is the end of this introductory tutorial. More in-depth information about programming Atmel AVR microcontrollers for embedded applications is provided in ECTE333 Digital Hardware course at the School of Electrical, Computer and Telecommunication Engineering, University of Wollongong, and also at our website http://www.elec.uow.edu.au/avr.
10