in~.
MCS@-51PROGRAMMER'SGUIDE AND INSTRUCTION SET
DJNZ
<byte>, <rel-addr>
Function:
DecrementandJumpif Not
=0
Description:
DJNZ decrementsthe location indicated by 1, and branchesto the address indicatedby the
second operandif the resulting value is not zero. An originalvalue of OOH will underflowto
OFFH.No tlags are at%cted.The branch destinationwouldbe computedby addingthe signed
relative-displacement v aluein the last instructionbyteto the PC, after incrementingthe PC to
the first byte of the followinginstruction.
The location decreznented maybe a register or directlyaddressedbyte.
Note: When
this
instruction is used to modfi an output port, the value used as the original
port data will be read from the output data latch, not the input pins.
Example:
Internal RAM locations 40H, 50~
and 60H containthe values OIH, 70H, and 15H,respec-
tively. The instructionsequence,
DJNZ 40H,LABEL-1
DJNZ 50H,LABEL-2
DJNZ 60H,LABEL-3
will cause a jump to the instructionat label LABEL-2 withthe valuesOOH, 6 FH, and 15Hin
the three W
locations The first jump was not taken becausethe result was zero.
This instruction provideaa simpleway of executinga programloop a givennumberof times,
or for addinga moderatetime delay (from 2 to 512machinecycles)with a singleinstruction.
The instruction sequence,
MOV
R2,#8
TOOOLE:
CPL
P1.7
DJNZ
R2,TOOGLE
will toggle P1.7 eight times, causing four output pukes to appear at bit 7 of output Port 1.
Each pulse will last three machinecycles;two for DJNZ and one to alter the pin.
DJNZ Rn,rel
Bytee:
2
cycles:
2
Encoding:
I 1101
11'"1
EEl
Operation:
DJNZ
(PC!)-
(PC) + 2
m)
-(w
– 1
w
~~~
0 or (I@ < t)
(PC) + (PC)+
rd
2-43
Need help?
Do you have a question about the MCS 51 and is the answer not in the manual?