Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Standard view
Full view
of .
Look up keyword
Like this
0 of .
Results for:
No results containing your search query
P. 1
8051 Tutorial

8051 Tutorial

Ratings: (0)|Views: 7|Likes:
Published by Gaurav Kanabar

More info:

Published by: Gaurav Kanabar on Feb 17, 2011
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less





8051 Tutorial
(C) Copyright 1997, 1998, 1999 by Vault Information Services. All Rights Reserved.http:\\www.8052.com
8051 Tutorial: Introduction
Despite it’s relatively old age, the 8051 isone of the most popular microcontrollers in usetoday. Many derivative microcontrollers have sincebeen developed that are based on--andcompatible with--the 8051. Thus, the ability toprogram an 8051 is an important skill for anyonewho plans to develop products that will takeadvantage of microcontrollers.Many web pages, books, and tools areavailable for the 8051 developer.I hope the information contained in thisdocument/web page will assist you in mastering8051 programming. While it is not my intention thatthis document replace a hardcopy book purchasedat your local book store, it is entirely possible thatthis may be the case. It is likely that this documentcontains everything you will need to learn 8051assembly language programming. Of course, thisdocument is free and you get what you pay for soif, after reading this document, you still are lost youmay find it necessary to buy a book.This document is both a tutorial and areference tool. The various chapters of thedocument will explain the 8051 step by step. Thechapters are targeted at people who areattempting to learn 8051 assembly languageprogramming. The appendices are a usefulreference tool that will assist both the noviceprogrammer as well as the experiencedprofessional developer.This document assumes the following:
A general knowledge of programming.
An understanding of decimal, hexidecimal, andbinary number systems.
A general knowledge of hardware.That is to say, no knowledge of the 8051is assumed--however, it is assumed you’ve donesome amount of programming before, have abasic understanding of hardware, and a firm graspon the three numbering systems mentioned above.The concept of converting a number fromdeciminal to hexidecimal and/or to binary is notwithin the scope of this document--and if you can’tdo those types of conversions there are probablysome concepts that will not be completelyunderstandable.This document attempts to address the need ofthe typical programmer. For example, there arecertain features that are nifty and in some casesvery useful--but 95% of the programmers willnever use these features.
8051 Tutorial: Types of Memory
The 8051 has three very general types ofmemory. To effectively program the 8051 it isnecessary to have a basic understanding of thesememory types.
On-Chip Memory
refers to any memory(Code, RAM, or other) that physically exists on themicrocontroller itself. On-chip memory can be ofseveral types, but we'll get into that shortly.
External Code Memory
is code (orprogram) memory that resides off-chip. This isoften in the form of an external EPROM.
External RAM
is RAM memory thatresides off-chip. This is often in the form ofstandard static RAM or flash RAM.
Code Memory
Code memory is the memory that holdsthe actual 8051 program that is to be run. Thismemory is limited to 64K and comes in manyshapes and sizes: Code memory may be found
, either burned into the microcontroller asROM or EPROM. Code may also be storedcompletely
in an external ROM or, morecommonly, an external EPROM. Flash RAM isalso another popular method of storing a program.Various combinations of these memory types mayalso be used--that is to say, it is possible to have4K of code memory
and 64k of codememory
in an EPROM.When the program is stored on-chip the64K maximum is often reduced to 4k, 8k, or 16k.This varies depending on the version of the chipthat is being used. Each version offers specificcapabilities and one of the distinguishing factorsfrom chip to chip is how much ROM/EPROMspace the chip has.However, code memory is most commonlyimplemented as off-chip EPROM. This isespecially true in low-cost development systemsand in systems developed by students.
Programming Tip: 
Since code memory is restricted to 64K, 8051 programs are limited to 64K.Some assemblers and compilers offer ways to get around this limit when used with specially wired hardware. However, without such special compilers and hardware, programs are limited to 64K.
External RAM
As an obvious opposite of
Internal RAM 
,the 8051 also supports what is called
External RAM 
. As the name suggests, External RAM is anyrandom access memory which is found
.Since the memory is off-chip it is not as flexible interms of accessing, and is also slower. Forexample, to increment an Internal RAM location by1 requires only 1 instruction and 1 instructioncycle. To increment a 1-byte value stored inExternal RAM requires 4 instructions and 7instruction cycles. In this case, external memory is7 times slower!What External RAM loses in speed andflexibility it gains in quantity. While Internal RAM islimited to 128 bytes the 8051 supports ExternalRAM up to 64K.
Programming Tip: 
The 8051 may only address 64k of RAM. To expand RAM beyond this limit requires programming and hardware tricks. You may have to do this "by hand" since many compilers and assemblers, while providing support for programs in excess of 64k, do not support more than 64k of RAM.This is rather strange since it has been my experience that programs can usually fit in 64k but often RAM is what is lacking. Thus if you need more than 64k of RAM,check to see if your compiler supports it-- but if it doesn't, be prepared to do it by hand.
On-Chip Memory
.As mentioned at the beginning of thischapter, the 8051 includes a certain amount of on-chip memory. On-chip memory is really one of two(SFR) memory. The layout of the 8051's internalmemory is presented in the following memorymap:As is illustrated in this map, the 8051 has abank of 128 bytes of
Internal RAM 
. This Internal RAMis found
on the 8051 so it is the fastest RAMavailable, and it is also the most flexible in terms ofreading, writing, and modifying it’s contents. InternalRAM is volatile, so when the 8051 is reset thismemory is cleared.The 128 bytes of internal ram is subdividedas shown on the memory map. The first 8 bytes (00h- 07h) are "register bank 0". By manipulating certainSFRs, a program may choose to use register banks1, 2, or 3. These alternative register banks arelocated in internal RAM in addresses 08h through1Fh.We'll discuss "register banks" more in a laterchapter. For now it is sufficient to know that they "live"and are part of internal RAM.Bit Memory also lives and is part of internalRAM. We'll talk more about bit memory very shortly,but for now just keep in mind that bit memory actuallyresides in internal RAM, from addresses 20h through2Fh.The 80 bytes remaining of Internal RAM, fromaddresses 30h through 7Fh, may be used by uservariables that need to be accessed frequently or athigh-speed. This area is also utilized by themicrocontroller as a storage area for the operating
. This fact severely limits the 8051’s stack since,as illustrated in the memory map, the area reservedfor the stack is only 80 bytes--and usually it is lesssince this 80 bytes has to be shared between thestack and user variables.
Register Banks
The 8051 uses 8 "R" registers which areused in many of its instructions. These "R"registers are numbered from 0 through 7 (R0, R1,R2, R3, R4, R5, R6, and R7). These registers aregenerally used to assist in manipulating valuesand moving data from one memory location toanother. For example, to add the value of R4 tothe Accumulator, we would execute the followinginstruction:
However, as the memory map shows, the"R" Register R4 is really part of Internal RAM.Specifically, R4 is address 04h. This can be see inthe bright green section of the memory map. Thusthe above instruction accomplishes the same thingas the following operation:
ADD A,04h
This instruction adds the value found inInternal RAM address 04h to the value of theAccumulator, leaving the result in the Accumulator.Since R4 is really Internal RAM 04h, the aboveinstruction effectively accomplished the samething.But watch out! As the memory mapshows, the 8051 has four distinct register banks.When the 8051 is first booted up, register bank 0(addresses 00h through 07h) is used by default.However, your program may instruct the 8051 to

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->