Microprocessor Systems & Interfacing EEE-342: Comsats University

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

COMSATS UNIVERSITY

MICROPROCESSOR SYSTEMS & INTERFACING


EEE-342

Lab # 05 Introduction to Assembly Language Programming of AVR


Microcontrollers

Name

Registration Number

Instructor’s Name MA’AM ASMA RAMAY


OBJECTIVES
 To get started with assembly language programming for AVR microcontrollers.
 Implementing loops and conditional execution instructions in Assembly language

REQUIRED TOOLS:

SOFTWARE TOOLS:
 AVR Studio/ Atmel Studio
 Proteus ISIS
 AVRDUDESS

HARDWARE TOOLS:

PRE-LAB
5.1 INTRODUCTION TO ASSEMBLY PROGRAMMING
Assembly language is a low-level programming language for a computer or other programmable
devices. It is an alphanumeric representation of machine code. The instructions used in writing
programs in assembly language are not general but specific to the microcontroller. AVR 8-bits
microcontrollers have a common instruction set.

5.1.1 ADVANTAGES OF ASSEMBLY LANGUAGE:


 Assembly language is fast.
 Program written in assembly take less execution time.
 We know exact time for each instruction.
 We can measure accurate execution time and hence able to generate specific delays.
 It requires less memory.
 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 don’t
make any difference.
5.1.2 PROCESSOR REGISTERS:
There are 32 general-purpose 8-bit registers, R0–R31. All arithmetic and logic operations
operate on those registers; only load and store instructions access RAM. A limited number of
instructions operate on 16-bit register pairs.
The last three register pairs are used as pointer registers for memory addressing. They are
known as X (R27:R26), Y (R29:R28) and Z (R31:R30). Instructions which allow an
immediate value are limited to registers R16–R31 (8-bit operations) In addition to these 32
general-purpose registers, the CPU has a few special-purpose registers:
 PC: 16 bit program counter
 SP: 16 bit stack pointer
 SREG: 8 bit status register
5.2 ASSEMBLY LANGUAGE INSTRUCTIONS:
Some common assembly language instructions used for AVR microcontroller programming are
given below:

IN-LAB TASK:
Task 1: Generate a square wave signal of frequency 1 KHz and 40% duty cycle on a digital
I/O pin of ATmega328P.
Steps:
1: Initialize stack pointer.
2: Configure Pin 0 of Port B as output.
3: Send 1 on PB0.
5: Call Delay of 400us.
6: Send 0 on PB0.
7: Call delay of 600us.
8: Repeat forever
.include "m328Pdef.inc"

ldi r16,HIGH(RAMEND) 
out SPH, R16 
ldi r16,LOW(RAMEND)
out SPL, R16

start:   
ldi r16,0xff   
out DDRD,r16   

forever:
ldi r19,0xff     
out PORTD,r19  
rcall delay_300ms 
ldi r19,0x00   
out PORTD,r19    
rcall delay_300ms  
rjmp forever  ;keep doing this forever 

delay_300ms :  
ldi r21,12 

loop2:
rcall delay_25ms 
dec r21 
brne loop2
ret

delay_25ms : 
ldi r20,250 
loop1:
rcall delay_100us 
dec r20 
brne loop1 
ret
delay_100us:  
ldi r18, 4 
 
l1:   ldi r19,100 
l2:   nop 
       dec r19 
       brne l2 
       dec r18 
       brne l1
ret

PROTEUS SIMULATION
HARDWARE IMPLEMENTATION

TASK 2: WRITE A PROGRAM TO TOGGLE ALL BITS OF PORT D WITH SOME


DELAY (SPECIFIED BY LAB INSTRUCTOR).
.include "m328Pdef.inc"
;Initialize stack pointer.

ldi r16,HIGH(RAMEND)
out SPH, R16
ldi r16,LOW(RAMEND)
out SPL, R16

start:
ldi r16,0x55 ;load 0x01 into a register r16
out DDRB,r16 ;configure PB0 as output: DDRB=0x01

forever:
out PORTD,R16 ; PB=01
rcall delay_250ms
com R16
rjmp forever; keep doing this forever

delay_250ms:
ldi R19,100

L3:
rcall delay
dec R19
brne L3

delay:
ldi R17,100
L1:
ldi R18,100
L2:
Nop
Dec R18
brne L2
dec R17
brne L1
ret

POST-LAB TASK:
Blinking LED’s with different frequencies. 
Description: 
Connect 4 LED’s to PORT B of AT mega 328P.Using loops and conditions write an
assembly program that makes these LED blinks. 
LED 0 should blink an ON/OFF time of 200ms 
LED 1 should blink an ON/OFF time of 400ms 
LED 2 should blink an ON/OFF time of 800ms 
LED 3 should blink an ON/OFF time of 1600ms 
Simulate on proteus. 

CODE:
.include "m328Pdef.inc"
    ldi r16,HIGH(RAMEND) 
    out SPH, R16 
    ldi r16,LOW(RAMEND)
out SPL, R16
start:
  ldi r16,0xff 
       out DDRD,r16   
forever:   
       ldi r19,0xff     
       out PORTD,r19   
       rcall delay_200ms 
       ldi r19,0xFE
       out PORTD,r19    
       rcall delay_200ms
       ldi r19,0xFD     
       out PORTD,r19
  rcall delay_200ms
  ldi r19,0xFC     
       out PORTD,r19 
  rcall delay_200ms
       ldi r19,0xFB     
       out PORTD,r19
  rcall delay_200ms
  ldi r19,0xFA  
       out PORTD,r19
  rcall delay_200ms
  ldi r19,0xF9   
       out PORTD,r19
  rcall delay_200ms
  ldi r19,0xF8     
       out PORTD,r19
    rcall delay_200ms
       ldi r19,0xF7     
       out PORTD,r19
  rcall delay_200ms
  ldi r19,0xF6     
       out PORTD,r19 
   
    rcall delay_200ms
       ldi r19,0xF5     
       out PORTD,r19
  rcall delay_200ms
  ldi r19,0xF4     
       out PORTD,r19    
    rcall delay_200ms
       ldi r19,0xF3     
       out PORTD,r19
  rcall delay_200ms
  ldi r19,0xF2     
       out PORTD,r19
    rcall delay_200ms
       ldi r19,0xF1     
       out PORTD,r19
  rcall delay_200ms
  ldi r19,0xF0     
       out PORTD,r19  
  rcall delay_200ms  
       rjmp forever  ;keep doing this forever 

  
  delay_200ms : ;This subroutine will call delay of 25ms 8 times  
             ;to generate a delay of 200ms 
       ldi r21,8 
loop2: 
       rcall delay_25ms 
       dec r21 
       brne loop2
       ret
delay_25ms : ;This subroutine will call delay of 100us 250 times  
             ;to generate a delay of 25ms 
       ldi r20,250 
loop1: 
       rcall delay_100us 
       dec r20 
       brne loop1 
       ret
  delay_100us:  ;This subroutine will generate a delay of 100us 
; Inner loop count =4 (Ignoring overhead) 
; Delay = (1/16M)*4*4*100 
       ldi r18, 4 
 l1:   ldi r19,100 
 l2: 
       nop 
       dec r19 
       brne l2 
       dec r18 
       brne l1 
       ret

CONCLUSION/CRITICAL ANALYSIS:

The objectives of this lab were to learn about assembly language programming for AVR
microcontrollers and implementation of loops and conditional execution instructions in Assembly
language. Assembly language is a low-level programming language for a computer or other
programmable devices. Basic Instructions of the Assembly Language, their Syntax Limitations
were taught in tis lab. Assembly is also case insensitive hence commands written in upper or
lower case don’t make any difference. There are 32 general-purpose 8-bit registers, R0–R31.
Advantages of assembly are:
 Program written in assembly take less execution time. 
 We know exact time for each instruction. 
 We can measure accurate execution time and hence able to generate specific delays. 
 It requires less memory.

TASK 1 OUTCOMES: in this task duty cycle of 40% was generated by changing the delay time
of outer and inner loop and afterwards the output waves were observed on oscilloscope.
TASK 2 OUTCOME: in this task PORTD bits were toggled with a delay of 250ms (as specified
by the instructor). The output was observed on the LEDs that is 8 LEDs blinked alternatively.

You might also like