EEE316 Microprocessors: Lecture #3
EEE316 Microprocessors: Lecture #3
Microprocessors
LECTURE #3
Asst. Prof. Dr. Volkan
KILIÇ
Today’s discussion
• Chapter 3 review
– Looping in PIC
– Loop inside loop
– Other conditional jumps
– All conditional branches are short jumps
– Calculating the short branch address
– Unconditional branch instruction
Looping in PIC
• Repeat a sequence of instructions or a certain
number of times
• Two ways to achieve looping:
– Using DECFSZ instruction
– Using BNZ\BZ instructions
DECFSZ instruction
Back ……………….
……………….
DECF fileReg, f
BNZ Back
Example
• Write a program to
a) Clear WREG
b) Add 3 to WREG ten times and place
the result in SFR PORTB
Solution
COUNT EQU 0x25 ;use loc 25h for counter
MOVLW d'10'
MOVWF COUNT
MOVLW 0
AGAIN ADDLW 3
DECF COUNT,F
BNZ AGAIN
MOVWF PORTB
Solution
Example 3-3
• What is the maximum number of times that the
loop can be repeated?
• All locations in the FileReg are 8-bit
• Hence the max. loop size is 255 time
Loop inside a loop (nested loop)
e.g. Write a program to:
a) Load the PORTB SFR register with the value 55H
b) Complement PORTB 700 times
Solution
R1 EQU 0x25
R2 EQU 0x26
COUNT_1 EQU d'10'
COUNT_2 EQU d'70'
Solution
Address Data
MOVLW 0x55
MOVWF PORTB
MOVLW COUNT_1
25H (R1) 10
MOVWF R1
LOP_1 MOVLW COUNT_2
MOVWF R2 26H (R2) 70
LOP_2 COMF PORTB, F …
DECF R2, F …
BNZ LOP_2
DECF R1, F
F81H(PORTB) 55
BNZ LOP_1
Solution
Looping 100,000 times
• Because two registers give us a maximum value
of 65025, we can use three registers to get up to
more 16 million iterations
17
Other Conditional Branch Instructions
BZ (Branch if Z=1)
• The Z flag is checked
– If it is high, it jumps to the target address.
• For example
OVER DECF PORTB, W
BZ OVER
• In this example, if PORTB is zero, it jumps to the
label OVER
Example 3-5