8051 Microcontroller Project Report
8051 Microcontroller Project Report
An embedded system is a special purpose microprocessor system designed to perform one or few dedicated
functions, often with real-time computing constraints.
It is usually embedded as part of complete device including hardware and mechanical parts and
has fixed software. In contrast, a general purpose computer (PC) can do many functions depending upon
programming. Since, the embedded system is dedicated to specific tasks; it is optimized to reduce size and cost
of the product and increase reliability and performance.
Typically, an embedded system is housed on a single microprocessor board with program stored in
ROM. Complexity of an embedded system varies from low, with a single microcontroller chip, to very high
with multiple units, peripherals and networks housed inside a large chassis or enclosure.
Embedded systems span all aspects of modern life and there are many examples of their use.
Telecommunications system employs numerous embedded systems from telephone switches for the network to
mobile phones at the end user. Consumer electronics like mp3 player, digital watches, video-game consoles,
digital cameras, DVD players, GPS receivers and many houses hold appliances like microwave ovens, washing
machines, and dishwashers have embedded systems to provide flexibility, efficiency and features.
Transportation systems from flight to automobiles increasingly use embedded systems. Medical equipment is
continuing to advance with more embedded systems for vital signs monitoring, electronic stethoscopes for
amplifying sounds, and various medical imaging (CT, MRT, and PET) for non-invasive internal inspections.
Designing a microprocessor based system is expensive. Such systems requires additional hardware like time
circuit, peripheral interface circuit, RAM, ROM etc. in addition to the microprocessor chip.
To solve this problem, microprocessor system is implemented with a single chip called Microcontroller.
INTRODUCTION
Intel first produced a microcontroller in 1976 under the name MCS-48, which was an 8 bit microcontroller.
Later in 1980 they released a further improved version (which is also 8 bit), under the name MCS-51. The most
popular microcontroller 8051 belongs to the MCS-51 family of microcontrollers by Intel. Following the success
of 8051, many other semiconductor manufacturers released microcontrollers under their own brand name but
using the MCS-51 core. Global companies and giants in semiconductor industry like Microchip, Zilog, Atmel,
Philips, and Siemens released products under their brand name. The specialty was that all these devices could be
programmed using the same MCS-51 instruction sets. They basically differed in support device configurations
like improved memory, presence of an ADC or DAC etc. Intel then released its first 16 bit microcontroller in
1982, under name MCS-96
OVERVIEW TO 8051
The Intel 8051 is a very popular general purpose microcontroller widely used for small scale embedded
systems. Many vendors such as Atmel, Philips, and Texas Instruments produce MCS-51 family
microcontroller chips.
The 8051 is an 8-bit microcontroller with 8 bit data bus and 16-bit address bus. The 16 bit address bus
can address a 64K( 216) byte code memory space and a separate 64K byte of data memory space. The
8051 has 4K on-chip read only code memory and 128 bytes of internal Random Access Memory (RAM)
Besides internal RAM, the 8051 has various Special Function Registers (SFR) such as the Accumulator,
the B register, and many other control registers.
34 8-bit general purpose registers in total.
Two 16 bit /Counter timers
3 internal interrupts (one serial), 2 external interrupts.
4 8-bit I/O ports (3 of them are dual purposed). One of them used for serial port,
Some 8051 chips come with UART for serial communication and ADC for analog to digital conversion.
FEATURES OF 8051
Some of the features that have made the 8051 popular are:
4 KB on chip program memory.
128 bytes on chip data memory (RAM).
4 register banks.
128 user defined software flags.
8-bit data bus.
16-bit address bus.
32 general purpose registers each of 8 bits.
16 bit timers (usually 2, but may have more, or less).
3 internal and 2 external interrupts.
Bit as well as byte addressable RAM area of 16 bytes.
Four 8-bit ports, (short models have two 8-bit ports).
16-bit program counter and data pointer.
1 Microsecond instruction cycle with 12 MHz Crystal.
The general schematic diagram of 8051 microcontroller is shown below. We can see 3 system inputs, 3 control
signals and 4 ports (for external interfacing). A Vcc power supply and ground is also shown. Now lets explain
and go through each in detail. System inputs are necessary to make the micro controller functional. So the first
and most important of this is power, marked as Vcc with a GND (ground potential). Without proper power
supply, no electronic system would work. XTAL 1 and XTAL 2 are for the system clock inputs from crystal
clock circuit. RESET input is required to initialize microcontroller to default/desired values and to make a new
start.
There are 3 control signals, EA, PSEN and ALE. These signals known as External Access (EA), Program Store
Enable (PSEN), and Address Latch Enable (ALE) are used for external memory interfacing.
As mentioned above, control signals are used for external memory interfacing. If there is no requirement of
external memory interfacing then, EA pin is pulled high (connected to Vcc) and two others PSEN and ALE are
left alone. You can also see a 0.1 micro farad decoupling capacitor connected to Vcc (to avoid HF oscillations at
input).
There are four ports numbered 0,1,2,3 and called as Port 0, Port 1, Port 2 and Port 3 which are used for external
interfacing of devices like DAC, ADC, 7 segment display, LED etc. Each port has 8 I/O lines and they all are bit
programmable.
Pins 1 8:- Known as Port 1. Unlike other ports, this port does not serve any other functions. Port 1 is an
internally pulled up, quasi bi directional I/O port.
The 8051 requires an external oscillator circuit. The oscillator circuit usually runs the crystal generates
11.0592 MHz frequency. pulses in one second. The pulse is used to synchronize the system operation in
a controlled pace.
A machine cycle is minimum amount time a simplest machine instruction must take
Instruction with a memory oprand needs multiple memory access (machine cycles).
The first 6 crystal pulses (clock cycle) is used to fetch the opcode and the second 6 pulses are used to
perform the operation on the operands in the ALU. This gives an effective machine cycle rate at 1MIPS
(Million Instructions / Second).
The CPU has many important registers. The Program Count (PC) always holds the code memory
location of next instruction. The CPU is the heart of any computer which is in charge of computer
operations.
It fetches instructions from the code memory into the instruction Register (IR), analyzes the op-code of
the instruction, updates the PC to the location of next instruction, fetches the oprand from the data
memory if necessary, and finally performs the operation in the Arithmetic-Logic Unit (ALU) within the
CPU.
The B register is a register just for multiplication and division operation which requires more register
spaces for the product of multiplication and the quotient and the remainder for the division.
The immediate result is stored in the accumulator register (Acc) for next operation and the Program
Status Word (PSW) is updated depending on the status of the operation result.
Memory
The 8051 code (program) memory is read-only, while the data memory is read/write accessible. The
program memory (in EPROM) can be rewritten by the special programmer circuit.
The 8051 memory is organized in a Harvard Architecture. Both the code memory space and data
memory space begin at location 0x00 for internal or external memory which is different from the
Princeton Architecture where code and data share same memory space.
The advantage of the Harvard Architecture is not only doubling the memory capacity of the
microcontroller with same number of address lines but also increases the reliability of the
microcontroller, since there are no instructions to write to the code memory which is read only.
Now let us dive into the program memory organization 0f 8051. It has an internal program of 4K size and if
needed an external memory can be added (by interfacing) of size 60K maximum. So in total 64K size memory
is available for 8051 micro controller. By default, the External Access (EA) pin should be connected Vcc so that
instructions are fetched from internal memory initially. When the limit of internal memory (4K) is crossed,
control will automatically move to external memory to fetch remaining instructions. If the programmer wants to
fetch instruction from external memory only (bypassing the internal memory), then he must connect External
Access (EA) pin to ground (GND).
You may already know that 8051 has a special feature of locking the program memory (internal) and hence
protecting against software piracy. This feature is enabling by program lock bits. Once these bits are
programmed, contents of internal memory cannot be accessed using an external circuitry. However locking the
software is not possible if external memory is also used to store the software code. Only internal memory can be
locked and protected. Once locked, these bits can be unlocked only by a memory-erase operation, which in turn
will erase the programs in internal memory too.
8051 is capable of pipelining. Pipelining makes a processor capable of fetching the next instruction while
executing previous instruction. It is something like multi tasking, doing more than one operation at a time. 8051
is capable of fetching first byte of the next instruction while executing the previous instruction.
In the MCS-51 family, 8051 has 128 bytes of internal data memory and it allows interfacing external data
memory of maximum size up to 64K. So the total size of data memory in 8051 can be upto 64K (external) +
128 bytes (internal). Observe the diagram carefully to get more understanding. So there are 3
separations/divisions of the data memory:1) Register banks.
2) Bit addressable area.
3) Scratch pad area.
Register banks form the lowest 32 bytes on internal memory and there are 4 register banks designated bank #0,
#1, #2 and #3. Each bank has 8 registers which are designated as R0, R1R7. At a time only one register bank
is selected for operations and the registers inside the selected bank are accessed using mnemonics R0...R1... Etc.
Other registers can be accessed simultaneously only by direct addressing. Registers are used to store data or
operands during executions. By default register bank #0 is selected (after a system reset).
The bit addressable area of 8051 is usually used to store bit variables. The bit addressable area is formed by the
16 bytes next to register banks. They are designated from address 20H to 2FH (total 128 bits). Each bit can be
accessed from 00H to 7FH within 128 bits from 20H to 2FH. Bit addressable area is mainly used to store bit
variables from application program, like status of an output device like LED or Motor (ON/OFF) etc. We need
only a bit to store this status and using a complete byte addressable area for storing this is really bad
programming practice, since it results in wastage of memory.
The scratch pad area is the upper 80 bytes which is used for general purpose storage. Scratch pad area is from
30H to 7FH and this includes stack too.
PORT P0 (pins 32 to
39): PORT P0 can
be used as a general purpose 8 bit port when no external memory is present, but if external memory access is
required then PORT P0 acts as a multiplexed address and data bus that can be used to access external memory
in conjunction with PORT P2. P0 acts as AD0-AD7.
PORT P1 (Pins 1 to 8): The port P1 is a general purpose input/output port which can be used for a variety of
interfacing tasks. The other ports P0, P2 and P3 have dual roles or additional functions associated with them
based upon the context of their usage. The port 1 output buffers can sink/source four TTL inputs. When 1s are
written to portn1 pins are pulled high by the internal pull-ups and can be used as inputs.
PORT P2 (pins 21 to 28): PORT P2 can also be used as a general purpose 8 bit port when no external memory
is present, but if external memory access is required then PORT P2 will act as an address bus in conjunction
with PORT P0 to access external memory. PORT P2 acts as A8-A15, as can be seen from fig 1.1
PORT P3 (Pins 10 to 17): PORT P3 acts as a normal IO port, but Port P3 has additional functions such as,
serial transmit and receive pins, 2 external interrupt pins, 2 external counter inputs, read and write pins for
memory access.
Let us begin this article with a simple question. What is an addressing mode ?. A simple question always has
a simple answer too. Addressing mode is a way to address an operand. Operand means the data we are
operating upon (in most cases source data). It can be a direct address of memory, it can be register names, it can
be any numerical data etc. I will explain this with a simple data move instruction of 8051.
MOV A,#6AH
Here the data 6A is the operand, often known as source data. When this instruction is executed, the data 6AH is
moved to accumulator A. There are 5 different ways to execute this instruction and hence we say, we have got 5
addressing modes for 8051. They are
1) Immediate Addressing Mode
The
picture
above describes the above instruction and its execution. The opcode for MOV A, # data is 74H. The opcode is
saved in program memory at 0202 address. The data 6AH is saved in program memory 0203. (See, any part of
the program memory can be used, this is just an example) When the opcode 74H is read, the next step taken
would be to transfer whatever data at the next program memory address (here at 0203) to accumulator A (E0H
is the address of accumulator). This instruction is of two bytes and is executed in one cycle. So after the
execution of this instruction, program counter will add 2 and move to o204 of program memory.
Note: The # symbol before 6AH indicates that operand is a data (8 bit). If # is not present then the
hexadecimal number would be taken as address.
This is another way of addressing an operand. Here the address of the data (source data) is given as operand. Let
us take an example.
MOV A, 04H
Here 04H is the address of register 4 of register bank#0. When this instruction is executed, what ever data is
stored in register 04H is moved to accumulator. In the picture below we can see, register 04H holds the data
1FH. So the data 1FH is moved to accumulator.
Note: We have not used # in direct addressing mode, unlike immediate mode. If we had used #, the data
value 04H would have been transferred to accumulator instead 0f 1FH.
As shown in picture above this is a 2 byte instruction which requires 1 cycle to complete. Program counter will
increment by 2 and stand in 0204. The op-code for instruction MOV A, address is E5H. When the instruction at
0202 is executed (E5H), accumulator is made active and ready to receive data, Then program control goes to
next address that is 0203 and look up the address of the location (04H) where the source data (to be transferred
to accumulator) is located. At 04H the control finds the data 1F and transfers it to accumulator and hence the
execution is completed.
In this addressing mode we use the register name directly (as source operand). An example is shown below.
MOV A, R4
At a time registers can take value from R0,R1to R7. You may already know there are 32 such registers. So
how you access 32 registers with just 8 variables to address registers? Here comes the use of register banks.
There are 4 register banks named 0, 1, 2 and 3. Each bank has 8 registers named from R0 to R7. At a time only
one register bank can be selected. Selection of register bank is made possible through a Special Function
Register (SFR) named Processor Status Word (PSW). PSW is an 8 bit SFR where each bit can be programmed.
Bits are designated from PSW.0 to PSW.7 Register banks are selected using PSW.3 and PSW.4.These two bits
are known as register bank select bits as they are used to select register banks. A picture below shows the PSW
register and the Register Bank Select bits with status.
So in register direct addressing mode, data is transferred to accumulator from the register (based on which
register bank is selected).
So we see that op-code for MOV A, R4 is EC. The op-code is stored in program memory address 0202 and
when it is executed the control goes directly to R4 of the respected register bank (that is selected in PSW). If
register bank #0 is selected then the data from R4 of register bank #0 will be moved to accumulator. (Here it is
2F stored at 04 H). 04 H is the address of R4 of register bank #0. Movement of data (2F) in this case is shown
as bold line. Now please take a look at the dotted line. Here 2F is getting transferred to accumulator from data
memory location 0C H. Now understand that 0C H is the address location of Register 4 (R4) of register bank
#1. Programmers usually get confused with register bank selection. Also keep in mind that data at R4 of register
bank #0 and register bank #1 (or even other banks) will not be same. So, wrong selection of register banks will
result in undesired output.
Also note that the instruction above is 1 byte and requires 1 cycle for complete execution. This means using
register direct addressing mode can save program memory.
So in this addressing mode, address of the data (source data to transfer) is given in the register operand.
MOV A, @R0
Here the value inside R0 is considered as an address, which holds the data to be transferred to accumulator.
Example: If R0 holds the value 20H, and we have a data 2F H stored at the address 20H, then the value 2FH
will get transferred to accumulator after executing this instruction.
So the op-code for MOV A, @R0 is E6H. Assuming that register bank #0 is selected. So the R0 of register bank
#0 holds the data 20H. Program control moves to 20H where it locates the data 2FH and it transfers 2FH to
accumulator.
This is a single byte instruction and the program counter increments 1 and moves to 0203 of program memory.
Note: Only R0 and R1 are allowed to form a register indirect addressing instruction. In other words
programmer can make any instruction either using @R0 or @R1. All register banks are allowed.
The op-code for the instruction is 93H. DPTR holds the value 01FE, where 01 is located in DPH (higher 8 bits)
and FE is located in DPL (lower 8 bits). Accumulator now has the value 02H. A 16 bit addition is performed and
now 01FE H+02 H results in 0200 H. What-ever data is in 0200 H will get transferred to accumulator. The
previous value inside accumulator (02H) will get replaced with new data from 0200H. New data in the
accumulator is shown in dotted line box.
This is a 1 byte instruction with 2 cycles needed for execution. What you infer from that? The execution time
required for this instruction is high compared to previous instructions (which all were 1 cycle).
The other example MOVC A, @A+PC works the same way as above example. The only difference is, instead
of adding DPTR with accumulator, here data inside program counter (PC) is added with accumulator to obtain
the target address.
Introduction
The process of writing program for the microcontroller mainly consists of giving instructions (commands) in
the specific order in which they should be executed in order to carry out a specific task. As electronics cannot
understand what for example an instruction if the push button is pressed- turn the light on means, then a
certain number of simpler and precisely defined orders that decoder can recognize must be used. All commands
are known as INSTRUCTION SET. All microcontrollers compatible with the 8051 have in total of 255
instructions, i.e. 255 different words available for program writing.
At first sight, it is imposing number of odd signs that must be known by heart. However, It is not so
complicated as it looks like. Many instructions are considered to be different, even though they perform the
same operation, so there are only 111 truly different commands. For example: ADD A,R0, ADD A,R1, ... ADD
A,R7 are instructions that perform the same operation (addition of the accumulator and register). Since there are
8 such registers, each instruction is counted separately. Taking into account that all instructions perform only 53
operations (addition, subtraction, copy etc.) and most of them are rarely used in practice, there are actually 2030 abbreviations to be learned, which is acceptable.
Types of instructions:
Depending on operation they perform, all instructions are divided in several groups:
Arithmetic Instructions
Branch Instructions
Logic Instructions
Bit-oriented Instructions
The first part of each instruction, called MNEMONIC refers to the operation an instruction performs (copy,
addition, logic operation etc.). Mnemonics are abbreviations of the name of operation being executed. For
example:
LJMP LAB5 - Means: Long Jump LAB5 (long jump to the address marked as LAB5);
JNZ LOOP - Means: Jump if Not Zero LOOP (if the number in the accumulator is not 0, jump to the
address marked as LOOP);
The other part of instruction, called OPERAND is separated from mnemonic by at least one whitespace and
defines data being processed by instructions. Some of the instructions have no operand, while some of them
have one, two or three. If there is more than one operand in an instruction, they are separated by a comma. For
example:
JZ TEMP - if the number in the accumulator is not 0, jump to the address marked as TEMP;
CJNE A, #20, LOOP - compare accumulator with 20. If they are not equal, jump to the address marked
as LOOP;
Arithmetic instructions
Arithmetic instructions perform several basic operations such as addition, subtraction, division, multiplication
etc. After execution, the result is stored in the first operand. For example:
ADD A,R1 - The result of addition (A+R1) will be stored in the accumulator.
Mnemonic
Description
Byte
Cycle
ADD A, Rn
ADD A, direct
ADD A, @Ri
ADD A, #data
ADDC A, Rn
ADDC A, direct Adds the direct byte to the accumulator with a carry flag
ADDC A, @Ri
ADDC A, #data
SUBB A, Rn
SUBB A, direct
SUBB A, @Ri
SUBB A, #data
INC A
INC Rn
INC Rx
INC @Ri
DEC A
DEC Rn
DEC Rx
DEC @Ri
INC DPTR
MUL AB
Multiplies A and B
DIV AB
Divides A by B
DA A
Branch Instructions
There are two kinds of branch instructions:
Unconditional jump instructions: Upon their execution a jump to a new location from where the program
continues execution is executed.
Conditional jump instructions: A jump to a new program location is executed only if a specified condition is
met. Otherwise, the program normally proceeds with the next instruction.
Branch Instructions
Mnemonic
Description
Byte
Cycle
ACALL addr11
LCALL addr16
RET
RETI
AJMP addr11
Absolute jump
LJMP addr16
Long jump
SJMP rel
Short jump
JNC rel
JB bit,rel
JBC bit,rel
JMP @A+DPTR
JZ rel
JNZ rel
CJNE A rel
CJNE Rn,#data
CJNE @Ri,#data
DJNZ Rn,rel
DJNZ Rx,rel
NOP
No operation
Mnemonic
Description
Byte
Cycle
MOV A,Rn
MOV A,direct
MOV A,@Ri
MOV A,#data
MOV Rn,A
MOV Rn,direct
MOV Rn,#data
MOV direct,A
MOV direct,Rn
MOV direct,direct
MOV direct,@Ri
MOV direct,#data
MOV @Ri,A
MOV @Ri,direct
MOV @Ri,#data
MOV DPTR,#data
3-10
3-10
4-11
4-11
MOVX A,@Ri
MOVX A,@DPTR
MOVX @Ri,A
MOVX @DPTR,A
(16-bit address)
PUSH direct
POP direct
XCH A,Rn
XCH A,direct
XCH A,@Ri
XCHD A,@Ri
Logic Instructions
Logic instructions perform logic operations upon corresponding bits of two registers. After execution, the result
is stored in the first operand
Mnemonic
Description
Byte
Cycle
ANL A,Rn
ANL A,direct
ANL A,@Ri
ANL A,#data
ANL direct,A
ANL direct,#data
ORL A,Rn
OR register to accumulator
ORL A,direct
ORL A,@Ri
ORL direct,A
ORL direct,#data
XRL A,Rn
XRL A,direct
XRL A,@Ri
XRL A,#data
XRL direct,A
CLR A
CPL A
SWAP A
RL A
RLC A
RR A
RRC A
Bit-oriented Instructions
Similar to logic instructions, bit-oriented instructions perform logic operations. The difference is that these are
performed upon single bits.
Bit-oriented Instructions
Mnemonic
Description
Byte
Cycle
CLR C
CLR bit
SETB C
SETB bit
CPL C
CPL bit
ANL C,bit
ANL C,/bit
ORL C,bit
ORL C,/bit
MOV C,bit
MOV bit,C
A - accumulator;
Rn - is one of working registers (R0-R7) in the currently active RAM memory bank;
Direct - is any 8-bit address register of RAM. It can be any general-purpose register or a SFR (I/O port,
control register etc.);
addr11 - is an 11-bit address. May be within the same 2KB page of program memory as the first byte of
the following instruction;
rel - is the address of a close memory location (from -128 to +127 relative to the first byte of the
following instruction). On the basis of it, assembler computes the value to add or subtract from the number
currently stored in the program counter;
8051 Programming
microcontroller memory. This is the moment when the last link in the chain - the programmer - appears on the
scene. It is a small device connected to a PC via some of the ports and has a socket for placing chip in.
Labels;
Orders;
Directives; and
Comments.
Every program line to be compiled must start with a symbol, label, mnemonics or directive;
Text following the mark ; in a program line represents a comment ignored (not compiled) by the
assembler; and
All the elements of one program line (labels, instructions etc.) must be separated by at least one space
character. For the sake of better clearness, a push button TAB on a keyboard is commonly used instead of it, so
that it is easy to delimit columns with labels, directives etc. in a program.
Typical applications:8051 chips are used in a wide variety of control systems, telecom applications, robotics as well as in the
automotive industry. By some estimation, 8051 family chips make up over 50% of the embedded chip market.