Docsity

Addressing Modes in 8051 Microcontroller: A Comprehensive Guide, Study notes of Computer Science

Toggle sidebar
bg1
Home Automation, Networking, and Entertainment Lab
Dept. of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN
Chung-Ping Young
楊中平
ADDRESSING MODES
The 8051 Microcontroller and Embedded
Systems: Using Assembly and C
Mazidi, Mazidi and McKinlay
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26

Partial preview of the text

Download Addressing Modes in 8051 Microcontroller: A Comprehensive Guide and more Study notes Computer Science in PDF only on Docsity!

Home Automation, Networking, and Entertainment Lab Dept. of Computer Science and Information EngineeringNational Cheng Kung University, TAIWAN

ADDRESSING MODES The 8051 Microcontroller and EmbeddedSystems: Using Assembly and C Mazidi, Mazidi and McKinlayChung-Ping Young^ 楊中平

‰^ The CPU can access data in various ADDRESSINGways, which are calledMODES HANEL

addressing

modes^ ¾^ Immediate^ ¾^ Register^ ¾^ Direct^ ¾^ Register indirect^ ¾^ Indexed

Accessingmemories

‰^ We can use EQU directive to access IMMEDIATEimmediate dataADDRESSINGMODE(cont’) ‰^ We can also use immediate addressingmode to send data to 8051 ports HANEL

Count^ EQU 30...^ ...MOV^ R4,#COUNT^

;R4=1EH

MOV^ DPTR,#MYDATA

;DPTR=200H

ORG^ 200HMYDATA: DB^ “America” MOV P1,#55H

‰^ Use registers to hold the data to bemanipulated REGISTERADDRESSINGMODE ‰^ The source and destination registersmust match in size^ ¾^ MOV HANEL MOV A,R0^ ;copy contents of R0 into AMOV R2,A^ ;copy contents of A into R2ADD A,R5^ ;add contents of R5 to AADD A,R7^ ;add contents of R7 to AMOV R6,A^ ;save accumulator in R6^ DPTR,A^ will give an errorMOV DPTR,#25F5HMOV R7,DPLMOV R6,DPH ‰ The movement of data between Rnregisters is not allowed ¾ MOV^ R4,R7^ is invalid

‰^ The SFR ( ACCESSINGMEMORYSFR Registersand TheirAddresses HANEL

Special Function Register)can be accessed by their names or bytheir addresses MOV 0E0H,#55H^ ;is the same asMOV A,#55h^ ;load 55H into AMOV 0F0H,R0^ ;is the same asMOV B,R0^ ;copy R0 into B ‰ The SFR registers have addressesbetween 80H and FFH ¾ Not all the address space of 80 to FF isused by SFR ¾ The unused locations 80H to FFH arereserved and must not be used by the8051 programmer

Department of Computer Science and Information EngineeringNational Cheng Kung University, TAIWAN ACCESSINGMEMORYSFR Registersand TheirAddresses(cont’)^ HANEL

0A8H Interrupt enable controlIE*

0B8H Interrupt priority controlIP*

0B0H Port 3P3*

0A0H Port 2P2*

90H Port 1P1*

80H Port 0P0*

83H High byteDPH

82H Data pointer 2 bytesDPTRLow byteDPL

81H Stack pointerSP

0D0H Program status wordPSW*

0F0H B registerB*

0E0H AccumulatorACC*

Address

Special Function Register (SFR) AddressesNameSymbol

Example 5-1 ACCESSING Write code to send 55H to ports P1 and P2, usingMEMORY(a) their names (b) their addresses Solution : SFR Registers (a)^ MOV A,#55Hand TheirAddresses(cont’) HANEL

;A=55HMOV P1,A ;P1=55HMOV P2,A ;P2=55H(b) From Table 5-1, P1 address=80H; P2 address=A0H MOV A,#55H ;A=55HMOV 80H,A ;P1=55HMOV 0A0H,A ;P2=55H

‰^ Only direct addressing mode is allowed ACCESSINGfor pushing or popping the stackMEMORY^ ¾^ PUSH AStack andDirectAddressingMode HANEL is invalid ¾ Pushing the accumulator onto the stackmust be coded as^ PUSH 0E0H Example 5-2 Show the code to push R5 and A onto the stack and then pop themback them into R2 and B, where B = A and R2 = R5 Solution: PUSH 05 ;push R5 onto stackPUSH 0E0H ;push register A onto stackPOP 0F0H ;pop top of stack into B;now register B = register APOP 02 ;pop top of stack into R2;now R2=R

Example 5-3 Write a program to copy the value 55H into RAM memory locations ACCESSING40H to 41H using(a) direct addressing mode, (b) register indirect addressing modeMEMORYwithout a loop, and (c) with a loop Solution: Register (a)MOV^ A,#55HIndirectAddressingMode(cont’) HANEL ;load^ A^ with^ value

55H

MOV^ 40H,A^ ;copy^ A^ to

RAM^ location^ 40H MOV^ 41H.A^ ;copy^ A^ to

RAM^ location^ 41H (b)MOV^ A,#55H^ ;load

A^ with^ value^ 55HMOV R0,#40H ;load the^ pointer.^ R0=40HMOV @R0,A ;copy A^ to^ RAM^ R0^ points^

to INC^ R0^ ;increment

pointer.^ Now^ R0=41h MOV^ @R0,A^ ;copy^ A^ to

RAM^ R0^ points^ to (c)MOV^ A,#55H^

;A=55HMOV R0,#40H ;load^ pointer.R0=40H,MOV R2,#02 ;load^ counter,^ R2= AGAIN:^ MOV^ @R0,A^

;copy^55 to^ RAM^ R0^ points

to INC^ R0^ ;increment

R0^ pointer DJNZ^ R2,AGAIN^ ;loop^ until

counter^ =^ zero

‰^ The advantage is that it makes ACCESSINGaccessing data dynamic rather thanMEMORYstatic as in direct addressing mode^ ¾^ Looping is not possible in directRegisteraddressing modeIndirectAddressing^ Example 5-4 Mode^ Write a program to clear 16 RAM locations starting at RAM address(cont’)60H^ Solution:^ CLR A HANEL

;A=0MOV R1,#60H^ ;load pointer. R1=60HMOV R7,#16^ ;load counter, R7=16AGAIN: MOV @R1,A^ ;clear RAM R1 points toINC R1^ ;increment R1 pointerDJNZ R7,AGAIN ;loop until counter=zero

‰^ R0 and R1 are the only registers that ACCESSINGcan be used for pointers in registerMEMORYindirect addressing modeRegister ‰^ Since R0 and R1 are 8 bits wide, theirIndirectuse is limited to access anyAddressinginformation in the internal RAMMode(cont’) ‰^ Whether accessing externallyconnected RAM or on-chip ROM, weneed 16-bit pointer^ ¾^ In such case, the DPTR register is used HANEL

‰^ Indexed addressing mode is widely ACCESSINGused in accessing data elements ofMEMORYlook-up table entries located in theprogram ROMIndexedAddressing ‰^ The instruction used for this purpose isMode and^ MOVC A,@A+DPTROn-chip ROM^ ¾^ Use instructionAccess HANEL

MOVC, “C” means code ¾ The contents of A are added to the 16-bitregister DPTR to form the 16-bit addressof the needed data

‰^ The look-up table allows access to ACCESSINGelements of a frequently used tableMEMORYwith minimum operations^ Example 5-8 Look-up Table^ Write a program to get the x value from P1 and send x(cont’) HANEL

2 to P2,

continuously Solution:^ ORG^0 MOV^ DPTR,#300H

;LOAD TABLE ADDRESSMOV A,#0FFH ;A=FFMOV P1,A ;CONFIGURE P1 INPUT PORT

BACK:MOV^ A,P^

;GET X

MOV^ A,@A+DPTR^

;GET X SQAURE FROM TABLE

MOV^ P2,A^

;ISSUE IT TO P

SJMP BACK^

;KEEP DOING IT

ORG^ 300HXSQR_TABLE:DB^ 0,1,4,9,16,25,36,49,64,81END

‰^ In many applications, the size of ACCESSINGprogram code does not leave anyMEMORYroom to share the 64K-byte codespace with dataIndexed^ ¾^ The 8051 has another 64K bytes ofAddressingmemory space set aside exclusively forMode anddata storageMOVX^ ƒ^ This data memory space is referred to asexternal memory and it is accessed only by the^ MOVX HANEL instruction ‰ The 8051 has a total of 128K bytes ofmemory space ¾ 64K bytes of code and 64K bytes of data ¾ The data space cannot be shared betweencode and data