8051 Memory Organization BY D. BALAKRISHNA, Research Assistant, IIIT-H Chapter 1: Memory Organization There are 2 types of memories available in 8051 microcontroller. Program memory/c code memory (ROM) Data memory (RAM) Program Memory is used to store the program permanently. Data Memory is used for temporarily storing data & results, and used during the execution of program. The following figure shows the memory types of 8051 microcontroller. Figure 1.0: Memory types of 8051
1.1 Program Memory 8051 has 4k bytes of ROM to store program permanently. This ROM is having the address locations 0000 to 0FFF. As we know 8051 has 16-bits of Address bus can accesss the external memory up to 2 16 locations i..e. 16k bytes. The following figure shows the Internal & External Program memory of 8051 Figure1.1: Program memory of 8051 We already discussedd in Pin configuration of 8051, by using EA pin we can access external memory. If EA = 1: :8051 will use both memories, first internal thenexternal (if exists). If EA = 0:8051 will use External memory regardless of internal memory. 1.2 Data Memory 8051 has 128 bytes of on-chip Data memory for temporary storage purpose. This memory is volatile in nature. Resetting the 8051 empties the complete memory.
1.2.1 Bank Registers: The first 32 bytes are reserved for bank registers Bank 0, Bank 1, Bank 2, and Bank 3. Each bank contains 8 registers (R0 R7) & holds 1 byte of memory for each register. In multitasking, many machine cycles are required for switching from one task to another for saving & retrieving. By using these bank registerss we can use same register but switches the bank, so the time taken is very less. 1.2.2 Bit memory 16 bytes of memory from addresses 20H through 2FH is allocated to Bit memory. It is a subset of Internal RAM, but the 8051 provides special instructions to access these 16 bytes of memory on a bit by bit basis. This memory can be able to access a number of bit variables. Ex: SETB and CLR. The following figure illustrates the data memory of 8051 microcontroller. Figure 1.2: Data memory of 8051 1.2.3 Scratch pad areaa 80 bytes of memory from addressess 30H to 7FH is allocated for general purpose operationscalled as Scratch pad area whichh is used for storing temporary operations.
Chapter 2 Special Function Registers There are only 21 SFRs in 8051 microcontroller.sfr registers lies in the address range of 80h to FFh of internal RAM. Out of 21 registers 10 registers are Byte addressable, and 11 registers are Bit & Byte addressable. The address of SFRs whose are Bit & Byte addressable ends with 0 or 8. The following table shows the list of SFRs and corresponding addresses. Name Function Internal RAM Address (Hex) P0 I/O Port latch 80 SP Stack Pointer 81 DPL Addressing Ext. memory 82 DPH Addressing Ext. memory 83 PCON Power Control 87 TCON Timer/Counter Control 88 TMOD Timer/Counter Mode Control 89 TL0 Timer 0 Low Byte 8A TL1 Timer 1 Low Byte 8B TH0 Timer 0 High Byte 8C TH1 Timer 1 High Byte 8D P1 I/O Port latch 90 SCON Serial Port Control 98 SBUF Serial Port Data Buffer 99 P2 I/O Port latch A0 IE Interrupt Enable Control A8 P3 I/O Port latch B0 IP Interrupt Priority B8 PSW Program Status Word D0 ACC Accumulator E0 B Arithmetic F0 Table 2.0: Special Function registers of 8051.
The following figure shows the Special function registers of 8051. SFRs of 8051 are divided into 3 types: I/O Port SFRs Control SFRs Other SFRs 2.1 Accumulator Figure2.0: Special Function registers of 8051. An accumulator is a register which stores the intermediate arithmetic and logic results. It is an 8-bit register and is the most versatile registerr of the 8051, because the most number of instructionss that will use the accumulator.
Figure 2.1: Accumulator ACC is usually accessed by direct addressing and its physical address is 0E0H. Accumulator is both byte and bit addressable. Example: If we want to add the number 1 and 2, the resulting 3 willl be stored in the Accumulator. 2.2 "R" REGISTERS: The "R" registers are a set of eight registerss (R0 R7).Thesee registerss are used as auxiliary registerss in many operations. Example: If we want to add the number 1 and 2, the original number 1 is stored in the Accumulator, the value 2 is stored in Rx register (where x lies between 0 to 7). The "R" registerss are also used to store valuestemporarily. 2.3 "B" REGISTER: The "B" register is 8- bit register; it is similar to the Accumulator. B-register is only used for two instructions: MUL and DIV. If we want to multiply/divide A by another number easily, wecan store the other number in B-register and use these two instructions.
Figure 2.3: B-Register Aside from the MUL and DIV instructions, the B register used as temporary storage register like "R" registers. 2.4 Program Counter (Pc) The Program Counter (PC) is a 16 bit (2-byte) register which holds the addresss of next instructio on is goingg to be executed. At the point of Reset or at startt up of 8051, PC always starts at 0000H and incremented each time when an instruction is executed. Figure 2.4: Program Counter of 8051 PC isn t always incremented by one. Since some instructions require 2 or 3 bytes the PC willl be incremented by 2 or 3 in these cases. 2.5 The Stack Pointer (Sp) The Stack Pointer is 8-bit (1-byte) in size, and holds the address of next value to be removed from stack. Whenever wepush a value ontoo the stack, the 8051 first increments the value of SP and then stores the value at the resulting memory location. Whenever wepop a value off the stack, the 8051 returns the value from the memory location (indicated by SP) and then decrements the value of SP.
Figure 2.5: Stack Pointer of 8051 On reset or at start up the SP will be initialized to 07h. SP can be modified by six instructions: PUSH, POP, ACALL, LCALL, RET, and RETI. 2.6 Data Pointer (Dptr) The Data Pointer (DPTR) is a 16-bit (2-byte) register. This DPTR is divided into 2 8-bit registers: DPL & DPH. We can access these DPL & DPH individually or Combine called DPTR. The following figure shows the DPTR register. Figure 2.6: DPTR register of 8051 DPTR is used to point the data to access external memory (RAM). The 8051 will access external memory at the address indicatedd by DPTR. It is also used to store 2-byte values which have nothing to do with memory locations.
2.7 Port Registers As we know 8051 has 4 I/O ports, each port Register. And all 4 port registers are bit as well as following figure shows the Port registers of 8051. contains its own Port byte addressable. The Figure 2.7: Port Registers 2.8 Program Status Word (Psw) PSW register is 8-bits in size. It contains several status bits that reflect the current state of the CPU. The following figure shows PSW register. PSW register is both bit and byte addressable. The address of PSW starts from D0H. The various individual bits are explained below.
P (Parity Flag) This bit indicates the Parity used. It is mainly used during data transfer via serial communication. If the number of 1s in accumulator is even this bit automatically goes to 0, otherwise goes to 1. Bit-1: This bit is user definedd bit. OV (Overflow) If the result is beyond the range this OV flag will set to 1. If the result of an arithmetical operation is larger than 255 this OV flag will be set (1), otherwise cleared (0). RS0 & RS1: Figure 2.8: Program Status Word of 8051 These two bits are used to select one of four Bank registers of RAM. The following table explains the selection of register bank. RS1 0 0 1 1 RS1 0 1 0 1 Bank Bank 0 (00 07h) Bank 1 (08 0Fh) Bank 2 (10 17h) Bank 3 (18 1Fh) Table 2.8: Register Bank selection
F0 - Flag 0: This is a general-purpose bit available for use. AC (Auxiliary Carry): This pin is used in BCD operations only. C (Carry): This pin is used for arithmetic operations. If carry occur in arithmetic operations, the accumulat tor is unable to store entire result. In this situation this carry flag will set. 2.9 PCON The Power Control SFR is used to control the 8051's power control modes. 8051 has two power saving modes. They are: 1. Idle Mode 2. Power Down mode. Figure 2.9: PCON register of 8051 IDL By setting IDL bit to 1 (i.e., =0)the 8051 entered to Idle mode..in this mode of operationthe clock is turned off to CPU, but the interrupt, timer and serial port functioning normal. PD: By setting the PD bit to 1 the 8051 entered to Power down Mode. When 8051 entered into this mode of operation the internal clock to the entire microcontroller is stopped (frozen).the 8051 will wake up by Hardware Reset only. GF1: General-purpose bit (available for use) ).
GF0: General-purpose bit (available for use). SMOD: By setting this bit Baud rate will twice as much higher. 2.10 TCON The Timer Control is Special function register which is used to activate or deactivate Timer/Counter. We will discuss about this TCON register clearly in TIMERS/ COUNTERS concepts. 2.11 TMOD The Timer Mode Control is Special function register which is used to control Timer/Counter. We will discuss about this TMOD register clearly in TIMERS/ COUNTERS concepts. We will also discuss about in TIMERS/ COUNTERSare abouttl0, TH0, TL1, and TH1 registers. 2.12 IE (Interrupt Enable) The IE register enables or disables the interrupt. We will discuss about this IE register clearly in INTERRUPTS concepts. 2.13 IP (Interrupt Priority) The IP register specifies the priority level of each interrupt (high or low priority). We will discuss about this IP register clearly in INTERRUPTS concepts. 2.14 SMOD & SBUF: These two registers control the serial communication. We will discuss about these two SMOD & SBUF registers clearly in Serial Communication concepts.