Docsity

PIC Microcontroller Fundamentals: Branching, Looping, and Stack Operations, Lecture notes of Embedded Systems

Partial preview of the text

Download PIC Microcontroller Fundamentals: Branching, Looping, and Stack Operations and more Lecture notes Embedded Systems in PDF only on Docsity!

Chapter 3:

BRANCH, CALL, AND

TIME DELAY LOOP

“PIC Microcontroller and Embedded Systems”, Muhammad

Ali Mazidi, Rolin D. Mckinlay, Danny Causey Prentice Hall,

Embedded

System Design,

Bahria University

Islamabad

SECTION 3.

BRANCH INSTRUCTIONS

AND LOOPING

DECFSZ

instructio

n and

looping

  • (^) The DECFSZ (decrement fileReg skip zero)

It is widely use instruction to for looping

  • (^) This instruction decremented fileReg, if its content is

zero, it skips the next instruction.

  • (^) By placing the “ GOTO target ” instruction right below it

we can create a loop

  • (^) The target address of the “ GOTO target ” instruction is

the beginning of the loop

DECFSZ

instructio

n and

looping

  • (^) Flowchart for the DECFSZ instruction

BNZ

instructio

n for

looping

  • (^) BNZ (branch if not zero) is used for looping

It uses the zero flag in the status register

  • (^) In last two instructions, fileReg is decremented: if it is

not zero ,it branches (jumps) back to target address

referred to by the label

  • (^) Prior to the start of the loop the fileReg is loaded with

the counter value

  • (^) BNZ instruction refers to the Z flag of the status

register affected by previous instruction.

BNZ

instructio

n for

looping

BNZ

instructio

n for

looping

Loop

inside

loop

  • (^) Loop inside loop

It is shown previous example that maximum count

in 255.

What happens if we want to repeat an action more

than 255?

We use loop inside loop called nested loop

We use two registers to hold the count

Loop

inside

loop

Loop

inside

loop

  • (^) Two register give a maximum value of 65025

(255x255), we use three registers to get up to more

than 16 million (2^24) iterations. For 100,000 time

Other

condition

al jumps

BZ

  • (^) BZ(branch if Z=1)

In this instruction zero flag is checked

If high (z=1), jumps to the target address

If PORTB is zero, it jump to the label OVER

We don’t have to perform an arithmetic instruction

such as decrement to use the BZ instruction.

OVER MOVF PORTB, W ; read Port b and put it in WREG

BZ OVER ; jump if WREG is zero

Other

condition

al jumps

BZ

Other

condition

al jumps

BNZ

Condition

al jumps

are short

jumps

  • (^) All conditional jumps are short jumps

It means that the address of the target must be with in

the 256 bytes if the 256 bytes if the contents of the

program counter (PC)