Está en la página 1de 8

FACULTY OF COMPUTER AND INFORMATION SCIENCES, AIN SHAMS UNIVERSITY

Lab 1
AVR Assembly and Introduction to AVR Studio
and Arduino Uno
OBJECTIVES:

1. What is MCU?
2. Common AVR Assembly Instructions
3. What is Arduino?
4. Setup the working environment.
5. Exercises
6. What is Proteus?
7. Assignment.

1. WHAT IS MCU?

Microcontroller is a single chip that contains the following


1. CPU
2. RAM: to store data
3. ROM: to store program
4. I/O Pins: to get/put signals from/to the circuit
5. External Interrupts: to interrupt the running program upon external event
6. Timers: waiting function that takes a configurable machine cycles
7. And more….

There are many types of microcontrollers such as Intel’s 8051, PIC from Microchip, ARM, and Our
MCU for this course which is AVR.

We concern ATMega328Pwhich is one of the most popular MCUs in the AVR family,which has
basically the following capabilities:
1. RAM: 2 KB
2. ROM: 32 KB
3. EEPROM: 1 KB
4. I/O pins: 23 programmable lines.
5. Check the datasheet for ATMega328 to find the remaining features

2. WHAT IS ARDUINO?

Arduino is an open hardware framework that facilities the development of embedded systems.
The framework is built around the ATMega MCUs family. We are using Arduino Uno which
makes use of ATMega328p. The board exposes the MCU ports as pin headers so that it can be
easily connected to Arduino Shields more about Shields later on the course.

Figure 1 shows the mapping between the MCU pins and the Arduino pin header.

2
FIGURE 1

As an example I13 on the Arduino board is mapped to Port B pin number 5.

3. COMMON AVR ASSEMBLY INSTRUCTIONS

Students should be able to use the following instructions as they are required for the lab. All of
these instructions are the basics for the AVR Assembly.

3
Instruction Format Description Example
LDI Rd,k Load Immediate value into
LDI LDI R19, 15
register
ADD ADD Rd,Rr Addition of two registers ADD R0,R1
SUB SUB Rd,Rr Subtraction of two registers SUB R0,R1
LDS Rd,k Load from data memory at
LDS LDS R2, 0x36
address k into register Rd
STS k,Rd Store in data memory at
STS STS 0x01, R2
address k from register Rd
IN Rd,A Read data from Input
Load from data memory port in Register
IN (I/O Mem) at I/O specific IN R16, 0x18
address A into register Rd Or
IN R16,PORTB
OUT A,Rr Send data from
Store in data memory (I/O
Register to Output port
OUT Mem) at I/O specific Output
OUT 0x18, R16
port from register Rr
OUT PORTB, R16
DEC DEC Rd Decrement register value DEC R19
MOV Rd,Rr Move data from one register
MOV MOV R18, R19
to another
AND Rd,Rr Bit wise and operation
AND AND R19, R18
between two registers
COM Rd 1’s complement of value in
COM COM R16
register Rd
NEG Rd 2’s complement of value in
NEG NEG R17
register Rd
JMP JMP L Jump to specific location JMP LOOP_LABEL

Those instructions should be enough to carry on the exercise programs.

4. SETUP THE WORKING ENVIROMENT

The first lab material contains the following tools that need to be installed.

 AVR Studio
o Simple setup process. Just keep the default options.
 Arduino USB Driver
o Connect the Arduino board and wait till the windows asks for the driver location
and point to the driver folder in the Lab1 material folder.
 CSYSArduinoUploader
o Does not need setup it is a standalone executable

Once you install the required tools/drivers follow the next steps to create your first project.

1- Open Start Menu – Atmel AVT Tools - AVR Studio 5


2- Then click new project
3- Choose Atmel AVR Assembler as we will write our codes with assembly for now. Later,
when you want to write C code you should choose Atmel AVR GCC instead.

4
4- Name your project Lab1_Demo
5- Choose the location for the project and keep the default settings and press next
6- Choose the debug platform to be AVR Simulator and the Device to be ATmega328P
7- Press OK button
Note: If debugger option is hidden while creating project:
I. View menu  Toolbars  Device and Debugger
II. In the toolbar, click “ATMEGA328p”
III. In “Debugging”, choose “AVR Simulator”.

Note: when writing C code


8- In Project menu → Lab1_Demo Properties (Alt+F7) → Toolchain → AVR/GNU C
Compiler → Symbols → Add F_CPU=16000000UL to Defined Symbols. As the
Arduino Uno board has crystal with this frequency.
9- Now you are ready to open the .asm file and edit the code.

Show the students how to debug the code with AVR Studio without the need to test each time on
Arduino.

5. EXCERCISES

Simple Exercise

A program to output value on port C.

Main:

;The first 2 lines will be explained in detail in CH4


LDI R16, 0xFF;
OUT DDRC, R16; Set Port C to be Output (all pins = 1)

LDI R16, 0x0F; Output Value 00001111 to pins of Port C


AGAIN: OUT PORTC , R16;
RJMP AGAIN;
Try to write the value in different data formats (Dec, Hex, Binary).

HARDWARE SETUP

Students first should connect LEDs to PORT C and connect the other terminals to the ground.

Example 2-3

A program to get data from PINB and send it to the I/O register of PORT C continuously.

5
Main:
;The first 4 lines will be explained in detail in CH4
LDI R16, 0x00;
OUT DDRB, R16; Set Port B to be Input (all pins = 0)
LDI R16, 0xFF;
OUT DDRC, R16; Set Port C to be Output (all pins = 1)

Again: IN R16 , PINB;


OUT PORTC , R16;
JMP Again;

ADDITIONAL HARDWARE SETUP

Students should connect PORT B pins to a wires that will be connected to either ground or VCC
to simulate external inputs.

Example 2-4

A program to toggle the I/O register of PORT C (B in book) continuously forever.

Main:
;The first 4 lines will be explained in detail in CH4
LDI R16, 0xFF;
OUT DDRC, R16; Set Port B to be Output (all pins = 1)
LDI R20, 0x55;
OUT PORTC, R20;

L1: COM R20;


OUT PORTC, R20;
CALL Delay
JMP L1;

;Delay Calculation will be explained in detail in Ch3


Delay: ;Delay function(200Ms)
LDI R22, 20
DelayLoop:
LDI R18, 160
Loop1:
LDI R19, 199
Loop2:
INC R19
DEC R19
DEC R19
BRNE Loop2
// Loop2 End
INC R18
DEC R18
DEC R18
BRNE Loop1
// Loop1 End
DEC R22
BRNE DelayLoop
RET

6
6. WHAT IS PROTEUS

Proteus is a circuit simulator. You can draw the circuit yourself then put in the HEX code file
generated after successfully compile of your code. Then Proteus will simulate and show you the
results.

The following instructions will help you build your first circuit on Proteus

1. Open Proteus (Schematics Capture Tool) check Figure 2 for reference.

FIGURE 2

2. Once the application opened you should get used to its different parts. Most important
tool bars will be explained now.

FIGURE 3

7
3. Each are in Proteus Schematic Capture program do certain job
a. This is the first are you should interact with. By double clicking on the white are
you open the components selector dialog so you can add more components to the
schematic. Students should add two components for now
i. ATMEGA328p
ii. LED-Blue
Also they should make use of the components selector dialog filter text
box.
b. Once the user defined the components, he selects each component and places it
on the work space. After that he wires the PINs of the ATMEGA to the LEDs.
c. This area is used to place logical signals like VCC, Ground and so on. Student
should select it and place ground as shown in the work space area. Finally he
should connect the ground to the two LEDs terminals as shown.
d. This area is used to zoom/pan on the work space.
e. Finally the user double clicks on the ATMEGA on the work space and browse to
the HEX file he is going to use. And press play button defined in E region in
Figure 3

7. ASSIGNMENT

Q4 in page 70

Write instructions to add the values 0x16 and 0xCD. Place the result in location 0x400 of data
memory and also output the result on port B. Test your output on LEDs.

8. REFERENCES

 Book: AVR Microcontroller and Embedded Systems


 http://www.arduino.cc

También podría gustarte