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

ES Chapter3 2

This document discusses branch, call, and time delay loop instructions in PIC microcontrollers. It covers branching instructions like GOTO, conditional branches using flags, looping using DECFSZ and BNZ/BZ, nested loops, and unconditional branching. It also discusses the CALL instruction for calling subroutines, how the stack and stack pointer work to save the return address, and examples of using CALL and RETURN.

Uploaded by

Hans John D'cruz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views

ES Chapter3 2

This document discusses branch, call, and time delay loop instructions in PIC microcontrollers. It covers branching instructions like GOTO, conditional branches using flags, looping using DECFSZ and BNZ/BZ, nested loops, and unconditional branching. It also discusses the CALL instruction for calling subroutines, how the stack and stack pointer work to save the return address, and examples of using CALL and RETURN.

Uploaded by

Hans John D'cruz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 59

PIC Microcontroller and

Embedded Systems
Muhammad Ali Mazidi, Rolin McKinlay and Danny Causey

Eng. Husam Alzaq


The Islamic Uni. Of Gaza

The PIC uCs 3-1


Chapter 3:
Branch, Call and Time
Delay Loop
 Branch instruction and
looping
 Call instruction and stack PIC Microcontroller
and Embedded Systems
 PIC18 Time Delay and Muhammad Ali Mazidi,
Rolin McKinlay and
instruction pipeline Danny Causey, February
2007.

The PIC uCs 3-2


Objective
 Code PIC Assembly language instructions to
create loops and conditional branch
instructions
 Code Goto instructions for unconditional
jump

The PIC uCs 3-3


Branch instructions and looping
 Looping in PIC
 Loop inside loop
 Other conditional jumps
 All conditional branches are short jumps
 Calculating the short branch address
 Unconditional branch instruction

The PIC uCs 3-4


Looping in PIC
 Repeat a sequence of instructions or a
certain number of times
 Two ways to do looping
 Using DECFSZ instruction
 Using BNZ\BZ instructions

The PIC uCs 3-5


DECFSZ instruction
 Decrement file
register, skip the
next instruction if
the result is equal
0
 DECFSZ fileRef, d
 GOTO instruction
follows DECFSZ

The PIC uCs 3-6


Example 3-1
 Write a program to  Solution
a) Clear WREG COUNT EQU 0x25
b) Add 3 to WREG MOVLW d'10'
ten times and MOVWF COUNT
place the result in MOVLW 0
SFR PORTB
AGAIN ADDLW 3
DECFSZ COUNT,F
GOTO AGAIN
MOVWF PORTB
The PIC uCs 3-7
Figure 3-1. Flowchart for the
DECFSZ Instruction

The PIC uCs 3-8


Using BNZ\BZ instructions
 Supported by PIC18 families
 Early families such as PIC16 and PIC12 doesn’t
support these instruction
 These instructions check the status flag

Back ……………….
……………….
DECF fileReg, f
BNZ Back

The PIC uCs 3-9


Example 3-2
 Write a program to  Solution
a) Clear WREG COUNT EQU 0x25
b) Add 3 to WREG MOVLW d'10'
ten times and MOVWF COUNT
place the result in MOVLW 0
SFR PORTB
AGAIN ADDLW 3
DECF COUNT,F
BNZ AGAIN
MOVWF PORTB
The PIC uCs 3-10
Figure 3-2. Flowchart for Example 3-2
The PIC uCs 3-11
Example 3-3
 What is the maximum number of times that
the loop can be repeated?
 All locations in the FileReg are 8-bit
 The max. loop size is 255 time

The PIC uCs 3-12


Loop inside a loop
 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'
The PIC uCs 3-13
Solution
MOVLW 0x55 Address Data
MOVWF PORTB
MOVLW COUNT_1
MOVWF R1 25H (R1) 10
LOP_1 MOVLW COUNT_2
26H (R2) 70
MOVWF R2 …
LOP_2 COMPF PORTB, F …
DECF R2, F
BNZ LOP_2 F81H
DECF R1, F (PORTB) 55
The PIC uCs
BNZ LOP_1 3-14
The PIC uCs
Figure 3-3. Flowchart 3-15
Figure 3-3. (continued)
The PIC uCs 3-16
Other conditional jumps
 All of the 10 conditional jumps are 2-byte
instructions
 They requires the target address
 1 byte address (short branch address)
 Relative address

 Recall: MOVF will affect the status Reg.


 In the BZ instruction, the Z flag is
checked. If it is high, that is equal 1, it
jumps to the target address.

The PIC uCs 3-17


Flag Bits and Decision Making

The PIC uCs 1-18


Example 3-5
 Write a program to determine if the loc.
0x30 contains the value 0. if so, put 55H in it.
 Solution:
 MYLOC EQU Ox30
 MOVF MYLOC, F
 BNZ NEXT
 MOVLW 0x55
 MOVWF MYLOC
 NEXT ...
The PIC uCs 3-19
Example 3-6
 Find the sum of the values 79H, F5H, and
E2H. Put the sum in fileReg loc. 5H and 6H.
Address Data Address Data Address Data

5H (Low- 5H (Low- 5H (Low-


Byte) 0 Byte) 0 Byte) 50
6H (High- 6H (High- 6H (High-
Byte) 0 Byte) 1 Byte) 2
… … …
79+F5
79 = 16E 6E 6E+E2 50
The PIC uCs = 150 3-20
Solution
L_Byte EQU 0x5
H_Byte EQU 0x6 N_1 ADDLW 0xF5
ORG 0h BNC N_2
MOVLW 0x0 INCF H_Byte,F
MOVWF H_Byte N_2 ADDLW 0xE2
ADDLW 0x79 BNC OVER
BNC N_1 INCF H_Byte,F
INCF H_Byte,F OVER MOVWF L_Byte
END
The PIC
The PICuCs
uCs 3-21
Example 3-7
000000 0E00 00004 MOVLW 0x0
000002 6E06 00005 MOVWF H_Byte
000004 0F79 00006 ADDLW 0x79
000006 E301 00007 BNC N_1
000008 2A06 00008 INCF H_Byte,F
00000A 0FF5 00009 N_1 ADDLW 0xF5
00000C E301 00010 BNC N_2
00000E 2A06 00011 INCF H_Byte,F
000010 0FE2 00012 N_2 ADDLW 0xE2
000012 E301 00013 BNC OVER
000014 2A06 00014 INCF H_Byte,F
000016 6E05
The PIC uCs
00015 OVER MOVWF L_Byte 3-22
Example 3-7

The PIC uCs 3-23


Example 3-8

The PIC uCs 3-24


Question?
Which is better, to use BNZ along with DECF
or DCFSNZ??

The PIC uCs 3-25


Unconditional branch
instruction
 Control is transferred unconditionally to
the target location (at ROM)
 Tow unconditional branches
 GOTO
 BRA

The PIC uCs 3-26


Figure 3-4. GOTO Instruction
The PIC uCs 3-27
BRA Instruction

Figure 3-5. BRA (Branch Unconditionally


Instruction Address Range
The PIC uCs 3-28
BRA Instruction
Forward
jump

Backward
The PIC uCs
jump 3-29
GOTO to itself
 Label and $ can be used to keep uC busy
(jump to the same location)
 HERE GOTO HERE
 GOTO $

 OVER BRA OVER


 BRA $

The PIC uCs 3-30


PIC18 Call instruction

Section 3-2
The PIC uCs 3-31
Call instruction

Call a
subrutine

Call Rcall
4-byte instruction 2-byte instruction
Long Call Relative Call
The PIC uCs 3-32
CALL Instruction

110S

Figure 3-6. CALL Instruction


The PIC uCs 3-33
CALL Instruction
 Control is transferred to subroutine
 Current PC value, the instruction address
just below the CALL instruction, is stored
in the stack
 push onto the stack

 Return instruction is used to transfer the


control back to the caller,
 the previous PC is popped from the stack

The PIC uCs 3-34


Stack and Stack Pointer (SP)
 Read/Write Memory
 Store the PC Address
 21-bit (000000 to 1FFFFF)
 5-bit stack, total of 32 locations
 SP points to the last used location of the
stack
 Location 0 doesn’t used
 Incremented pointer

The PIC uCs 3-35


Figure 3-7. PIC Stack 31 × 21
The PIC uCs 3-36
Return from Subroutine
 The stack is popped and the top of the stack
(TOS)is loaded into the program counter.
 If ‘s’ = 1, the contents of the shadow
registers WS, STATUSS and BSRS are
loaded into their corresponding registers, W,
STATUS and BSR.
 If ‘s’ = 0, no update of these registers occurs
(default).

The PIC uCs 3-37


Example 3-9
 Toggle all bits of to SFR register of PORTB
by sending to it values 55H and AAH
continuously. Put a delay in between issuing of
data to PORTB .
 Analyze the stack for the CALL instructions

The PIC uCs 3-38


Solution
MYREG EQU 0x08
PORTB EQU 0x0F8
ORG 20H
ORG 0
DELAY MOVLW 0xFF
BACK MOVLW 0x55
MOVWF PORTB MOVWF MYREG
CALL DELAY AGAIN NOP
MOVLW 0xAA NOP
MOVWF PORTB DECF MYREG, F
CALL DELAY BNZ AGAIN
RETURN
GOTO BACK
The PIC
The PICuCs
uCs
END 3-39
Example
3-10
Address Data
4
3
2
1 000008
000016

The PIC uCs 3-40


Figure 3-8. PIC Assembly Main
Program That Calls Subroutines
The PIC uCs 3-41
RCALL (Relative Call)
 2-Byte instruction
 The target address must be within 2K
 11 bits of the 2 Byte is used
 Save a number of bytes.

The PIC uCs 3-42


Example 3-12

The PIC uCs 3-43


PIC18 Time Delay and
instruction pipeline

Section 3-3
The PIC uCs 3-44
Delay Calculating for PIC18
 Two factors can affect the accuracy of
the delay
1. The duration of the clock period, which is
function of the Crystal freq
 Connected to OSC! And OSC2
2. The instruction cycle duration
 Most of the PIC18 instructions consumes 1
cycle
• Use Harvard Architecture
• Use RISC Architecture
• Use the pipeline concept between fetch and execute.
The PIC uCs 3-45
Figure 3-9. Pipeline vs. Non-pipeline
The PIC uCs 3-46
PIC multistage pipeline
 Superpipeline is used to speed up
execution.
 The process of executing instructions is
split into small steps
 Limited to the slowest step

The PIC uCs 3-47


Figure 3-10. Pipeline Activity After
the Instruction Has Been Fetched
The PIC uCs 3-48
Figure 3-11. Pipeline Activity
for Both Fetch and Execute
The PIC uCs 3-49
Instruction Cycle time for the
PIC
 What is the Instruction Cycle ?
 Most instructions take one or tow cycles
 BTFSS can take up to 3 cycles
 Instruction Cycle depends on the freq. of
oscillator
 Clock source: Crystal oscillator and on-chip
circuitry
 One instruction cycle consists of four
oscillator period.

The PIC uCs 3-50


Example 3-14
 Find the period of the instruction cycle you
chose 4 MHz crystal? And what is required
time for fetching an instruction?
 Solution
 4 MHz/4 =1 MHz
 Instruction Cycle = 1/1MHz = 1 usec
 Fetch cycle = 4 * 1 usec = 4 usec

The PIC uCs 3-51


Branch penalty
 Queue is needed for prefetched
instruction
 If the prefetched instruction is incorrect,
the CPU must flush the memory. When??

The PIC uCs 3-52


Branch penalty

The PIC uCs 3-53


BTFSC and BTFSS

The PIC uCs 3-54


Example 3-15
 Find how long it take to execute each of
the following instructions for a PIC18 with
4 MHz
 MOVLW
 ADDLW CALL
 DECF GOTO
 NOP BNZ
 MOVWF

The PIC uCs 3-55


Delay calculation for PIC18
Example 3-16
 Find the size of the delay in the following
program if the crystal freq. is 4MHz.
DELAY MOVLW 0xFF
MOVWF MYREG
AGAIN NOP
NOP
DECF MYREG, F
BNZ AGAIN
RETERN
The PIC uCs 3-56
Example 3-17
MYREG EQU 0x08 ORG 300H
ORG 0 DELAY MOVLW 0xFA
BACK MOVLW 0x55 MOVWF MYREG
MOVWF PORTB AGAIN NOP
CALL DELAY NOP
MOVLW 0xAA NOP
MOVWF PORTB DECF MYREG, F
CALL DELAY BNZ AGAIN
GOTO BACK RETURN
The PIC uCs 3-57
DELAY_500MS

Example 3-20 MOVLW


MOVWF
D'20'
R4
BACK
R2 EQU 0x2 MOVLW D'100'
MOVWF R3
R3 EQU 0x3 AGAIN
R4 EQU 0x4 MOVLW D'250'
MOVWF R2
MOVLW 0x55
HERE NOP
MOVWF PORTB NOP
BACK DECF R2, F
BNZ HERE
CALL DELAY_500MS DECF R3, F
COMF PORTB BNZ AGAIN
DECF R4, F
GOTO BACK
BNZ BACK
The PIC uCs RETURN 3-58
Chapter 3: Summary
 Looping in PIC Assembly language is
performed using an instruction to decrement a
counter and to jump to the top of the loop if
the counter is not zero.
 Assembly language includes conditional and
unconditional, and call instructions.
 PIC18 uses Superpipeline is used to speed up
execution.
Next: Chapter 4
PIC I/O Port
Programming
The PIC uCs 3-59

You might also like