University of Melbourne 640251 - Instrumentation for Scientists Microprocessors: Lab 6- AVR Studio Tutorial & Reverse Polish Calculator Aims: • • •

Familiarize. yourself with ATMEGA128 hardware, Learn to use AVR studio and Code Vision C Explore assembly instructions of the ATMEGA128.

Equipment: ATMEGA128 board (Bilby), Straight-through serial Cable battery power supply, programmer, Computer with serial ports and software, Cathode Ray Oscilloscope. References: For this lab class you will need: Instruction Set Reference and the Bilby schematic diagram handed out during this LAB. Also, Bring a bound A4 log book for writing your observations and keeping your notes. You should also to refer to lecture notes, hand-outs and other references.

PART 1 : AVR Studio Tutorial 1.5 Hours To Start AVR Studio click on: Start->Programs->ATMEL AVR Tools->AVR Studio 4 Once the program has started, you will be looking at a screen like this

640251 - Instrumentation for Scientists 21 April 2008

AVR Microcontroller Lab6 Page 1

To create a new project, click on "Create New Project" on the Welcome Screen or go to the "Project" menu and select "New". The dialog box shown in the next figure appears.

To create a new project, click on "Create New Project" on the Welcome Screen or go to the "Project" menu and select "New". The dialog box shown in the next figure appears. Enter the project name FLI (Flash Light Intelligently), and click NEXT Select the AVR simulator, and part type = ATMEGA128. Click Finish The AVR STUDIO window will appear as below. We can now enter the Assembly code of the program. (AVR Studio has opened an editor window called fli.asm). Alternatively you can add or create other assembly language files by right clicking on "Assembler" in the Workspace window

640251 - Instrumentation for Scientists 21 April 2008

AVR Microcontroller Lab6 Page 2

Enter the assembly code below, and when you have completed click build. ;****************************************************** .include "m128def.inc" ;this file defines all names for I/O locations in the atmega128 rjmp RESET ;Reset Handle ;****************************************************** RESET: .def temp = r16 .def x1 = r17 ldi temp,low(RAMEND) out SPL,temp ldi temp,high(RAMEND) out SPH,temp ;init Stack Pointer ldi x1, $FF OUT DDRA, x1

; PORT A = all outputs

again: out PORTA, x1 inc x1 rjmp again 640251 - Instrumentation for Scientists 21 April 2008

AVR Microcontroller Lab6 Page 3

;****************************************************** Save the file. (Select "Save" from the "File" menu.) Build the file. Select Build->Build ( or PRESS F7 ) 1. Have a demonstrator check that there are no errors.

The AVR Studio window should now look something like the following picture:

SIMULATE YOUR CODE Build and RUN the file. Select Build->Build and RUN ( or PRESS CONTROL-F7 )

640251 - Instrumentation for Scientists 21 April 2008

AVR Microcontroller Lab6 Page 4

STEP THROUGH YOUR CODE SELECT DEBUG->STEP INTO (OR PRESS F11) In the I/O view window, Click on the I/O PORTA tab. The PORTA bits will be displayed below. Watch the BITS next to PORTA and PINA as you step through the main loop. 2. Describe what is happening. What does it mean when an item goes RED?

640251 - Instrumentation for Scientists 21 April 2008

AVR Microcontroller Lab6 Page 5

The Watch View Open the Watch window by selecting VIEW->Watch A WATCH window will appear. You can view the value of all defined variables (by the .def directive) and symbols used by the assembler in this window. 3. Double click in the WATCH window, name field and enter x1 Step through your main loop. Observe the value of x1. Describe your observations.

The CPU contains 32 registers. To open the register view. From the VIEW tab select REGISTER. The registers view window

will appear. You can drag, drop and resize this window. Continue stepping your program by pressing the F11 key.

Save the Project Select file->save. 4. Program the Bilby - Refer to the Pony Prog Tutorial on Pages 7..10 of these notes. After programming check that the Bilby is outputting the expected waveforms on pins PA0 to PA3.

640251 - Instrumentation for Scientists 21 April 2008

AVR Microcontroller Lab6 Page 6

Digital to analog conversion 5. Draw a schematic diagram of a 4-bit R-2R ladder. 6. Construct a 4-bit R-2R ladder using only 10K resistors on Protoboard. Connect the R-2R ladder to PORT A bits 0..3. 7. Have a demonstrator view your working board. 8. Draw the output waveform in your lab book. 9. Convert the program to generate an inverse sawtooth waveform.

Pony Prog Tutorial To start the Programmer select Start->AllPrograms->PonyProg->PonyProg2000 The program will start and you will be presented with a window something like this:

Select the device type to program. From the DEVICE tab select AVR Micro -> ATMEGA128

640251 - Instrumentation for Scientists 21 April 2008

AVR Microcontroller Lab6 Page 7

Configure Pony Prog to use the Serial Port Programmer and Use Communications port Com1. Select Setup->InterFace Setup->IO Port Setup.

Select: Serial, SI Prog I/O, Com1.

Programming the FLASH program memory space First Load the Program File fli.hex From the File menu, select Open Program (FLASH) File... and locate fli.hex Connect the programmer header to the Bilby’s ISP Program Header.

640251 - Instrumentation for Scientists 21 April 2008

AVR Microcontroller Lab6 Page 8

The window shows the contents of the file FLI.HEX

Next select Command -> Erase This sets all bits to 1's in the program memory space. Then select Command -> Write Program (FLASH) Confirm to write to the device. After a minute or two the program will be verified as being loaded successfully. You should receive the message WRITE SUCCESSFUL. If not, get a demonstrator to help you.

640251 - Instrumentation for Scientists 21 April 2008

AVR Microcontroller Lab6 Page 9

RELOADING THE PROGRAM FILE Program development involves many cycles of writing code, - Designing a solution assembly, - Converting to machine codes device programming, - Loading the code into the chip testing, - Testing that the last code changes worked, and the program performs as desired. It is important to RE-LOAD the program into PonyProg before erasing and reprogramming the device again.

640251 - Instrumentation for Scientists 21 April 2008

AVR Microcontroller Lab6 Page 10

PART 2: REVERSE POLISH CALCULATOR and CodeVision C - 1.5 Hours Aims: Learn to design and write a application program in the Embedded C language for an Atmel ATMEGA128L. Equipment: Bilby (ATMEGA128), Serial Programmer, power supply, PC, Straight-through serial Cable, CodeVision reference. Introduction: In many systems, a stack is used for the passing of parameters and return addresses. If the number of parameters does not match within a procedure, then a ‘stack crash’ occurs when the return address is invalid, and a return instruction is executed. To get around this problem, a separate numeric stack can be used. This is how the Intel 80x87... floating point co-processor works. It has a 8-number deep floating point stack. In Intel Architecture all floating-point processor instructions all start with the letter F. For example FILD (Floating Point Integer Load) loads an integer onto the floating point stack and converts to floating point. FISTP Floating point Integer Store and Pop converts the top-of-stack to integer (performs rounding) and saves the number to an integer variable. In this lab we will add functions to skeleton code of a calculator. In our calculator we provide a stack, and subroutines: NLDD Numeric Stack LoaD from D register NSTPD Numeric Stack STore and Pop to D register The Problem: 1. You are required to write a 16 bit calculator program which you can use to test the Bilby instructions using C code. How the program should work: (a) prompt the user for a command, (b) select the correct stack based operation, (c) print the stack after each operation, Calculator commands are: n Enter a number to the numeric stack p Pull (Pop) a number from the numeric stack + Add Subtract / Divide * Multiply ^ Square r squareRoot d drop value off stack s show the numeric Stack REVERSE POLISH CALCULATORS is a simple method where the numbers are pushed onto the stack, then the operations are performed on the stack, and the results are stored on the stack. 640251 - Instrumentation for Scientists 21 April 2008

AVR Microcontroller Lab6 Page 11

Example use of the calculator n 1234 n 4321 + Will push 1234 onto the stack, then 4321 onto the stack, finally the + command will pop 2 values from the stack, add them together and push them back onto the stack. Hint: use atoi and printf. Procedures: Answer all questions, perform all the exercises and prepare your report in your lab book as you proceed. You may refer to lecture notes, hand-outs and other references. During this lab, use Hyperterminal or Code Vision’s Terminal for communications to HCCOM. Configure Hyperterminal for 9600 baud, 8 data bits, no parity, 1 stop bit and Flow Control : none. You may use notepad to write your program. Save the file as LAB3.C. Use the CodeVision Compiler to generate assembler and hex codes to a .hex file. Use Pony Prog to load the code into the Bilby. Once the file is successfully transferred, the Bilby will reset and you can run the program. The problem is Divided into functional blocks: (1) Print a prompt message(s) to the terminal, (2) read the command as a single character (3) Gather responses from the user, (4) Select the appropriate subroutine, (5) execute the subroutine (6) On error, stop immediately and display a suitable error message. Marks will be given for each function successfully implemented. for (i = MAX_STACK-1; i > numericStackPointer; i--) { printf ("stack [%d] = %d\r", i, dataStack [i]); }

640251 - Instrumentation for Scientists 21 April 2008

AVR Microcontroller Lab6 Page 12