Professional Documents
Culture Documents
Micro Controller
Micro Controller
Micro Controller
1. MOV A , Rn
Ex . MOV A , R6
Ex . MOV A , 30H
4 . MOV A , #data
Description : Move immediate data to accumulator
Operation : data [A]
Bytes : 1
Ex . mov A , #25
After execution of above instruction A register contains 25H.
#symbol indicates in instruction that it is immediate data.
5. MOV Rn , A
Description : move data from A to register
Operation : [A] [Rn]
Bytes : 1
Ex. MOV R5 , A
6. MOV direct , A
Description : Move accumulator to direct byte
Operation : [A] [Direct]
Bytes : 2
Ex. MOV 40H , A
If a contents is 22H , after the execution memory location 40H
contains 22H.
7. MOV @Ri , A
Description : Move accumulator to indirect memory specified (i.e.
index register Ri)
Bytes : 1
Operation : [A] [[Ri]]
Ex. MOV @Ro , A
9.MOV Rn , #data
Description : Move immediate byte to register.
Bytes : 2
Operation : Data [Rn]
Ex. MOV R2 , #EFH . Contents of R2 becomes EFH
10. MOV direct , Rn
Description : Move register to direct byte.
i.e. move the contents of register to the internal data memory
Bytes : 2
Operation : [Rn] [direct address]
Ex. MOV 30H , R2
MOVC INSTRUCTION
Syntax : MOVC A , @A+ ( base register ) Where base register
may be DPTR or PC .
Function : move code byte
Bytes : 1
Description : The MOVC instruction load the A with a code byte or
constant from program memory . The address of the byte fetched
is the sum of the original unsigned 8 bit accumulator contents and
the contents of a 16 bit base register which may be either data
pointer or PC
1) MOVC A , @A+DPTR
Description : move code byte relative to DPTR i.e. copy the code
byte , found at the ROM address formed by adding A and the
DPTR to A .
Bytes : 1
[ DPTR ]
[A]
1234
effective address
123A
2)MOVC A , @A+PC
Description : move code byte relative to PC . In this case , The
PC is incremented to the address of the next instruction . Then
the byte is fetched from the address formed by the sum of 8 bit
accumulator contents and the contents of a 16 bit register PC.
Operation : PC = PC+1
Bytes : 1
(A+PC) (A)
MOVX instruction
Syntax : MOVX destination , source
Description : move external data byte . The MOVX instruction
transfer data between the accumulator and a byte of external data
memory. There are two types of instructions , differing whether 8
bit or 16 bit indirect address to external data RAM.
1) MOVX @Ri , A
Description : move A to external RAM ( 8 bit address ) i.e. move
the contents of accumulator to the external data memory pointed
by Ri .
Bytes : 1
Operation : [A] [[Ri]]
Ex : MOVX @Ro , A.
If [A] = 33H and [Ro] = 40H , then after execution of
2) MOVX A , @Ri
Description : move indirect external RAM (8 bit address) to
accumulator
i.e. move the contents of external data memory pointed by Ri to
accumulator
Bytes : 1
Operation : [[Ri]] [A]
Ex : MOVX A , @Ro
If [Ro] = 40H and [40H] = 33H then after execution accumulator
contains 33H
bits i.e. contents of DPH while Po outputs low order eight bits i.e.
contents of DPL with data .
1) MOVX A , @DPTR
Description : move the contents of external data memory pointed
by DPTR to accumulator
Bytes : 1
Operation [DPTR] [A]
If DPTR contains 8000H , And the contents of location 8000H is
22H , then after execution of instruction , A contains 22H.
2) MOVX @DPTR , A
Description : move the contents of accumulator to external data
memory location pointed by DPTR.
Bytes : 1
Operation : [A] [DPTR]
If [A] = 15H , [DPTR] = 1234 , then after execution the contents of
location 1234H becomes 15H.
The PUSH and POP opcodes specify the direct address of the
data . The data moves between an area of internal RAM , Known
as the STACK and the specified direct address . The stack pointer
is a special function register ( SP ) contains the address of RAM
where data from the source address will be pushed or where data
to be popped to the destination address . No flags are affected .
1) PUSH direct
Description : Push direct byte on to stack .
The stack pointer is incremented by one . The contents
of the indicated variable are then copied into the internal RAM
location addressed by the stack pointer.
Bytes : 2
Operation : SP = SP+1
(Direct address) ((SP))
Ex : PUSH 0E0H
0E0H is the address of accumulator . If [a] = 25H and
[SP]=30H then the location 31H contains 25H after the execution
of above instruction
2) POP direct
Description : POP direct byte from stack i.e. The contents of
internal RAM location addressed by the stack pointer is read . The
stack pointer is decremented by one . The value read is then
transferred to the direct address indicated in the instruction
Bytes : 2
Ex : POP 80H
If [SP] = 30H and [30H] = 55H , Then the internal
XCH Instruction
Syntax : XCH Operand.
XCH instruction Loads the accumulator with the byte value of
the specified operand while simultaneously storing the previous contents
of the accumulator in the specified operand .
Ex .
1. XCH A , Rn
Description : Exchange register with accumulator
Bytes : 1
Operation : [A]
[Rn]
Swap
XCH A ,R3
If [A] = 10H , [R3] = 15H then after execution [A] = 15H & [R3] = 10H.
2 . XCH A , direct
Description : Exchange direct byte with A .i.e. exchange the contents of
accumulator with the internal data memory.
Operation : [A]
[direct address]
Swap
Ex . XCH A , 40H
If [A] = 30 & contents of location 40H = 20H then after execution .
[A] = 20H & contents of location 40H = 30H.
3. XCH A , @Ri
Description : Exchange indirect RAM with A i.e. exchange the contents
of the memory location specified by the index register Ri with A.
Operation : [A]
[[Ri]]
swap
Ex . XCH A , @Ro
If [Ro] = 20H , [20H] = 15H & [A] = 33H , then after execution
[20H] = 33H & [A] =15H
4. XCHD instruction
exchange digit.
[[Ri]]
3-0
3-0
Ex . XCHD A , @Ro
If [Ro] = 30H , [30H] = FFH & [A] = 00H
Then after execution , [30H] = FOH & [A] = OFH.
ARITHMETIC INSTRUCTIONS
ADD , ADDC , SUBB , DA , DIV , MUL , INC & DEC are all
arithmetic instructions used in 8051 micro controller .
ADD Instruction
Syntax : ADD A , Source .
The ADD instruction adds a byte value to the accumulator and stores the
result back in accumulator .The destination operand is always in register
2. ADD A , Rn
Description : Add the contents of register and accumulator and stores the
result back on the accumulator .
Flags affected :CY , AC , OV & P
Bytes :1
Operand : (A) + (Rn) (A)
Ex . Add A , R2
If [R2] = 44H , [A] = 15H , after execution [A] = 44 +15 = 59H.
3. ADD A , Direct
Description : Add the contents of memory location and accumulator and
stores the result in A .
Flags affected : CY , AC , OV & P
Bytes :2
Operation : [A] +[Direct address] [A]
Ex . ADD A , 30H
If [30H] = 25H & [A] = 15H , after execution [A] = 3AH.
4. ADD A , @Ri
Description : Add the contents of the memory location specified by Ri
with accumulator store the result in accumulator .
Flags affected : AC , CY , OV & P
Bytes : 1
Description : [A] + [[Ri]] [A]
Ex. ADD A , @Ro
[Ro] = 30H , [30H] = 25H , &[A] = 13H , after execution
ADDC Instruction
The ADDC instruction adds a byte value and the value of the
carry flag to the A. the result of addition are stored back in the
accumulator.
1. ADDC A , Rn
Description : Add register contents to accumulator with carry
flag and store the result in accumulator.
Flag affected: CY, AC, P & OV.
Bytes : 1
Operation: [A] + [Rn] + [C] = [A]
e.g. ADD A, R2
if [R2]=13H, [A]=20H, [C]=0
after execution [A]=20+13+0=33H
2. ADDC A, #data
Description: Add the immediate data to A with carry & store
result in A.
Flags affected: CY, AC, OV & P
Operation: [A] + data + [C] [A]
Bytes: 2
e.g. ADDC A, #25H ;
if [A]=13H
after execution [A] = 25+13+1 = 39H
3. ADDC A, @Ri
Description: Add memory location specified by the index
register& the value of the carry flag to the accumulator & the
result is stored in A.
Flags affected: CY, AC, P & OV
Bytes: 1
SUBB Instruction
The SUBB instruction subtracts the specified byte variable & the
carry flag from the accumulator. The result is stored always in
accumulator. This instruction sets the carry flag if a borrow is
needed for bit 7 and clears otherwise.
Syntax: SUBB destination, source
1 .SUBB A, Rn
Description: Subtract the register content and the carry flag
from the contents of accumulator and the result stored back in
the accumulator.
Flags affected: CY, AC, P & OV
Bytes: 1
Operation: [A] [Rn] [C] [A]
e.g. SUBB A, R2
if [R2]=40H, [A]=70H & [C]=1
after execution [A] = [A] [Rn] [C] = 70-40-1 = 29H
2 .SUBB A, address
Description: subtract the content of the address indicated in the
instruction and the carry flag from the contents of accumulator
and store the result in accumulator.
Flags affected: CY, AC, P & OV
Bytes: 2
Operation: [A] [address] [C] [A]
e.g. SUBB A, 30H
if [A]=75H, [30H]=25H &[C]=1
after execution [A] = [A] [30H] [C] = 75-25-1 = 49H
3 .SUBB A, @Ri
Description: Subtract the contents of memory location and the
carry flag from the content of accumulator and store the result
in the accumulator.
Flags affected: CY, AC, P & OV
Bytes: 1
Operation: [A] [(R1)] [C] [A]
e.g. SUBB A, @Ri
if [A]=35H, [R1]=20H, [20H]=13H & [C]=1
after execution [A] [(R1)] [C] = 35-13-1 = 21H
4 .SUBB A, #data
Description: subtract 8 bit data & carry flag from the
accumulator and store the result in the accumulator.
Flags affected: CY, AC, OV & P
Bytes: 2
INC Instruction
The INC instruction increments the specified operand by 1. No
flags are affected by this instruction.
1 .INC A
Description: Increment Accumulator i.e. contents of
accumulator are incremented by 1.
Bytes: 1
Operation: [A]+1 = [A]
Ex. If [A] =33H After execution [A]=34H
2 .INC Rn
Description: The content of register is incremented by 1.
Bytes: 1
Operation: [Rn+1] + 1 [Rn]
Ex. If [Rn] =16H After execution [Rn]=17H
3 .INC direct
Description: The content of internal data memory location is
incremented by 1.
Bytes: 2
Operation: [direct address]+1 [direct address]
Ex. If [direct address]=17H
After execution [direct address]=18H
4 .INC @Ri
Description: The content of the internal data memory location
pointed by index register Ri is incremented by 1.
Bytes: 1
Operation: [(Ri)] + 1 [(Ri)]
Ex. INC @R0
If [R0]=30H, [30H]=12H
After execution [(R0)] =13H
5 .INC DPTR
Description: Increment data pointer i.e. add 1 to the 16 bit
DPTR
Bytes: 1
Operation: [DPTR]+1[DPTR]
Ex . If [DPTR]=1770H
After execution [DPTR]=1771H
DEC Instruction
The DEC instruction decrements the specified operand by 1.
1 .DEC A
Description: Decrement the content of accumulator by 1.
Bytes: 1
Operation: [A] -1 [A]
Ex .If [A]=17H
4 .DEC @Ri
Description: The content of internal data memory location
specified by register Ri is decremented by 1.
Bytes: 1
Operation: [(Ri)] -1 [(Ri)]
e.g. DEC @R0
if Ro=30H & [30H]=11H after execution [(R0)]=10H
MUL Instruction
Syntax: MUL AB
DIV Instruction
Syntax: DIV AB
The DIV instruction divides the 8 bit data on accumulator by the 8
bit data in register B. after division, the integer part of the quotient
is stored in the accumulator and the remainder is stored in the B
register. The CY & OV flags are cleared.
Bytes: 1
Operation: A/B= [A] [B] i.e. A Quotient and B Reminder
e.g. DIV AB
if [A]=13H & [B]=02H
after execution [A]=09H & [B]=01H
DA Instruction
Syntax: DA A
Description: Decimal adjust accumulator for addition BCD number
system is from 0000 to 1001
Packed BCD 1000 1000 BCD no. in lower byte & upper
byte
Unpacked BCD 0000 1100 BCD no. in only lower byte
The problem of adding two packed BCD no. is, after adding
packed BCD numbers, the result is no longer a BCD no.
e.g. MOV A, #17H
ADD A, #28H
After adding, we get 3FH which is not BCD because if we
convert 3FH to binary we get 0011 1111. Here 1111 is not BCD.
The DA instruction in the 8051 is provided to correct the
problem associated with BCD addition. The mnemonic DA has
accumulator as its operand. The DA instruction will add 6 to the
lower nibble or higher nibble if needed.
Summary or working of DA instruction
Hex
MOV A, #29H
29
0010 1001
MOV B, # 18H
18
0001 1000
ADD A, B
hex
DA A
41
0100 0001
06
0000 0110
BCD 47
0100 0111
Logical Instructions
There are of two bytes
1)Byte level logical operators
2)Bit level logical operators
Bytes: 3
Operation: [direct address] AND data=[direct address]
E.g. ANL 30H, #35H
ORL Instructions
Syntax: ORL destination, source
Description: The ORL instruction performs a bitwise logical OR
operation on the specified operand and result of which will be
stored in destination operand. This instruction can be written in all
addressing modes.
1. ORL A , Rn
2. ORL A , direct
3 .ORL A , @ Ri
4 .ORL A , #data
5 . ORL direct , A
6. ORL direct , # data
XRL Instruction
Syntax: XRL destination byte source byte
1 . XRL A , Rn
2 . XRL A , direct
3 . XRL A , @Ri
4 . XRL A , #direct
5 . XRL direct , A
6 . XRL direct , #data
Function: 00[A]
SWAP Instruction
Syntax: SWAP A
Swap nibbles within the accumulator.
Bytes: 1
The swap instruction interchanges the lower nibble (A0 A3) with
the upper nibble (A4 A7) inside the register A.
[A]=1001 1010
After SWAP instruction, contents of accumulator becomes
= 1 AND 1 = 1
Looping
Repeating a sequence of instructions a certain number of times is
called a LOOP.
In the 8051, the loop action is performed by the instruction, DJNZ
reg, label.
1 .DJNZ reg, label (Decrement register and jump if not zero)
Bytes: 2
Operation: Prior to the start of the loop the register is loaded
with the counter for the no. of repetition. In this instruction,
the register is decremented if it is not zero, it jumps to the
target address referred by the label. In this instruction, both
register decrement and decision to jump are combined into a
single instruction.
Conditional jumps
1 .JZ (Jump if A=0)
If the contents of A=0, then it jumps to the target address. it
can only check to see whether the A is zero. It does not
apply to any other register.
e.g. MOV A, R0
JZ OVER
MOV A, R1
JZ OVER
:
:
OVER : - -- - - - -
Result
I/P
ARRAY
30H 01
31H 02
32H 03
33H 04
34H 05
20H 05 A
size
o/p
40H 0F
SUM
41H 00
SUM+1
CALL Instruction
CALL instruction is used to call a subroutine. Subroutines are
often used to perform tasks that need to be performed frequently.
This makes a program more structured in addition to saving
memory space.
There are two instructions
1 A CALL
2 L CALL
LCALL (3
bytes)
(PC)+2(PC)
(PC)+2(PC)
(SP)+1(SP)
(SP)+1(SP)
(PC0-7)(SP)
(PC0-7)(SP)
(SP+1)(SP)
(SP+1)(SP)
(PC8-15)(SP)
(PC8-15)(SP)
(SP)-1(SP)
(SP) (PC0-7)
(SP)-1(SP)
4 .RETI (return from interrupt)
The RETI instruction is used to end an interrupt service
routine. This instruction pops the higher order & lower order
byte of the PC from the stack, decrementing the stack
pointer. Execution of this instruction returns to the instruction
immediately after the point at which interrupt was detected.
Bytes: 1
Machine Cycle
For the CPU to execute an instruction takes a certain number of
clock cycles. In 8051 family, these clocks are referred as machine
cycle depends on the frequency of the crystal oscillator connected
to 8051 system. One machine cycle lasts 12 oscillations period.
16MHz/12=1.333MHz machine cycles
1/1.333 = 0.75 micro sec