Microcontroller 8051 - PART1
Microcontroller 8051 - PART1
Microcontroller 8051 - PART1
Refer programs taken in practicals and skills also for theory paper
With Harvard architecture processor can access In Von Nuemann architecture either program or data
program and data memory at the same time which can be accessed at a time.
can help to perform faster program execution
With Harvard architecture it is possible to have With Von Nuemann architecture since only single bus is
different address and data bus width for program available, bus characteristics are same for program and
and data memory data.
HW design of this architecture is complex and HW design for this architecture is simple and cost
expensive. efficient.
Since program and data are different programs are Since program and data share the same memory, a
prone to crash caused by program error itself. program error can rewrite the instruction and result into
undesirable behaviour or crash.
03 Instruction Cycles Complex Instruction taking multiple Simple Instruction taking 1 cycle
cycles
04 Design Feature- Designed around Instruction set (no Designed around pipeline
Pipeline pipeline)
07 Addressing modes Many Instruction and addressing modes Few Instruction and addressing modes (3-5)
(12-14)
08 Complexity Complexity in the micro program Complexity in the compiler
10 Memory accress Only LOADs, STOREs access memory Any Instruction may access memory
General Purpose RAM: 30h to 7Fh. Even if 80 bytes of Internal RAM memory are available for general-purpose data
storage, user should take care while using the memory location from 00 -2Fh
since these locations are also the default register space, stack space, and bit addressable space. It is a good practice to use
general purpose memory from 30 – 7Fh. The general purpose RAM can be accessed using direct or indirect addressing
modes.
SFR List
Port 1
Port 1 occupies a total of 8 pins (pins 1 through 8) . It can be used as input or output. In
contrast to Port 0 , this port does not need any pull-up resistors since it already has
pull-up resistors internally. Upon reset port 1 is configured as an output port. To make
Port 1 an input port it must be programmed as such by writing 1 to all its bits.
Port 2
Port 3 occupies a total of 8 pins, pin 10 through 17. It can be used as input or output. P3 does not need any pull-up
resistors , the same as P1 and P2. Although Port 3 is configured as an output port upon reset, Port 3 has additional
function of providing some extremely important signals such as interrupts. Table depicts the alternate functions of port
2. P3.0 and P3.1 are used for the RxD and TxD serial communication signals. P3.2 and P3.3 are used for external
interrupts. Bits P3.4 and P3.5 are used for timers 0 and 1. Bits P3.6 and P3.7 are used to provide WR and RD signals for
external memories in 8051 based system.
Addressing Modes
What is an Addressing Mode?
An Addressing Mode is a way to locate a target Data, which is also
called as Operand. The 8051 Family of Microcontrollers allows five
types of Addressing Modes for addressing the Operands. They are:
• Immediate Addressing
• Register Addressing
• Direct Addressing
• Register – Indirect Addressing
• Indexed Addressing
Programming model:
The CPU registers are used to store the data temporarily. The information may be data to be processed or address
pointing the data to be fetched. The majority of registers are 8 bits. The 8-bit registers are shown in the diagram from
MSB (most significant bit) D7 to the LSB (least significant bit) D0. The most widely used registers of 8051 are A
(accumulator), B, R0, R1, R2, R3, R4, R5, R6, R7, DPTR (data pointer), and PC (program counter). All these registers are
8 bits except DPTR and the program counter. The accumulator is used to hold one operand before execution and hold
the result after execution. The program counter points to the address of next instruction to be fetched. It is a auto
increment register. As the size of program counter is 16 bit. 8051 can access the program addresses from
0000H-FFFFH. When 8051 is powered-up the program counter contents will be 0000H. This means that it expects the
first opcode to be stored at ROM address 0000H. For this reason in the 8051 system, the first opcode must be burned
memory location 0000H of program ROM since this is where it looks for the first instruction when it is booted.
PSW : Programm Status Word
It is 8 bit register. Its address is D0H and It is bit and byte accessible. It has 4 conditional flags or math flags which sets or
resets according to condition of result. It has 3 control flags, by setting or resetting bit required operation or function can
be achieved.
1. Carry Flag(CY): During addition and subtraction any carry or borrow is generated then carry flag is set otherwise carry
flag resets. It is used in arithmetic, logical, jump, rotate and Boolean operations.
2. Auxiliary carry flag(AC): If during addition and subtraction any carry or borrow is generated from lower 4 bit to higher
4 bit then AC sets else it resets. It is used in BCD arithmetic operations.
3. Overflow flag(OV): If in signed arithmetic operations result exceeds more than 7 bit than OV flag sets else resets.It is
used in signed arithmetic operations only.
4. Parity flag(P): If in result, even no. Of ones "1" are present than it is called even parity and parity flag sets. In result
odd no. Of ones "1"are present than it is called odd parity and parity flag resets.
ii. CONTROL FLAGS:
1. FO: It is user defined flag. The user defines the function of this flag. The user can set ,test n clear this flag through
software.
2. RS1 and RS0: These flags are used to select bank of register by resetting those flags which are as shown in table :
• The 128 bytes of RAM inside the 8051 are assigned the
address 00 to 7FH. They can be accessed directly as
memory locations and are divided into three different
groups as follows −
• 32 bytes from 00H to 1FH locations are set aside for
register banks and the stack.
• 16 bytes from 20H to 2FH locations are set aside for
bit-addressable read/write memory.
• 80 bytes from 30H to 7FH locations are used for read
and write storage; it is called as scratch pad. These 80
locations RAM are widely used for the purpose of
storing data and parameters by 8051 programmers.
• Instruction Timings
The 8051 internal operations and external read/write operations are controlled by the oscillator clock.
T-state, Machine cycle and Instruction cycle are terms used in instruction timings.
T-state is defined as one subdivision of the operation performed in one clock period. The terms 'Tstate' and 'clock
period' are often used synonymously.
Machine cycle is defined as 12 oscillator periods. A machine cycle consists of six states and each state lasts for two
oscillator periods. An instruction takes one to four machine cycles to execute an
instruction.
Instruction cycle is defined as the time required for completing the execution of an instruction. The 8051 instruction
cycle consists of one to four machine cycles.
Eg. If 8051 microcontroller is operated with 12 MHz oscillator, find the execution time for the
following four instructions.
1. ADD A, 45H
2. SUBB A, #55H
3. MOV DPTR, #2000H
4. MUL AB
Since the oscillator frequency is 12 MHz, the clock period is, Clock period = 1/12 MHz = 0.08333 µS.
Time for 1 machine cycle = 0.08333 µS x 12 =1 µS.
Instruction No. of machine cycles Execution time
• 1. ADD A, 45H 1 1 µ
• Instruction pipelining is mechanism used in CPU to increase overall instruction
execution throughput.
• For a general four stage pipelining each instruction is divided into four stages as below
M OV direct, direct Move byte to the direct address from the direct address Direct 3 2
MOV direct, #data Move immediate data byte to the direct address Immediate 3 2
MOV a,@Ri Move into A the byte from the address pointed by Ri Indirect 2 2
MOV direct, @Ri Move into direct address from address pointed by Ri indirect 1 1
MOV @Ri, direct Move from the direct address to the address poined by ri Indirect 2 2
MOVC A, @A+PC Move the code or constant into A the byte Indirect 1 2
from the program memory address
pointed by hypothetical addition of PC
with the A
itself
MOX-type Instructions
MOVX A,@Ri Move the external data byte into a from Indirect 1 2
the memory
address pointed by Ri
An XCH instruction is for exchanging the A register with a source using the register (direct or
indirect addresing0 mode.
Table gives features of 8-bit AND, OR and XOR instruction. These instructions have 4 addressing modes such as
register, immediate, direct and indirect.
ORL C, bit OR between CF and bit , place the result in C Direct bit addressing 2 2
Relative Addressing.
Relative addressing is used only with conditional jump instructions. The relative address, (offset), is an 8 bit
signed number, which is automatically added to the PC to make the address of the next instruction. The 8 bit
signed offset value gives an address range of +127 to —128 locations.
The jump destination is usually specified using a label and the assembler calculates the jump offset
accordingly. The advantage of relative addressing is that the program code is easy to relocate and
the address is relative to position in the memory.
Eg. SJMP LOOP1
Absolute addressing
Absolute addressing is used only by the AJMP (Absolute Jump) and ACALL (Absolute Call) instructions.
These are 2 bytes instructions. The absolute addressing mode specifies the lowest 11
bit of the memory address as part of the instruction. The upper 5 bit of the destination address arethe upper 5 bit
of the current program counter. Hence, absolute addressing allows branching only within the current 2 Kbyte
page of the program memory.
Eg. AJMP LOOP1
ACALL LOOP2
• Long Addressing
• The long addressing mode is used with the instructions LJMP and LCALL. These are 3 byte
• instructions. The address specifies a full 16 bit destination address so that a jump or a call can be
• made to a location within a 64 Kbyte code memory space.
• Eg. LJMP FINISH
• LCALL DELAY
8051 has three jump instructions: Long- it jumps to 16-bit address, Absolute- it jumps within 2 K bytes and Short- it jumps
to address within 128 bytes above or below the present address.
Long, absolute and short jump instructions
2 2
DJNZ direct, Rel Decrement byte at the direct and Relative (offset)
jump if byte is still not zero
Jump after comparison
Direct 16- 3 2
LCALL addr16 Call to the next address given by two bit address
bytes in the instruction
Direct 11 2 2
ACALL addr11 Call the next address given by 11 bits in
the instruction.
Stack 1 2
RET Return to PC the saved PCL and PCH address
from the stack.
Interrupt Control Flow (RETI instruction)