You are on page 1of 6

8051 Tutorial: Types of Memory

The 8051 has three very general types of memory. To effectively program the 8051 it is necessary to have a basic understanding of these memory types. The memory types are illustrated in the following graphic. They are: On- hip !emory" #$ternal ode !emory" and #$ternal %&!.

On-Chip Memory refers to any memory ' ode" %&!" or other( that physically e$ists on the microcontroller itself. On-chip memory can be of several types" but we)ll get into that shortly. External Code Memory is code 'or program( memory that resides off-chip. This is often in the form of an e$ternal #*%O!. External RAM is %&! memory that resides off-chip. This is often in the form of standard static %&! or flash %&!.

Code Memory
ode memory is the memory that holds the actual 8051 program that is to be run. This memory is limited to +,- and comes in many shapes and si.es: ode memory may be found on-chip" either burned into the microcontroller as %O! or #*%O!. ode may also be stored completely off-chip in an e$ternal %O! or" more commonly" an e$ternal #*%O!. /lash %&! is also another popular method of storing a program. 0arious combinations of these memory types may also be used--that is to say" it is possible to have ,- of code memory on-chip and +,1 of code memory off-chip in an #*%O!. 2hen the program is stored on-chip the +,- ma$imum is often reduced to ,1" 81" or 1+1. This varies depending on the version of the chip that is being used. #ach version offers specific capabilities and one of the distinguishing factors from chip to chip is how much %O!3#*%O! space the chip has. 4owever" code memory is most commonly implemented as off-chip #*%O!. This is especially true in low-cost development systems and in systems developed by students.

ro!rammin! Tip: 5ince code memory is restricted to +,-" 8051 programs are limited to +,-. 5ome assemblers and compilers offer ways to get around this limit when used with specially wired hardware. 4owever" without such special compilers and hardware" programs are limited to +,-.

External RAM
&s an obvious opposite of Internal RAM" the 8051 also supports what is called External RAM. &s the name suggests" #$ternal %&! is any random access memory which is found off-chip. 5ince the memory is off-chip it is not as fle$ible in terms of accessing" and is also slower. /or e$ample" to increment an 6nternal %&! location by 1 re7uires only 1 instruction and 1 instruction cycle. To increment a 1-byte value stored in #$ternal %&! re7uires , instructions and 8 instruction cycles. 6n this case" e$ternal memory is 8 times slower9 2hat #$ternal %&! loses in speed and fle$ibility it gains in 7uantity. 2hile 6nternal %&! is limited to 1:8 bytes ':5+ bytes with an 805:(" the 8051 supports #$ternal %&! up to +,-. ro!rammin! Tip: The 8051 may only address +,1 of %&!. To e$pand %&! beyond this limit re7uires programming and hardware tric1s. ;ou may have to do this <by hand< since many compilers and assemblers" while providing support for programs in e$cess of +,1" do not support more than +,1 of %&!. This is rather strange since it has been my e$perience that programs can usually fit in +,1 but often %&! is what is lac1ing. Thus if you need more than +,1 of %&!" chec1 to see if your compiler supports it-- but if it doesn)t" be prepared to do it by hand.

On-Chip Memory
&s mentioned at the beginning of this chapter" the 8051 includes a certain amount of on-chip memory. On-chip memory is really one of two types: 6nternal %&! and 5pecial /unction %egister '5/%( memory. The layout of the 8051)s internal memory is presented in the following memory map:

&s is illustrated in this map" the 8051 has a ban1 of 1:8 bytes of Internal RAM. This 6nternal %&! is found on-chip on the 8051 so it is the fastest %&! available" and it is also the most fle$ible in terms of reading" writing" and modifying its contents. 6nternal %&! is volatile" so when the 8051 is reset this memory is cleared. The 1:8 bytes of internal ram is subdivided as shown on the memory map. The first 8 bytes '00h - 08h( are <register ban1 0<. =y manipulating certain 5/%s" a program may choose to use register ban1s 1" :" or >. These alternative register ban1s are located in internal %&! in addresses 08h through 1/h. 2e)ll discuss <register ban1s< more in a later chapter. /or now it is sufficient to 1now that they <live< and are part of internal %&!. =it !emory also lives and is part of internal %&!. 2e)ll tal1 more about bit memory very shortly" but for now ?ust 1eep in mind that bit memory actually resides in internal %&!" from addresses :0h through :/h. The 80 bytes remaining of 6nternal %&!" from addresses >0h through 8/h" may be used by user variables that need to be accessed fre7uently or at high-speed. This area is also utili.ed by the microcontroller as a storage area for the operating stack. This fact severely limits the 8051s stac1 since" as illustrated in the memory map" the area reserved for the stac1 is only 80 bytes--and usually it is less since this 80 bytes has to be shared between the stac1 and user variables.

Re!ister "an#s
The 8051 uses 8 <%< registers which are used in many of its instructions. These <%< registers are numbered from 0 through 8 '%0" %1" %:" %>" %," %5" %+" and %8(. These registers are generally used to assist in manipulating values and moving data from one memory location to another. /or e$ample" to add the value of %, to the &ccumulator" we would e$ecute the following instruction: A$$ A%R&

Thus if the &ccumulator '&( contained the value + and %, contained the value >" the &ccumulator would contain the value @ after this instruction was e$ecuted. 4owever" as the memory map shows" the <%< %egister %, is really part of 6nternal %&!. 5pecifically" %, is address 0,h. This can be see in the bright green section of the memory map. Thus the above instruction accomplishes the same thing as the following operation: A$$ A%0&h This instruction adds the value found in 6nternal %&! address 0,h to the value of the &ccumulator" leaving the result in the &ccumulator. 5ince %, is really 6nternal %&! 0,h" the above instruction effectively accomplished the same thing. =ut watch out9 &s the memory map shows" the 8051 has four distinct register ban1s. 2hen the 8051 is first booted up" register ban1 0 'addresses 00h through 08h( is used by default. 4owever" your program may instruct the 8051 to use one of the alternate register ban1sA i.e." register ban1s 1" :" or >. 6n this case" %, will no longer be the same as 6nternal %&! address 0,h. /or e$ample" if your program instructs the 8051 to use register ban1 >" <%< register %, will now be synonomous with 6nternal %&! address 1 h. The concept of register ban1s adds a great level of fle$ibility to the 8051" especially when dealing with interrupts 'we)ll tal1 about interrupts later(. 4owever" always remember that the register ban1s really reside in the first >: bytes of 6nternal %&!. ro!rammin! Tip: 6f you only use the first register ban1 'i.e. ban1 0(" you may use 6nternal %&! locations 08h through 1/h for your own use. =ut if you plan to use register ban1s 1" :" or >" be very careful about using addresses below :0h as you may end up overwriting the value of your <%< registers9

"it Memory
The 8051" being a communications-oriented microcontroller" gives the user the ability to access a number of bit variables. These variables may be either 1 or 0. There are 1:8 bit variables available to the user" numberd 00h through 8/h. The user may ma1e use of these variables with commands such as 5#T= and B%. /or e$ample" to set bit number :, 'he$( to 1 you would e$ecute the instruction: 'ET" (&h 6t is important to note that =it !emory is really a part of 6nternal %&!. 6n fact" the 1:8 bit variables occupy the 1+ bytes of 6nternal %&! from :0h through :/h. Thus" if you write the value //h to 6nternal %&! address :0h youve effectively set bits 00h through 08h. That is to say that: MO) (0h%*0++h is e7uivalent to:

'ET" 00h 'ET" 01h 'ET" 0(h 'ET" 0,h 'ET" 0&h 'ET" 05h 'ET" 0-h 'ET" 0.h &s illustrated above" bit memory isnt really a new type of memory. 6ts really ?ust a subset of 6nternal %&!. =ut since the 8051 provides special instructions to access these 1+ bytes of memory on a bit by bit basis it is useful to thin1 of it as a separate type of memory. 4owever" always 1eep in mind that it is ?ust a subset of 6nternal %&!--and that operations performed on 6nternal %&! can change the values of the bit variables. ro!rammin! Tip: 6f your program does not use bit variables" you may use 6nternal %&! locations :0h through :/h for your own use. =ut if you plan to use bit variables" be very careful about using addresses from :0h through :/h as you may end up overwriting the value of your bits9 =it variables 00h through 8/h are for user-defined functions in their programs. 4owever" bit variables 80h and above are actually used to access certain 5/%s on a bit-by-bit basis. /or e$ample" if output lines *0.0 through *0.8 are all clear '0( and you want to turn on the *0.0 output line you may either e$ecute: MO) 0%*01h or you may e$ecute: 'ET" 80h =oth these instructions accomplish the same thing. 4owever" using the 5#T= command will turn on the *0.0 line without effecting the status of any of the other *0 output lines. The !O0 command effectively turns off all the other output lines which" in some cases" may not be acceptable. ro!rammin! Tip: =y default" the 8051 initiali.es the Stack Pointer '5*( to 08h when the microcontroller is booted. This means that the stac1 will start at address 08h and e$pand upwards. 6f you will be using the alternate register ban1s 'ban1s 1" : or >( you must initiali.e the stac1 pointer to an address above the highest register ban1 you will be using" otherwise the stac1 will overwrite your alternate register ban1s. 5imilarly" if you will be using bit variables it is usually a good idea to initiali.e the stac1 pointer to some value greater than :/h to guarantee that your bit variables are protected from the stac1.

'pe/ial +un/tion Re!ister 0'+R1 Memory

5pecial /unction %egisters '5/%s( are areas of memory that control specific functionality of the 8051 processor. /or e$ample" four 5/%s permit access to the 8051s >: input3output lines. &nother 5/% allows a program to read or write to the 8051s serial port. Other 5/%s allow the user to set the serial baud rate" control and access timers" and configure the 8051s interrupt system. 2hen programming" 5/%s have the illusion of being 6nternal !emory. /or e$ample" if you want to write the value <1< to 6nternal %&! location 50 he$ you would e$ecute the instruction: MO) 50h%*01h 5imilarly" if you want to write the value <1< to the 8051s serial port you would write this value to the '"2+ 5/%" which has an 5/% address of @@ 4e$. Thus" to write the value <1< to the serial port you would e$ecute the instruction: MO) 33h%*01h &s you can see" it appears that the 5/% is part of 6nternal !emory. This is not the case. 2hen using this method of memory access 'its called direct address(" any instruction that has an address of 00h through 8/h refers to an 6nternal %&! memory addressA any instruction with an address of 80h through //h refers to an 5/% control register. ro!rammin! Tip: 5/%s are used to control the way the 8051 functions. #ach 5/% has a specific purpose and format which will be discussed later. Cot all addresses above 80h are assigned to 5/%s. 4owever" this area may COT be used as additional %&! memory even if a given address has not been assigned to an 5/%.

You might also like