Beruflich Dokumente
Kultur Dokumente
40
VCC
30 pF 19
12 MHz
XTL1 XTL2
18 30 pF
P0.7 P0.6 P0.5 P0.4 P0.3 P0.2 P0.1 P0.0 P1.7 P1.6 P1.5 P1.4 P1.3 P1.2 P1.1 P1.0 P2.7 P2.6 P2.5 P2.4 P2.3 P2.2 P2.1 P2.0
32 33 34 35 36 37 38 39 8 7 6 5 4 3 2 1 28 27 26 25 24 23 22 21
29 30 31 9
8051
17 16 15 14 13 12 11 10
VSS
20
22
Port 2 Dual purpose port: 1) General purpose I/O port 2) High-byte of the address bus with external code memory or external data memory greater than 256 bytes
23
PSEN (Program Store Enable) Control signal used in the fetching of an instruction When low, it enables external code memory When high, the program instructions are fetched from internal ROM 24
XTAL2
26
Write to a port pin loads data into a port latch that drives a transistor connected to the port pin Read-modify-write operations (CPL P1.5) read the latch to avoid misinterpreting the voltage level in the event the pin is heavily loaded Read operations (MOV C, P1.5) read the pin; port latch must have previously been set to a 1 otherwise the FET is ON and pulls the output low. If a port latch is cleared or a system reset occurs then it cannot function as an input unless the latch is set first (eg. SETB P1.5)
27
Memory Organization
Notable features: Registers and I/O ports are memory mapped (accessible like any other memory location) Stack resides within internal RAM Internal memory is divided between: Register banks (00H-1FH) Bit-addressable RAM (20H-2FH) General-purpose RAM (30H-7FH) Special function registers (80H-FFH) 28
Byte address 7F
Bit address
Byte address FF F0 7F E0 E7 7E E6 7D E5 D5 B5 A5
D0 D7 D6 30 2F 2E 2D 2C 2B 2A 29 28 27 26 25 24 23 22 21 20 1F 18 17 10 0F 08 07 00 B8 7F 77 6F 67 5F 57 4F 47 3F 37 2F 27 1F 17 0F 07 7E 76 6E 66 5E 56 4E 46 3E 36 2E 26 1E 16 0E 06 7D 7C 75 74 6D 6C 65 64 5D 5C 55 54 4D 4C 45 44 3D 3C 35 34 2D 2C 25 24 1D 1C 15 14 0D 0C 05 04 7B 73 6B 63 5B 53 4B 43 3B 33 2B 23 1B 13 0B 03 7A 72 6A 62 5A 52 4A 42 3A 32 2A 22 1A 12 0A 02 79 71 69 61 59 51 49 41 39 31 29 21 19 11 09 01 78 70 68 60 58 50 48 40 38 30 28 20 18 10 08 00 -
D0 PSW B8 IP
B0 B7 B6 A8 AF -
B0 P3 A8 IE
AB AA A9 A5 A5 A5
A0 A7 A6 99 98 90 8D 8C 8B 8A 89 88 87 83 82 81 80
A5 P2 SBUF SCON P1 TH1 TH0 TL1 TL0 TMOD TCON PCON DPH DPL SP P0
9F 97
9E 96
99 91
98 90
8F
8E
Not bit addressable Not bit addressable Not bit addressable Not bit addressable Not bit addressable 8D 8C 8B 8A Not bit addressable Not bit addressable Not bit addressable Not bit addressable 85 84 83 82
89
88
87
86
81
80
RAM
29
General-Purpose RAM
30
Bit-Addressable RAM
210 bit-addressable locations 128 bytes are located from 20H to 2FH Rest are in special function registers Bits can be set, cleared, ANDed, Ored, etc,... Most microprocessors require a read-modify-write sequence to achieve the same effect. I/O ports are also bit-addressable 128 general-purpose bit-addressable locations from 20H to 2FH which can be accessed as bytes or bits For example, to set bit 67H: SETB 67H 31
Register Banks
Contained in the bottom 32 locations of internal memory 8051 instruction set supports 8 registers, R0-R7, and by default are located at 00H-07H ex. to read the contents of 05H into the accumulator:
MOV A,R5 ;ONE BYTE INSTRUCTION, REG ADDRESSING
Instructions using registers R0 to R7 (ie. Register addressing) are shorter than the same instructions using direct addressing Frequently used data values should use registers. Active register bank may be changed by changing the appropriate bits in the program status word (PSW)
32
E0H is both the byte address of the entire ACC and the bit address of the least significant bit in the ACC ex. Port 1 is at byte address 90H or 10010000B. The bits within Port 1 have addresses 90H to 97H, or 10010xxxB 33
OV P
Carry Flag (C or CY) Dual purpose 1. Arithmetic operations set if there is a carry out of bit 7 during an add, or set if there is a borrow into bit 7 during a subtract eg. If the ACC contains FFH, then the instruction ADD A,#1 leaves the ACC equal to 00H and sets the carry flag in the PSW 2. Boolean accumulator 1-bit register for Boolean instructions operating on bits. eg. The following instruction ANDs bit 25H with the carry flag and places the result back in the carry flag
34
35
36
8E 142 As a signed number, 8EH represents -116, which is not the correct result of 142, therefore, the OV bit is set.
37
38
B Register
B Register (or accumulator B) Used along with the accumulator for multiply and divide operations. MUL AB instruction multiplies the 8-bit unsigned values in A and B and leaves the 16-bit result in A (low-byte) and B (high-byte) Similarly, DIV AB divides A by B, leaving the integer result in A and the remainder in B B register can also be used as a general-purpose scratch-pad register It is bit addressable through bit addresses F0H to F7H
39
Stack Pointer
8-bit register that contains the address of the data item currently on the top of the stack. Pushing data on the stack increments the SP before writing the data. Popping from the stack reads data and then decrements the SP Stack is kept in internal RAM and is limited to addresses accessible by indirect addressing. (first 128 bytes of on-chip RAM) To reinitialize the SP with the stack beginning at 60H: MOV SP,#5FH
This would limit the stack to 32 bytes, since the uppermost address of onchip RAM is 7FH. The value 5FH is used, since the SP increments to 60H before the first push operation Default (or reset value) is 07H, thereby storing the first data value in 08H. If SP is not reinitialized, the register bank 1 (maybe 2 and 3) is not available, since this area of internal RAM is the stack
40
Data Pointer
16-bit register at addresses 82H (DPL, low-byte) and 83H (DPH, high byte) used to access external code or data memory The following code writes 55H into external RAM location 1000H: MOV MOV MOVX A,#55H DPTR,#1000H @DPTR, A ; X denotes external memory
41
Port Registers
4 I/O ports: P0, P1, P2, P3 All ports are bit addressable Each bit can be specified using the dot operator (ie. P1.7) P0, P2, P3 may not be available for I/O if external memory is used or if some other special features are used (interrupts, serial port, etc.) P1 is always available for I/O Example: consider the interface to a device with a status bit called BUSY, which is set when the device is busy and clear when it is ready. If BUSY connects to Port 1 bit 5, the following loop could be used to wait for the device to become ready: WAIT: JB P1.5,WAIT
This instruction means If bit P1.5 is set, jump to the lable WAIT. Ie. Jump back and check it again.
42
Remaining Registers
Timer Registers 2 16-bit timers are at TL0, TH0, TL1, TH1 Controlled by using TMOD and TCON Serial Port Registers SBUF: buffer for serial port SCON: control register for serial port Interrupt Registers IE: interrupt enable IP: interrupt priority
43
External Memory
Memory cycle A0-A15 24 lines D0-D7 a) Nonmultiplexed (24 lines) Memory cycle A8-A15 16 lines AD0-AD7 b) Multiplexed (16 lines) FIGURE 2-7 Multiplexing the address bus (low-byte) and data bus Data Address
44
Port 2 PSEN
A8-A15 OE
45
PSEN
Port 2
PCH
PCH
Port 0
PCL
Opcode
PCL
Byte 2
FIGURE 2-9 Read timing for external code memory (opcode fetch)
46
Port 0
PCL
Opcode
DPL
Data in
FIGURE 2-10 Read timing for external data memory (ie. MOVX A,@DPTR instruction)
47
NC (no connection)
48
Address Decoding
Address bus Data bus
D0-D7 D0-D7 PSEN OE EPROM 8K bytes A0-A12 CS CS CS A15 C A14 B A13 A +5V
74HC138 0 1 2 3 4 5 6 7
RD WR
E1 E2 E3
49