Documente Academic
Documente Profesional
Documente Cultură
(3 + 1)
Course Book:
The AVR Microcontroller and Embedded System Using assembly and C, 2 nd edition, by M. Ali
Mazidi, Sarmad Naimi and Sepehr Naimi.
Course Distribution
1- Before Midterm (Ch # 1 to Ch # 7)
2- After Midterm (Ch # 8 to Ch # 13)
Ch # 3: Branch, Call, And Time Delay Loop
In the sequence of instructions to be executed, it is often necessary to transfer program control to a different
location. (Loops)
Such instructions are called control transfer instructions.
Loops or jumps
Conditional Jumps
Those jump which depends upon values of flag registers or status register( Sreg)
Unconditional branches (Jumps).
Those jumps which are independent of any prior condition or status of flags.
Independent of any condition
SECTION 3.1:
BRANCH INSTRUCTIONS AND LOOPING
Looping
Repeating a sequence of instructions or an operation a certain number of times is called a loop.
In the AVR, there are several ways to repeat an operation many times.
One way is to repeat the operation over and over until it is finished, as shown below in AVR.
SECTION 3.1:
BRANCH INSTRUCTIONS AND LOOPING
Conditional Jumps
Using BRNE instruction for looping
The BRNE (branch if not equal) instruction uses the zero flag in the status register. The BRNE instruction is used as follows:
BACK: .........; start of the loop
.........; body of the loop
.........; body of the loop
DEC Rn ; decrement Rn, Z = 1 if Rn 0
Rn is a register that define that how many time this loop will run.
SECTION 3.1:
BRANCH INSTRUCTIONS AND LOOPING
Write a program to (a) clear R20, then (b) add 3 to R20 ten times, and (c) send the sum to PORTB. Use the zero
flag and BRNE.
LDI Rl6, 10 ; Rl6 = 10 (decimal) for counter
LDI R20, 0 ; R20 = 0
LDI R21, 3 ; R21 = 3
AGAIN:
ADD R20, R21 ; add 03 to R20 (R20 = sum)
DEC Rl6 ; decrement Rl6 (counter)
BRNE AGAIN ; repeat until COUNT = 0
OUT PORTB, R20 ; send sum to PORTB
Quiz # 1 (For G1 only)
Each question carry 3 marks
time = 20 mints
1. 2D = 45 decimal
2. 0010 1000 1011 = 29B
3. 6B2 = 1714 decimal
4. 2^12 = 4096 location, each location = 4 bits. Org =4096*4=4K*4 16k= 4K(4096
locations)*4(each location)
5. Erasable Prom and Electrically earasble PROm
6. Registers, ALU Unit, program counter and instruction decoder
SECTION 3.1:
BRANCH INSTRUCTIONS AND LOOPING
Write a program to (a) load the PORTB register with the value Ox55, and (b) complement Port B 700 times.
LDI Rl6, Ox55 ;Rl6 = Ox55
OUT PORTB, Rl6 ; PORTB = Ox55
LDI R20, 10 ;load 10 into R20 (outer loop count)
LOP_l:
LDI R21, 70 ;load 70 into R21 (inner loop count)
LOP_2:
COM Rl6 ;complement R16
OUT PORTB, Rl6 ;load PORTB SFR with the complemented value
DEC R21 ;Dec R21 (inner loop)
BRNE LOP_2 ;repeat it 70 times
DEC R20 ;Dec R20 (outer loop)
BRNE LOP_1 ;repeat it 10 times
SECTION 3.1:
BRANCH INSTRUCTIONS AND LOOPING
BRSH example
Q#1 – Why flowchart is used. Also draw and define each element used in flowchart.
Q#2 – Make flow chart of the following.
1) Write a program to (a) clear R20, then (b) add 3 to R20 ten times, and (c) send the sum to PORTB. Use the zero flag and
BRNE.
2) Write a program to (a) load the PORTB register with the value Ox55, and (b) complement Port B 700 times.