Professional Documents
Culture Documents
Lecture 4
The 8051 Architecture
Vocational Training Council, Hong Kong. Week 4 1
In this Lecture
Overview General physical & operational features Block diagram Pin assignments Logic symbol Hardware description Pin description Read-modify-write port instructions
Vocational Training Council, Hong Kong. Week 4
Week 4
4 x 8 = 32 16 80 -----------128
Four 8-bit I/O ports (P0-P3) Two 16-bit timers (Timer0 & Timer1) One serial receiver-transmitter interface Five interrupt sources (2 external & 3 internal) One oscillator (generates clock signal)
Vocational Training Council, Hong Kong. Week 4
Boolean instructions work with 1 bit at a time Assume clock frequency = 12MHz, it takes about 4 Qs (i.e. 4 x 10-6s) to carry out a 8-bit multiplication instruction
Week 4
Interrupt Control
Counter Inputs
CPU
OSC
Bus Control
I/O Ports
Serial Port
Week 4
8051
Week 4
XTAL2
EA PSEN ALE P O R T 1
SECONDARY FUNCTIONS
P O R T 3
P O R T 2
ADDRESS BUS
Week 4
Hardware Description
Oscillator circuit Program counter (PC) Data pointer (DPTR) Accumulator (A) register B register Flags Program status word (PSW) Internal memory (ROM, RAM, additional memory) 9. Stack & stack pointer (SP) 10. Special function register (SFR)
Vocational Training Council, Hong Kong. Week 4
1. 2. 3. 4. 5. 6. 7. 8.
Oscillator Circuit
The heart of the 8051 Produces clock pulses Synchronize all 8051s internal operations
Week 4
10
Machine Cycle
Machine cycle is the basic repetitive process that the CPU performs once it is powered on. A machine cycle consists of a fixed number of clock cycles (pulses). It is different for different kinds of CPU. The 8051 family needs 12 clock cycles for a machine cycle. The CPU takes one or more machine cycles to complete an instruction. More complex instructions require more number of machine cycles to complete the instruction. The number of machine cycles of the 8051 instructions are ranging from 1 to 4.
Week 4
11
Example 4-1 4Find the elapse time of the machine cycle for: (a) XTAL = 11.0592 MHz (b) XTAL = 16 MHz (c) XTAL = 20 MHz
Solution: (a) 11.0592 MHz / 12 = 921.6 kHz Machine cycle = 1 / 921.6 kHz = 1.085 Qs (b) 16 MHz / 12 = 1.333 MHz Machine cycle = 1 / 1.333 MHz = 0.75 Qs (c) 20 MHz / 12 = 1.667 MHz Machine cycle = 1 / 1.667 MHz = 0.60 Qs
Vocational Training Council, Hong Kong. Week 4
12
Either One !
13
DPTR is under the control of program instructions and can be specified by its 16-bit name, or by each individual byte name, DPH and DPL DPTR does not have a single internal address; DPH and DPL are each assigned an address (83H and 82H)
Vocational Training Council, Hong Kong. Week 4
14
Accumulator (A Register)
Most versatile CPU register and is used for many operations, including addition, integer multiplication and division, and Boolean bit manipulations A register is also used for all data transfer between the 8051 and any external memory
Week 4
15
B Register
B register is used with the A register for multiplication and division operations (eg. MUL AB DIV AB) No other special function other than as a location where data may be stored
Week 4
16
Flags
Flags are 1-bit registers provided to store the results of certain program instructions Other instructions can test the condition of the flags and make decisions based on the flag states Flags are grouped inside the program status word (PSW) and the power control (PCON) registers for convenient addressing Math flags: respond automatically to the outcomes of math operations (CY, AC, OV, P) User flags: general-purpose flags that may be used by the programmer to record some event in the program (F0, GF0, GF1)
Vocational Training Council, Hong Kong. Week 4
17
Week 4
18
19
The 8051 maintains even parity with the accumulator A, ie; the number of ones in the accumulator together with the parity bit (in the program status word, PSW) is always even.
Week 4
20
Note: X can be 0 or 1
Week 4
21
Internal Memory
A functioning computer must have memory for
program code bytes, commonly in ROM, and RAM memory for variable data that can be altered as the program runs 8051 has internal RAM (128 bytes) and ROM (4Kbytes) 8051 uses the same address but in different memories for code and data Internal circuitry access the correct memory based on the nature of the operation in progress Can add memory externally if needed
Vocational Training Council, Hong Kong. Week 4
22
128 bytes internal RAM 4EEE3410 Microcontroller bytes each (R0-R register banks of 8 Applications 16 bytes of bit-addressable area 80 bytes of general purpose memory
7F
Bank 2
Bank 3
2F 2E 2D 2C 2B 2A 29 28 27 26 25 24 23 22 21 20
7F 77 6F 67 5F 57 4F 47 3F 37 2F 27 1F 17 0F 07
78 70 68 60 58 50 48 40 38 30 28 20 18 10 08 00
Bank 0
Bank 1
30
Working Registers
Vocational Training Council, Hong Kong.
Bit Addressable
General Purpose
Week 4
23
Example 2-5 2State the contents of RAM locations after the following program: MOV R0, #99H Assume register bank 0 MOV R1, #85H was selected ! MOV R2, #3FH MOV R7, #63H MOV R5, #12H After the execution of the above program we have the following: RAM location 0 has value 99H RAM location 1 has value 85H RAM location 2 has value 3FH RAM location 7 has value 63H RAM location 5 has value 12H
Week 4
24
Week 4
25
Example 2-6 2Repeat Example 2-5 using RAM addresses instead of register names.
This is called direct addressing mode and uses the RAM address location for the destination address. MOV 00, #99H MOV 01, #85H MOV 02, #3FH MOV 07, #63H MOV 05, #12H
Week 4
26
Example 2-7 2State the contents of RAM locations after the following program:
SETB MOV MOV MOV MOV MOV PSW.4 R0, #99H R1, #85H R2, #3FH R7, #63H R5, #12H
6
AC
5
F0
4
RS1
3
RS0
2
OV
1
--
0
P
PSW
CY
By default, PSW.3=0 and PSW.4=0; therefore, the instruction SETB PSW.4 sets RS1=1 and RS0=0, thereby selecting register bank 2. Register bank 2 uses RAM locations 10H 17H. After the execution of the above program we have the following RAM location 10 has value 99H RAM location 11 has value 85H RAM location 12 has value 3FH RAM location 17 has value 63H RAM location 15 has value 12H
Vocational Training Council, Hong Kong. Week 4
27
28
Stack Operation
Store Data Get Data
SP = 0A
Address 0A
SP = 0A
SP = 09
Store Data
Address 09
Get Data
SP = 09
SP = 08
Store Data
Address 08
Get Data
SP = 08
SP = 07
Storing Data on the Stack (Increment then store)
SP = 07
Getting Data From the Stack
Week 4
29
Example 2-8 2Show the stack and stack pointer for the following. Assume the default stack area. MOV R6, #25H MOV R1, #12H MOV R4, #0F3H PUSH 6 PUSH 1 PUSH 4
After PUSH 6 0B 0A 09 08 SP = 07
Vocational Training Council, Hong Kong.
After PUSH 1 0B 0A 09 12 08 25 SP = 09
After PUSH 4 0B 0A F3 09 12 08 25 SP = 0A
Week 4
0B 0A 09 08 25 SP = 08
30
Example 2-9 2Examine the stack, show the contents of the registers and SP after execution of the following instruction. All values are in hex. POP 3 ;POP stack into R3 POP 5 ;POP stack into R5 POP 2 ;POP stack into R2
After POP 3 0B 54 0A F9 09 76 08 6C Start SP = 0B 05 04 03 02 ?? ?? ?? ?? 0B 54 0A F9 09 76 08 6C SP = 0A 05 04 03 02 ?? ?? 54 ?? After POP 5 0B 54 0A F9 09 76 08 6C SP = 09 05 04 03 02 F9 ?? 54 ?? After POP 2 0B 54 0A F9 09 76 08 6C SP = 08 05 04 03 02 F9 ?? 54 76
Week 4
31
Example 2-10 2Show the stack and stack pointer for the following. MOV SP, #5FH MOV R2, #25H MOV R1, #12H MOV R4, #0F3H PUSH 2 PUSH 1 PUSH 4
After PUSH 2 63 62 61 60 Start SP = 5F
Vocational Training Council, Hong Kong.
After PUSH 1 63 62 61 12 60 25 SP = 61
After PUSH 4 63 62 F3 61 12 60 25 SP = 62
Week 4
63 62 61 60 25 SP = 60
32
Week 4
33
Week 4
34
Week 4
35
Internal ROM
Internal ROM occupies the code address space from 0000H to 0FFFH (Size = 4K byte) Program addresses higher than 0FFFH will automatically fetch code bytes from external program memory Code bytes can also be fetched exclusively from an external memory by connecting the external access pin (EA) to ground
Week 4
36
37
38
Port 0
Occupies a total of 8 pins (Pins 32-39) Can be used for :
Input only Output only Input and output at the same time (i.e. some pins for input and the others for output)
Can be used to handle both address and data Need pull-up resistors
Week 4
39
The following code will continuously send out to port 0 the alternating values 55H and AAH
MOV MOV ACALL CPL SJMP A, #55H P0, A DELAY A BACK
BACK:
Week 4
40
FFH = 111111112
In the following code, port 0 is configured first as an input port by writing 1s to it, and then data is received from that port and sent to P1
MOV MOV MOV MOV SJMP A, #0FFH P0, A A, P0 P1, A BACK
BACK:
Week 4
41
Week 4
42
Port 1
Occupies a total of 8 pins (Pins 1-8) Can be used as input or output Does not need any pull-up resistors Upon reset, port 1 is configured as an output port No alternative functions
Week 4
43
BACK:
Week 4
44
45
Port 2
Occupies a total of 8 pins (Pins 21-28) Similar function as Port 1 Can be used as input or output Does not need any pull-up resistors Upon reset, port 2 is configured as an output port
Week 4
46
BACK:
Week 4
47
BACK:
Week 4
48
Week 4
49
Port 3
Occupies a total of 8 pins (Pins 10-17) Similar function as Port 1 and Port 2 Can be used as input or output Does not need any pull-up resistors Upon reset, port 3 is configured as an output port Pins can be individually programmable for other uses Most commonly be used to provide some important signals (e.g. interrupts)
Week 4
50
Function
RxD TxD INT0 INT1 T0 T1 WR RD
Pin
10 11 12 13 14 15 16 17
Week 4
51
Read-Modify-Write Feature Read-ModifyA method used to access the 8051 ports Combining all 3 actions in a single instructions :
Read the data at the port Modify (do operation on) the data at the port Write the results to the port Example:
AGAIN: MOV XRL ACALL SJMP P1, #55H P1, #0FFH DELAY AGAIN
ANL P1, A ORL P2, A XRL P3, A JBC P1.1, LABEL CPL P3.0 INC P2 DEC P2 DJNZ P3, LABEL
Week 4
52
Week 4
53
P1.2 A, #45H
54
Summary
General physical & operational features 8051 hardware description 8051 pin description Read-modify-write port instructions
Week 4
55
Review Questions
1. 2. In the 8051, the program counter is _________ bits wide. True or false. Every member of the 8051 family, regardless of the maker, wakes up at memory 0000H when it is powered up. At what ROM location do we store the first opcode of an 8051 program? The instruction MOV A, #44H is a ______-byte instruction. What is the ROM address space for the 8052 chip? The flag register in the 8051 is called __________. What is the size of the flag register in the 8051? Which bits of the PSW register are user-definable?
3. 4. 5. 6. 7. 8.
Week 4
56
Review Questions
9. Find the CY and AC flag bits for the following code.
MOV ADD A, #0FFH A, #01
10. Find the CY and AC flag bits for the following code. MOV A, #0C2H ADD A, #3DH 11. What is the size of the SP register? 12. With each PUSH instruction, the stack pointer register (SP) is _______ (incremented/decremented) by 1. 13. With each POP instruction, the SP _____ (incremented/decremented) by 1. 14. ON power up, the 8051 uses RAM location _____ as the first location of the stack.
Vocational Training Council, Hong Kong. Week 4
57
Review Questions
15. On power up, the 8051 uses bank ____ for registers R0 R7. 16. On power up, the 8051 uses RAM locations _____ to _____ for register R0 R7 (register bank 0). 17. Which register bank is used if we alter RS0 and RS1 of the PSW by the following two instruction? SETB SETB PSW.3 PSW.4
18. In Question 17, what RAM locations are used for register R0 R7?
Week 4
58
Read reference
The 8051 Microcontroller and Embedded Systems Using Assembly and C, Mazidi
Chapter 8
P.217 P.227
Chapter 1 Chapter 2
Week 4
59