8051 Microcontroller
8051 Microcontroller
8051 Microcontroller
Prepared by
A. Gokul Raj, M.E.,
Assistant Professor,
Department of Electrical and Electronics Engineering,
Muthayammal Engineering College (Autonomous),
Rasipuram – 637 408.
Unit – II 8051 Microcontroller
Microcontroller
1. 8051Microcontroller is an 8-bit microcontroller created in 1981. It is most popular and
commonly used microcontroller.
2. An 8-bit microcontroller has an 8-bit data bus and 16- bit address bus.
3. It is an integrated chip designed under very large scale integration techniques .
4. It consist of a processor with other peripheral devices like memory, I/O ports and timer.
A microcontroller contain all these component in a single chip
5. A microcontroller does not require much additional interfacing ICs for operation and it
functions
6. A microcontroller clock speed is limited only to a few tens of MHz
Classification of microcontrollers
Intel 4004 4 bit (2300 PMOS trans, 108 kHz) 1971
RL A
Rotate left the accumulator. Each bit is shifted one location to the left, with bit
7 going to bit 0
RRC A
Rotate right through the carry.
Each bit is shifted one location to the right, with bit 0 going into the carry bit in
the PSW, while the carry was at goes into bit 7
RLC A
Rotate left through the carry. Each bit is shifted one location to the left, with bit
7 going into the carry bit in the PSW, while the carry goes into bit 0.
Branch (JUMP) Instructions
Jump and Call Program Range
There are 3 types of jump instructions. They are:-
• Relative Jump
• Short Absolute Jump
• Long Absolute Jump
Relative Jump
Jump that replaces the PC (program counter) content with a new address that is
greater than or less than is called a relative jump.
The advantages of the relative jump are
Only 1 byte of jump address needs to be specified in the 2's complement form
• Specifying only one byte reduces the size of the instruction and speeds up
program execution.
• The program with relative jumps can be relocated without reassembling to
generate absolute jump addresses.
Instructions that use Relative Jump
SJMP <relative address>; this is unconditional jump
The remaining relative jumps are conditional jumps
• JC <relative address>
• JNC <relative address>
• JB bit, <relative address>
• JNB bit, <relative address>
• JBC bit, <relative address>
• CJNE <destination byte>, <source byte>, <relative address>
• DJNZ <byte>, <relative address>
• JZ <relative address>
• JNZ <relative address>
Short Absolute Jump
In this case only 11bits of the absolute jump address are needed.
In 8051, 64 Kbyte of program memory space is divided into 32 pages of 2
kbyte each. The instruction length becomes 2 bytes.
Long Absolute Jump/Call
The entire program memory from 0000H to FFFFH use long absolute jump.
The absolute address has to be specified in the op-code, the instruction length
is 3 bytes
Another classification of jump instructions is
• Unconditional Jump
• Conditional Jump
The unconditional jump is a jump in which control is transferred
unconditionally to the target location.
LJMP (long jump). This is a 3-byte instruction.
First byte is the op-code and second and third bytes represent the 16-bit target
Address which is any memory location from 0000 to FFFFH.
AJMP(Absolute jump)this causes unconditional branch to the indicated
address, by loading the 11 bit address to 0 -10 bits of the program counter.
SJMP (short jump). This is a 2-byte instruction. First byte is the op-code and
second byte is the relative target address, 00 to FFH
To calculate the target address of a short jump, the second byte is added to the
PC value
Conditional Jump instructions.
JBC Jump if bit = 1 and clear bit JNB Jump if bit = 0
JB Jump if bit = 1
JNC Jump if CY = 0
JC Jump if CY = 1
CJNE reg,#data Jump if byte ≠ #data
CJNE A,byte Jump if A ≠ byte
DJNZ Decrement and Jump if A ≠ 0
JNZ Jump if A ≠ 0
JZ Jump if A = 0
All conditional jumps are short jumps
Bit level jump instructions:
Bit level JUMP instructions will check the conditions of the bit and if condition
is true, it jumps to the address specified in the instruction. All the bit jumps are
relative jumps.
Subroutine CALL And RETURN Instructions
Subroutines are handled by CALL and RET instructions
There are two types of CALL instructions
LCALL address(16 bit)
This is long call instruction which unconditionally calls the subroutine located
at the indicated 16 bit address.
This is a 3 byte instruction.
ACALL address(11 bit)
This is absolute call instruction which unconditionally calls the subroutine
located at the indicated 11 bit address. This is a 2 byte instruction.
RET instruction
RET instruction pops top two contents from the stack and load it to PC.
Bit manipulation instructions.
8051 has 128 bit addressable memory.
Bit addressable SFRs and bit addressable PORT pins.
It is possible to perform following bit wise operations for these bit addressable
locations.
LOGICAL AND
ANL C,BIT(BIT ADDRESS)
ANL C, /BIT;
LOGICAL OR
• ORL C,BIT(BIT ADDRESS)
• ORL C, /BIT
CLR bit
• CLR bit
• CLR C
CPL bit
• CPL bit
• CPL C
ADDRESSING MODES
Various methods of accessing the data are called addressing modes. 8051
addressing modes are classified as follows.
Immediate addressing
Register addressing.
Direct addressing.
Indirect addressing.
Indexed addressing
Relative addressing.
Absolute addressing.
Long addressing.
Bit inherent addressing.
Bit direct addressing.
Immediate addressing.
In this addressing mode the data is provided as a part of instruction itself. In
other words data immediately follows the instruction.
Eg. MOV A,#30H
ADD A, #83
Register addressing.
In this addressing mode the register will hold the data. One of the eight
general registers (R0 to R7) can be used and specified as the operand.
MOV A,R0
ADD A,R6
Direct addressing
There are two ways to access the internal memory. Using direct address and
indirect address.
Using direct addressing mode we can not only address the internal memory but
SFRs also.
In direct addressing, an 8 bit internal data memory address is specified as part
of the instruction
In this addressing mode, data is obtained directly from the memory.
MOV A,60h
ADD A,30h
Indirect addressing
The indirect addressing mode uses a register to hold the actual address that
will be used in data movement.
Registers R0 and R1 and DPTR are the only registers that can be used as data
pointers.
Indirect addressing cannot be used to refer to SFR registers.
Both R0 and R1 can hold 8 bit address
MOV A,R0
ADD A,R1
Indexed addressing.
In indexed addressing, either the program counter (PC), or the data pointer
(DTPR)—is used to hold the base address, and the A is used to hold the offset
address
Indexed addressing is used with JMP or MOVC instructions
MOVC A, @A+DPTR // copies the contents of memory location pointed by
the sum of the accumulator A and the DPTR into accumulator A.
MOVC A, @A+PC // copies the contents of memory location pointed by
the sum of the accumulator A and the program counter into accumulator A.
Relative Addressing
Relative addressing is used only with conditional jump instructions.
The relative address is an 8 bit signed number, which is automatically added to
the PC to make the address of the next instruction.
The advantage of relative addressing is that the program code is easy to
relocate and the address is relative to position in the memory.
SJMP
LOOP1 JC
BACK
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.
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
Bit Inherent Addressing
In this addressing, the address of the flag which contains the operand, is
implied in the opcode of the instruction.
Eg. CLR C ;
Bit Direct Addressing
In this addressing mode the direct address of the bit is specified in the
instruction.
The RAM space 20H to 2FH and most of the special function registers are bit
addressable.
Bit address values are between 00H to 7FH.
Eg. CLR 07h ; Clears the bit 7 of 20h RAM space
SETB 07H ; Sets the bit 7 of 20H RAM space.
Timing diagram
The timing diagram provides the information about the various condition of the
signal in which the machine cycle is executed
Machine cycle
It consist of a sequence of 6 states numbered S1 through S6.Each state time
lasts for two oscillator period.
The machine cycle take 12 oscillator period or 1microsecond if the oscillator
frequency is 12MHz.
Each phase divided into Phase1 half and a Phase 2 half.
OSC S1 S2 S3 S4 S5 S6 S1 S2 S3 S4 S5 S6 S1 S2 S3 S4
ALE
Read next opcode
Read opcode Read next opcode
S1 S2 S3 S4 S5 S6
1 byte 1 cycle instruction ex INC A
S1 S2 S3 S4 S5 S6
2 byte 1 cycle instruction ex ADD A
S1 S2 S3 S4 S5 S6 S1 S2 S3 S4 S5 S6
1 byte 2 cycle instruction ex INC DPTR
Normally two programs are fetch are generated during each machine cycle ,
even if the instruction is executed.
If the instruction is executed it does not need more code bytes.
CPU simply the extra fetch and the program counter not incremented
Execution of one cycle instruction begins during State 1 of machine cycle
when the opcode is latched into the instruction register
The second fetch occur during S4 of the same machine cycle
Execution is complete at the end of state 6 of this machine cycle
The MOVX instruction take two machine cycle to execute.
No program fetch is generated during the second cycle of a MOVX instruction
To fetch or execute the sequence of MOVX instruction
To fetch or execute the sequence are same whether the program memory is
internal or external to the chip
Execution time does not depend on the whether the program memory is
internal or external
S1 S2 S3 S4 S5 S6 S1 S2 S3 S4 S5 S6
ALE
PSEN
RD
P2
WITHOUT A MOVX
The signals and timing involved in program fetch when memory program is
external
If program memory is external then the program memory read
PSEN is normally activated twice per machine cycle
S1 S2 S3 S4 S5 S6 S1 S2 S3 S4 S5 S6
ALE
PSEN
RD
P2
WITH A MOVX
If an access to external data memory two PSEN is skipped because the
address and data bus is mainly used for data memory access
Data memory bus cycle takes twice as much time as Program memory bus
cycle
ALE is used to latch the low address byte from P0 into the address latch
PSEN is not activated and program address are not emitted
ALE continuous to activate twice per machine cycle and is available as an
clock output signal
One ALE skipped during the execution of the MOVX instruction