microcontroller mcs-51: branch instruction

27
MICROCONTROLLER MCS- 51: BRANCH INSTRUCTION Arkhom JODTANG Civil Aviation Training Center

Upload: arkhom-jodtang

Post on 24-Jun-2015

592 views

Category:

Education


3 download

DESCRIPTION

Microcontroller MCS-51: Branch Instruction Week 3 (ATAE1, ATAI1)

TRANSCRIPT

Page 1: Microcontroller MCS-51: Branch Instruction

MICROCONTROLLER MCS-51: BRANCH INSTRUCTIONArkhom JODTANG

Civil Aviation Training Center

Page 2: Microcontroller MCS-51: Branch Instruction

2

Branch Instruction

Contents Flowchart

Branch Instructions Design program

Page 3: Microcontroller MCS-51: Branch Instruction

3

Flowchart

A flowchart is a diagrammatic representation that illustrates the sequence of operations to be performed to get the solution of a problem.

Flowcharts are generally drawn in the early stages of formulating computer solutions.

Flowcharts facilitate communication between programmers and business people.

These flowcharts play a vital role in the programming of a problem and are quite helpful in understanding the logic of complicated and lengthy problems.

Page 4: Microcontroller MCS-51: Branch Instruction

4

Flowchart Symbols

Terminator The start or end of program flow

Initial / Preparation Setting a value at the beginning of the

process or initialize the routine Process

Any kind of processing function, such as a variable assignment or mathematical operation.

Predefined Process A named process, such as a subroutine, a

module, Procedure or Function.

Start

Page 5: Microcontroller MCS-51: Branch Instruction

5

Flowchart Symbols

Condition Decision Select flow direction from

condition

Connector Jumping destination

Flow lineconnect the flowchart symbols and show the sequence of operations during the program execution.

True

False

Page 6: Microcontroller MCS-51: Branch Instruction

6

Flowchart Sample

Start

A = 30H

Process

End

MainLoop:

DJNZ A, #10H, M

ainLoop:

Page 7: Microcontroller MCS-51: Branch Instruction

7

Sample of Branch Instruction(Unconditional Jump)

ORG 0HMOV R1,#40HLoop:

MOV @R1,#22HINC R1JMP Loop

END

Page 8: Microcontroller MCS-51: Branch Instruction

8

Sample of Branch Instruction(Conditional Jump)

ORG 0HMOV R1,#40HLoop:

MOV @R1,#11HINC R1CJNE R1, #50H, Loop

END

Page 9: Microcontroller MCS-51: Branch Instruction

9

SJMP rel.

Jump to specified relative address -128 to +127 locations (Short Jump) No condition. Sample

SJMP NO_Task Jump to label name is No_Task

Page 10: Microcontroller MCS-51: Branch Instruction

10

AJMP adr11

Jump to any specific location Long distance. (211 = 2kByte long) No condition. Sample

AJMP Loop3 Jump to label name is Loop3

Page 11: Microcontroller MCS-51: Branch Instruction

11

JMP adr16, LJMP adr16

Jump to any specific location Long distance. (216 = 64kByte long) No condition. 3 Bytes instruction 4 Machine cycles Sample

LJMP Program5 Jump to label name is Program5

Page 12: Microcontroller MCS-51: Branch Instruction

12

JC rel

Condition: Jump if carry bit is set Jump to specific destination Short Jump 3 Machine cycles Sample

JC Store_It If Carry bit = Set, Jump to label “Store_It”

Page 13: Microcontroller MCS-51: Branch Instruction

13

JNC

Condition: Jump if carry bit is clear Jump to specific destination Short Jump 3 Machine cycles Sample

JNC EasyTask If Carry = 0, Jump to EasyTask

Page 14: Microcontroller MCS-51: Branch Instruction

14

JNC

Sample To assign value of Carry Flag to R0

ORG 00HMOV R0, #00HJNC CarryIsZeroMOV R0,#01HCarryIsZero:END

Page 15: Microcontroller MCS-51: Branch Instruction

15

JB bit, rel

Condition: Jump if addressed bit is set Jump to specific destination Short Jump 4 Machine cycles Sample

JB 00h, EasyTask If bit address 00h is set, Jump to label EasyTask:

JB P0.4, PressedS4 If bit P0.4 = set. Jump to label PressedS4

Page 16: Microcontroller MCS-51: Branch Instruction

16

JNB bit, rel

Condition: Jump if addressed bit is clear Jump to specific destination Short Jump 4 Machine cycles Sample

JNB 00h, EasyTask If bit address 00h is clear, Jump to label

EasyTask:

Page 17: Microcontroller MCS-51: Branch Instruction

17

JBC bit, rel

Condition: Jump if addressed bit is set Clear bit Jump to specific destination Short Jump 4 Machine cycles Sample

JBC 00h, EasyTask If bit address 00h is set, jump to label

EasyTask:, Clear bit 00h

Page 18: Microcontroller MCS-51: Branch Instruction

18

JZ rel

Condition: Jump if Acc equal to zero Jump to specific destination Short Jump 3 Machine cycles Sample

JZ EmptyNow If ACC = 0, Jump to label EmptyNow:

Page 19: Microcontroller MCS-51: Branch Instruction

19

JNZ rel

Condition: Jump if Acc not equal zero Jump to specific destination Short Jump 3 Machine cycles Sample

JNZ SomeValue If ACC > 0, Jump to label Somevalue:

Page 20: Microcontroller MCS-51: Branch Instruction

20

CJNE A, Rx, rel

Condition: Jump if Acc not equal to Rx Jump to specific destination Short Jump 4 Machine cycles Sample

CJNE A, 30h, Loop2 If ACC ≠ 30h, Jump to label Loop2:

Page 21: Microcontroller MCS-51: Branch Instruction

21

CJNE A, #X, rel

Condition: Jump if Acc not equal to #X Jump to specific destination Short Jump 4 Machine cycles Sample

CJNE A, #3Eh, Loop3 If ACC ≠ #30h, Jump to label Loop3:

Page 22: Microcontroller MCS-51: Branch Instruction

22

CJNE Rn, #X, rel

Condition: Jump if Rn not equal to #X Jump to specific destination Short Jump 4 Machine cycles Sample

CJNE R4, #40h, Loop4 If R4 ≠ #40h, Jump to label Loop4:

Page 23: Microcontroller MCS-51: Branch Instruction

23

CJNE @Ri, #X, rel

Condition: Jump if register addressed by Ri not equal to #X

Jump to specific destination Short Jump 4 Machine cycles Sample

CJNE @R0, #E0h, Loop5 If register which is addressed by R0 ≠ #E0h,

Jump to label Loop5:

Page 24: Microcontroller MCS-51: Branch Instruction

24

CJNE (Sample)

Write program to fill the memory area with counting number start from 1H to 30H. Start from address 30h

Page 25: Microcontroller MCS-51: Branch Instruction

25

DJNZ Rn, rel

Process: Decrease Rn by one, then Condition: Jump if Rn not equal to zero Jump to specific destination Short Jump 3 Machine cycles Sample

DJNZ R5, Loop6 Decrease R5 then check, If R5 = 0, Jump to

label Loop6:

Page 26: Microcontroller MCS-51: Branch Instruction

26

DJNZ Rx, rel

Process: Decrease Rx by one, then Condition: Jump if Rx not equal to zero Jump to specific destination Short Jump 4 Machine cycles Sample

DJNZ R5, Loop6 Decrease R5 then check, If R5 = 0, Jump to

label Loop6:

Page 27: Microcontroller MCS-51: Branch Instruction

27

DJNZ (Sample)

; DJNZ is very convenion for ; specified number of turn for some

process. ORG 0H MOV R5,#10D Loop: INC A ; Process DJNZ R5, Loop END