Anda di halaman 1dari 7

Lab 03- Introduction to Assembly language programming

Lab 03
Introduction to Programming in Assembly for AVR
Microcontrollers and Basic Input/Output Operations
using Assembly

Prepared by
Abdul Samad

Department of Electrical Engineering CIIT, Islamabad

Objectives:
To familiarize with PCB designing and etching process.
To get started with assembly language programming for
microcontrollers.

Tools:
For PCB designing: Cooper sheet, Ferric Chloride ,Printer & Drill
machine.
AVR Studio, Proteus ISIS
ISIS.
COMSATS Institute of Information Technology, Islamabad

Lab 03- Introduction to Assembly language programming

Introduction to Programming in Assembly:


The main advantage of using assembly language is that it is fast and
quick.
Program written in assembly take less time as we know exact time for
each instruction. Such knowledge of time required for execution of
every instruction makes us capable of measuring accurate times and
generating specific delays.
Another advantage is that programs are short hence if the program
memory is short assembly becomes the preferred choice.
The use of machine codes gives us precise control over microcontroller
that we want to program. Assembly is also case insensitive hence
commands written in upper or lower case dont make any difference.

AVR Assembler:
We will be using AVR assembler in this lab. This assembler is automatically
installed when AVR Studio is installed.
The Assembler translates assembly source code into object code. The
generated object code can be used as input to a simulator or an emulator such
as the Atmel AVR In-Circuit Emulator. The Assembler also generates a
PROMable code and an optional EEPROM file which can be programmed
directly into the program memory and EEPROM memory of an AVR
microcontroller.

Assembly Project Setup in AVR Studio:


Follow the following steps to setup an assembly language project in AVR
Studio.
1- After opening AVR Studio go to project menu and select project wizard.
The following window will be displayed:

COMSATS Institute of Information Technology, Islamabad

Lab 03- Introduction to Assembly language programming

2-Select new project to get to the following window:

COMSATS Institute of Information Technology, Islamabad

Lab 03- Introduction to Assembly language programming

3-Select AVR Assembler as project type. Specify the project location and give
your project a suitable name and click next to get the following window:

4-Select AVR Simulator in debug platform and ATMEGA16 in Device list. Then
clicking on finish will complete the project setup.

Assembly language programming:


Registers:
Registers are 8 bit storage capacity which can store unsigned numbers from
0-255 or signed numbers from -127 to 128 (most significant bit indicator of
sign). The AVR core combines a rich instruction set with 32 general purpose
working registers. All the 32 registers are directly connected to the Arithmetic
Logic Unit (ALU), allowing two independent registers to be accessed in one
single instruction executed in one clock cycle. They are named form R0 to R31
but we can assign them names for our convenience using command .DEF.
Look at the example below
.DEF MyPreferredRegister = R16 ; Its a directive and does not generate any
code
COMSATS Institute of Information Technology, Islamabad

Lab 03- Introduction to Assembly language programming

Now we can use MyPreferredRegister instead of R16 and use it to access R16
wherever needed.
Semicolon ; is always succeeded by comments in other words you can insert
comments after semicolon in assembly. Consider an Example
LDI temp, 150 ; will load 150 to R16 where LDI is Load Immediate
Registers R26 to R31 are used as two-byte pointers by more advanced
commands. Similarly R0 to R15 also have some restrictions for usage.
Therefore we usually stick with R16-R25 for our use.
Some examples for assembly instructions:
ANDI Rx,K; Bit-And of register Rx with a constant value K
ORI Rx,K; Bit-OR of register Rx with a constant value K
CPI Rx,K; Compare the content of the register Rx with a constant value K
SBR Rx,M; Set all bits in register Rx to one, that are one in the constant
mask M
CBR Rx,0b00110000; Clear bits 4 & 5 (Clear bits that are set in constant mask)
SER Rx; Set all bits in register Rx to one (equal to LDI Rx,255)
SUBI Rx,K; Subtract the constant K from the content of register Rx and
store the result in register Rx

COMSATS Institute of Information Technology, Islamabad

Lab 03- Introduction to Assembly language programming

Example program in assembly language


.include "m16def.inc"
.EQU F_CPU=1000000
.cseg
.org 0
///location 1 38 dedicated for interrupt service routines(ISR)
rjmp main
//Interrupt vector table comes here
.org 0x2A
main:
ldi r16, 0xff
out DDRB, r16 //Declaring PORTB as output
//Initializing stack pointer
ldi r16, high(RAMEND)
out SPH, r16
ldi r16, low(RAMEND)
out SPL, r16
//Stack pointer initialized
loop:
//turn on led
ldi r16,0x01
out PORTB,r16//turns on led
call delay
ldi r16,0x00
out PORTB,r16//turns off led
call delay
rjmp loop
delay:
ldi r16,255
L1:
dec r16 //1 clock
Ldi r17,255 //1 clock
brne L2 //2 clocks
ret //4 clocks
L2:
dec r17
brne L2
rjmp L1 //2 clocks
COMSATS Institute of Information Technology, Islamabad

Lab 03- Introduction to Assembly language programming

In lab task 1:
Interface a led seven segment display to the ATMEGA16 microcontroller
and display numbers from 0 t0 9 one after the other. There should be a
delay of 1 second between each digit. The display should restart from 0
after displaying 9.
The proteus circuit is shown below.

Post lab task:


Implementation of traffic signal in assembly language and its simulation
on proteus.

COMSATS Institute of Information Technology, Islamabad

Anda mungkin juga menyukai