Beruflich Dokumente
Kultur Dokumente
HCM
L Ch Thng
Instruction Set
255 instructions
1-byte instructions: 139 2-byte instructions: 92 3-byte instructions: 24
L Ch Thng
sites.google.com/site/chithong
L Ch Thng
First_program.lst LOC OBJ LINE 0000 1 0000 75900F 2 0003 D297 3 0005 C290 4 5
0000H
Checksum = 2s complement of all data
SOURCE ORG 0000H MOV P1,#0FH SETB P1.7 CLR P1.0 END
75H 90H 0FH D2H 97H C2H 90H
3
Code Memory
Opcode
:0700000075900FD297C2902A :00000001FF
end of record type
L Ch Thng
0006H
R1
L Ch Thng
B
4
sites.google.com/site/chithong
L Ch Thng
Internal RAM
128 locations from address 00H to 7FH (256 locations for 8052) The content of one location is 8 bit. Register banks: address 00H to 1FH Bit Addressable RAM: address 20H to 2FH General purpose RAM: address from 30H to 7FH
L Ch Thng
sites.google.com/site/chithong
L Ch Thng
30H
Ex: MOV 31H,R4 This instruction moves (writes) the content of register R4 to location 31H.
31H R4
L Ch Thng 7
sites.google.com/site/chithong
L Ch Thng
20H
Not affected
CY
26H
L Ch Thng 9
sites.google.com/site/chithong
L Ch Thng
Register Banks
4 banks: Bank 0 (default), Bank 1, Bank 2, and Bank 3 Change register bank by selecting bits RS1 and RS0 (in register PSW) One bank includes 8 registers: R0 through R7
o o o o o o o o o o o R0 of Bank 0 is location 00H R7 of Bank 0 is location 07H R0 of Bank 1 is location 08H R7 of Bank 1 is location 0FH R0 of Bank 2 is location 10H R7 of Bank 2 is location 17H R0 of Bank 3 is location 18H
L Ch Thng 11
L Ch Thng
12
sites.google.com/site/chithong
L Ch Thng
Register Banks
Ex: use default bank (Bank 0)
R5
05H
MOV A, R5 = MOV A, 05H Read the contents of register R5 Read the contents of location 05H into the accumulator into the accumulator
L Ch Thng
13
L Ch Thng
14
sites.google.com/site/chithong
L Ch Thng
Ex: MOV PSW, #00010000B ; select bank 2 or SETB RS1 CLR RS0 Try it: Select bank 1 .. .. L Ch Thng
15
sites.google.com/site/chithong
L Ch Thng
17
Addressing Modes
Immediate Addressing Register Addressing Direct Addressing Indirect Addressing Relative Addressing Absolute Addressing Long Addressing Indexed Addressing
L Ch Thng 18
sites.google.com/site/chithong
L Ch Thng
L Ch Thng
19
Immediate Addressing
- Source operand is constant - Use # sign (Source) MOV MOV MOV ADD MOV A,#12 A,#0C4H R1,#1110B A,#11110001B DPTR,#2000H
DPL 0 0 0 0 0 0 0 0 A A R1 CY 0 0 0 0 1 1 0 0 1 1 0 0 0 1 0 0 0 0 0 0 1 1 1 0 1 A 1 0 1 1 0 1 0 1
DPH 0 0 1 0 0 0 0 0
L Ch Thng
20
sites.google.com/site/chithong
10
L Ch Thng
Register Addressing
Ex: MOV MOV MOV ANL INC DEC MOV INC R1,#36H A,R1 R7,#0FH A,R7 A A DPTR,#2000 DPTR
DPL 0 0 0 0 0 0 0 1
21 L Ch Thng
R1 A R7 A A A
0 0 1 1 0 1 1 0 0 0 1 1 0 1 1 0 0 0 0 0 1 1 1 1 0 0 0 0 0 1 1 0 0 0 0 0 0 1 1 1 0 0 0 0 0 1 1 0
(source)
DPH 0 0 1 0 0 0 0 0
Direct Addressing
Access internal RAM and SFRs
MOV A,70H MOV R0,40H MOV 56H,A MOV 0D0H,A MOV PSW,A ; copy contents of RAM at 70H to A ; copy contents of RAM at 40H to A ; put contents of A into RAM at 56H ; put contents of A into PSW ; same
L Ch Thng
22
sites.google.com/site/chithong
11
L Ch Thng
MOV A,30H
A 30H Direct addressing
MOV A,#30H
0 0 1 1 0 0 0 0 Immediate addressing
L Ch Thng
23
MOV R2,5
R2 05H
R2
MOV R2,#5
0 0 0 0 0 1 0 1
L Ch Thng
24
sites.google.com/site/chithong
12
L Ch Thng
Addressing Modes
Immediate Addressing Register Addressing Direct Addressing Indirect Addressing Relative Addressing Absolute Addressing Long Addressing Indexed Addressing
L Ch Thng 25
Indirect Addressing
The address of the source or destination is specified in registers. Use registers R0 or R1 for 8-bit address (internal or external RAM) Use @ sign to access the content of the memory location: @R0, @R1 Ex: MOV R0,#30H ;R0 30H MOV A,@R0 ;A(R0): read content of internal RAM at ; address specified by R0 to A
R0 0 0 1 1 0 0 0 0 Internal RAM 7FH 30H 00H
L Ch Thng
26
sites.google.com/site/chithong
13
L Ch Thng
L Ch Thng
27
L Ch Thng
28
sites.google.com/site/chithong
14
L Ch Thng
L Ch Thng
29
L Ch Thng
30
sites.google.com/site/chithong
15
L Ch Thng
R0,#30H ;R0 30H A,@R0 ;A(R0): read content of eXternal RAM at ; address specified by R0 to A
R0
0 0 1 1 0 0 0 0
External RAM
30H 00H
L Ch Thng
32
sites.google.com/site/chithong
16
L Ch Thng
DPTR
40H
00H
External RAM
4000H 00H
L Ch Thng
33
DPTR,#31FFH ; DPTR Address @DPTR,A ; write content of A to external RAM ; at address 31FFH
DPTR
31H
FFH
External RAM
31FFH 00H
L Ch Thng
34
sites.google.com/site/chithong
17
L Ch Thng
L Ch Thng
35
L Ch Thng
36
sites.google.com/site/chithong
18
L Ch Thng
L Ch Thng
37
L Ch Thng
38
sites.google.com/site/chithong
19
L Ch Thng
L Ch Thng
39
Your Turn!
Write a program to move data from internal RAM at address 56H to external RAM at address 4123H.
L Ch Thng
40
sites.google.com/site/chithong
20
L Ch Thng
MOV A,@R1
L Ch Thng
42
sites.google.com/site/chithong
21
L Ch Thng
Your Turn!
Write a program to output (send) the content of internal RAM, from address 20H to 45H, to Port 1
L Ch Thng
43
Addressing Modes
Immediate Addressing Register Addressing Direct Addressing Indirect Addressing Relative Addressing Absolute Addressing Long Addressing Indexed Addressing
L Ch Thng 44
sites.google.com/site/chithong
22
L Ch Thng
Relative Addressing
Used in SJMP instruction A relative address (or offset) is an 8-bit signed value. It is added to PC to form a new value for PC. Range: -128 ~ +127 Usually used with label
Ex: SJMP LABEL1 is in memory at locations 0100H and 0101H PC = 0102H If LABEL1 is the label representing an instruction at location 0107H Relative offset is 0107H 0102H = 5 Opcode of SJMP LABEL1 is 80 05
L Ch Thng 45
Relative Addressing
Ex: SJMP LABEL2 is in memory at locations 2040H and 2041H PC = 2042H If LABEL2 is the label representing an instruction at location 2038H Relative offset is 2038H 2042H = -10 = F6H Opcode of SJMP LABEL1 is 80 F6
L Ch Thng
46
sites.google.com/site/chithong
23
L Ch Thng
Your Turn!
Ex: AGAIN: ORG 0000H SETB P1.0 NOP NOP CLR P1.0 NOP SJMP AGAIN END a. Find the opcode of SJMP AGAIN instruction? b. Find the duration of P1.0=1? P1.0=0? c. What is the result of this program?
L Ch Thng
47
Absolute Addressing
Used in AJMP, ACALL instruction Usually used with label
Within any 2K page, the upper five address bits are the same for the source and destination addresses. The lower 11 bits of the destination are supplied in the instruction
sites.google.com/site/chithong
24
L Ch Thng
Long Addressing
Used in LCALL and LJMP instruction Use full 16-bit address Usually used with label Ex: LCALL SUBPROGRAM1 LJMP LABEL2
L Ch Thng
49
Indexed Addressing
Base address (PC or DPTR) + Offset (A) Used with JMP or MOVC Ex: MOVC A, @A+DPTR MOVC A, @A+PC JMP @A+DPTR Effective Address
L Ch Thng
50
sites.google.com/site/chithong
25
L Ch Thng
Indexed Addressing
Ex: An even number from 0 to 6 is in the Accumulator. The following sequence of instructions branches to one of four AJMP instructions in a jump table starting at JMP_TBL. MOV DPTR, # JMP_TBL JMP @A + DPTR JMP_TBL: AJMP LABEL0 AJMP LABEL1 AJMP LABEL2 AJMP LABEL3 If the Accumulator equals 04H when starting this sequence, execution jumps to label LABEL2. Because AJMP is a 2-byte instruction, the jump instructions start at every other address.
L Ch Thng 51
Instruction Types
Data transfer Arithmetic Logical Boolean variable Program branching
L Ch Thng
52
sites.google.com/site/chithong
26
L Ch Thng
L Ch Thng
53
L Ch Thng
54
sites.google.com/site/chithong
27
L Ch Thng
L Ch Thng
55
Data Transfer
MOV dest, source MOV A,#72H MOV R4,#62H MOV B,0F9H MOV MOV MOV MOV Note 1: MOV Note 2: MOV DPTR,#7634H DPL,#34H DPH,#76H P1,A A,#72H A,R3 ;move (out) A to Port 1 MOV A,72H MOV A,3 ;R3 of bank 0 is location 3
L Ch Thng 56
sites.google.com/site/chithong
28
L Ch Thng
L Ch Thng
57
Loop=0?
L Ch Thng
58
sites.google.com/site/chithong
29
L Ch Thng
Loop=0?
L Ch Thng
59
Addr=37H? Y
L Ch Thng
60
sites.google.com/site/chithong
30
L Ch Thng
Your Turn!
Write a program to read data from internal RAM from location 20H to location 29H and output to Port 1
L Ch Thng
61
Solutions (1)
Write a program to read data from internal RAM from location 20H to location 29H and output to Port 1 Method 1: using DJNZ ORG 0000H MOV R7,#10 MOV R0,#20H Loop:MOV P1,@R0 ;Output to Port1 INC R0 DJNZ R7,loop END
L Ch Thng
62
sites.google.com/site/chithong
31
L Ch Thng
Solutions (2)
Write a program to read data from internal RAM from location 20H to location 29H and output to Port 1 Method 2: using CJNE ORG 0000H MOV R0,#20H Loop:MOV P1,@R0 ;Output to Port1 INC R0 CJNE R0,#2AH,loop END
L Ch Thng
63
Look-up Table
MOV MOV MOVC DB A,#ENTRY_NUMBER DPTR,#TABLE A,@A+DPTR data1, data2, data3,
TABLE:
L Ch Thng
64
sites.google.com/site/chithong
32
L Ch Thng
L Ch Thng
65
Your Turn!
Given a packed-BCD number in location 34H of internal RAM. Write a program that displays the low decade of this number on the common-anode 7-seg LED connected to Port 2 (schematic).
L Ch Thng
66
sites.google.com/site/chithong
33
L Ch Thng
Solutions
ORG 0000H MOV 34H,#25H MOV A,34H ANL A,#0FH MOV DPTR,#TABLE MOVC A,@A+DPTR MOV P2,A SJMP DONE TABLE: DB 40h,79h,24h,30h,19h DB 12h,02h,78h,00h,10h DONE: NOP END (source)
L Ch Thng
68
sites.google.com/site/chithong
34
L Ch Thng
Look-up Table
MOV A, #ENTRY_NUMBER CALL LOOK_UP LOOK_UP: INC A MOVC A, @A+PC RET TABLE: DB data1, data2, data3,
L Ch Thng
69
Jump Table
MOV MOV RL JMP JUMP_TABLE: AJMP AJMP AJMP DPTR,#JUMP_TABLE A,INDEX_NUMBER A @A+DPTR CASE0 CASE1 CASE2
L Ch Thng
70
sites.google.com/site/chithong
35
L Ch Thng
L Ch Thng
71
L Ch Thng
72
sites.google.com/site/chithong
36
L Ch Thng
L Ch Thng
73
L Ch Thng
74
sites.google.com/site/chithong
37
L Ch Thng
L Ch Thng
75
L Ch Thng
76
sites.google.com/site/chithong
38
L Ch Thng
Your Turn!
Given 10 8-bit unsigned numbers in internal RAM at starting address 30H. Write a program to calculate the sum of these numbers and store the result in internal RAM at address 2FH. Assume that the result is less than or equal 255.
L Ch Thng
78
sites.google.com/site/chithong
39
L Ch Thng
Solutions
Given 10 8-bit unsigned numbers in internal RAM at starting address 30H. Write a program to calculate the sum of these numbers and store the result in internal RAM at address 2FH. Assume that the result is less than or equal 255. ORG 0 MOV R0,#30H CLR A MOV R2,#10 loop: ADD A,@R0 INC R0 DJNZ R2,loop MOV 2FH,A END
L Ch Thng
79
Rotate
Rotate instructions operate only on A RL A
MOV RR A,#0F0H A ; A 11110000 ; A 11100001
RR A
MOV RR A,#0F0H A ; A 11110000 ; A 01111000
L Ch Thng 80
sites.google.com/site/chithong
40
L Ch Thng
RLC A
MOV A, #3CH SETB C RLC A ; A 3CH(00111100) ; C 1 ; A 01111001, C1
L Ch Thng 81
L Ch Thng
82
sites.google.com/site/chithong
41
L Ch Thng
Swap
SWAP A
; A 72H ; A = 27H
L Ch Thng
83
L Ch Thng
84
sites.google.com/site/chithong
42
L Ch Thng
L Ch Thng
86
sites.google.com/site/chithong
43
L Ch Thng
L Ch Thng
87
Boolean Variable
ORG 0 LOOP: MOV C,P1.0 ANL C,P1.1 MOV P1.2,C SJMP LOOP END
L Ch Thng
88
sites.google.com/site/chithong
44
L Ch Thng
Your Turn!
L Ch Thng
89
Solutions
ORG 0 LOOP: MOV C,P1.4 CPL C ANL C,P1.5 CPL C ORL C,P1.6 MOV P1.7,C SJMP LOOP END
L Ch Thng 90
sites.google.com/site/chithong
45
L Ch Thng
L Ch Thng
91
L Ch Thng
92
sites.google.com/site/chithong
46
L Ch Thng
Program Branching
LJMP(long jump) LJMP is an unconditional jump. It is a 3-byte instruction. It allows a jump to any memory location from 0000 to FFFFH. AJMP(absolute jump) In this 2-byte instruction, It allows a jump to any memory location within the 2k block of program memory. SJMP(short jump) In this 2-byte instruction. The relative address range of 00-FFH is divided into forward and backward jumps, that is , within -128 to +127 bytes of memory relative to the address of the current PC.
Unconditional jumps
SJMP : 8-bit offset LJMP : 11-bit address (2KB segment) AJMP : 16-bit
L Ch Thng 93
Conditional Jumps
JZ JNZ DJNZ CJNE A,byte CJNE reg,#data JC JNC JB JNB JBC Jump if A=0 Jump if A 0 Decrement and jump if 0 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
L Ch Thng 94
sites.google.com/site/chithong
47
L Ch Thng
CALL Instruction
LCALL(long call) This 3-byte instruction can be used to call subroutines located anywhere within the 64K byte address space of the 8051. ACALL (absolute call) ACALL is 2-byte instruction. the target address of the subroutine must be within 2K byte range.
L Ch Thng
95
L Ch Thng
96
sites.google.com/site/chithong
48
L Ch Thng
L Ch Thng
97
Subroutines
call to the subroutine ... ACALL SUBLABEL ... ... SUBLABEL: ... ... the subroutine RET MAIN:
L Ch Thng
98
sites.google.com/site/chithong
49
L Ch Thng
L Ch Thng
99
L Ch Thng
100
sites.google.com/site/chithong
50
L Ch Thng
DJNZ an example
Ex: Write a program to write 40H to internal RAM from location 30H to location 36H. (source) ORG 0000H MOV R5,#7;Loop=7 MOV R1,#30H;Address=30H Again: MOV @R1,#40H INC R1 DJNZ R5,Again END
Loop 7 Addr 30H
Loop=0?
101
L Ch Thng
AGAIN:
L Ch Thng
102
sites.google.com/site/chithong
51
L Ch Thng
L Ch Thng
103
Solutions
Write a program to copy a block of 10 bytes from RAM location starting at 37h to RAM location starting at 59h. Solution: MOV R0,#37h MOV R1,#59h MOV R2,#10 L1: MOV A,@R0 MOV @R1,A INC R0 INC R1 DJNZ R2,L1 ; source pointer ; dest pointer ; counter
L Ch Thng
104
sites.google.com/site/chithong
52
L Ch Thng
Blinky Program
P1.0 1 Delay P1.0 0 Delay
schematic
L Ch Thng 105
Blinky Program
ORG 0 LOOP: SETB P1.0 ACALL DELAY CLR P1.0 ACALL DELAY SJMP LOOP DELAY: MOV R6,#200 DL1: MOV R7,#250 DJNZ R7,$ DJNZ R6,DL1 RET END (source) Waveform? Period? Frequency?
L Ch Thng 106
sites.google.com/site/chithong
53
L Ch Thng
Blinky Program
ORG 0 LOOP: SETB P1.0 ACALL DELAY CLR P1.0 ACALL DELAY SJMP LOOP DELAY: MOV R6,#200 DL1: MOV R7,#250 DJNZ R7,$ DJNZ R6,DL1 RET END
1MC x 1 2 MC x 250
501 MC
L Ch Thng
107
Blinky Program
ORG 0 LOOP: SETB P1.0 ACALL DELAY CLR P1.0 ACALL DELAY SJMP LOOP DELAY: MOV R6,#200 DL1: MOV R7,#250 DJNZ R7,$ DJNZ R6,DL1 RET END
100,601 MC
L Ch Thng
108
sites.google.com/site/chithong
54
L Ch Thng
Blinky Program
ORG 0 LOOP: SETB P1.0 ACALL DELAY CLR P1.0 ACALL DELAY SJMP LOOP DELAY: MOV R6,#200 DL1: MOV R7,#250 DJNZ R7,$ DJNZ R6,DL1 RET END
100,601 MC 100,603 MC 2 MC
L Ch Thng
109
Blinky Program
ORG 0 LOOP: SETB P1.0 ACALL DELAY CLR P1.0 ACALL DELAY SJMP LOOP DELAY: MOV R6,#200 DL1: MOV R7,#250 DJNZ R7,$ DJNZ R6,DL1 RET END 1 MC 100,063 + 2 MC 1 MC 100,063 + 2 MC 2 MC
sites.google.com/site/chithong
55
L Ch Thng
Blinky Program
If using 12 MHz crystal, 1 MC = 1 s tH = 100,066 MC = 100,066 s tL = 100,068 MC = 100,068 s T = tH + tL = 200,134 s f = 1/T = 4.99 Hz P1.0 1 Delay tH P1.0 0 Delay tL
tH T tL
L Ch Thng
111
sites.google.com/site/chithong
56
L Ch Thng
Delay
L Ch Thng
114
sites.google.com/site/chithong
57
L Ch Thng
Delay
L Ch Thng
115
lap:
delay: Delay
L Ch Thng
116
sites.google.com/site/chithong
58
L Ch Thng
P1.3 1
tH T tL
L Ch Thng
117
sites.google.com/site/chithong
59
L Ch Thng
L Ch Thng
119
lap:
L Ch Thng
120
sites.google.com/site/chithong
60
L Ch Thng
L Ch Thng
121
lap:
sites.google.com/site/chithong
61
L Ch Thng
A = 05H?
Y Statement 1
Skip:
L Ch Thng
123
An Example
Ex: Write a program to write 40H to internal RAM from location 30H to location 36H. (source) ORG 0000H Addr 30H MOV R1,#30H;Address=30H Again: MOV @R1,#40H INC R1 (Addr) 40H CJNE R1,#37H,Again END AddrAddr+1
N
Addr=37H? Y
L Ch Thng
124
sites.google.com/site/chithong
62
L Ch Thng
A = 05H?
Y
Statement 2
Statement 1
L Ch Thng
125
A 05H?
Y Statement 1
L Ch Thng
126
sites.google.com/site/chithong
63
L Ch Thng
A 05H?
Y Statement 1
CJNE A,#05H,Next Next: JNC GT_Eq SJMP Continue GT_Eq: (Statement 1) Continue: (Continue) CJNE A,#05H,$+3 JNC GT_Eq SJMP Continue GT_Eq: (Statement 1) Continue: (Continue)
L Ch Thng 127
An Example
Examine the content of A, if 5 A 10 then output A to Port 1; if not, output A to Port 2. ORG 0 CJNE A,#5,$+3 JC PORT2 CJNE A,#11,$+3 JNC PORT2 MOV P1,A SJMP DONE PORT2:MOV P2,A DONE: NOP END
L Ch Thng 128
sites.google.com/site/chithong
64
L Ch Thng
A 05H?
Y
Statement 2
Statement 1
GT_Eq: Next:
L Ch Thng
129
Bit Testing
Given a 20-byte string in internal RAM, starting at address 40H. Write a program that output even numbers to Port 2.
L Ch Thng
130
sites.google.com/site/chithong
65
L Ch Thng
Bit Testing
Given a 20-byte string in internal RAM, starting at address 40H. Write a program that output even numbers to Port 2. ORG 0 MOV R4,#20 MOV R0,#40H LOOP: MOV A,@R0 JB ACC.0,NEXT MOV P2,A NEXT: INC R0 DJNZ R4,LOOP END ; Number of loops ; Address pointer ; Read data from internal RAM to A ; skip if odd number ; Output to Port 2 if even number
L Ch Thng
131
Your Turn!
Given a 20-byte string in external RAM, starting at address 4000H. Write a program that output odd numbers to Port 2.
L Ch Thng
132
sites.google.com/site/chithong
66
L Ch Thng
Solutions
Given a 20-byte string in external RAM, starting at address 4000H. Write a program that output odd numbers to Port 2. ORG 0 MOV R4,#20 ; Number of loops MOV DPTR,#4000H ; Address of external RAM LOOP: MOVX A,@DPTR ; Read data from external RAM to A JNB ACC.0,NEXT; skip if even number MOV P2,A ; Output to Port 2 if odd number NEXT: INC DPTR DJNZ R4,LOOP END L Ch Thng
133
Bit Testing
Given a 100-byte unsigned number string in external RAM at address starting from 0100H. Write a program that sends positive numbers to Port 1 and negative numbers to Port 2. Hint: - A positive number has MSB = 0. - A negative number has MSB = 1. - Use JB / JNB instruction --
L Ch Thng
134
sites.google.com/site/chithong
67
L Ch Thng
Bit Testing
Given a 100-byte unsigned number string in external RAM at address starting from 0100H. Write a program that sends positive numbers to Port 1 and negative numbers to Port 2. ORG 0000H MOV DPTR,#0100H MOV R4,#100 loop: MOVX A,@DPTR JNB ACC.7,positive MOV P2,A SJMP next positive: MOV P1,A next: INC DPTR DJNZ R4,loop END
L Ch Thng 135
Bit Testing
P1.0 P1.1
XOR
P1.2
LOOP:
SKIP:
L Ch Thng
136
sites.google.com/site/chithong
68
L Ch Thng
Example Problem
A 4-bit DIP switch and a common-anode 7-segment LED are connected to an 8051 as shown in the following figure. Write a program that continually reads a 4-bit code from the DIP switch and updates the LEDs to display the appropriate hexadecimal character. For example, if the code 1100B is read, the hexadecimal character C should appear, thus, segments a through g respectively should be ON, OFF, OFF, ON, ON, ON, and OFF. Note that setting an 8051 port pin to 1 turns the corresponding segment ON.
L Ch Thng
137
References
I. Scott MacKenzie , The 8051 Microcontroller, 2nd Edition, Prentice-Hall, 1995 Kenneth J. Ayala, The 8051 Microcontroller: Architecture, Programming, and Applications, West Publishing Company hsabaghianb@kashanu.ac.jr , Lecture notes
L Ch Thng
138
sites.google.com/site/chithong
69