Top Banner
Embedded System Spring, 2011 Lecture 6: Branch, Call and Time Delay Loop Eng. Wazen M. Shbair
26

Embedded System Spring, 2011 Lecture 6: Branch, Call and Time Delay Loop Eng. Wazen M. Shbair

Jan 29, 2016

Download

Documents

arvid

Embedded System Spring, 2011 Lecture 6: Branch, Call and Time Delay Loop Eng. Wazen M. Shbair. Today’s Lecture. Branch instructions and looping. Branch instructions and looping. Looping in PIC Loop inside loop Other conditional jumps All conditional branches are short jumps - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Embedded System Spring, 2011 Lecture 6: Branch, Call and Time Delay Loop Eng. Wazen M. Shbair

Embedded SystemSpring, 2011Lecture 6: Branch, Call and Time Delay LoopEng. Wazen M. Shbair

Page 2: Embedded System Spring, 2011 Lecture 6: Branch, Call and Time Delay Loop Eng. Wazen M. Shbair

2IUG- Embedded System

Today’s Lecture

Branch instructions and looping

Page 3: Embedded System Spring, 2011 Lecture 6: Branch, Call and Time Delay Loop Eng. Wazen M. Shbair

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

Page 4: Embedded System Spring, 2011 Lecture 6: Branch, Call and Time Delay Loop Eng. Wazen M. Shbair

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

Page 5: Embedded System Spring, 2011 Lecture 6: Branch, Call and Time Delay Loop Eng. Wazen M. Shbair

5

DECFSZ instruction

Decrement file register, skip the next instruction if the result is equal 0

DECFSZ fileRef, d

GOTO instruction follows DECFSZ

Page 6: Embedded System Spring, 2011 Lecture 6: Branch, Call and Time Delay Loop Eng. Wazen M. Shbair

6

Page 7: Embedded System Spring, 2011 Lecture 6: Branch, Call and Time Delay Loop Eng. Wazen M. Shbair

7

Example

Write a program to

a) Clear WREG

b) Add 3 to WREG ten times and place the result in SFR PORTB

Page 8: Embedded System Spring, 2011 Lecture 6: Branch, Call and Time Delay Loop Eng. Wazen M. Shbair

8

Solution

COUNT EQU 0x25 MOVLW d'10'

MOVWF COUNTMOVLW 0

AGAIN ADDLW 3 DECFSZ COUNT,FGOTO AGAIN MOVWF PORTB

Page 9: Embedded System Spring, 2011 Lecture 6: Branch, Call and Time Delay Loop Eng. Wazen M. Shbair

9

Solution

Page 10: Embedded System Spring, 2011 Lecture 6: Branch, Call and Time Delay Loop Eng. Wazen M. Shbair

10

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

Page 11: Embedded System Spring, 2011 Lecture 6: Branch, Call and Time Delay Loop Eng. Wazen M. Shbair

11

Example

Write a program to

a) Clear WREG

b) Add 3 to WREG ten times and place the result in SFR PORTB

Page 12: Embedded System Spring, 2011 Lecture 6: Branch, Call and Time Delay Loop Eng. Wazen M. Shbair

12

Solution

COUNT EQU 0x25 MOVLW d'10'

MOVWF COUNTMOVLW 0

AGAIN ADDLW 3 DECF COUNT,FBNZ AGAINMOVWF PORTB

Page 13: Embedded System Spring, 2011 Lecture 6: Branch, Call and Time Delay Loop Eng. Wazen M. Shbair

13

Solution

Page 14: Embedded System Spring, 2011 Lecture 6: Branch, Call and Time Delay Loop Eng. Wazen M. Shbair

14

Example

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

Page 15: Embedded System Spring, 2011 Lecture 6: Branch, Call and Time Delay Loop Eng. Wazen M. Shbair

15

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'

3-15

Page 16: Embedded System Spring, 2011 Lecture 6: Branch, Call and Time Delay Loop Eng. Wazen M. Shbair

16

Solution

MOVLW 0x55

MOVWF PORTB

MOVLW COUNT_1

MOVWF R1

LOP_1 MOVLW COUNT_2

MOVWF R2

LOP_2 COMPF PORTB, FDECF R2, F

BNZ LOP_2

DECF R1, FBNZ LOP_1

Address Data

 

25H (R1) 10

26H (R2) 70

F81H(PORTB)55

Page 17: Embedded System Spring, 2011 Lecture 6: Branch, Call and Time Delay Loop Eng. Wazen M. Shbair

17

Looping 100,00o times

Because two registers give us a maximum value of 65025, we can use three registers to get up to more 16 million iterations

Page 18: Embedded System Spring, 2011 Lecture 6: Branch, Call and Time Delay Loop Eng. Wazen M. Shbair

18

Page 19: Embedded System Spring, 2011 Lecture 6: Branch, Call and Time Delay Loop Eng. Wazen M. Shbair

19

Other conditional jumps

Page 20: Embedded System Spring, 2011 Lecture 6: Branch, Call and Time Delay Loop Eng. Wazen M. Shbair

20

BZ (Branch if Z=1)

The Z flag is checked If it high it jumps to the target address.

For example

OVER PORTB, W

JZ OVER In this example , if PORTB is zero, it jumpe to the

label OVER

Page 21: Embedded System Spring, 2011 Lecture 6: Branch, Call and Time Delay Loop Eng. Wazen M. Shbair

21

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 ...

Page 22: Embedded System Spring, 2011 Lecture 6: Branch, Call and Time Delay Loop Eng. Wazen M. Shbair

22

BNC (branch if no carry)

The carry flag bit in the status register is used to make the decision whether to jump. If it (c=0), the CPU starts to fetch and execute

instructions from the address of the label. If it (c=1), the CPU will execute the instruction below

BNC

Page 23: Embedded System Spring, 2011 Lecture 6: Branch, Call and Time Delay Loop Eng. Wazen M. Shbair

23

Example

Find the sum of the values 79H, F5H, and E2H. Put the sum in fileReg loc. 5H and 6H.

Page 24: Embedded System Spring, 2011 Lecture 6: Branch, Call and Time Delay Loop Eng. Wazen M. Shbair

24

Page 25: Embedded System Spring, 2011 Lecture 6: Branch, Call and Time Delay Loop Eng. Wazen M. Shbair

25

Quiz

Page 26: Embedded System Spring, 2011 Lecture 6: Branch, Call and Time Delay Loop Eng. Wazen M. Shbair

26IUG- Embedded System 26

References

Jie Hu , ECE692 Embedded Computing Systems , Fall 2010.

PIC Microcontroller And Embedded Systems: using Assembly and C for PIC 18, M. Mazidi, R. McKinlay and D. Causey, Prentice Fall, 2008.

Eng. Husam Alzaq, Embedded System Course, IUG, 2010