8051 Architecture Full
8051 Architecture Full
Contents:
Introduction Block Diagram and Pin Description of the 8051 Registers Memory mapping in 8051 Stack in the 8051 I/O Port Programming Timer Interrupt
Microprocessors:
General-purpose microprocessor
CPU for Computers No RAM, ROM, I/O on CPU chip itself ExampleIntels x86, Motorolas 680x0
Many chips on mothers board
Data Bus
RAM
ROM
I/O Port
Timer
Microcontroller :
A smaller computer On-chip RAM, ROM, I/O ports... ExampleMotorolas 6811, Intels 8051, Zilogs Z8 and PIC 16X
CPU
RAM ROM
A single chip
I/O Port
Serial Timer COM Port Microcontroller
Block Diagram
External interrupts Interrupt Control On-chip ROM for program code
Timer/Counter
On-chip RAM
Timer 1 Timer 0
Counter Inputs
CPU
Serial Port
OSC
Bus Control
4 I/O Ports
P0 P1 P2 P3
TxD RxD
Address/Data
8051 (8031)
Registers
A B R0 R1 R2 R3 R4 R5 R6 Some 8051 16-bit Register PC PC DPTR DPH DPL
10
Instruction Groups
The 8051 has 255 instructions The instructions are grouped into 5 groups
11
Arithmetic Instructions
ADD
ADDC
8-bit addition between the accumulator, a second operand and the previous value of the CY flag.
Useful for 16-bit addition in two steps. The CY flag is set/reset appropriately.
12
13
Arithmetic Instructions
DA
SUBB
Subtract an operand and the previous value of the borrow (carry) flag from the accumulator.
A A - <operand> - CY. The result is always saved in the accumulator. The CY flag is set/reset appropriately.
14
Example: write code that subtracts content of R6 from R7 and leave the result in R7
MOV A,R7 CLR C SUBB A, R6 MOV R7,A
Arithmetic Instructions
INC
The operand can be a register, a direct address, an indirect address, the data pointer.
DEC
MUL AB / DIV AB
Multiply A by B and place result in A:B. Divide A by B and place result in A:B.
16
DEC DPL MOV R7,DPL CJNE R7, #FFH, SKIP DEC DPH SKIP: (continue)
Logical Operations
ANL / ORL
18
Logical Operations
XRL
Works on bytes only. XRL A, #0FFh
CPL / CLR
Complement / Clear. Work on the accumulator or a bit.
CLR P1.2
19
Logical Operations
RL / RLC / RR / RRC
SWAP A
Swap the upper and lower nibbles of the accumulator. Built into conditional branching instructions.
No compare instruction.
20
MOV A, Rn MOV A, direct MOV A, @Ri MOV A, #data MOV Rn, A MOV Rn, direct MOV Rn, #data MOV direct, A MOV direct, Rn MOV direct, direct MOV direct, @Ri MOV direct, #data MOV @Ri, A MOV @Ri, direct MOV @Ri, #data
21
MOV
MOV
22
MOVC
MOVC MOVC
A, @A+DPTR A, @A+PC
23
MOVX
Data transfer between the accumulator and a byte from external data memory.
MOVX MOVX MOVX MOVX
24
PUSH / POP
Push and Pop a data byte onto the stack. The data byte is identified by a direct address from the internal RAM locations.
PUSH POP
DPL 40H
25
XCH
XCHD
Exchange lower digit of accumulator with the lower digit of the memory location specified.
XCHD A, @Ri The lower 4-bits of the accumulator are exchanged with the lower 4-bits of the internal memory location identified indirectly by the index register. The upper 4-bits of each are not modified.
26
Boolean Operations
This group of instructions is associated with the single-bit operations of the 8051. This group allows manipulating the individual bits of bit addressable registers and memory locations as well as the CY flag.
27
Boolean Operations
CLR
SETB
CPL
CPL 40H
28
Boolean Operations
JC / JNC
Jump to a relative address if CY is set / cleared. Jump to a relative address if a bit is set / cleared.
JB / JNB
JB
ACC.2, <label>
JBC
29
Branching Instructions
Uses an 8-bit signed offset relative to the 1st byte of the next instruction. Uses a 16-bit address. 3 byte instruction capable of referencing any location in the entire 64K of program memory.
30
Branching Instructions
The upper 3-bits of the address combine with the 5-bit opcode to form the 1st byte and the lower 8-bits of the address form the 2nd byte.
The 11-bit address is substituted for the lower 11-bits of the PC to calculate the 16-bit address of the target.
The location referenced must be within the 2K Byte memory page containing the AJMP instruction.
31
JMP @A + DPTR
Branching Instructions
Both forms push the 16-bit address of the next instruction on the stack and update the stack pointer.
32
Branching Instructions
Pop the return address from the stack and continue execution there.
33
Branching Instructions
Jump if the A == 0 / A != 0
34
Branching Instructions
35
Branching Instructions
Compare the magnitude of the two operands and jump if they are not equal.
The values are considered to be unsigned. The Carry flag is set / cleared appropriately.
36
A, direct, rel A, #data, rel Rn, #data, rel @Ri, #data, rel
Branching Instructions
Decrement the first operand by 1 and jump to the location identified by the second operand if the resulting value is not zero.
DJNZ DJNZ
No Operation
NOP
37
; bit=1 ; bit=0
; CY=1 ;bit 0 from port 0 =1 ;bit 7 from port 3 =1 ;bit 2 from ACCUMULATOR =1 ;set high D5 of RAM loc. 20h
CLR instruction is as same as SETB i.e.: CLR C ;CY=0 But following instruction is only for CLR: CLR A ;A=0
Jump if A=0
Jump if A/=0 Decrement and jump if A/=0
CJNE A,byte
CJNE reg,#data JC JNC JB JNB JBC
Jump if A/=byte
Jump if byte/=#data Jump if CY=1 Jump if CY=0 Jump if bit=1 Jump if bit=0 Jump if bit=1 and clear bit