0% found this document useful (0 votes)
14 views

Lab 5

The document describes a lab experiment on assembly language programming for AVR microcontrollers. It includes objectives, in-lab tasks to generate a square wave and toggle port bits, and a post-lab task to blink LEDs with different frequencies. The conclusion discusses what was learned about AVR programming and setting ports in assembly language.

Uploaded by

Junaid Arshad
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Lab 5

The document describes a lab experiment on assembly language programming for AVR microcontrollers. It includes objectives, in-lab tasks to generate a square wave and toggle port bits, and a post-lab task to blink LEDs with different frequencies. The conclusion discusses what was learned about AVR programming and setting ports in assembly language.

Uploaded by

Junaid Arshad
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

LAB # 5

Introduction to Assembly Language Programming of AVR


Microcontrollers
Group Members:
Taha Saeed FA18-EEE-032-ISB.
Farheen Afzaal FA18-EEE-008-ISB.
Name : Zohaib Ali Arif
Reg.No.: FA18-EEE-037-ISB.
Class : EEE-5
Teacher Name: Faiza Rajab Dad
LAB # 5
Introduction to Assembly Language Programming of AVR
Microcontrollers
OBJECTIVES
 To get started with assembly language programming for AVR microcontrollers.
 Implementing loops and conditional execution instructions in Assembly language

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"
;Initialize stack pointer.
ldi r16,HIGH(RAMEND)
out SPH, R16
ldi r16,LOW(RAMEND)
out SPL, R16
start:
ldi r16,0x01 ;load 0x01 into a register r16
out DDRB,r16 ;configure PB0 as output: DDRB=0x01
forever:
ldi r19,0x01 ;load 0x01 into a register
out PORTB,r19 ;PB0=1
rcall delay_400us
ldi r19,0x00
out PORTB,r19 ;PORTB=0x00
rcall delay_600us
rjmp forever ;keep doing this forever
delay_400us: ;This subroutine will call delay of 100us 4 times
;to generate a delay of 400us
ldi r20,4
loop1:
rcall delay_100us
dec r20
brne loop1
ret
delay_600us: ;This subroutine will call delay of 100us 6 times
;to generate a delay of 600us
ldi r20,6
loop2:
rcall delay_100us
dec r20
brne loop2
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

TASK 2:
WRITE A PROGRAM TO TOGGLE ALL BITS OF PORT D
WITH SOME DELAY (SPECIFIED BY LAB INSTRUCTOR).

.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,0x55
out PORTD,r19
rcall delay_400us
ldi r19,0xAA
out PORTD,r19
rcall delay_600us
rjmp forever
delay_400us: ;This subroutine will call delay of 100us 4 times
;to generate a delay of 400us
ldi r20,4
loop1:
rcall delay_100us
dec r20
brne loop1
ret
delay_600us: ;This subroutine will call delay of 100us 6 times
;to generate a delay of 600us
ldi r20,6
loop2:
rcall delay_100us
dec r20
brne loop2
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
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. 

.include "m328Pdef.inc"
ldi r16,HIGH(RAMEND)
out SPH, R16
ldi r16,LOW(RAMEND)
out SPL, R16
start:
ldi r16,0b00001111
out DDRB,r16
forever:
ldi r19,0b00001111
out PORTB,r19
rcall delay_200us;1
ldi r19,0b00001110
out PORTB,r19
rcall delay_200us;2
ldi r19,0b00001101
out PORTB,r19
rcall delay_200us;3
ldi r19,0b00001100
out PORTB,r19
rcall delay_200us;4
ldi r19,0b00001011
out PORTB,r19
rcall delay_200us;5
ldi r19,0b00001010
out PORTB,r19
rcall delay_200us;6
ldi r19,0b00001001
out PORTB,r19
rcall delay_200us;7
ldi r19,0b00000000
out PORTB,r19
rcall delay_200us;8
ldi r19,0b00000111
out PORTB,r19
rcall delay_200us;9
ldi r19,0b00000110
out PORTB,r19
rcall delay_200us;10
ldi r19,0b00000101
out PORTB,r19
rcall delay_200us;11
ldi r19,0b00000100
out PORTB,r19
rcall delay_200us;12
ldi r19,0b00000011
out PORTB,r19
rcall delay_200us;13
ldi r19,0b00000010
out PORTB,r19
rcall delay_200us;14
ldi r19,0b00000001
out PORTB,r19
rcall delay_200us;15
ldi r19,0b00000000
out PORTB,r19
rcall delay_200us;16

rjmp forever
delay_200us: ;This subroutine will call delay of 100us 4 times
;to generate a delay of 400us
ldi r20,2
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:
In this lab, I have learnt the assembly language of AVR microcontroller. I implemented two
tasks. In first task I generated a square wave of 1KHz and 40% Duty cycle on PROTEUS
(virtually). I set the pins according to my need. The second task I toggled all the bits which
were coming to my desired output port. The main target of this lab was to get used to all the
commands of AVR. We learnt how to set output ports and input ports of AVR and dry run our
code on PROTEUS and debugged the errors also.

Lab Assessment

Pre Lab /5
Performance /25
/5
Results /5
Viva /5
Critical Analysis /5
Instructor Signature and Comments

You might also like