Tutorial 2
Note: We use AVR ATmega32 microprocessor in this homework.
Q1. (30 pts) Find the time delay for the delay subroutine shown below if the system
has an AVR with a frequency of 10 MHz:
LDI R20, 200
BACK: LDI R25, 100
NOP
NOP
NOP
HERE: DEC R25
BRNE HERE
DEC R20
BRNE BACK
Ans.
Instruction MCs
LDI R20, 200 1
BACK: LDI R25, 100 1
HERE: NOP 1
NOP 1
NOP 1
DEC R25 1
BRNE HERE 1/2
DEC R20 1
BRNE BACK 1/2
1 MC = 1/(10 MHz) = 0.1 s
Total MCs = Time delay = 1+(1+(3+1+2)x100–1+1+2)x200–1= 120600 MCs
= 120600 x 0.1 s = 12060 s = 12.06 ms
If we include RET instruction (4 MCs) Time delay = 12060.4 s
Q2. (15 pts) Write a program to get 8-bit data from PORTC and send it to PORTB
and PORTD.
Ans.
.include “M32DEF.INC”
.org 0
LDI R16, 0
OUT DDRC, R16; PORTC: Input port
LDI R16, 0xFF
OUT DDRB, R16; PORTB: Output port
OUT DDRD, R16; PORTD: Output port
LOOP: IN R16, PINC
OUT PORTB, R16
OUT PORTD, R16
RJMP LOOP
Q3. (25 pts) Write a program to monitor the PA0 bit. When it is HIGH, send $99 to
PORTB. If it is LOW, send $66 to PORTB.
Ans.
.include “M32DEF.INC”
.rg 0
LDI R16, $FF
OUT DDRB, R16 ; PORTB: Output Port
CBI DDRA, 0 ; PA0 is input pin
AGAIN: SBIC PINA,0 ; Skip next if PA0 is clear
RJMP OVER ; Jump to OVER if PA0 is high
LDI R16,0x66
OUT PORTB, R16 ; Send 0x66 to PORTD when PA0 = 0
RJMP AGAIN
OVER: LDI R16,0x99
OUT PORTB, R16 ; Send 0x99 to PORTD when PA0 = 1
RJMP AGAIN
Q4. (30 pts) Write a program to monitor the PB5 and PB6 bits. When both of them
are HIGH, send $AA to PORTC; otherwise, send $55 to PORTC.
Ans.
.include “M32DEF.INC”
.org 0
LDI R16, $FF
OUT DDRC, R16 ; PORTC: Output Port
CBI DDRB, 5 ; PB5 is input pin
CBI DDRB, 6 ; PB6 is input pin
AGAIN: SBIC PINB,5 ; Skip next if PB5 is clear
RJMP OVER ; Jump to OVER if PB5 is high
RJMP OTHER
OVER: SBIS PINB,6 ; Skip next if PB6 is set
RJMP OTHER ; Jump to OVER if otherwise
LDI R16,$AA
OUT PORTC,R16 ; Send $AA to PORTC if PB5=PB6=1
RJMP AGAIN
OTHER: LDI R16,$55
OUT PORTC,R16 ; Send $55 to PORTC if otherwise
RJMP AGAIN