Introduction to Assembly Language Programming of AVR Microcontrollers

COMSATS UNIVERSITY ISLAMABAD

 

MICROPROCESSOR SYSTEMS AND INTERFACING

                        LAB REPORT 4

SUBMITTED TO:

SIR KHIYAM IFTIKHAR

SUBMITTED BY:

JUNAID AHMAD

Ibrar ahmad

Jarrar malik

REGISTRATION NO:

CIIT/FA19-BEE-089/ISB

Ciit/fa19-bee-083/isb

Ciit/fa19-bee-087/isb

DATE:

15-10-2021

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

 

 Required Tools:

 

Software Tools:

· AVR Studio/ Microchip Studio

· Proteus ISIS

 · AVRDUDESS

Hardware Tools:

· Arduino Nano

·  Oscilloscope

· Resistors 470 Ω

·  LED

·  Wires

In-Lab:

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

 

Code:


.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_16ms

 ldi r19,0x00

 out PORTB,r19 ;PORTB=0x00

 rcall delay_600us

 rjmp forever ;keep doing this forever

delay_16ms: ;This subroutine will call delay of 100us 4 times

 ;to generate a delay of 400us

ldi r20,20

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, 225

l1: ldi r19,225

 l2:

 nop

 dec r19

 

 brne l2

 dec r18

 brne l1

 ret


Simulation:

 

Hardware task:



Task 2:

Write a program to toggle all bits of PORT D with some delay (Specified by Lab Instructor). Simulate it on Proteus and implement it on Hardware.

 

Steps:  

1:  Initialize stack pointer. 

2:  Make Port B an output port.

3:  Load 0x55 in any general purpose register, say R16.

4:  Output R16 on Port B. 

5:  Call Delay.

6: Complement R16.

7: Repeat Forever

 

Code:

.include "m328Pdef.inc"

 

// lodaing stack pointer

 

ldi r16, HIGH(RAMEND)

out sph, r16

ldi r16, LOW(RAMEND)

out spl, r16

 

// make port B an output port

ldi r16, 0xFF

out DDRB, r16 // port b is output

 

// load 0x55

ldi r16, 0x55

 

// output r16 on port B

out PORTB, r16

 

// delay

call delay_200ms

 

// load 0xAA in

ldi r16, 0xAA

 

// output r16 on port B

out PORTB, r16

 

 

 

 

 

delay_200ms:

 

                ldi R19,80 // 80*100*100*4*1/16MHz

 

                outer_loop:

                rcall inner_loop

                dec R19

                brne outer_loop

 

                inner_loop:

                ldi R17,100

 

                label1:

                ldi R18,100

                label2:

                Nop

                Dec R18

                brne label2

                dec R17

                brne label1

                ret

 

 

 

Simulation:





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

 11:   ldi r19,100

 l2:

       nop

       dec r19

       brne l2

       dec r18

       brne l1

       ret

 

Simulation:







CRITICAL ANAYSIS:

 

In this lab we studied about the time delay and their calculation in AVR microcontrollers. We can either implement time delays by manual calculations and keeping registers engaged or by using the delay.h library for the microcontroller units.

In first 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. In second task a delay of 200 milliseconds as instructed by the lab teacher was implemented and its effect was observed on LEDs connected on Port D.

 

 

 

 

 

 

 

Post a Comment

Post a Comment (0)

Previous Post Next Post