Module 3
Module 3
Introduction
Block Diagram and Pin Description of the
8051
Registers
Memory mapping in 8051
Stack in the 8051
I/O Port Programming
Timers
Interrupts & Applications
Why do we need to learn
Microcontrollers ?
■ Its not an exaggeration if I say that, today
there is no electronic gadget on the earth
which is designed without a Microcontroller.
Ex: communication devices, digital
entertainment, portable devices etc…
I/O
Serial A single chip
Timer COM
Port Microcontroller
Port
How it is different from a
Microprocessor ??
■ General-purpose microprocessor
CPU for Computers
No RAM, ROM, I/O on CPU chip itself
Example : Intel’s x86, Motorola’s 680x0
Data Bus
CPU
General-
Serial
Purpose RAM ROM I/O Timer COM
Micro- Port
Port
processor
Address Bus
Microprocessor vs. Microcontroller
Microprocessor Microcontroller
■ CPU is stand-alone, RAM, • CPU, RAM, ROM, I/O and
ROM, I/O, timer are separate timer are all on a single chip
■ designer can decide on the • fix amount of on-chip ROM,
amount of ROM, RAM and I/ RAM, I/O ports
O ports.
• Highly bit addressable
■ expansive
• for applications in which cost,
■ versatility
power and space are critical
■ general-purpose
• single-purpose
EVOLUTION
Flashback !!!!
In 1970 and 1971, about the same time Intel was inventing its
microprocessor, Gary Boone, an engineer at Texas Instruments was working on
a similar idea. This extraordinary breakthrough was given the rather humdrum
name of the TMS1802NC. It is named as microcontroller.
During Flash Programming, this pin acts as program pulse input (PROG).
Pin 31 (EA/VPP): Pin 31 is the External Access Enable Pin i.e. allows external
program Memory. Code from external program memory can be fetched only if this pin
is LOW. Else internal program memory and external program memory is used.
During Flash Programming, this Pin receives 12V Programming Enable Voltage
(VPP).
Flash memory is just like EEPROM: Electrically erasable programmable read only
memory.
PSEN: This is an output pin. PSEN stands for “program store enable.” This pin is
used to read external program memory when ‘0’ else external data memory. If we use
an external ROM for storing the program, then logic 0 appears on it, which indicates
Microcontroller to read data from the memory.
https://www.electronicshub.org/8051-microcontroller-memory-organization/
EA :
Memory Organization
• 8051 has 2 types of memory
– Program memory (ROM)
– Data memory (RAM)
• Few KB of ROM and 128 or 256 bytes of RAM
• 16 address bus capable of addressing 64KB of
memory
• External memory can also be used
▪ Almost all modern variants of 8051 Microcontroller have 256B of RAM. In this 256B, the
first 128B i.e. memory addresses from 00H to 7FH is divided into Working Registers
(organized
almost all modern as variants
RegisterofBanks), Bit – Addressable
8051 Microcontroller have Area
256B ofandRAM.
General
In thisPurpose
256B, theRAM first(also
128B
known as
i.e. memory Scratchpad
addresses from area).
00H Into the
7FHfirst 128B in
is divided of to
RAM (fromRegisters
Working 00H to 7FH), the first
(organized 32B i.e.
as Register
memory
Banks), from addresses
Bit – Addressable Area00H
andtoGeneral
1FH consists
Purpose ofRAM
32 Working Registers
(also known that are organized
as Scratchpad area). as
four
In the firstbanks
128Bwith 8 Registers
of RAM (from 00H in each Bank.
to 7FH), the first 32B i.e. memory from addresses 00H to 1FH
consists of 32 Working Registers that are organized as four banks with 8 Registers in each Bank.
• DATA registers
• CONTROL
registers
• Timers
• Serial ports
• Interrupt system
•Analog to Digital converter
Addresses 80h – FFh
•Digital to Analog converter
etc.. Direct Addressing is used to
access SFRs
RAM memory space allocation in the 8051
7FH
Second 2FH
block Bit-Addressable RAM
20H
1FH Register Bank 3
18H
17H
First block Register Bank 2
10H
0FH Register Bank 1
08H
07H
Register Bank 0
00H
8051 Register bank
Second block
• 16 bit addressable register
each of 8 bits
• Total 128 bits
• Address 20h to 2Fh
• Both bit addressable and
byte addressable
Third block
• Address 30 to 7Fh
• 80 locations
• Do not have any special function
• Scratch pad RAM
Special Function Registers -SFR
• 8051 microcontroller has a dedicated SFR that
controls various functionalities and peripheral
devices. These registers are essential for
programming the microcontroller’s behavior.
• Direct Addressing
• 80h to FFh
Accumulator
• The Register A is located at the address E0H in
the SFR memory space. The Accumulator is
used to hold the data for almost all the ALU
Operations
B Register
• The B Register is used along with the ACC in
Multiplication and Division operations.
♦ Example:
MOV R0, A
♦ Example:
♦ Examples:
;Store the content of
;accumulator into the memory
MOV @R0,A ;location pointed to by
;register R0. R0 could have an
;8-bit address, such as 60H.
♦ Examples:
ADD A,#030H ;Add 8-bit value of 30H to
;the accumulator register
;(which is an 8-bit register).
;function call:
Timer_Init().
Indexed Addressing
♦ After the execution of the above instructions, the program will branch to
address 1F08H (1F00H+08H) and transfer into the accumulator the data
byte retrieved from that location (from the look-up table)
Keil software introduction:
Download: https://www.keil.com/download/product/
Product: C51
Device: AT89C51ED2
Demo: https://www.youtube.com/watch?v=mhHJV21CDjs.
Instruction Types
♦ The C8051F020 instructions are divided into five functional
groups:
Arithmetic operations
Logical operations
Data transfer operations
Boolean variable operations
Program branching operations
53
Arithmetic Operations
♦ Rn refers to registers
R0-R7 of the currently
selected register bank
54
ADD A,<source-byte> ADDC A,<source-byte>
♦ ADD adds the data byte specified by the source operand to
the accumulator, leaving the result in the accumulator
♦ ADDC adds the data byte specified by the source operand,
the carry flag and the accumulator contents, leaving the
result in the accumulator
♦ Operation of both the instructions, ADD and ADDC, can
affect the carry flag (CY), auxiliary carry flag (AC) and the
overflow flag (OV)
CY=1 If there is a carryout from bit 7; cleared otherwise
AC =1 If there is a carryout from the lower 4-bit of A i.e. from bit 3;
cleared otherwise
OV=1 If the signed result cannot be expressed within the number
of bits in the destination operand; cleared otherwise
55
SUBB A,<source-byte>
♦ SUBB subtracts the specified data byte and the carry flag together from
the accumulator, leaving the result in the accumulator
CY=1 If a borrow is needed for bit 7; cleared otherwise
AC =1 If a borrow is needed for bit 3, cleared otherwise
OV=1 If a borrow is needed into bit 6, but not into bit 7, or into bit 7,
but not into bit 6.
♦ Example:
The accumulator holds 0C1H (11000001B), Register1 holds 40H
(01000000B) and the CY=1.The instruction,
SUBB A, R1
gives the value 70H (01110000B) in the accumulator, with the CY=0 and
AC=0 but OV=1
56
INC <byte>
♦ Increments the data variable by 1. The instruction is used in register,
direct or register direct addressing modes
♦ Example:
INC6FH
If the internal RAM location 6FH contains 30H, then the instruction
increments this value, leaving 31H in location 6FH
♦ Example:
MOVR1, #5E
INCR1
INC@R1
♦ If R1=5E (01011110) and internal RAM location 5FH contains 20H, the
instructions will result in R1=5FH and internal RAM location 5FH to
increment by one to 21H
57
DEC <byte>
♦ The data variable is decremented by 1
58
INC DPTR
♦ Increments the 16-bit data pointer by 1
59
MUL AB
♦ Multiplies A & B and the 16-bit result stored in [B15-8], [A7-0]
♦ The Low order byte of the 16-bit product will go to the accumulator
and the High order byte will go to the B register
♦ If the product is greater than 255 (FFH), the overflow flag is set;
otherwise it is cleared. The carry flag is always cleared.
♦ If ACC=85 (55H) and B=23 (17H), the instruction gives the product
1955 (07A3H), so B is now 07H and the accumulator is A3H. The
overflow flag is set and the carry flag is cleared.
60
DIV AB
♦ Divides A by B
61
DA A
♦ This is a decimal adjust instruction
♦ It adjusts the 8-bit value in ACC resulting from operations
like ADD or ADDC and produces two 4-bit digits (in packed
Binary Coded Decimal (BCD) format)
♦ Effectively, this instruction performs the decimal conversion
by adding 00H, 06H, 60H or 66H to the accumulator,
depending on the initial value of ACC and PSW
♦ If ACC bits A3-0 are greater than 9 (xxxx1010-xxxx1111), or
if AC=1, then a value 6 is added to the accumulator to
produce a correct BCD digit in the lower order nibble
♦ If CY=1, because the high order bits A7-4 is now exceeding
9 (1010xxxx-1111xxxx), then these high order bits will be
increased by 6 to produce a correct proper BCD in the high
order nibble but not clear the carry
62
Logical Operations
♦ Example:
ANLA,R2
If ACC=D3H (11010011) and R2=75H (01110101), the
result of the instruction is ACC=51H (01010001)
♦ Example:
ANLP1,#10111001B
64
ORL <dest-byte>,<source-byte>
♦ This instruction performs the logical OR operation on the
source and destination operands and stores the result in the
destination variable
♦ Example:
ORLA,R2
If ACC=D3H (11010011) and R2=75H (01110101), the
result of the instruction is ACC=F7H (11110111)
♦ Example:
ORLP1,#11000010B
This instruction sets bits 7, 6, and 1 of output Port 1
65
XRL <dest-byte>,<source-byte>
♦ This instruction performs the logical XOR (Exclusive OR)
operation on the source and destination operands and
stores the result in the destination variable
♦ Example:
XRLA,R0
If ACC=C3H (11000011) and R0=AAH (10101010), then the
instruction results in ACC=69H (01101001)
♦ Example:
XRLP1,#00110001
This instruction complements bits 5, 4, and 0 of
output Port 1
66
CLR A and CPL A
CLR A
♦ This instruction clears the accumulator (all bits set to 0)
♦ No flags are affected
♦ If ACC=C3H, then the instruction results in ACC=00H
CPL A
♦ This instruction logically complements each bit of the accumulator (one’s
complement)
♦ No flags are affected
♦ If ACC=C3H (11000011), then the instruction results in ACC=3CH (00111100)
67
RL A
♦ The 8 bits in the accumulator are rotated one bit to the left.
Bit 7 is rotated into the bit 0 position.
68
RLC A
♦ The instruction rotates the accumulator contents one bit to
the left through the carry flag
♦ Bit 7 of the accumulator will move into carry flag and the
original value of the carry flag will move into the Bit 0
position
69
RR A
♦ The 8 bits in the accumulator are rotated one bit to the right.
Bit 0 is rotated into the bit 7 position.
70
RRC A
♦ The instruction rotates the accumulator contents one bit to
the right through the carry flag
♦ The original value of carry flag will move into Bit 7 of the
accumulator and Bit 0 rotated into carry flag
71
SWAP A
♦ This instruction interchanges the low order 4-bit nibbles
(A3-0) with the high order 4-bit nibbles (A7-4) of the ACC
72
Data Transfer Instructions
Mnemonic Description
MOV @Ri, direct [@Ri] = [direct]
MOV @Ri, #data [@Ri] = immediate data
MOV DPTR, #data 16 [DPTR] = immediate data
MOVC A,@A+DPTR A = Code byte from [@A+DPTR]
MOVC A,@A+PC A = Code byte from [@A+PC]
MOVX A,@Ri A = Data byte from external ram [@Ri]
MOVX A,@DPTR A = Data byte from external ram [@DPTR]
MOVX @Ri, A External[@Ri] = A
MOVX @DPTR,A External[@DPTR] = A
PUSH direct Push into stack
POP direct Pop from stack
XCH A,Rn A = [Rn], [Rn] = A
XCH A, direct A = [direct], [direct] = A
XCH A, @Ri A = [@Rn], [@Rn] = A
XCHD A,@Ri Exchange low order digits
73
MOV <dest-byte>,<source-byte>
♦ This instruction moves the source byte into the destination location
♦ The source byte is not affected, neither are any other registers or flags
♦ Example:
MOVR1,#60;R1=60H
MOVA,@R1 ;A=[60H]
MOVR2,#61;R2=61H
ADDA,@R2 ;A=A+[61H]
MOVR7,A ;R7=A
74
MOV DPTR, #data 16
♦ This instruction loads the data pointer with the 16-bit
constant and no flags are affected
♦ Example:
MOVDPTR,#1032
♦ This instruction loads the value 1032H into the data pointer,
i.e. DPH=10H and DPL=32H.
75
MOVC A,@A + <base-reg>
♦ This instruction moves a code byte from program memory into ACC
♦ The effective address of the byte fetched is formed by adding the original 8-bit
accumulator contents and the contents of the base register, which is either the
data pointer (DPTR) or program counter (PC)
♦ 16-bit addition is performed and no flags are affected
♦ The instruction is useful in reading the look-up tables in the program memory
♦ If the PC is used, it is incremented to the address of the following instruction
before being added to the ACC
♦ Example:
CLRA
LOC1: INCA
MOVC A,@A + PC
RET
Look_up DB 10H
DB 20H
DB 30H
DB 40H
♦ The subroutine takes the value in the accumulator to 1 of 4 values
defined by the DB (define byte) directive
♦ After the operation of the subroutine it returns ACC=20H
76
MOVX <dest-byte>,<source-byte>
♦ This instruction transfers data between ACC and a byte of external data
memory
♦ There are two forms of this instruction, the only difference between them
is whether to use an 8-bit or 16-bit indirect addressing mode to access
the external data RAM
♦ The 8-bit form of the MOVX instruction uses the EMI0CN SFR to
determine the upper 8 bits of the effective address to be accessed and
the contents of R0 or R1 to determine the lower 8 bits of the effective
address to be accessed
♦ Example:
MOV EMI0CN,#10H ;Load high byte of
;address into EMI0CN.
MOV R0,#34H ;Load low byte of
;address into R0(or R1).
MOVX A,@R0 ;Load contents of 1034H
;into ACC.
77
MOVX <dest-byte>,<source-byte>
♦ The 16-bit form of the MOVX instruction accesses the memory location
pointed to by the contents of the DPTR register
♦ Example:
MOVDPTR,#1034H ;Load DPTR with 16 bit
;address to read (1034H).
MOVX A,@DPTR ;Load contents of 1034H
;into ACC.
♦ The above example uses the 16-bit immediate MOV DPTR instruction to
set the contents of DPTR
♦ Alternately, the DPTR can be accessed through the SFR registers DPH,
which contains the upper 8 bits of DPTR, and DPL, which contains the
lower 8 bits of DPTR
78
PUSH Direct
♦ This instruction INCREMENTS THE STACK POINTER (SP) BY 1
♦ Example:
PUSH 22H
PUSH 23H
79
POP Direct
♦ This instruction reads the contents of the internal RAM location
addressed by the stack pointer (SP) and decrements the stack pointer
by 1. The data read is then transferred to the Direct address which is an
internal memory or a SFR. No flags are affected.
♦ Example:
POPDPH
POPDPL
♦ If SP=51H originally and internal RAM locations 4FH, 50H and 51H
contain the values 30H, 11H and 12H respectively, the instructions
above leave SP=4FH and DPTR=1211H
POPSP
♦ If the above line of instruction follows, then SP=30H. In this case, SP is
decremented to 4EH before being loaded with the value popped (30H)
80
XCH A,<byte>
♦ This instruction swaps the contents of ACC with the contents
of the indicated data byte
♦ Example:
XCHA,@R0
81
XCHD A,@Ri
♦ This instruction exchanges the low order nibble of ACC (bits
0-3), with that of the internal RAM location pointed to by Ri
register
♦ The high order nibbles (bits 7-4) of both the registers remain
the same
♦ Example:
XCHDA,@R0
♦ All bit accesses use direct JNC rel Jump if C not set
♦ Example:
CLRP2.7
If Port 2 has been previously written with DCH (11011100),
then the operation leaves the port set to 5CH (01011100)
84
SETB <bit>
♦ This operation sets the specified bit to 1
♦ Example:
SETB C
SETB P2.0
♦ If the carry flag is cleared and the output Port 2 has the
value of 24H (00100100), then the result of the instructions
sets the carry flag to 1 and changes the Port 2 value to 25H
(00100101)
85
CPL <bit>
♦ This operation complements the bit indicated by the operand
♦ Example:
CPLP2.1
CPLP2.2
♦ If Port 2 has the value of 53H (01010011) before the start of
the instructions, then after the execution of the instructions it
leaves the port set to 55H (01010101)
86
ANL C, <source-bit>
♦ This instruction ANDs the bit addressed with the Carry bit and stores the result in
the Carry bit itself
♦ If the source bit is a logical 0, then the instruction clears the carry flag; else the
carry flag is left in its original value
♦ If a slash (/) is used in the source operand bit, it means that the logical
complement of the addressed source bit is used, but the source bit itself is not
affected
♦ Example:
MOVC,P2.0;Load C with input pin
;state of P2.0.
ANLC,P2.7;AND carry flag with ;bit 7
of P2.
MOVP2.1,C;Move C to bit 1 of Port 2.
ANLC,/OV ;AND with inverse of OV flag.
♦ If P2.0=1, P2.7=0 and OV=0 initially, then after the above instructions,
P2.1=0, CY=0 and the OV remains unchanged, i.e. OV=0
87
ORL C, <source-bit>
♦ This instruction ORs the bit addressed with the Carry bit and stores the result in
the Carry bit itself
♦ It sets the carry flag if the source bit is a logical 1; else the carry is left in its
original value
♦ If a slash (/) is used in the source operand bit, it means that the logical
complement of the addressed source bit is used, but the source bit itself is not
affected
♦ Example:
MOV C,P2.0 ;Load C with input pin
;state of P2.0.
ORL C,P2.7 ;OR carry flag with
;bit 7 of P2.
MOV P2.1,C ;Move C to bit 1 of
;port 2.
ORL C,/OV ;OR with inverse of OV
;flag.
88
MOV <dest-bit>,<source-bit>
♦ The instruction loads the value of source operand bit into the destination
operand bit
♦ One of the operands must be the carry flag; the other may be any
directly-addressable bit
♦ Example:
MOVP2.3,C
MOVC,P3.3
MOVP2.0,C
♦ If P2=C5H (11000101), P3.3=0 and CY=1 initially, then after the above
instructions, P2=CCH (11001100) and CY=0.
89
JC rel
♦ This instruction branches to the address, indicated by the label, if the
carry flag is set, otherwise the program continues to the next instruction
♦ Example:
CLRC
SUBB A,R0
JC ARRAY1
MOVA,#20H
♦ The carry flag is cleared initially. After the SUBB instruction, if the value
of A is smaller than R0, then the instruction sets the carry flag and
causes program execution to branch to ARRAY1 address, otherwise it
continues to the MOV instruction.
90
JNC rel
♦ This instruction branches to the address, indicated by the label, if the
carry flag is not set, otherwise the program continues to the next
instruction
♦ Example:
CLRC
SUBB A,R0
JNCARRAY2
MOVA,#20H
♦ The above sequence of instructions will cause the jump to be taken if the
value of A is greater than or equal to R0. Otherwise the program will
continue to the MOV instruction.
91
JB <bit>,rel
♦ This instruction jumps to the address indicated if the
destination bit is 1, otherwise the program continues to the
next instruction
♦ Example:
JB ACC.7,ARRAY1
JB P1.2,ARRAY2
92
JNB <bit>,rel
♦ This instruction jumps to the address indicated if the
destination bit is 0, otherwise the program continues to the
next instruction
♦ Example:
JNBACC.6,ARRAY1
JNBP1.3,ARRAY2
93
JBC <bit>,rel
♦ If the source bit is 1, this instruction clears it and branches to
the address indicated; else it proceeds with the next
instruction
♦ Example:
JBCP1.3,ARRAY1
JBCP1.2,ARRAY2
CJNE @Ri,#data,rel
DJNZ Rn,rel
Decrement and Jump if Not
Zero
DJNZ direct,rel
NOP No Operation
95
ACALL addr11
♦ This instruction unconditionally calls a subroutine indicated by the
address
♦ The operation will cause the PC to increase by 2, then it pushes the
16-bit PC value onto the stack (low order byte first) and increments the
stack pointer twice
♦ The PC is now loaded with the value addr11 and the program execution
continues from this new location
♦ The subroutine called must therefore start within the same 2 kB block of
the program memory
♦ Example:
ACALL LOC_SUB
96
LCALL addr16
♦ This instruction calls a subroutine located at the indicated address
♦ The PC is then loaded with the value addr16 and the program execution
continues from this new location
♦ Example:
LCALL LOC_SUB
♦ RET pops the high byte and low byte address of PC from
the stack and decrements the SP by 2
98
RETI
♦ This instruction returns the program from an interrupt
subroutine
♦ RETI pops the high byte and low byte address of PC from
the stack and restores the interrupt logic to accept additional
interrupts
♦ SP decrements by 2 and no other registers are affected.
However the PSW is not automatically restored to its
pre-interrupt status
♦ After the RETI, program execution will resume immediately
after the point at which the interrupt is detected
♦ Suppose SP=0BH originally and an interrupt is detected
during the instruction ending at location 0213H
Internal RAM locations 0AH and 0BH contain the values 14H and
02H respectively
The RETI instruction leaves SP=09H and returns
99
program execution to location 0234H
AJMP addr11
♦ The AJMP instruction transfers program execution to the
destination address which is located at the absolute short
range distance (short range means 11-bit address)
♦ Example:
AJMP NEAR
10
LJMP addr16
♦ The LJMP instruction transfers program execution to the
destination address which is located at the absolute long
range distance (long range means 16-bit address)
♦ Example:
LJMP FAR_ADR
♦ This will be the new address where the program would branch
to unconditionally
♦ Example:
SJMP RELSRT
♦ Example:
MOVDPTR, #LOOK_TBL
JMP@A + DPTR
LOOK_TBL:AJMP LOC0
AJMP LOC1
AJMP LOC2
If the ACC=02H, execution jumps to LOC1
♦ Example:
SUBB A,#20H
JZ LABEL1
DECA
10
JNZ rel
♦ This instruction branches to the destination address if any bit
of ACC is a 1; else the program continues to the next
instruction
♦ Example:
DECA
JNZLABEL2
MOVRO, A
10
CJNE <dest-byte>,<source-byte>,rel
♦ This instruction compares the magnitude of the dest-byte and the
source-byte and branches if their values are not equal
♦ The carry flag is set if the unsigned dest-byte is less than the unsigned
integer source-byte; otherwise, the carry flag is cleared
♦ Example:
CJNE R3,#50H,NEQU
… … ;R3 = 50H
NEQU: JC LOC1 ;If R3 < 50H
… … ;R7 > 50H
LOC1: … … ;R3 < 50H
10
DJNZ <byte>,<rel-addr>
♦ This instruction is ”decrement jump not zero”
♦ It decrements the contents of the destination location and if the resulting
value is not 0, branches to the address indicated by the source operand
♦ An original value of 00H underflows to FFH
♦ No flags are affected
♦ Example:
DJNZ 20H,LOC1
DJNZ 30H,LOC2
DJNZ 40H,LOC3
♦ If internal RAM locations 20H, 30H and 40H contain the values 01H,
5FH and 16H respectively, the above instruction sequence will cause a
jump to the instruction at LOC2, with the values 00H, 5EH, and 15H in
the 3 RAM locations.
Note, the first instruction will not branch to LOC1 because the [20H] = 00H,
hence the program continues to the second instruction
Only after the execution of the second instruction (where the
location [30H] = 5FH), then the branching takes place
10
NOP
♦ This is the no operation instruction
♦ The instruction takes one machine cycle operation time
♦ Hence it is useful to time the ON/OFF bit of an output port
♦ Example:
CLRP1.2
NOP
NOP
NOP
NOP
SETB P1.2
10
However, the relation between machine cycle and clock cycle depends upon the
manufacturer. Ex: AT89C51 by Atmel consider 12 clock cycles per machine cycle. But,
other 8051 manufactured by Dallas consider 1 clock cycles per machine cycle.
Instruction cycle: Fetch, decode, execute, write back into memory and etc.
• Note: Mnemonic will occupy 1
byte memory.