MIP Lab03
MIP Lab03
Programming
EL-303
LABORATORY MANUAL
Fall 2017
LAB 03
Introduction to AVR Atmega32, Atmel Studio and Proteus 8 to Perform
Assembly Language I/O and operational Coding and Simulation
LAB #3
Hardware/ PC
Software Atmel Studio 6
Required: Proteus 8
Introduction:
Applications of microcontroller
Microcontroller Family:
Microcontroller
Family
ATMEGA 32
C vs. Assembly
Writing to Ports:
Set all the pins of DDRx of the specific port to 1 and then output the data to Portx register
connected to the specific port.
Example Code:
Atmel Studio 6 should launch and display a Start Page. To create a new AVR project,
click on the New Project... button, or navigate to File => New => Project...
The dialogue box that appears should look similar to given Figure. Under Installed
Templates, make sure Assembler is selected.
Select AVR Assembler Project as the project type.
In the Name text box, type the name of the project, such as Lab3.
Make sure that that the checkbox for Create directory for solution is checked.
The location of the project can be changed by clicking on the Browse... button next to
the path name, and navigating to the desired location for the new project.
Click OK to continue.
The next dialogue requires a device selection. First, ensure that the dropdown menu
labeled Device Family: selects either All or megaAVR, 8-bit.
In the ‘Device Selection’ dialog that appears, search for ATmega32 and
then click button OK.
A project file will be created and Atmel Studio displays an initial file Lab3.asm (see
Figure).
Type a simple program in Lab3.asm file as shown in the figure given below.
Building Project:
Project Simulation
Once a project has been created, and you have written an assembly program, it will need to be
tested. This is accomplished by running the program on a simulated microcontroller built into
Atmel Studio 6. Atmel Studio 6 has the capability to simulate almost every AVR
microcontroller offered by Atmel. For the purposes of this tutorial, the ATmega32 will be the
microcontroller that will be simulated. This microcontroller was selected earlier during the
project creation phase. (To change the microcontroller, right-click on your project name in the
Solution Explorer and select Properties. This will open a tab that allows you to configure
various properties of your project. Make sure the Build tab is selected, and then click the
Change Device... button and select a different microcontroller.)
1. Before the program can be simulated, the program must first be compiled. There are three
ways to do this.
(a) In the main Atmel Studio 6 menu, navigate to Build => Build Solution.
(b) Click on the Build Solution icon on the main toolbar.
(c) Press the F7 key.
2. If the code was successfully compiled, a message in the Output window at the bottom
should read “Build succeeded”. If it does not say this, then there were some errors in the
code. Clicking on the errors in the Error
List will highlight the line of code causing the error in the editor window.
Project Debugging:
Once the code has been successfully compiled, simulation can begin. There are two ways to
simulate the chip: debugging mode, which allows a line-by-line simulation, and run mode,
which continuously runs the program.
(a) There are a few ways to run in debug mode
i. Follow the menu Debug => Start Debugging and Break.
ii. Click on the Start Debugging and Break icon.
iii. Press Alt+F5.
(b) To debug the program step by step, Use Step into to go to next instruction, or Step Over
to go to previous instruction.
A yellow arrow will appear in the code window; it indicates the Assembly instruction
to be executed next.
While debugging the Assembly program, you can change the contents of a register. For
example, to change Port B Input Pins register (PINB), click on the value column of 8
PINB and enter a new value. This change will take effect immediately. As PORTB is
being treated as input, so, you have to change the value of DDRB=0xFF as shown in
figure.
Debugging Steps:
1- Before debugging you can see values of Program counter, all four ports and registers
from 0-31.
2- After executing the first line you can see that register R17 has value 0x00.
3- After next step DDRA will have the value of register R17 i.e, 0x00.
4-After execution of next line register R18 will have value of 0xff.
5-Now DDRB register will have value stored in register R18 i.e, 0xff
6-In this step pinb value will be stored in register R19 as in this case it is 0x12.
7- In the next step value of debugging value of register R19 will be displayed on
portb.
Status Register:
Status Register has various flag bits which are set or cleared by the processor on
execution of arithmetic operation
These bits indicate the status of the operation
Carry/Borrow has occurred
Overflow has occurred
Result is zero
These bits are used to implement conditional instructions
STS Command:
To visualize data stored in a specific memory location, use Memory (EEPROM) as shown in
the figure.
Simulation on Proteus
Double Click on AVR Atmega32 MCU. Edit Component Window will be opened.
Choose the specific Hex file, and Click Open and then Press OK.
Lab 3 Performance:
Task 3.1
Write Assembly language program for Atmega32 microcontroller to write 0xAA in Register
R23, and Copy register R23 value into register R24. Display the results on Atmel Studio
Processor view.
Task 3.2
Write Assembly language program for Atmega32 microcontroller which takes input from
PORTA and PORTB. Add and subtract both Numbers and display result on PORTC and
PORTD respectively. Show the results on Atmel Studio I/O view.
Generate Hex file from Atmel Studio and Implement simulation in Proteus 8.
Task 3.3
Write code to design an Up Counter that counts from 0xF0 to 0xFF. Debug the code and Check
the Status register bits and Counter Current value in Processor View at each count.
Task 3.4
Write code to design a Down Counter that counts from 0xFF to 0xF0. Debug the code and
Check the Status register bits and Counter Current value in Processor View at each count.
Task 3.5
Write code to take input from PORTA, store the input value on 0x200 memory location
(EEPROM). Then transfer the data from 0x200 to 0x300 location.