2 Objective程式中的資料可能是放在 Register 中，或在RAM 中某一位址上，或在 ROM 一塊特殊區域放置資料，或者是指令中直接給予定值。設計 8051 IC 的人們，提供這些存取資料的方式。這些方式便叫作 Addressing Mode。中文稱為“定址模式”：決定參數位址的模式也許不同家的 Assembler 會有不同的指令寫法，但基本上 addressing mode 都是一樣的。
3 Sections 5.1 Immediate and register addressing modes 5.2 Accessing memory using various address modes5.3 Bit addresses for I/O and RAM5.4 Extra-128-byte on-chip RAM in 80525.3 節 bit address的部份是原本 chapter 8 的部份.
4 Section 5.1 Immediate and Register Addressing Modes
5 What is Addressing Mode The CPU can access data in various ways.The data could be in a register, or in memory（RAM or ROM）, or be provided as an immediate value.These various ways of accessing data are called addressing mode.
6 Addressing Mode in the 8051 Five addressing mode in the 8051： 1. immediate2. register3. direct4. register indirect5. indexedWe use MOV as an example. One can use any instruction as long as that instruction supports the addressing mode.先不管這些名詞的意義, 先猜猜看有哪些方法可以存取記憶體 ROM 和 RAM.我們只是用MOV來當範例, 只要 instruction 本身有支援的 addressing mode, programmer 都可以拿來用.Ex: INC 包括 2,3,4 三種addressing mode: “INC R0”, “INC 20H”. 見 Table 10.
7 Addressing Mode 1 1. immediate － the operand is a constant MOV A,#01FH2. register － the operand is in a registerMOV A,R03. direct － access the data in the RAM with addressMOV A,01FH4. register indirect － the register holds the RAM address of the dataMOV5. indexed － for on-chip ROM accessMOVC
8 Immediate Addressing Mode The source operand is a constant.The immediate value can be loaded into any of the registers.The immediate data must be preceded by the pound sign, ‘＃’.The immediate value is bounded by the size of register.Please use the simulation tools to find the the machine code and the content of registers after execution.See Tables 10 ＆11 (page 614).When the instruction is assembled, the operand comes immediately after the opcode.
9 Example of Immediate Mode（1/2） MOV A,#25H ;A=25HC 3E MOV R4,#62 ;R4=62=3EHInstruction Opcodes in Table 11Hex code Mnemonic Operands ByteMOV A, #data7C MOV R4, #dataInstruction Opcodes in Table 10Mnemonic Oscillator PeriodMOV A, #dataMOV Rn, #dataRn - Register R7-R0 of the currently selected Register Bank. in ‘Table 10 page 418
10 Example of Immediate Mode（2/2） MOV DPTR,#4521HInstruction Opcodes in Tables 10, 11Hex code Mnemonic Operands Byte Oscillator PeriodMOV DPTR, #dataDPTR =DPH+DPL：MOV DPL,#21HMOV DPH,#45HDPTR has high byte DPH (address 83) and low byte DPL (address 82)However, 8051 CPU does not provide a special machine instructions for MOV DPH, #data or MOV DPL,#data,Although 8051 Assembler provide the assembly instructions for DPH and DPL要注意存入DTPR的值要小於65536.
11 EQU (1/2) The EQU directive is used in the immediate addressing mode. ORG 0HCOUNT EQU 30C 1E MOV R4,#COUNTMOV DPTR,#MYDATAORG 200HD MYDATA:DB "America"ENDAssembler will transfer the COUNT=30=1EH to the immediate value.MYDATA is the address of “America”, point to “A”Assembler will transfer the MYDATA to the address 200H.ORG 0HCOUNT EQU 30C 1E MOV R4,#COUNT ;R4=30MOV DPTR,#MYDATA ;DPTR=200HORG 200HD MYDATA DB "America"END
12 EQU (2/2)In Pass 1, assembler reads these directives and creates a table:COUNT 1EHMYDATA 200HIn Pass 2, assembler transfers COUNT and MYDATA into numbers and creates machine codes:MOV R4,#COUNT → 7C 1EMOV DPTR,#MYDATA →
13 Addressing Mode 2 1. immediate － the operand is a constant MOV A,#01FH2. register － the operand is in a registerMOV A,R03. direct － access the data in the RAM with addressMOV A,01FH4. register indirect － the register holds the RAM address of the dataMOV5. indexed － for on-chip ROM accessMOVC
14 Register Addressing Mode Register addressing mode involves the use of registers to hold the data.Register means Rn, A & CY.The source and destination registers must match in size.The movement of data between Rn registers is not allowed. “MOV R4,R7” is illegal.You can find that the opcode in register addressing mode is short！Register 指的是 Rn, A, DPTR & CY 等常用的 register.因為這些 register 的存取特別常用, 所以會有特別的 instructions 及相對應之 opcode. Opcode 會特別短, 因為不需要將 register 轉成相對應之 RAM address.
15 Example of Register Mode（1/2） E MOV A,R0FA MOV R2,AD ADD A,R5Instruction Opcodes in Table 11Hex code Mnemonic Operands ByteE MOV A,RFA MOV R2,A2D ADD A,R
16 Example of Register Mode（2/2） E MOV A,R0FA MOV R2,AD ADD A,R5Instruction Opcodes in Table 10Mnemonic Oscillator PeriodMOV A, RnMOV Rn, AADD A, Rn
17 Section 5.2 Accessing Memory Using Various Address Modes
18 Addressing Mode 3 1. immediate － the operand is a constant MOV A,#01FH2. register － the operand is in a registerMOV A,R03. direct － access the data in the RAM with addressMOV A,01FH4. register indirect － the register holds the RAM address of the dataMOV5. indexed － for on-chip ROM accessMOVC
19 Direct Addressing Mode (1/2) There are 128 bytes of RAM in the 8051.The RAM has been assigned address FH.00-1FH：the register banks and stack20-2FH：bit-addressable space to save single-bit data30-7FH：scratch pad RAMThere is no name for some RAM locations -- so we need to use direct address mode to access them.If an number begins without a pound sign, ‘＃’, then Assembler think it as the RAM address.It is most often used to access RAM location 30-7FH.This is due to the fact that register bank locations are accessed by the register names of R0-R7 -- register mode.There is no such name for other RAM locations. -- so use direct address mode.“Direct” 直接的 means “data address”, 直接給予其記憶體位址.
20 Direct Addressing Mode (2/2) In direct addressing mode, the data is in a RAM memory location whose address is known, and this address is given as a part of the instruction.We can use the direct addressing mode to access128-byte on chip RAMSpecial Function Registers (they are RAM addresses too.)
21 Example of Direct Mode（1/2） A MOV R0,40HF MOV 56H,AMOV DPTR,#4521HMOV DPH,#45H5 000A MOV DPL,#21HInstruction Opcodes Table 11Hex code Mnemonic Operands BytesA MOV R0, data addrF MOV data addr., AMOV data addr., #dataDPTR has high byte DPH (address 83) and low byte DPL (address 82)
22 Example of Direct Mode（2/2） A MOV R0,40HF MOV 56H,AMOV DPTR,#4521HMOV DPH,#45H5 000A MOV DPL,#21HInstruction Opcodes Table 10Mnemonic Oscillator PeriodMOV Rn, directMOV direct, AMOV direct, #data
23 Register Bank（1/2）If we use register bank 0, then the following instructions 2＆3 do the same works：C 64 MOV R4,#100E5 04 MOV A,4 ;direct modeEC MOV A,R4 ;register modeInitially, the 8051 uses the register bank 0.R4 has RAM address 04H.
24 Register Bank（2/2）If we use register bank 1, then the following instructions 3＆4 do the different works：D2 D3 SETB RS0 ;RS0=1C 64 MOV R4,#100E5 04 MOV A,4 ;A=0EC MOV A,R4 ;A=100=64HRS1=PSW.4=0 ＆ RS0=PSW.3=1 register bank 0The bit address of RS0 is D3.Initially, the content of RAM is 00H.R4 has RAM address 0CH.RAM 0CH has the value 100.
25 SFR（Special Function Register） There are many special functions registers in the We call them SFR.Example：A, B, PSW, and DPTRThe 8051 Assembler provides that the SFR can be accessed by their name or by their addresses.See Table 5-1 for SFR addressesThe SFR have addresses between 80H and FFH.Not all the address space of 80H to FFH is used by the SFR.The addresses 00-0FH are addresses of RAM memory inside the So SFR needs to use the addresses 80H to FFH.The addresses 80H to FFH is not a really RAM.The unused locations 80H to FFH are reserved and must not be used by the 8051 programmer.We can access SFR by direct addressing mode.
26 Table 5-1: Special Function Register (SFR) Addresses（1/2） SymbolNameAddressACC*Accumulator0E0HB*B register0F0HPSW*Program status word0D0HSPStack pointer81HDPTRData pointer 2 bytesDPLLow byte82HDPHHigh byte83HP0*Port 080HP1*Port 190HP2*Port 20A0HP3*Port 30B0HIP*Interrupt priority control0B8HIE*Interrupt enable control0A8HTMODTimer/counter mode control89H
27 Table 5-1: Special Function Register (SFR) Addresses （2/2） SymbolNameAddressTCON*Timer/counter control88HT2CON*Timer/counter 2 control0C8HT2MODTimer/counter mode control0C9HTH0Timer/counter 0 high byte8CHTL0Timer/counter 0 low byte8AHTH1Timer/counter 1 high byte8DHTL1Timer/counter 1 low byte8BHTH2Timer/counter 2 high byte0CDHTL2Timer/counter 2 low byte0CCHRCAP2HT/C 2 capture register high byte0CBHRCAP2LT/C 2 capture register low byte0CAHSCON*Serial control98HSBUFSerial data buffer99HPCONPower control87H*bit addressable (discussed further in Chapter 8)
28 ACC and Its Address ACC has SFR address 0E0H. E0 55 MOV 0E0H,#55HMOV A,#55HD2 E1 SETB A.1Compare their code size and execution time.“ACC＊”, ＊ means this register is bit addressable. You can access each bit of ACC independently.E0 55 MOV 0E0H,#55H : 2 bytes 2 MCsMOV A,#55H : 2 bytes 1 MCFor address 0E0H, if the instruction is for a bit “SETB”, then it refers to A.0if the instruction is for a byte “MOV”, then it refers to A.SFR addr. 0E7 0E E5 0E E3 0E2 0E1 0E0ACCA.7A.6A.5A.4A.3A.2A.1A.0
29 Example 5-1 Write code to send 55H to ports P1 and P2, using (a) their names(b) their addresses.Solution:(a) MOV A,#55H ;A=55HMOV P1,A ;P1=55HMOV P2,A ;P2=55H(b) From Table 5-1, P1 address = 90H; P2 address = A0HMOV A,#55H ;A=55HMOV 90H,A ;P1=55HMOV 0A0H,A ;P2=55H
30 Stack Another major use of direct addressing mode is the stack. In the 8051 family, only direct addressing mode is allowed for pushing onto the stack or for popping from the stack.That is “PUSH A” is invalid. ”PUSH 3” is pushing the content of RAM address 3H.
31 Example 5-2Show the code to push R5, R6, and A onto the stack and then popthem back them into R2, R3, and B.We want：B = A, R2 = R6, and R3 = R5.Solution:; Register bank 0PUSH ;push R5 onto stack C0 05PUSH ;push R6 onto stack C0 06PUSH 0E0H ;push register A onto stackPOP 0F0H ;pop top of stack into register BPOP ;pop top of stack into R2POP ;pop top of stack into R3C PUSH R5C PUSH R6C0 E0 PUSH AC PUSH 05C PUSH 06C0 E0 PUSH 0E0H
32 Addressing Mode 4 1. immediate － the operand is a constant MOV A,#01FH2. register － the operand is in a registerMOV A,R03. direct － access the data in the RAM with addressMOV A,01FH4. register indirect － the register holds the RAM address of the dataMOV5. indexed － for on-chip ROM accessMOVC
33 Register Indirect Addressing Mode In the register indirect addressing mode, a register is used as a pointer to the data.That is, this register holds the RAM address of the data.Only registers R0 and R1 can be used to hold the address of an operand located in RAM.Usually, R0 and R1 are denoted by Ri.When R0 and R1 hold the addresses of RAM locations, they must be preceded by the sign.So it is called “indirect”.If you forget sign, 8051 Assembler will use the content of Ri.使用 indirect addressing mode 的時機, (1) 通常是因為要處理相連的一串 memory, (2) 改 pointer R0/R1 會比改整個程式中的 address 有彈性 (3) 8052 的 upper 128-byte RAM 只能用 indirect addressing mode 處理.
34 Example of Register Indirect Mode（1/2） MOV 20H,#100MOV R0,#20HE MOVRAM1. put 64H to addr. 20H2. let R0 be the data address1E F :R H3. copy the content in addr. R0=20H to AA H
35 Example of Register Indirect Mode（2/2） F0 80 MOV B,#080HMOV R1,#31HA7 F0RAM3. copy B to the RAM location with addr. R1=31H2F :2. let R1 be the data addressR H1. let B=80HB H
36 Example 5-3 (1/3)Write a program to copy the value 55H into RAM memory locations 40H to 44H using(a) direct addressing mode,(b) register indirect addressing mode without a loop,(c) with a loop.Solution of (a) :MOV A,#55HMOV 40H,AMOV 41H,AMOV 42H,AMOV 43H,AMOV 44H,ARAMcopy A to the RAM location of addr. 43HA H
37 Example 5-3 (2/3)Solution of (b) register indirect addressing mode without a loopMOV A,#55H ;load A with value 55HMOV R0,#40H ;load the pointer. R0=40H;copy A to RAM location where R0; points toINC R ;increment pointer. Now R0=41HINC R ;R0=42HINC R ;R0=43HINC R0RAMR HA H
38 Example 5-3 (3/3) Solution of (c) with a loop: MOV A,#55H ;A=55H MOV R0,#40H ;load pointer. R0=40H,MOV R2,#05H ;load counter, R2=5AGAIN: MOV @R0,A ;copy 55 to RAM location; R0 points toINC R ;increment R0 pointerDJNZ R2,AGAIN ;loop until counter = 0在最後一回合, INC R0 得到 R0=45, DJNZ R2,AGAIN 中R2 由 1減為 0 就離開 loop, 故A沒有寫入45H.
39 Advantage of Register Indirect Addressing Mode One of the advantages of register indirect addressing mode is that it makes accessing data dynamic rather than static. (Flexible!)Solution (c) in Example 5-3 is the most efficient and is possible only because of register indirect addressing mode.Looping is not possible in direct addressing mode.See Examples 5-4, 5-5, too.Their use is limited to accessing any information in the internal RAM.Use looping is more flexible too.R0&R1 是唯一可用於 indirect addressing mode的register, 所以若要存取 ROM, 則沒有辦法用R0或R1來表示, 也就是說 indirect addressing mode 無法使用.If we need to access externally connected RAM or on-chip ROM, we need a 16-bit pointer. In such cases, the DPTR register is used. (必須用 indexed addressing mode, 不可以用 indirect addressing mode)
40 Example 5-4Write a program to clear 16 RAM locations starting at RAM address60H.Solution:CLR A ;A=0MOV R1,#60H ;load pointer. R1=60HMOV R7,#16 ;load counter, R7=10HAGAIN: MOV @R1,A ;clear RAM location R1; points toINC R ;increment R1 pointerDJNZ R7,AGAIN ;loop until counter = 0
41 Example 5-5Write a program to copy a block of 10 bytes of data from RAM locations starting at 35H to RAM locations starting at 60H.Solution:MOV R0,#35H ;source pointerMOV R1,#60H ;destination pointerMOV R3,#10 ;counterBACK: MOV ;get a byte from sourceMOV @R1,A ;copy it to destinationINC R ;increment source pointerINC R ;increment destination; pointerDJNZ R3,BACK ;keep doing it 10 times
42 Addressing Mode 5 1. immediate － the operand is a constant MOV A,#01FH2. register － the operand is in a registerMOV A,R03. direct － access the data in the RAM with addressMOV A,01FH4. register indirect － the register holds the RAM address of the dataMOV5. indexed － for on-chip ROM accessMOVC
43 Indexed Addressing Mode Indexed addressing mode is used to access 8051 on-chip ROM.It is widely used in accessing data elements of look-up table entries located in the program ROM space of the 8051.A look-up table is a ROM block where the data is given previously (then you can access it frequently).Ex: MOVC
44 MOVC MOVC: to access internal ROM Transfer data between internal ROM and A.The “C” means code16-bit memory address is held by DPTR or PCMOV DPTR,# ;ROM addressMOVC;access the ROM data with address A+DPTRMOVC;access the ROM data with address A+PCMOVC 的用法包括 MOVC 及 MOVC除了可以用 DPTR 存 ROM address, another register used in indexed addressing mode is the PC. See Appendix A.PC的值不用事先給定.
45 Example of MOVC Register Indexed addressing Mode： MOV DPTR,#MYDATAE CLR AMOVCF MOV R0,AFE HERE: SJMP HEREMYDATA: DB "USA"DPTR=#MYDATA=0008HA+DPTR=0008HROME4A 41A H
46 MOVX MOVX: to access external RAM/ROM connected to the 8051 Transfer data between external memory and A.See Chapter 14 in page 431.The “X” means external (memory space must be implemented externally).Note: RAM can be read and write.16-bit external memory address is held by DPTRMOVX or MOVX @DPTR,A8-bit external memory address is held by R0/R1MOVX or MOVX @Ri,A只能用 register A.External memory 可以是 (1) 64k data ROM, (2) 64k data ROM 加上 64k program ROM, (3) 64k data RAM, (4) 64k data/program ROM, (5) 64k data ROM 加上 64k program ROM 加上 64k program RAM (6) 256k Data RAMProgram ROM 中可以有 code 及 data, 但 data ROM 無法讓 program 共享.要能存取 external memory, 除了用 MOVX, 硬體上需要 control pins 來控制讀取那一個 memory, 寫入或讀出.
47 Example 5-6 (1/2)In this program, assume that the word “USA” is burned into ROMlocations starting at 200H, and that the program is burned intoROM locations starting at 0. Analyze how the program works andstate where “USA” is stored after this program is run.Solution:ROME4:DPTR H HClear A= A+DPTR= 0200HU S AR HA HR HR H
48 Example 5-6 (2/2) ORG 0000H ;burn into ROM from 0 MOV DPTR,#200H ;DPTR=0200HCLR A ;clear A(A=0)MOVC ;get the char spaceMOV R0,A ;save it in R0INC DPTR ;DPTR=201MOVC ;get the next charMOV R1,A ;save it in R1INC DPTR ;DPTR=202MOV R2,A ;save it in R2HERE:SJMP HERE ;stay hereORG 200HMYDATA: DB “USA”END ;end of program
49 Example 5-7 (1/2)Assuming that ROM space starting at 250H contains “America”, write a program to transfer the bytes into RAM locations starting at 40H.Solution of (a) This method uses a counter：ORG 0000MOV DPTR,#MYDATA ;InitializationMOV R0,#40HMOV R2,#7BACK: CLR AMOVCMOV @R0,AINC DPTRINC R0DJNZ R2,BACKHERE: SJMP HEREORG 250HMYDATA: DB “AMERICA”ENDROMRAM:DDAME R I C AA 41R0 40DPTR
50 Example 5-7 (2/2)Solution of (b) This method uses null char for end of string：ORG 0000MOV DPTR,#MYDATAMOV R0,#40H ;No “MOV R2,#7”BACK: CLR AMOVCJZ HERE ;if A=0MOV @R0,A ;leave the blockINC DPTRINC R0SJMP BACKHERE: SJMP HEREORG 250HMYDATA: DB “AMERICA”,0 ;notice null char;for end of stringEND
51 Example 5-8Write a program to get the x value from P1 and send x2 to P2,continuously.Solution:ORG 0MOV DPTR,#XSQR_TABLEMOV A,#0FFHMOV P1,A ;P1 as INPUT PORTBACK: MOV A,P ;GET XMOVC ;Count the addr.MOV P2,A ;Issue it to P2SJMP BACKORG 300HXSQR_TABLE:DB 0,1,4,9,16,25,36,49,64,81ENDPlease trace the program by set A=0, then A=1, then A=2,...BACK: 應放在 MOV A,#0FFH的位置才對.
52 Example 5-9 Answer the following questions for Example 5-8. (a) Indicate the content of ROM locations H.(b) At what ROM location is the square of 6, and what value shouldbe there?(c) Assume that P1 has a value of 9: what value is at P2 (in binary)?Solution:(a) All values are in hex.300 = (00) 301 = (01) 302 = (04) 303 = (09)304 = (10) 4×4=16=10H305 = (19) 5×5=25=19H306 = (24) 6×6=36=24H307 = (31) 308 = (40) 309 = (51)(b) ROM Addr.=306H; the value is 24H=36(c) P2 = B=51H=81 in decimal.
53 Example 5-10Write a program to toggle P1 at total of 200 times. Use RAM location 32H to hold your counter value instead of registers R0-R7.Solution:MOV P1,#55HMOV 32H,#200LOP1: CPL P1ACALL DELAYDJNZ 32H,LOP1Usually, we use R0-R7 of bank 0, 8-1FH for stack.所以要儲存資料可能要用到scratch pad. 這個範例就是以 direct addressing mode 存取 32H 的範例.可以不要教.
55 Bit-addressable RAMThe bit-addressable RAM locations are (byte addresses) 20H to 2FH.Only 16 bytes of RAM are bit-addressable.16 * 8 bits = 128 bits (in decimal) = 80H bits (in hex)They are addressed as 00 to 7FHThe internal RAM locations 20 to 2FH are both byte-addressable and bit-addressable.We can use only the single-bit instructions in Table 5-2 to access these bits.Only direct addressing mode is used here.Note that the bit addresses 80H to F7H belong to SFR.
56 Figure 8-3. 128 Bytes of Internal RAM Byte address7FGeneral purpose RAM30Bit-addressable locations2F7F7E7D7C7B7A797877767574737271706F6E6D6C6B6A696867666564636261605F5E5D5C5B5A595857565554535251504F4E4D4C4B4A494847464544434241403F3E3D3C3B3A393837363534333231302F2E2D2C2B2A292827262524232221201F1E1D1C1B1A191817161514131211100F0E0D0C0B0A090807060504030201002E2D2C2B2A292827262524232221201FBank 31817Bank 2100FBank 10807Default register bank for R0 - R700
57 Table 5-2: Single-Bit Instructions FunctionSETBbitSet the bit (bit = 1)CLRClear the bit (bit = 0)CPLComplement the bit (bit = NOT bit)JBbit,targetJump to target if bit = 1 (jump if bit)JNBJump to target if bit = 0 (jump if no bit)JBCJump to target if bit = 1, clear bit (jump if bit, then clear)
58 Example 5-11Find out to which byte each of the following bits belongs.Give the address of the RAM byte in hex.(a)SETB 42H;set bit 42H to (d)SETB 28H;set bit 28H to 1(b)CLR 67H;clear bit 67H (e)CLR 12 ;clear bit 12 (decimal)(c)CLR 0FH;clear bit 0FH (f)SETB 05Solution:(a) RAM bit address of 42H belongs to D2 of RAM location 28H(b) RAM bit address of 67H belongs to D7 of RAM location 2CH(c) RAM bit address of 0FH belongs to D7 of RAM location 21H(d) RAM bit address of 28H belongs to D0 of RAM location 25H(e) RAM bit address of belongs to D4 of RAM location 21H(f) RAM bit address of belongs to D5 of RAM location 20H
59 Bit-addressable of SFR Every SFR register is assigned a bye address and ports P0-P3 are parts of the SFR.8051 I/O ports are all bit-addressable.Only registers B, A, PSW, IP, IE, SCON, TCON are bit-addressable.
60 Figure 5-2. SFR RAM Address (Byte and Bit) (1/2) Special Function RegistersByte addressBit addressF7 F6 F5 F4 F3 F2 F1 F0E7 E6 E5 E4 E3 E2 E1 E0D7 D6 D5 D4 D3 D2 D1 D0A7 A6 A5 A4 A3 A2 A1 A0AF AC AB AA A9 A8B7 B6 B5 B4 B3 B2 B1 B0BC BB BA B9 B8FFF0E0D0B8B0A8A0BACCPSWIPP3IEP2
61 Figure 5-2. SFR RAM Address (Byte and Bit) (2/2) Special Function Registers9F 9E 9D 9C 9B 9A8F 8E 8D 8C 8B 8Anot bit addressable9998908D8C8B8A89SBUFSCONTH1TL0P1TMODDPH888783828180TH0TL1TCONPCONDPLSPP0
62 I/O Bit AddressesWhen “SETB P1.0” is assembled, it becomes “SETB 90H” since P1.0 has RAM address of 90H.SETB P1.0SETB 90HThe machine codes for SETB P1.0 and SETB 90H are both D2 90.
63 Table 5-3: Bit Addresses for All Ports P0 Addr.P1 Addr.P2 Addr.P3 Addr.Port’s BitPPP2.0 A0P3.0 B0D0PPP2.1 A1P3.1 B1D1PPP2.2 A2P3.2 B2D2PPP2.3 A3P3.3 B3D3PPP2.4 A4P3.4 B4D4PPP2.5 A5P3.5 B5D5PPP2.6 A6P3.6 B6D6PPP2.7 A7P3.7 B7D7
64 Example 5-12For each of the following instructions, state to which port the bit belongs. Use Figure 5-3.(a) SETB 86H (b) CLR 87H (c) SETB 92H(d) SETB 0A7HSolution:(a) SETB 86H is for SETB P0.6 (D2 86)(b) CLR 87H is for CLR P0.7 (C2 87)(c) SETB 92H is for SETB P1.2 (D2 92)(d) SETB 0A7H is for SETB P2.7 (D2 A7)
65 Figure 5-3. Bits of the PSW Register PSW (Program Status Word) registerBit address D7 D D5 D D D2 D D0Byte addressCYAC--RS1RS0OVPD0RS1RS0Register BankAddress00H – 07H108H – 0FH210H – 17H318H – 1FH
66 Example 5-13 Write a program to save the accumulator in R7 of bank 2. Solution:CLR PSW.3 ;C2 D3SETB PSW.4 ;D2 D3MOV R7,A ;FF
67 Example 5-14While there are instructions such as JNC and JC to check the carry flag bit (CY), there are no such instructions for the overflow flag bit (OV).How would you write code to check OV?Solution:The OV flag is PSW.2 of the PSW register.We can use the following instruction to check the OV flag.JB PSW.2,TARGET ;jump if OV=1(machine code is 20 D2 rel-addr)有bit-addressable 這樣的 access 方式, 就可以
68 Example 5-15Write a program to see if the RAM location 37H contains an even number.If so, send it to P2.If not, make it even and then send it to P2.Solution:MOV A,37H ;RAM location 37H→AJNB ACC.0,YES ;If A is even, jump to YESINC A ;it is odd, make it evenYES:MOV P2,A ;send A to P2
69 Example 5-16Assume that bit P2.3 is an input and represents the condition of adoor. If it goes high, it means that the door is open.Monitor the bit continuously.Whenever it goes high, send a low-to-high pulse to port P1.5 to turn on a buzzer.Solution:HERE:JNB P2.3,HERE ;keep monitoring for highCLR P ;clear bit (P1.5=0)ACALL DELAYSETB P1.5SJMP HERElow-to-high pulse
70 Example 5-17The states of bits P1.2 and P1.3 of I/O port P1 must be saved beforethey are changed. Write a program to save the status of P1.2 in bitlocation 06 and the status of P1.3 in bit location 07.Solution:CLR ;clear bit address 06CLR ;clear bit address 07JNB P1.2,OVER ;If P1.2=0,jumpSETB ;OVER:JNB P1.3,NEXT ;If P1.3=0,jumpSETB ;NEXT:...
71 Example 5-18Write a program to save the status of bits P1.7 on RAM addressbit 05.Solution:CY is used as a bit buffer.MOV C,P1.7 ;save status of P1.2 on CYMOV 05,C ;save in RAM bit location 05The machine code isA2 97 for MOV C,P1.792 05 for MOV 05,C
72 Example 5-19Write a program to get the status of bits P1.7 and send it to pin P2.0.Solution:CY is used as a bit buffer.HERE: MOV C,P1.7 ;get bit and send to CYMOV P2.0,C ;send bit to port P2.0SJMP HERE
73 Using BIT and EQU Directive Assign bit-addressable I/O and RAM locationsname BIT bit-addressbit-address nameOVEN_HOT BIT P2.3HERE:JNB OVEN_HOT,HEREExamples 5-20 to 5-23We can also use the EQU directive to assign addresses.OVEN_HOT EQU P2.3Examples 5-24 to 5-25
74 Example 5-20Assume that bit P2.3 is an input and represents the condition of anoven. If it goes high, it means that the oven is hot.Monitor the bit continuously.Whenever it goes high, send a low-to-high pulse to port P1.5 to turn on a buzzer.Solution:OVEN_HOT BIT P2.3BUZZER BIT P1.5HERE:JNB OVEN_HOT,HERECLR BUZZERACALL DELAYCPL BUZZERSJMP HERESimilar to Ex5-16, except the use of BIT directive allows us to assign the OVEN_HOT and BUZZER bit to any port.
75 Example 5-21An LED is connected to pin P1.7. Write a program to toggle the LED forever.Solution:LED BIT P1.7 ;using BIT directiveHERE: CPL LED ;toggle LEDLCALL DELAY ;delaySJMP HERE ;repeat forever
76 Example 5-22A switch is connected to pin P1.7 and an LED to pin P2.0. Write a program to get the status of SW and send it to the LED.Solution:SW BIT P1.7LED BIT P2.0HERE: SETB P ;make P1.0 an inputMOV C,SW ;save P1.0 to carryMOV LED,C ;send to LEDSJMP HERE ;keep repeating和 Example 4-7 很像“MOV P2.7, P1.0” is wrong since such an instruction does not exists. 所以必須使用CY當作仲介. However, “MOV P2, P1” is a valid instruction.
77 Example 5-23 (1/2)Assume that RAM bit location 12H holds the status of whether there has been a phone call or not.If it is high, it means there has been a new call since it was checked the last time. Write a program to display “New Message” on an LCD if bit RAM 12H is high.If it is low, the LCD should say “No New Message”.Solution:Use CY to hold the status.Use JNC to check CY flag.We use “LCALL DISPLAY” to display message (see Chap. 12)
78 Example 5-23 (2/2) PHONBIT BIT 12H MOV C,PHONBIT ;copy bit location 12H toJNC NO ;check to see if is highMOV DPTR,#400H ;address of YES_MGLCALL DISPLAY ;displaySJMP EXIT ;get outNO: MOV DPTR,#420HLCALL DISPLAYEXIT:;data to be displayed on LCDORG 400HYES_MG: DB “New Message”ORG 420HNO_MG: DB “No New Message”Display YES_MGTestJump if CY＝0Not Jump if CY ≠0EXITMOV C, 12HDisplay NO_MGSJMP EXIT
79 Example 5-24 (1/2)A switch (SW) is connected to pin P1.7. Write a program to check the status of SW and perform the following:(a) If SW=0, send “No” to P2.(a) If SW=1, send “YES” to P2.Solution:This program is to show how to use EQU by port names.SW EQU P ;bit addressMYDATA EQU P ;byte address和 Example 4-6 很像
80 Example 5-24 (2/2) SW EQU P1.7 ;bit address MYDATA EQU P ;byte addressHERE: SETB SW ;make P1.7 an inputMOV C,SW ;save P1.7 to carryJC OVERMOV MYDATA,#’N’;SW=0MOV MYDATA,#’O’SJMP HEREOVER: MOV MYDATA,#’Y’;SW=1MOV MYDATA,#’E’MOV MYDATA,#’S’和 Example 4-6 很像
81 Example 5-25 (1/2)A switch (SW) is connected to pin P1.7. Write a program to check the status of SW and perform the following:(a) If SW=0, send “0” to P2.(a) If SW=1, send “1” to P2.Solution:This program is to show how to use EQU by bit/byte addresses.SW EQU 97H ;bit addressMYDATA EQU 0A0H ;byte address和 Example 4-6 很像
82 Example 5-25 (2/2) SW EQU 97H ;P1.7 bit address MYDATA EQU 0A0H ;P2 byte addressHERE: SETB SW ;make P1.7 an inputMOV C,SW ;save P1.7 to carryJC OVERMOV MYDATA,#’0’;SW=0, P2=30HSJMP HEREOVER: MOV MYDATA,#’1’;SW=1, P2=31H和 Example 4-6 很像
84 The Upper 128 Bytes of RAM in 8052 8052 has 256-bytes RAM.Lower 128 bytes: addresses 00-7FHUpper 128 bytes: addresses 80-FFHHowever, 80-FFH has been used by SFR.To send 55H to P1: MOV 90H,#55HSFRs are accessed by direct addressing modelWhere is the location of 90H?P1? or the RAM location 90H at 8052?
86 How to Distinguish Them? We use different addressing modes to access them:1. To access the SFRs, we use direct addressing mode.Ex: MOV 90H,#55H2. To access the upper 128 bytes, we use the indirect addressing mode, which uses R0 and R1 as pointers.Ex: MOV R0,#90HR0 and R1 have address values of 80H or higher.對於 8052, 若用 direct addressing mode, 一定是指 SFR, 如此才能與 8051 相容.所以要存取 upper 128-byte RAM, 只能用 indirect addressing mode. 或者說 indirect addressing mode 不可以用於 SFR.
87 Example 5-26Write a program for 8052 to put 55H into the upper RAM locations of 90-99H.Solution:MOV A,#55HMOV R0,#90HMOV R2,#10BACK: MOV @R0,A ;indirect addressing modeINC R ;increment R0 pointerDJNZ R2,BACK ;loop until counter = 0SJMP $END
88 Example 5-27 (1/2) Assume that the on-chip ROM has a message. Write a program to copy it from code space into the upper memory space starting at address 80H.Also, as you place a byte in upper RAM, give a copy to P0.Solution:This program is similar to Example 5-3.We use indirect addressing mode to access both the lower and upper 128-byte RAM.We also use the indexed addressing mode.和 Example 5-3 很像對於 8051/8052, indirect addressing mode 都能用來存取 lower 128-byte RAM 及 upper 128-byte RAM,看課本, 程式執行前 RAM 的值如 Figure 5-5; 程式執行後 RAM 的值如 Figure 5-6.
89 Example 5-27 (2/2) Solution : ORG 0000 MOV DPTR,#MYDATA MOV R1,#80H ;upper 128-byte RAMB1: CLR AMOVC ;read from codeJZ EXITMOV @R1,A ;copy to upper RAMMOV P0,A ;give a copy to P0INC DPTRINC R1SJMP B1EXIT: SJMP $ORG 300HMYDATA: DB “The promise of World Peace”,0END類似 Example 5-7.
90 You are able to (1/2)List the 5 addressing modes of the 8051 microcontrollerContrast and compare the addressing modesCode 8051 Assembly language instructions using each addressing modeList the SFR（special function registers）addressDiscuss how to access the SFRManipulate the stack using direct addressing mode
91 You are able to (2/2)Code 8051 instructions to manipulate a look-up tableAccess RAM, I/O, and ports using bit addressesDiscuss how to access the extra 128 bytes of RAM space in the 8052