Professional Documents
Culture Documents
Ap Eee Demc
Ap Eee Demc
Master’s Edition
N.DHANANJAYA
P. SRINIVAS
D.V. RAMANA
Price: 230
M.G.B Publications
Hyderabad & Tirupati. Cell: 9000 3050 79
ii
Acknowledgement
First of all, I would like to thank God, the almighty of giving me skills to write
this book.
I am very thankful to Smt. N.Deepa for giving me an opportunity to write this
book.
I specially grateful to my mom Smt. T. Geetha, My dad Late. Sri. T. Subba
Reddy, My Uncle K. Chandra Mouli Reddy, K. Bhaskar Reddy, Dr. K. Kumara
Swamy Reddy, C. Gurumurthy Reddy, C. Kumar Swamy Reddy, C. Yerrama
Reddy, & My brother T. Jagadeesh and the rest of my family, friends who
supported and encouraged me in spite of all the time it took me away from them.
I express my sincere gratitude to Dr. N. Sudhakar Reddy, Dr. D. Srinivasulu
Reddy, Dr. C. Chandrasekhar, Mrs. V. Madhurima, Mrs. N. Suguna and Mr. P.
Suresh Babu for their keen interest and encouragement in bringing out this book.
Finally, I wish to thank Sri. N. Giri Babu and the entire team of for bringing
out this book in a short time with quality printing.
Any suggestions for the improvement of this book and inclusion of new topics
will be acknowledged and appreciated.
Table of Contents
DEDICATED
TO
My Daughter MAANYA
Author
N. Dhananjaya
1
Basics of Digital Electronics
OBJECTIVES
Additional Information
Number Systems
Number is representation of a quantity. By using certain symbols, a quantity of
things is expressed. Instead of writing in words, a value may be mentioned by writing
some symbols. This symbolic representation is must to perform various mathematical
operations: Addition, subtraction, multiplication and division. This symbolic
representation of numerical value developed many number systems.
Additional Information
Binary Number System
1. For binary numbers, the binary digit at the extreme right is referred to as least
significant bit (LSB).
2. In the binary number 1001, the 1 at the right is the LSB. The left-most binary digit is
called the most significant bit (MSB).
3. The special terms used with the binary number system are explained as follows:
o Bit: It is an abbreviation for binary digit. It is either 0 or 1.
o Nibble: It is a group of 4 bits.
o Byte: It is a group of 8 bits.
o Computer Word: It is a set of fixed number of bits varying from 8 to 64 bits.
The length of a computer word usually called word size or word length.
0.7 x 2 = 1.4 1
0.4 x 2 = 0.8 0
0.8 x 2 = 1.6 1
0.6 x 2 = 1.2 1
0.2 x 2 = 0.4 0
0.710=0.101102
∴Binary equivalent of decimal number 41.710 = 101001.101102
1.3.2 Decimal to Octal Conversion
• The conversion from decimal to octal is similar to the conversion procedure for
decimal to binary conversion.
• The only difference is that power of 8 is used in place of 2 for division in the case
of integers and for multiplication in the case of fractional numbers.
NOTE: A method of converting a decimal number into an octal number is the repeated
division by 8 method.
Example 8: Convert a given decimal number 76 in to octal number system.
Oct/Nov-2013
Solution:
8 76
8 9 4 (LSD)
8 1 1
0 1 (MSD)
∴Octal equivalent of decimal number 7610 = 1148
Example 9: Convert a given decimal number 157 in to octal number system.
March/April-2013
Solution:
8 157
8 19 5 (LSD)
8 2 3
0 2 (MSD)
∴Octal equivalent of decimal number 15710 = 2358
Step 2: 24 23 22 21 20
15 1 1 1 1 F
↓ ↓
Hexadecimal number : A 2
↓ ↓
Hexadecimal number : 3 B
Binary number : 1 0 1 0 1 0 1 1 1 1 0 0 1 1 0 1
Hexadecimal number : 2 5 B
↓ ↓ ↓
Binary number : 0 0 1 0 0 1 0 1 1 0 1 1
∴ Binary number = 0010010110112
Step 2: Binary to octal
Binary number : 0 01 00 1 0 1 1 011
↓ ↓ ↓ ↓
Octal number : 1 1 3 3
∴ Octal number = 11338
1+0 0 1
1+1 1 0
In the last case i.e., adding 1 with a 1 results a 0 and a carry 1 has to be taken to the
higher position. The following examples illustrate the binary addition.
Example 45: Add (1101)2 and (0101)2
Solution:
2. Binary Subtraction
Binary subtraction is also carried out in the same way as decimal numbers are
subtracted. The subtraction is carried out from the least significant bits and preceded to
higher significant bits. When a1 is subtracted from a 0, a 1 is borrowed from immediate
higher significant bit. The following are the rules for binary subtraction.
A – B Borrow Difference
0–0 0 0
0–1 1 1
1–0 0 1
1–1 0 0
3. Binary Multiplication
The procedure for binary multiplication is the same as for decimal
multiplication. In fact, it would be noticed that it is easier than the decimal
multiplication. Since the only two numbers we have are 0 and 1.
The following are the rules for binary multiplication.
1. 0 × 0 = 0
2. 0 × 1 = 0
3. 1 × 0 = 0
4. 1 × 1 = 1
Example 49: Multiply the following binary numbers
a) 110 x 101 b) 11.01 x 10.1
Solutions:
a) Here 110 is called multiplicand and 101 is called multiplier
Additional Information
1’s Complement Subtraction
Subtraction of binary numbers can be accomplished by the direct method
described in section 1.9.2 or by using the 1’s complement method, which allows to
perform subtraction using only addition. For subtraction of two numbers we have two
cases.
• Subtraction of smaller number from larger number and
• Subtraction of larger number from smaller number.
The 1’s complement subtraction method for these two cases is as follows.
a) Subtraction of Smaller Number from Larger Number
1. Determine the 1’s complement of the smaller number.
2. Add the 1’s complement to the larger number.
3. Remove the carry and add it to the result. This is called end around carry.
Example 56: Subtract 1010112 from 1110012 using the 1’s complement method
Solution:
111001
+010100 ← 1’s complement of 1 0 1 0 1 1
1 001101
+1 ← Add end around carry
001110
∴ Final answer = 001110
b) Subtraction of Larger number from Smaller Number
1. Determine the 1’s complement of the larger number.
2. Add the 1’s complement to the smaller number.
3. Answer is in 1’s complement from. To get the answer in true form take the 1’s
complement and assign negative sign to the answer.
Example 57: Subtract 1110012 from 1010112 using the 1’s complement method
Solution:
101011
000110 ← 1’s complement of 1 1 1 0 0 1
111 ← Carry bits
1 1 0 0 0 1 ← Answer in 1’s complement form
∴ Answer in true form = - 0 0 1 1 1 0
∴ Final answer = 0 0 1 1 1 1
Case – 2: Subtraction of larger number from smaller number
1. Determine the 2’s complement of the large number.
2. Add the 2’s complement to the smaller number.
3. Answer is in the 2’s complement form. To get the answer in the true form take the
2’s complement and assign negative sign to the answer
Example 59: Subtract 1110012 from 1010112 using 2’s complement method.
Solution:
Additional Information
Use of codes in digital systems
• The digital systems like computers, microprocessors etc are required to handle the
data which may be numeric, alphabets or special characters.
• As these systems operate in a binary system and hence the numericals, alphabets
and other special characters are to be converted into binary format.
• This process of the converting into binary format is known as ‘coding’.
• The group of binary bits that represent numbers, alphabets or symbols are called
as ‘binary code’.
• Codes are also used for auxiliary functions as error detection.
• The different binary codes can be classified as:
o Weighted codes and Non-weighted codes
o Reflective codes and Sequential codes
o Alphanumeric codes
o Error Detecting and Correcting codes
Comparison of Weighted and Un-Weighted Codes
1.8 Compare weighted and Un-weighted codes.
Example 60: Convert each of the following decimal number into BCD.
(a) 10.45 (b) 27.19 (c) 10.24
Solutions:
Binary : 1 + 0 1 1 0
↓
Gray : 1 1
Binary : 1 0 + 1 1 0
↓
Gray: 1 1 1
Binary : 1 0 1 + 1 0
↓
Gray : 1 1 1 0
Step 5: Add the last adjacent pair.
Binary : 1 0 1 1 + 0
↓
Gray: 1 1 1 0 1
∴ The gray code for the binary number 10110 is 11101.
Example 62: Convert the following binary numbers to gray code numbers.
(a) 1010 (b) 110011
Solutions:
Step 2: Add the last binary digit just generated to the Gray digit in the next position.
Neglect carry.
Gray : 1 1 1 0 1
↓
Binary : 1 0
Step 3: Add the last binary digit generated to the next Gray digit.
Gray: 1 1 1 0 1
↓
Binary: 1 0 1
Step 4: Add the last binary digit generated to the next Gray digit.
Gray: 1 1 1 0 1
↓
Binary: 1 0 1 1 0
Step 5: Add the last binary digit generated to the next Gray digit. Discard carry.
Gray: 1 1 1 0 1
↓
Binary: 1 0 1 1 0
∴ Binary equivalent number for the gray code number 11101 is 10110.
1111(F) SI US / ? O ← o del
*Note: The hexadecimal digit representing each bit pattern is shown in parentheses.
ACK Acknowledge ETB End of transmission block
BEL Bell ETX End text
BS Back Space FF Form feed
CAN Cancel FS Form separator
CR Carriage return GS Group separator
DC1- DC4 Direct control HT Horizontal tab
DEL Delete idle LF Line feed
DLE Data link escape NAK Negative acknowledge
EM End of Medium NUL NULL
ENQ Enquiry RS Record separator
EOT End of transmission SI Shift in
ESC Escape SO Shift out
SOH Start of heading STX Start text
SUB Substitute SYN Synchronous idle
US Unit Separator VT Vertical tab
1.11.2. EBCDIC
• EBCDIC stands for Extended Binary Coded Decimal Interchange Code.
• It is an 8-bit code.
• It was first developed by IBM and is a coding method used by computers to present
letters, numbers, or other symbols in a binary language the computer can
understand.
Note: EBCDIC has a wider range of control characters than ASCII.
(x + y) + z = x + (y + z)
(x • y) • z = x • (y • z) For all x, y, z ∈ N
Postulate 5:
If • and + are two binary operators on a set N, • is said to be distributive over +
whenever,
x • (y + z ) = (x • y ) + (x • z )
Postulate 6:
For every element x∈N there exists an element x such that,
x + x =1
x • x=0
Thus Boolean algebra is defined on a set of elements N, together with two binary
operators ‘+’ and ‘ • ’ and unary operator (a bar over the variable), for which the above
postulates are satisfied.
1.13.1. Laws of Boolean Algebra
Boolean algebra has its own set of fundamental laws which are given below.
Law 1 A .0 = 0
Law 2 A .1 = A
AND laws
Law 3 A.A = A
Law 4 A.A = 0
Law 5 A+0=A
Law 6 A + 1 =1
OR laws
Law 7 A+A=A
Law 8 A + A =1
Law 12 A + (B + C ) = (A + B) + C
Associative laws
Law 13 A . (B . C ) = (A . B). C
Law 14 A . (B + C ) = A . B + A . C
Distributive laws
Law 15 A + (B . C ) = (A + B). (A + C )
Law 16 A + AB = A + B
Auxiliary laws
Law 17 A + A.B = A
Additional Information
Logic Gates
1. A digital circuit with one or more input signals but only one output signal is
called a logic gate. The output will depend upon the input signal/signals and
the type of gate.
2. Logic gates are electronic circuits because they are made up of a number of
electronic devices and components.
3. These are fundamental building blocks of digital systems.
4. Since a logic gate is a switching circuit (i.e., a digital circuit), its input and output
can have only one of the two possible states viz, either a high voltage (1) or a low
voltage (0) – it is either ON or OFF. Whether the output voltage of a logic gate is
high (1) or low (0) will depend upon the conditions at its input.
5. The gates are available today in the form of various IC families. The most
popular families are: Transistor Transistor Logic (TTL), Emitter Coupled Logic
(ECL), Metal-Oxide Semiconductor (MOS) and Complementary Metal Oxide
Semiconductor (CMOS).
6. Logic gates are broadly classified into three types as follows:
1.14 Explain the basic logic gates AND, OR, NOT gates with
truth tables.
The logic gates AND, OR and NOT are called basic logic gates.
1.14.1 AND Gate
An AND gate performs logical multiplication, more commonly known as the
AND function. It has two or more inputs and one output. The standard logic symbol for
a AND gate with two inputs A and B, and the output Y is shown in fig. 1.14.1(a) The
Boolean expression for AND function is Y = A ⋅ B.
A
Y = A⋅B
B
Fig. 1.14.1(a)
Working
1. The output of AND gate is low if any one input is low or all the inputs are low.
2. The output of AND gate is high if all the inputs are high.
The truth table for a two input AND gate is shown in fig. 1.14.1(b).
Inputs Output
A B Y=A.B
0 0 0
0 1 0
1 0 0
1 1 1
Fig. 1.14.1(b)
A
Y=A+B
B
Fig. 1.14.2(c)
Working
1. The output of an OR gate is high if any one input is high or all inputs are high.
2. The output of an OR gate is low if all the inputs are low.
The truth table for a two input OR gate is shown in fig. 1.14.2(d).
Inputs Output
A B Y = A+B
0 0 0
0 1 1
1 0 1
1 1 1
Fig. 1.14.2(d)
1.14.3 NOT Gate
1. The NOT gate is the simplest of all logic gates.
2. A NOT gate performs a basic logic function called inversion or complementation.
3. It has only one input and output.
4. The standard logic symbol for a NOT gate is shown in fig. 1.14.3(e).
5. The Boolean expression for NOT function is Y = A
A Y=A
Fig. 1.14.3(e)
Working
1. If a high level is applied to the input of the NOT gate, a low level appears on the
output.
1 0
Fig. 1.14.3(f)
Fig. 1.15.2(f)
*Note: The truth table for NOR gate is developed by inverting the output of the OR
gate.
Working
1. The output of NOR gate is low if any one input is high or all inputs are high.
2. The output of NOR gate is high if all the inputs are low.
0 1 1
1 0 1
1 1 0
From the truth table it can be seen that the output is high for unequal inputs and
output is low for equal inputs.
Fig. 1.18(a)
b) AND Gate by using NAND Gates
An AND gate can be obtained by connecting the output of a NAND gate to the
input of NOT gate as shown in fig. 1.18(b).
Fig. 1.18(b)
Fig. 1.18(c)
d) EX-OR Gate by using NAND Gates
An EX-OR gate by using NAND gates is shown in fig. 1.18(d).
Fig. 1.18(d)
The output expression of above circuit using is given by,
Y = A. AB B. AB
⎛ ⎞ ⎛ ⎞
= ⎜ A. AB ⎟ + ⎜ B. AB ⎟
⎝ ⎠ ⎝ ⎠
( ) (
= A A + B +B A + B )
= A A + A B + AB + BB
∴ Y = A B + AB = A ⊕ B
1.18.2. AND, OR, NOT using NOR gate
NOR gate is also called universal gate because it can also be used to construct an
AND gate, an OR gate, a NOT gate, EX-OR gate or any combination of these functions
as follows.
a) NOT Gate by using NOR Gate
As shown in fig. 1.18(e) if the two inputs of a NOR gate are connected together,
then we get NOT gate.
Fig. 1.18(e)
b) AND Gate by using NOR Gates
An AND gate can be produced by using three NOR gates as shown in fig. 1.18(f).
Fig. 1.18(f)
c) OR Gate by using NOR Gates
An OR gate can be produced by using two NOR gates as shown in fig. 1.86(g).
Fig. 1.18(g)
d) EX-OR Gate by using NOR Gates
An EX-OR gate by using NOR gates is shown in fig. 1.18(h).
Fig. 1.18(h)
The output expression of above circuit is given by,
Y =A+A+B+B+A+B =A+A+B+B+A+B
= A A + B + B A + B = A(A + B) + B(A + B)
= A A + AB + A B + BB ( QA ⋅ A = B ⋅ B = 0 )
∴ Y = A B + AB = A ⊕ B
Y = A + BC + D(E + F)
Step 1: Break the bar over the entire expression and change the sign (+ to .) between
the terms, A + BC and D(E + F)
(
Y = (A + BC) D E + F )
Step 2: Cancel the double bars over the left term:
Y = (A + BC)(D(E + F))
Step 3: Break the bar over the term D(E + F) and change the sign (. to +) between
D and E + F :
Y = (A + BC)(D + (E + F))
Step 4: Cancel the double bars over the term E + F : Y = (A + BC)(D + E + F)
Example 66: Express the following complement of product terms as sum of
complements using De-Morgan’s theorem:
a) ABC b) ABCD c) ABCDEF
Solution:
Use De-Morgan’s theorem as expressed in table: 2.2.
a) ABC = A + B + C
b) ABCD = A + B + C + D
c) ABCDEF = A + B + C + D + E + F
Example 67: Apply De-Morgan’s theorems to each expression.
a) (A + B) + C b) (A + B) + CD c) (A + B)CD + E + F
Solution:
a) (A + B) + C = (A + B).C = (A + B)C
(
b) (A + B) + CD = (A + B).CD = A + B CD )
(
c) (A + B)C D + E + F = [(A + B)CD] (E + F) = A B + C + D EF )
Maanya’s M.G.B Publications Digital Electronics
1‐52 Basics of Digital Electronics
Example 68: Apply De-Morgan’s theorems to each expression.
a) (A + B) + C b) (A + B) + CD c) (A + B)CD + E + F
Solutions: Oct/Nov-2013
a) (A + B) + C = (A + B).C = (A + B)C
b) (A + B) + CD = (A + B).CD = A + B CD ( )
c) (A + B)C D + E + F = [(A + B)CD] (E + F) = (A B + C + D )EF
Example 69: Simplify the following Boolean functions.
(a) Y= A B + AB (b) Y = A + BC + BC
Solutions:
a). Let Y = A B + AB = ⎛⎜ A B ⎞⎟ ⎛⎜ AB ⎞⎟ = (A + B) (A + B)
⎝ ⎠⎝ ⎠
=( A + B) (A + B) (Q A = A & B = B)
= A A + A B + AB + BB
∴ Y = AB + A B (Q A A = 0 & BB = 0)
b). Given ( )( )( ) (
Y = A + BC + B C = A BC BC = A B + C B + C )( )
( )( )
= A B + C B + C = (AB + AC)(B + C )
= A BB + A B C + ABC + ACC
= A B C + ABC (Q BB = 0 & CC = 0)
(
= A BC + B C )
∴ (
Y = A BC + B C )
Example 70: Simplify the following Boolean function:
Y= A AB ⋅ BAB
Solution:
Let Y= A AB BAB = A AB + BAB
= A AB + BAB
= A(A + B) + B(A + B)
= A A + A B + AB + BB
∴ Y = A B + AB (Q AA = 0 & BB = 0)
Maanya’s M.G.B Publications Digital Electronics
Basics of Digital Electronics 1‐53
(a) Y= A AB ⋅ BAB
Solution:
(a) Let Y= A AB BAB = A AB + BAB
= A AB + BAB
= A(A + B) + B(A + B)
= A A + A B + AB + BB
∴ Y = A B + AB (Q AA = 0 & BB = 0)
1.20. Classify different logic families.
• Digital circuits are invariably constructed with integrated circuits.
• An Integrated Circuit (IC) is a small silicon semiconductor crystal, called a chip,
containing electrical components such as transistors, diodes, resistors and
capacitors. The digital integrated circuits operate with binary signals and are made
up of interconnected digital gates.
• A number of circuit technologies are developed to manufacture the digital ICs.
Each circuit technology is referred to as a digital logic family.
• The classification of digital Logic families is shown in the fig. 1.1.
Fig. 1.1
The following points are worth noting regarding to the digital logic families.
Fig. 1.22(a)
b) +3.3V CMOS IC
• The input and output logic levels for a +5V CMOS IC is shown in the fig. 1.22(b).
Fig. 1.22(b)
• The ranges of input voltages (VIL) that can represent a valid LOW (logic 0) are from
0 to 0.8V.
• The ranges of input voltages (VIH) that can represent a valid HIGH (logic 1) are
from 2V to 3.3V.
• The ranges of output voltages (VOL) that represents a valid LOW (logic 0) are from 0
to 0.4V.
Maanya’s M.G.B Publications Digital Electronics
1‐56 Basics of Digital Electronics
• The ranges of output voltages (VOH) that represents a valid HIGH (logic 1) are from
2.4V to 3.3V.
*NoteThe ranges of values from 1.5V to 3.5V for 5V logic and 0.8V to 2V for 3.3V logic
are regions of unpredictable performance, and values in these ranges are unacceptable.
When an input voltages is in one of these ranges, it can be interpreted as either a HIGH
or a LOW by the logic circuit. Therefore, CMOS gates cannot be operated reliably when
the input voltages are in these unacceptable ranges.
Fig. 1.22(c)
+VCC
R R External
Pull-up
A Q Output(
B Y)
Q
Q
Open
Collector
Fig. 1.24.1
2. Operation
Case – 1: If A = 0 or B = 0 or A = B = 0
• If either A or B or both at low, then the base-emitter junction of Q1 is forward
biased and its base-collector junction is reverse biased.
• Thus there is a current through R 1 and the base-emitter junction of Q1 and into the
low input.
• There is no current into the base of Q 2 and hence Q 2 is in OFF.
• The emitter of Q 2 is at ground potential, keeping Q 4 into OFF.
• The output level will be high if an external load or resistor is connected between the
output and VCC.
Case – 2: If A = B = 1
• If both inputs are high, the base-emitter junction of Q1 is reverse biased and its
base-collector junction is forward biased.
Fig. 1.25.1
2. Operation
Case – 1: If A = 0 or B = 0 or A = B = 0
• If any of the two inputs or both are low, then the base-emitter junction of Q1 is
forward biased and its base-collector junction is reverse biased.
• Thus there is a current through R1 and the base-emitter junction of Q1 and into the
low input.
OBJECTIVES
Upon completion of the chapter the student should be able to Understand
the working of Combinational Logic circuits.
Fig. 2.1(a)
• The logic gates are the building blocks of Combinational Logic Circuits.
• Half adder, full adder, parallel adder, multiplexer, decoder, encoder, comparator,
etc are some other examples for combinational logic circuits.
b) Classification of Combinational Logic circuits:
2.2. Explain Half adder circuit using Ex-OR gate and an AND gate.
• Half adder is a combinational logic unit and it can add two bits at a time and
produce sum and carry.
• The logic symbol for half adder is shown in fig. 2.2(a). It consists of two inputs and
two outputs.
A Half Sum(S)
adder
B Carry (C0)
Fig. 2.2(a)
• The truth table shown in fig. 2.2(b) shows the logical operation of half adder.
Inputs Outputs
A B Carry( C0) Sum (S)
0 0 0 0
0 1 0 1
1 0 0 1
1 1 1 0
Fig. 2.2(b)
• Using the sum of products (SOP) method, we can write the expressions for sum(S)
and carry (C0) as follows,
Sum, S = A B + AB = A ⊕ B
Carry, C0 = A .B
• The fig. 2.2(c) shows logic circuit for a half adder using EX-OR and AND gates.
A
Sum= A ⊕ B
B
Carry =A.B
Fig. 2.2(c)
Fig. 2.3(a)
2.3.2. Half Adder using NOR Gates only
The fig. 2.3(b) shows the construction of half adder using 8 NOR gates.
Fig. 2.3(b)
=A + A + B+ B+ A + B
=A A+ B+B A+ B
= A (A + B) + B (A + B)
= A A + AB + A B + BB (Q A ⋅ A = B ⋅ B = 0 )
∴ Sum = A B + AB = A ⊕ B
Case – 2: Refer to fig. 2.3(b), Carry = A + B = A B = AB
Fig. 2.4(a)
• The truth table shown in fig. 2.4(b) shows the operation of full adder.
Inputs Outputs
A B C C0 S
0 0 0 0 0
0 0 1 0 1
0 1 0 0 1
0 1 1 1 0
1 0 0 0 1
1 0 1 1 0
1 1 0 1 0
1 1 1 1 1
Fig. 2.4(b)
Using the sum of products method, we can write the expression for sum (S) as:
Sum, S = A BC + AB C + A B C + ABC … (1)
(
= A B C + AB C + AB + A B C )
( )
= A B + AB C + ⎛⎜ A B + AB ⎞⎟C
⎝ ⎠
(
= A B + AB ⊕ C )
= (A ⊕ B) ⊕ C … (2)
This is simply EX-OR of A, B and C.
Consider now the output C0 in the truth table of fig. 2.4(b), we can write the sum
of products expression for C0 as,
C 0 = ABC + A BC + AB C + ABC … (3)
This expression can be simplified by factoring.
Carry, C0 = ABC + A BC + ABC + ABC + ABC + ABC
( ) ( )
= AB C + C + BC A + A + AC B + B ( )
=AB+BC+AC … (4)
Using equations (2) and (4), the logic circuit for full adder can be implemented as
shown in fig. 2.4(c).
Fig. 2.4(c)
Fig. 2.5(a)
Fig. 2.5(b)
Truth table of Full Adder
The truth table of a full adder which is constructed using two half adders and an
OR-gate as shown in the fig. 2.5(a) is shown in the fig. 2.5.
A B C Sum 1 Carry 1 Sum 2(final sum) Carry 2 C1+C2(final carry)
0 0 0 0 0 0 0 0
0 0 1 0 0 1 0 0
0 1 0 1 0 1 0 0
0 1 1 1 0 0 1 1
1 0 0 1 0 1 0 0
1 0 1 1 0 0 1 1
1 1 0 0 1 0 0 1
0 1 1 0 1 1 0 1
Fig. 2.5
Fig. 2.6
• This is repeated until all the bits of the two numbers are added.
C3 C2 C1 C0 Carry bits
A3 A2 A1 A0
+ B3 B2 B1 B0
C3 S3 S2 S1 S0
• One of the most serious drawbacks of this adder is that the delay increases linearly
with the bit length.
Additional Information
Fig. 2.7
• Fig. 2.7 shows 4-bit 2’s complement parallel adder/Subtractor circuit.
• It can add or subtract two 4-bit binary numbers. This is done by including EX-OR
gate with each full adder as shown in fig. 2.7. This adder/Subtractor circuit is
controlled by the control signal A/S .
• When A/S =0, the circuit acts as an adder, the number B3B2B1B0 is added to
A3A2A1A0 and gives Sum (S3S2S1S0).
Fig. 2.8
3 Time required for addition doesn’t depends on Time required for addition
number of bits. depends on number of bits.
4 It is faster. It is slower.
5 The registers used in this are storage registers The registers used in this
(Buffer registers). are shift registers.
Additional Information
MULTIPLEXER
Multiplexer means many into one. A multiplexer is a combinational logic circuit
with many inputs but only one output. The multiplexer acts like a digitally controlled
multi-position switch.
Fig. (a) shows the block diagram of a multiplexer with m(2n) input lines , one
output line Y, n control input lines.
The output will be one of the inputs D0 or D1 or D2 .....Dm-1 depending upon the
signals applied at control input lines.
A multiplexer with m inputs and 1 output is referred to as m X 1 multiplexer.
Examples: a) 4 x 1 multiplexer b) 8 x 1 multiplexer c) 16 x 1 multiplexer, etc.
Fig. (a)
Fig. 2.10(a)
Fig. 2.10(b)
Control inputs Output
S0 S1 Y
0 0 D0
0 1 D1
1 0 D2
1 1 D3
Fig. 2.10(c)
Additional Information
De-Multiplexer
Fig. (a)
De-multiplexer means one into many. The de-multiplexer (De-MUX) performs the
reverse operation to a multiplexer. It takes data from one line and distributes it over
number of output lines, hence the name data distributor.
The basic de-multiplexer with n control lines and m (=2n) output lines is shown in
fig. (a). The control input code determines to which output the data input will be
transmitted.
Fig. 2.13(a)
Fig. 2.13(b)
Control inputs Outputs
S0 S1 Y0 Y1 Y2 Y3
0 0 Din 0 0 0
0 1 0 Din 0 0
1 0 0 0 Din 0
1 1 0 0 0 Din
Fig. 2.13(c)
Fig. 2.16.
2.16.2. 3x8 Decoder (Binary to Octal Decoder)
1. Circuit Details
A digital circuit which converts binary to octal is called binary to octal decoder.
Fig. 2.16(a) shows logic symbol of binary to octal (or 3 x 8) decoder. It is also called 3 line
to 8 line decoder because there are 3 input lines and 8 output lines. Another name for it
is a 1 to 8 decoder because only 1 of 8 output lines is activated at a time.
Fig. 2.16(a)
Fig. 2.16(b)
As shown in fig. 2.16(b) 3 inputs are decoded into eight outputs, each output
representing one of the minterms of the 3 input variables. The three inverters provide
the complement of the inputs, and each one of eight AND gates generates one of the
minterms as follows.
2. Truth Table
The truth table for a 3 x 8 decoder is shown in fig. 2.16(c).
Inputs Outputs
A B C Y0 Y1 Y2 Y3 Y4 Y5 Y6 Y7
0 0 0 1 0 0 0 0 0 0 0
0 0 1 0 1 0 0 0 0 0 0
0 1 0 0 0 1 0 0 0 0 0
0 1 1 0 0 0 1 0 0 0 0
1 0 0 0 0 0 0 1 0 0 0
1 0 1 0 0 0 0 0 1 0 0
1 1 0 0 0 0 0 0 0 1 0
1 1 1 0 0 0 0 0 0 0 1
Fig. 2.16(c)
3. Operation
1. For instance, when the register contents are 010, the AND gate 3 has all high inputs,
therefore Y2 is high. The input content of 010 means the all other AND gates have at
least one low input. As a result, all other AND gates have low outputs.
2. If the register contents change to 011, only the AND gate 4 has all high inputs,
therefore only Y3 is high.
3. If the register contents change to 111, Y7 is the only high output.
4. In general, the subscript of the high output equals the decimal equivalent of the
binary number. This is why the circuit is called binary to octal decoder.
Fig. 2.18.(a)
Fig. 2.18(b)
2. Truth Table
Inputs Outputs
A B C D Y0 Y1 Y2 Y3 Y4 Y5 Y6 Y7 Y8 Y9
0 0 0 0 1 0 0 0 0 0 0 0 0 0
0 0 0 1 0 1 0 0 0 0 0 0 0 0
0 0 1 0 0 0 1 0 0 0 0 0 0 0
0 0 1 1 0 0 0 1 0 0 0 0 0 0
0 1 0 0 0 0 0 0 1 0 0 0 0 0
0 1 0 1 0 0 0 0 0 1 0 0 0 0
0 1 1 0 0 0 0 0 0 0 1 0 0 0
0 1 1 1 0 0 0 0 0 0 0 1 0 0
1 0 0 0 0 0 0 0 0 0 0 0 1 0
1 0 0 1 0 0 0 0 0 0 0 0 0 1
Fig. 2.18(c)
3. Operation
1. For instance, when the register contents are 0011, the Y3 AND gate has all high
inputs; therefore, Y3 is high.
2. Furthermore, register contents of 0011 mean that all other AND gates have at least
one low input. As a result, all other AND gates have low outputs. (Analyze the
circuit to convince yourself).
3. If the register contents change to 0100, only the Y4 AND gate has all high inputs;
therefore, only Y4 is high.
4. If the register contents change to 0111, Y7 is the only high output.
5. In general, the subscript of the high output equals the decimal equivalent of the
binary number stored in the register. This is why the circuit is called a binary to
decimal decoder.
Fig.(a)
a) Single Bit Comparator
• In a single bit comparator, let us assume the bits to be compared are A and B.
Case – 1:
• A is greater B if, A = 1 and B = 0
• From this statement we can write logic expression for A>B as,
• X = (A > B ) = A B …(1)
Case – 2:
• A is equal to B if the following condition is satisfied
• A = 1 and B = 1 (or) A = 0 and B = 0
• The logic expression for A = B can be written as,
• Y = (A = B) = A ⊕ B …(2)
Case – 3:
• A is less than B if, A = 0 and B = 1
• Form this statement we can write logic expression for A<B as,
X = (A < B ) = AB …(3)
Case – 4:
• The truth table for the comparator action is given in fig. (b).
A B X Y Z
A>B A=B A<B
0 0 0 1 0
0 1 0 0 1
1 0 1 0 0
1 1 0 1 0
Fig. (b)
Fig. (c)
OBJECTIVES
Upon completion of the chapter the student should be able to Understand
the working of Sequential Logic Circuits
3.1 Give the idea of Sequential logic circuits.
3.2 Explain NAND and NOR latches with truth tables
3.3 State the necessity of clock and give the concept of level clocking and edge triggering,
3.4 Draw and explain clocked SR flip flop with preset and clear inputs .
3.5 Construct level clocked JK flip flop using S-R flip-flop and explain with truth table
3.6 Explain race around condition and explain master slave JK flip flop.
3.7 Give the truth tables of edge triggered D and T flip flops and draw their symbols.
3.8 List any four applications of flip flops.
3.9 Define modulus of a counter
3.10 Draw and explain 4-bit asynchronous counter and its timing diagram.
3.11 Draw and explain asynchronous decade counter.
3.12 Draw and explain 4-bit synchronous counter.
3.13 Distinguish between synchronous and asynchronous counters.
3.14 State the need for a Register and list the four types of registers.
3.15 Draw and explain the working of 4 bit shift left and shift right registers
3.16 Draw and explain the working of 4-bit bi-directional shift register.
3.17 Draw and explain parallel in parallel out shift register
3.18 Explain the working of ring counter and list its applications
3.19 State memory read operation, write operation, access time, memory capacity, address lines
and word length.
3.20 Classify various types of memories based on principle of operation, physical characteristics,
accessing modes
and fabrication technology..
3.21 Explain basic principle of working of diode ROM
3.22 Distinguish between EEPROM and UVEPROM.
3.23 Explain the working of basic dynamic MOS RAM cell.
3.24 Compare static RAM and dynamic RAM
3‐2 Sequential Logic Circuits
Fig. 3.1
6. Sequential logic circuits are slower than the combinational logic circuits.
Additional Information
Fig. 3.1(a)
Fig. 3.1(b)
Fig. 3.2(a)
The logic symbol for active high input R-S flip-flop is shown in fig. 3.2(b).
Fig. 3.2(b)
Before going to analyze the R-S latch, we recall that a logic 1 at any input of a
NOR gate forces its output to a logic 0.
1. When R = 0 and S = 0, the output does not change and latch remains in its last state,
i.e. Q= Q0. This condition is called inactive state because nothing changes.
*Note: Here Q0 is previous state before applying inputs and Q is the output state after
applying inputs.
0 0 NC NC No change
0 1 1 0 Set
1 0 0 1 Reset
1 1 * * Forbidden
Fig. 3.2(c)
3.2.2. R-S latch using NAND Gates
Fig. 3.2(d) shows the R-S latch using two NAND gates. As shown in fig. 3.2(d), the
two NAND gates are cross coupled so that the output of NAND gate 1 is connected to
one of the inputs of NAND gate 2 and vice versa. The flip-flop has two outputs Q
and Q , and two inputs, Reset (R) and Set(S).
Fig. 3.2(d)
Before going to analyze the R-S latch, we recall that logic 0 at any input of a
NAND gate forces its output to logic 1.
1. A high on the S input will set the R-S latch, i.e. Q = 1 and Q = 0.
2. A high on the R input will reset it, i.e. Q = 0 and Q = 1.
3. If both R and S are high, the latch will remain in its previous state.
4. When both R and S inputs are low, the output is invalid.
The truth table for this latch is different from that for the NOR flip-flop. It is
shown in fig. 3.2(e).
Inputs Outputs
State
R S Q Q
0 0 * * Forbidden
0 1 1 1 Set
1 0 0 0 Reset
1 1 NC NC No change
Fig. 3.2(e)
Fig. 3.3(a)
• You can compare a clock with the traffic lights. They stop and allow the traffic in a
timely manner so that the traffic can flow smoothly with the least delays. If you just
let the traffic through there will be a big jam and the output is unpredictable.
• The clock signal is generally a rectangular pulse train or square wave as shown in
fig. 3.3(a).
• Digital systems can operate either synchronously or asynchronously.
• In synchronous system, the exact time at which any output can change states are
determined by clock.
• The output of a synchronous digital system can change states only when the clock
makes a transition. The transitions (also called edges) are pointed out in fig. 3.3.
• When the clock changes from 0 to 1, this is called the positive going transition
(PGT).
• Similarly when the clock goes from 1 to 0, this is the negative going transition
(NGT).
Fig. 3.4(a)
• Fig. 3.4(b) shows logic symbols of negative level clocked flip-flops.
Fig. 3.4(b)
3.4.2. Concept of Edge Triggering
• The term edge triggered means that the flip-flop changes state either at the positive
edge (rising edge) or at the negative edge (falling edge) of the clock pulse.
• Fig. 3.4(c) shows logic symbols of positive edge triggered flip-flops.
• Fig. 3.4(d) shows logic symbols of negative edge triggered flip-flops.
Fig. 3.4(c)
Fig. 3.4(d)
Level Clocked Flip-Flops versus Edge Triggered Flip-Flops
Sl.No. Level Clocked Flip-Flops Edge Triggered Flip-Flops
1 When the circuit is level clocked, the When a flip-flop is edge triggered, the
output can change while the clock is output can change only on the rising or
high or low. falling edge of the clock.
2 With level clocking, the output can With edge triggering, the output can
change during an entire half cycle of change only at one instant during the clock
the clock. cycle.
3 These flip-flops are rarely used. These flip-flops are widely used.
Fig. 3.5(d)
Fig. 3.7(a)
Maanya’s M.G.B Publications Digital Electronics
3‐10 Sequential Logic Circuits
The fig. 3.7(b) shows an alternate circuit diagram for J-K flip-flop.
1. Working
The functioning of the J-K flip-flop is identical to that of the R-S flip-flop, except
that it has no invalid state like that of the R-S flip-flop.
During the absence of clock pulse both the AND gates are disabled irrespective of
the inputs J and K. Thus the output remains in its previous state i.e. Q = Q0.
During the presence of clock pulse, the flip-flop operates as follows:
Fig. 3.7(b)
Case I: Inactive
1. When J = 0 and K = 0, no change of state takes place even if a clock pulse is
applied.
Case II: Reset
When J = 0 and K = 1, the upper AND gate (1) is disabled; so there is no way to set
the flip-flop. The only possibility is reset. When Q = 1, the output of lower AND gate (2)
becomes 1 upon application of a clock pulse. This forces Q to become low and the flip-
flop is reset. Thus the flip-flop is reset during a clock pulse only if Q was previously 1.
Case III: Set
When J = 1 and K = 0, the lower AND gate (2) is disabled; so it is impossible to
reset the flip-flop. But you can set the flip-flop as follows. When Q = 1, the output of
upper AND gate (1) becomes 1 upon application of a clock pulse. This forces Q to
become high and the flip-flop is set. Thus the flip-flop is set during a clock pulse only if
Q was previously 1.
Case IV: Toggle
When J and K are both high, it is possible to set or reset the flip-flop, depending
on the current state of the output. If Q is high, the lower gate passes a reset trigger on
the next positive clock pulse. On the other hand, when Q is low, the upper gate passes a
set trigger on the next positive clock pulse. Either way, Q changes to the complement of
the last state. Therefore, J = 1 and K = 1 means that the flip-flop will toggle on the next
positive clock pulse.
1 0 0 Q0 No change
1 0 1 0 Reset
1 1 0 1 Set
1 1 1 Q0 Toggle
Fig. 3.7(c)
Note: Toggle means switch to opposite state.
• The J-K flip-flop shown in fig. 3.7(b) is level clocked.
• If the total propagation delay ( Δt ) of the circuit is less than the duration of clock
pulse ( t p ) (when it is high), then the output will keep changing between 0 and 1
and finally the value of output will be ambiguous. This is known as the race
condition.
• Definition: Toggling the output more than once during a single clock pulse is called
race around condition.
3.7.2How to Avoid Race around Condition?
1. One method of avoiding race around condition is to use sharp clock pulse such
that t p < Δt .
[However it is difficult to set the clock period less than the propagation delay time because the
propagation delay is usually of the order of 20 ns]
2. Another method of avoiding race around condition is to make the flip-flop edge
triggered.
3. The master slave J-K flip-flop is more practical arrangement to avoid the race
around condition.
Fig. 3.8(a)
4. As shown in fig. 3.8(b) clock signal is connected directly to the master flip-flop, but
it is connected through inverter to the slave flip-flop. This implies that:
a) While the clock is high, the master is active and the slave is inactive.
b) While the clock is low, the master is inactive and the slave is active.
Fig. 3.8(b)
1. Working
Case I: When J = 1 and K = 0, the master sets on the positive clock. The high output Y of
the master drives the S input of the slave. Therefore at the negative clock, slave sets,
copying the action of the master.
Case II: When J = 0 and K = 1, the master resets on the positive clock. The high output
Y of the master goes to the R input of the slave. Therefore at the negative clock, slave
resets, again copying the action of the master.
Case III: When J = 1 and K = 1, master toggles on the positive clock and slave then
toggles on the negative clock.
2. Truth Table: The table shown in fig.3.8(c) summarizes the operation of J-K master
slave flip-flop.
Inputs Output
State
CLK J K Q
0 x x Q0 No change
0 1 0 Reset
1 0 1 Set
1 1 Q0 Toggle
Fig. 3.8(c)
*Note: The flip-flop responds for clock pulse instead of clock edge.
3. Timing Diagram
The timing diagram shown in the fig. 3.8(d) explains the operation of the master-slave
JK flip-flop.
Fig. 3.8(d)
3.9. Explain the level clocked D and T flip flops with the help
of truth table, circuit diagram and timing diagram
• D flip-flop may be obtained from clocked S-R flip-flop by connecting an inverter
(NOT gate) between S and R input terminals as shown in fig. 3.9(a).
3.9.1. Level Clocked D Flip-flop
1. Fig. 3.9(a) shows positive level clocked D flip-flop.
2. Unlike the S-R and J-K flip-flops, this flip-flop has only one synchronous control
input, D, which stands for data.
3. The NAND gates 1, 2, 3 and 4 form clocked S-R flip-flop.
4. The fifth NAND gate is used as an inverter to provide the complemented inputs.
Fig. 3.9(a)
a) Working
Case-I
As long as the CLK is at low level NAND gates 3 and 4 are disabled irrespective of
the value of D input. Thus the output remains in its previous state i.e. Q = Q0.
Case-II
When CLK is at high level, the input D controls the output Q as follows:
1. If D = 0, then S = 0 and R = 1 and therefore the output Q reset to logic 0.
2. If D = 1, then S = 1 and R = 0 and therefore the output Q set to logic 1.
Thus the output Q will be made equal to D only when the clock arrives, therefore,
the D flip-flop is also known as delay flip-flop.
The table shown in fig. 3.9(b) summarizes the operation of positive level clocked D
flip-flop.
Inputs Output
State
CLK D Q
0 x Q0 No change
1 0 0 Reset
1 1 1 Set
Fig. 3.9(b)
*Note: As shown in the truth table, the Q output follows the D input. For this reason D
latch is sometimes called transparent latch.
Fig. 3.9(c)
Fig. 3.9(c) shows a timing diagram. If the clock is low, the circuit is latched and the
Q output cannot be changed. While the clock is high, however, Q equals D; when D
goes high, Q goes high; when D goes low, Q goes low.
Note: The latch is transparent, meaning that the output follows the value of D while the
clock is high.
3.9.2. Level Clocked T Flip-Flop
In a J-K flip-flop if J = K the resulting flip-flop is referred to as a T flip-flop.
It has only one input referred to as a T-input. T flip-flop is also known as toggle
flip-flop. Generally T flip-flop IC’s are not available. It can be realized using J-K flip-
flops. Fig. 3.9(d) shows the realization of T flip-flop using J-K flip-flop.
Fig. 3.9(d)
1. Working
1. When CLK=0, both AND gates are disabled and hence there is no change in the
output.
2. For high CLK when T is low, J is low and K is low. Therefore the output is
remains unchanged.
3. For high CLK when T is high, J and K both are high and the output toggles.
The truth table for T flip-flop is shown in fig. 3.9(e).
Fig. 3.9(e)
2. Timing Diagram
Let a square wave is applied to the T input. The output wave forms Q and Q are
shown in fig. 3.9(f).
Fig. 3.9(f).
3.9.3. Symbols of D and T flip flops
The logic symbol of a D flip-flop is shown in the fig. 3.9(a).
Fig. 3. 9 (a)
Fig. 3. 9 (b) shows the logic symbols for positive level clocked T flip-flop.
Fig. 3. 9 (b)
Fig. 3.10(a)
Fig. 3.10(b)
• One way to make the flip-flop respond to only the edge of the clock pulse is to use
capacitive coupling. An R-C circuit is shown in fig. 3.10(b) which is to be inserted in
the clock input of the flip-flop.
• By deliberate design, the RC time constant is made much smaller than the clock
pulse width. The capacitor can charge fully when the clock goes HIGH. This
exponential charging produces a narrow positive spike across the resistor.
• Later, the trailing edge of the pulse results in a narrow negative spike.
• The circuit is so designed that one of the spikes (either the positive or negative) is
neglected and the edge triggering occurs due to the other spike.
Fig. 3.10(c)
• Fig. 3.10(c) shows a positive edge triggered D-latch, in which narrow positive and
negative spikes are obtained by a high pass R-C circuit of short RC time constant,
acting as a differentiator.
• The narrow positive spikes enable the input NAND gates 3 and 4, whereas the
narrow negative spikes do not produce any effect.
• The overall effect is to activate the input NAND gates 3 and 4 for a very short
duration of time during the narrow positive spike, due to which D and its
complement D are fed as inputs to the flip flop.
• This forces output Q to set to 1 or reset to 0.
• This type of triggering is called positive edge triggering because the clock is
changing the state from 0 (low) to 1 (high) on the positive edge of the clock.
1 x Q0 No change
↑ x Q0 No change
↓ 0 0 Reset
↓ 1 1 Set
Fig. 3.11(b)
3.11.2. Truth Table of Edge Triggered T Flip-flop
Case – 1: The table shown in fig. 3.11(c) summarizes the operation of the positive edge
triggered D flip-flop.
Inputs Output
State
CLK T Q
0 x Q0 No change
1 x Q0 No change
↓ x Q0 No change
↑ 0 Qn No change
↑ 1 Qn Toggle
Fig. 3.11(c)
Case – 2: The table shown in fig. 3.11(d) summarizes the operation of the negative edge
triggered D flip-flop.
Inputs Output
State
CLK T Q
0 x Q0 No change
1 x Q0 No change
↑ x Q0 No change
Maanya’s M.G.B Publications Digital Electronics
3‐20 Sequential Logic Circuits
↓ 0 Qn No change
↓ 1 Qn Toggle
Fig. 3.11(d)
4 3 2 1
Fig. 3.14(a)
3.14.1. Operation
• Before starting of the counting operation the clear pulse is applied, resetting all the
flip-flop’s to 0000.
Case - I:
• When the first clock pulse is given to the first flip-flop, it toggles the output from 0
to 1.
• The change in the output is applied as clock pulse for the second flip-flop. Since the
output changes from 0 to l, it constitutes a positive change and hence second, third
and fourth flip-flops are not affected.
• Since Q0 of flip-flop 1 represents the LSB, the outputs are read from left to
right, Q 3Q 2 Q1Q 0 .
10 1 0 1 0
11 1 0 1 1
12 1 1 0 0
13 1 1 0 1
14 1 1 1 0
15 1 1 1 1
16 0 0 0 0
Fig. 3.14(b)
3.14.2. Timing diagram
The timing diagram of a 4-bit asynchronous counter is shown in fig. 3.14(c).
Fig. 3.14(c)
4 3 2 1
Fig. 3.15(a)
3.15.1. Operation
• The decade counter counts same way as that of ripple counter up to 9th clock pulse.
• The tenth input pulse resets all flip-flops and the counter count becomes 0000.
• Initially, CLR goes low to produce: Q= 0000. When CLR returns to high, the counter is
ready for action.
• The output of the NAND gate is, Y = Q 3 Q1 and it is connected to the CLR input of each
flip-flop.
• This output is high for the first nine states (0000 to 1001). Nothing happens when
the circuit is counting from 0 to 9.
• On the tenth clock pulse, however, the Q becomes 1010(Q3Q2 Q1Q0). Which means
that Q3 and Q1 are high.
• Since Q3 and Q1 are connected to a NAND gate its output becomes low and hence
all the flip-flops are reset to 0000.
Clock pulse Flip-Flop outputs
(CLK) Q3 Q2 Q1 Q0
0 0 0 0 0
1 0 0 0 1
2 0 0 1 0
3 0 0 1 1
4 0 1 0 0
5 0 1 0 1
6 0 1 1 0
7 0 1 1 1
8 1 0 0 0
9 1 0 0 1
Fig. 3.15(b)
3.15.2 Timing diagram
The timing diagram of a asynchronous decoder counter shown in fig. 3.15(c).
Fig. 3.15(c)
4 3 2 1
Fig. 3.16(a)
3.16.1. Operation
• The counting sequence shows that the flip-flop Q0 toggles during each negative
going transition (NGT) of the clock pulse. For this reason its J0 and K0 inputs are
permanently connected to high.
• The counting sequence shows that the flip-flop Q1 toggles during each NGT of the
clock pulse only when Q0 =1.
• The counting sequence shows that the flip-flop Q2 toggles during each NGT of the
clock pulse only when Q0=Q1=1.
• In a like manner, we can see that the flip-flop Q3 has to toggle on each NGT that
occurs while Q0 = Q1 = Q2 =1.
Clock pulse Flip-Flop outputs
(CLK) Q3 Q2 Q1 Q0
0 0 0 0 0
1 0 0 0 1
2 0 0 1 0
3 0 0 1 1
4 0 1 0 0
5 0 1 0 1
6 0 1 1 0
7 0 1 1 1
8 1 0 0 0
9 1 0 0 1
10 1 0 1 0
11 1 0 1 1
12 1 1 0 0
13 1 1 0 1
14 1 1 1 0
15 1 1 1 1
16 0 0 0 0
Fig. 3.16(b)
3.16.2. Timing diagram
The timing diagram of a 4-bit synchronous counter is shown in fig. 3.16(c).
Fig. 3.16(c)
Sl.No.
Asynchronous Synchronous
2. All the flip-flops are not clocked All the flip-flops are clocked
simultaneously. simultaneously.
3. Logic circuit is very simple even for Logic circuit is complex as number
more number of states. of states increases.
4. Cost is low. Cost is high.
7. The speed of counters depends not only The speed of this counter depends
on the width of clock pulse but also on on the width of clock pulses
the propagation delay time of each flip- applied.
flop.
Fig. 3.18.1
1. Registers are commonly used for the temporary storage of data
2. within a digital system.
3. The storage capability of a register is one of its two basic functional characteristics
and makes it an important type of memory device.
Fig. 3.18.2
1. The shifting capability of a register permits the movement of data from stage
to stage within the register or into or out of the register upon application of
clock pulses.
2. Fig. 3.18.2 shows symbolically the types of data movement in shift register
operations.
3. The block represents any arbitrary 4-bit register, and the arrows indicate the
direction and type of data movement.
c) Types of Registers
In registers data may be moved in and out serially or parallelly. Depending upon this,
shift registers may be classified as:
1. Serial In and Serial Out (SISO) shift register
2. Serial In and Parallel Out (SIPO) shift register
Additional Information
Types of registers
1. Serial In Serial Out (SISO) Shift Register
In this case all the bits are loaded in and shifted out is bit by bit or serially as
shown in fig. (a).
Fig. (a)
It may be noted that each bit is moved in and/or out for each clock pulse. This is
as shown in fig. (a).
*Note:
1. Data shifting may be from right to left or left to right.
2. If the data is shifted from left to right, the shift register is called shift right register.
3. If the data is shifted from right to left, then the shift register is called shift left
register.
4. A SISO register introduces time delay in data transmission.
5. The IC 7491 is an example of SISO shift register.
2. Serial In Parallel Out (SIPO) Shift Register
In this case all the bits are loaded in is bit by bit (or serially), and shifted out
simultaneously i.e., at a time all the data bits are taken out. This is as shown in fig. (b).
Fig. (b)
*Note: The IC 74164 is an example of SIPO shift register.
Fig. (c)
4. Parallel In Parallel Out (PIPO) Shift Register
Fig. (d)
3.19. Draw and Explain the working of 4-bit shift left and
shift right registers with timing diagram
This type of shift register accepts data serially, i.e. one bit at a time, and also
output data serially.
Fig. (a)
3. With four stages, i.e. 4-flip-flops, the register can store up to four bits of data. Serial
data is applied at the D0 input of the first flip-flop through the input Din.
4. The Q0 output of the first flip-flop is connected to the D1 input of the second flip-
flop, the Q1 output of the second flip-flop is connected to the D2 input of the third
flip-flop and the Q2 output of the third flip-flop is connected to the D3 input of the
fourth flip-flop.
5. The data is outputted from the Q3 terminal of the last flip-flop.
*Note:
1. When serial data is transferred into a register, each new bit is clocked into the first
flip-flop at the positive going edge of each clock pulse.
2. The bit that was previously stored by the first flip-flop is transferred to the second
flip-flop is transferred to the third flip-flop, and so on.
3. The bit that was stored by the last flip-flop is shifted out.
Operation:
Method - I
In order to understand the working of the shift right register, let us assume that
we want to store the data 1101 in the register. This data will be applied at the input
(D0 ) of the first flip-flop one bit after another. First we shall apply the LSB (1) to D 0 .
1. At the positive going edge of the first clock pulse, the LSB bit 1 sent to the output of
the first flip-flop and hence Q 0 becomes 1. Therefore,
Q0 = 1, Q1 = 0, Q2 = 0 and Q3 = 0,
or
Q = Q0 Q1 Q2 Q3 = 1000 after the first clock pulse
Fig. (b)
Fig. (c)
Fig. (d)
Operation:
Method - I
In order to understand the working of the shift left register, let us assume that we
want to store the data 1101 in the register. This data will be applied at the input (D 3 ) of
the last flip-flop one bit after another. First we shall apply the MSB (1) to D 3 .
Fig. (e)
Method - II
Let Din = 1and Q = 0000.
1. All data inputs except the one of the right are 0s. The arrival of the first positive
clock edge sets the right flip-flop, and the stored word becomes,
Q = 0001
2. This new word means D1 now equals 1, as well as D0. When the next positive clock
edge hits, the Q1 and Q0 flip-flops sets and the register content becomes,
Q = 0011
3. The third positive clock edge results in,
Q = 0111
Fig. (f)
Fig. (a)
Case - II
When the Right/ Left signal is low, the AND gates 1, 3, 5 and 7 are disabled and
the AND gates 2, 4, 6 and 8 are enabled. The Q output of each flip-flop is passed
through the D input of the preceding flip-flop. When a positive going edge of clock
pulse arrives, the data are shifted one place to the left.
Thus a high on the Right/ Left line enables the shifting of data towards right and
a low enables it left. A high on this line enables the shifting of data towards right and a
low enables it towards left.
2. Timing Diagram
The timing diagram shows the operation the Bi-directional shift register
which initially shifts data towards the left. At interval t 5 , the register is configured to
shift right and at t 8 towards left and again towards right at interval t14 . A logic 1 is
Fig. (a)
Fig. (b)
Fig. (a)
Its sequence table and timing diagram are shown in fig. (b) & (c).
Clock pulse Outputs
Q0 Q1 Q2 Q3
Initial state 1 0 0 0
1 0 1 0 0
2 0 0 1 0
3 0 0 0 1
4 1 0 0 0
Fig. (c)
1. Operation
Method - I
1. In most instances, only a single 1 is in the register and is made to circulate around
the register as long as clock pulses are applied.
2. Initially, the first flip-flop is preset to a 1. So, the initial state is 1000, i.e. Q0 = 1, Q1 =
0, Q2 = 0 and Q3 = 0.
3. After each clock pulse, the contents of the register are shifted to the right by one bit
and Q3 is shifted to Q0. The sequence repeats after four clock pulses.
Method-II
1. When PRE goes low then back to high, the initial output word is,
Q = 1000
2. The first negative clock edge shift the MSB into the LSB position; the other bits shift
one position. Therefore, the output word becomes,
Q = 0100
3. The second negative clock edge causes another rotate right and the output word
changes to,
Q = 0010
4. After the third negative clock edge, the output word is,
Solution:
5M × 8 = 5 × 1,048,576 × 8 = 41,943,040 bits
1M × 16 = 1,048,576 × 16 = 16,777,216 bits
The 5M × 8 memory stores more bits.
6. Density: Another term for capacity. When we say that one memory device has a
greater density than another, we mean that it can store more bits in the same
amount of space.
7. Address: A number that identifies the location of a word in memory. Each word
stored in a memory device or system has a unique address. Addresses are always
expressed as a binary number are often used for convenience.
8. Read Operation: The operation whereby the binary word stored in a specific
memory location (address) is sensed and then transferred to another device. Simply
the process of retrieving (getting) the information is known as read operation. The
read operation is often called a fetch operation, since a word is being fetched from
memory. We will use both terms interchangeable.
9. Write Operation: The operation whereby a new word is placed into a particular
memory location. Simply the process of storing the information is known as read
operation. It is also referred to as a store operation. Whenever a new word is
written into a memory location, it replaces the word that was previously stored
there.
10. Access Time: A measure of a memory device’s operating speed. It is the amount of
time required to perform a read or write operation.
3.25. Differentiate:
i) Read Only Memory & Read write Memory
ii) Sequential access memory & Random Access Memory
Sl.No. RAM ROM
1. RAM stands for Random Access ROM Stands for Read Only
Memory. Memory.
2. It allows to perform both read and write It allows to perform read operation
operations. only.
3. It is a volatile memory i.e., whenever It is a non-volatile memory.
power is cut off the information will be
lost.
4. These memories are used to store the These memories are used to store the
data temporarily as user memory. data that are used repeatedly in
system applications such as tables,
conversions.
5. There are 2 types of RAM’s. They are: They are different types of ROM’s.
Static RAM and Dynamic RAM. They are: PROM, EPROM,
UVPROM and EEPROM.
6. • Static RAM: A static RAM contains • PROM: PROM stands for
more transistors and a capacitor to Programmable Read Only
store a bit. A static RAM can occupy Memory. These ROM’s can be
very large space. It is used for used by manufactures to store
specialized applications. the micro programs.
• EPROM: EPROM stands for
• Dynamic RAM: A dynamic RAM Erasable Programmable Read
contains one transistor and one Only Memory. The contents of
capacitor to store a bit. A transistor ROM can be erased by
acts as ON and OFF switch. A ultraviolet light.
capacitor is used to store a bit. • EEPROM: EEPROM stands for
Electrically Erasable
Programmable Read Only
memory.
7. RAM is a temporary memory. ROM is a permanent memory.
Fig. (a)
• A read only memory (ROM) is the simplest kind of memory.
• It is equivalent to a group of registers, each permanently storing a word.
• By applying control signals, we can read the word in any memory location. (“Read”
means to make the contents of the memory location appear at the output terminals
of the ROM).
1. Construction
• Fig. (a) shows one way to build a ROM.
• Each horizontal row is a register or memory location.
• The R0 register contains three diodes; the R1 register has one diode, and so on.
• The output of the ROM is the word, D = D3D2D1D0.
2. Working
Case - I
• In switch position 0, a high voltage turns on the diodes in the R0 register; all other
diodes are off. This means that a high output appears at D2, D1 and D0.
• Therefore, the word stored at memory location 0 is, D = 0111.
Case - II
Fig.
• Dynamic RAM stores the data as a charge on the capacitor. Fig. shows the dynamic
RAM cell.
• A dynamic RAM contains thousands of such memory cells.
• Since only a single MOSFET and capacitor are needed, the dynamic RAM contains
more memory cells as compared to static RAM per unit area.
• The disadvantage of dynamic RAM is that it needs refreshing of charge on the
capacitor after every few milliseconds. This complicates the system design, since it
requires the extra hardware to control refreshing of dynamic RAMs.
In this type of cell, the transistor acts as a switch. The basic simplified operation is
illustrated in fig.. As shown in the fig., the circuit consists of three tri-state buffers: input
buffer, output buffer and refresh buffer. Input and output buffers are enabled and
disabled by controlling R/ W line.
¾ When R/ W line is LOW, input buffer is enabled and output buffer is disabled.
¾ When R/ W line is HIGH, input buffer is disabled and output buffer is enabled.
With this basic information let us see the read, write and refresh operations.
1. Write Operation
To enable write operation R/ W line is made LOW which enables input buffer and
disabled output buffer, as shown in the fig. (a).
• To write a 1 into the cell, the DIN line is HIGH, and the transistor is turned ON by a
HIGH on the ROW line. This allows the capacitor to charge to a positive voltage.
Fig. (c)
2. Read Operation
To read data from the cell, the R/ W line is made HIGH, which enables output
buffer and disables input buffer. Then ROW line is made HIGH. It turns transistor ON
and connects the capacitor to the DOUT line through output buffer. This is illustrated in
fig. (c).
Maanya’s M.G.B Publications Digital Electronics
3‐52 Sequential Logic Circuits
3. Refresh Operation
To enable refresh operation R/ W line, ROW line and Refresh line are made
HIGH. This turns ON transistor and connects capacitor to COLUMN line. As R/ W is
HIGH, output buffer is enabled and the stored data bit is applied to the input of refresh
buffer. The enabled refresh buffer then produces a voltage on COLUMN line
corresponding to the stored bit and thus refreshing the capacitor as shown in fig. (d).
Fig. (d)
OBJECTIVES
Fig. 4.6(a)
1. CPU (Microprocessor)
CPU is a general purpose microprocessors such as Intel’s X86 family or Motorola’s
680×0 family.
The CPU of 8051 is capable in performing arithmetic and logical operations like
additions, subtraction, multiplication, division, logical AND, OR, EX-OR, rotate, clear
and complement. This CPU can do bit wise as well as byte wise manipulations. It works
based on the code written in the ROM.
2. ROM/RAM Memory
It is a fixed amount of on chip or internal memory. It stores programs and data
temporally during program execution. In other words the program codes are stored in
the ROM and the data could be stored in RAM.
Fig. 4.6(b)
3. I/O Ports
The 8051 consist of 4 I/O ports: port0, port 1, port 2 and port3. These are used
for interfacing I/O devices like switches, keyboard, LED/LCD, ADC, DAC, etc and also
for any other input/output operations. The external address and data buses are formed
only by using port lines.
4. Serial Port
Computers transfer data in two ways: parallel and serial. In serial communication
the data is sent one bit at a time. The 8051 has one serial port for this purpose. It is full
duplex hence it can transmit and receive simultaneously.
Fig.4.7
4.7.1. General purpose registers
The 8051 has 34 general purpose registers. They are Accumulator, Register B
and remaining 32 registers are arranged a part of the internal 128 byte RAM. All these
registers are programmable registers.
Fig. 4.7.1
GPRs Function
1. R0- • The lowest 32 bytes (address 00H to 1FH) of the on-chip RAM form 4
R7 banks.
• There are 128 bytes of RAM in the 8051.
• The lowest 32 bytes (address 00H to 1FH) of the on-chip RAM form 4
banks.
• The 4 register banks are numbered 0 to 3. Each bank is made up of 8
registers named R0 to R7. These are used to store data temporarily.
• RAM locations from 00H to 07H are set aside for bank 0 of R0-R7 where
R0 is RAM location 00H, R1 is RAM location 01H, R2 is location 02H,
and so on, until memory location 07H, which belongs to R7 of bank 0.
• The second bank of registers R0-R7 starts at RAM location 08H and goes
to location 0FH.
• The third bank of R0-R7 starts at memory location 10H and goes to
location 17H.
• Finally, RAM locations 18H to 1FH are set aside for the fourth bank of
R0-R7.
• The fig. a shows how the 32 bytes are allocated into 4 banks.
• Note that only one register bank can be accessed by 8051 at a time.
• The bits RS1 (PSW.4) and RS0 (PWS.3) of program status word (PSW)
Do You Know?
• While multiplying, it holds one of the 8-bit operands and after the
execution of the multiplication instruction; it stores the lower byte of the
result.
• While dividing, it holds an 8-bit dividend and after the execution of
division instruction, the quotient is stored in A-register.
3. B • It is an 8-bit register.
• It is used for multiplication and division along with the accumulator.
Do You Know?
• While multiplying, it holds one of the 8-bit operands and after the
execution of the multiplication instruction; it stores the higher byte of the
result.
• While dividing, it holds an 8-bit divisor and after the execution of
division instruction, the remainder is stored in B-register.
4.7.2. Special purpose registers
The microcontroller contains four special purpose registers. They are
1. Program counter
2. Stack Pointer
3. Data Pointer
4. Program Status Word
Maanya’s M.G.B Publications Microcontrollers
4‐12 8051 Microcontroller
SPRs Function
1. PC • The program counter (PC) is a 16-bit register.
• It is used to hold the address of next instruction to be fetched.
• The PC is automatically incremented to point the next instruction in
the program sequence after execution of the current instruction.
• The PC is the only register that does not have an internal (on-chip)
RAM address.
2. SP • A group of memory locations in RAM is referred to as stack. The stack
can store data as well as address during execution of a program.
• The 8-bit stack pointer (SP) register is used by the 8051 to access the
stack. It holds the address of top of the stack.
• After the RESET operation, the stack pointer is initialized to 07H,
causing the stack to begin at 08H.
• When data is stored on to the stack, the SP value is incremented by 1.
Similarly while retrieving the data from the stack; the SP value is
decremented by 1.
• The data is pushed onto the stack using instruction PUSH and the
data is retrieved using instruction POP.
3. DPTR • The DPTR register is made up of two 8-bit registers named DPH and
DPL as shown in fig. 4.7.2.
Fig. 4.7.2
• Its function is to hold a 16-bit address.
• The data pointer is used for addressing the off-chip data and code
with the MOVX and MOVC commands, respectively.
4. PSW • PSW (Program Status Word) is an 8-bit register. It consists of 4 flags
and 2-bits for bank selection of internal RAM.
Additional Information
Program Status Word (Flag Register)
• Program Status Word or simply PSW register is one of the most important SFRs.
• It is an 8-bit register. It consists of:
o 4 conditional flags that set /reset automatically to the outcomes of math
operations (CY, AC, OV & P).
o 2 Register bank select bits (RS1 & RS0).
o A general purpose flag (F0).
• Fig. 4.7.3 shows the bit pattern of the program status word.
Fig.4.7.3
a) Conditional Flags
1. Carry Flag (CY)
• Carry flag is set if there is a carry from bit 7.
• It is used in arithmetic, jump, rotates, and Boolean instructions.
2. Auxiliary Carry Flag (AC)
• Auxiliary Carry flag (AC) is set when there is a carry out of 3rd bit, during addition
or subtraction operation and otherwise cleared.
• Auxiliary Carry Flag is used for BCD operations only.
3. Overflow Flag (OV)
• The overflow flag is set if there is a carry from bit 7, but not from bit 6, or there is a
carry from bit 6, but not from bit 7 otherwise it is cleared.
Fig. 4.8
Fig. 4.8.1(a)
Bit Symbol Function
7 - Not used (reserved for future).
6 - Not used (reserved for future).
5 PT2 Not used (reserved for future).
4 PS Serial port interrupt priority bit
(Set to ‘1’ for having highest priority)
3 PT1 Timer 1 interrupt priority bit
(Set to ‘1’ for having highest priority)
2 PX1 External interrupt 1 priority bit
(Set to ‘1’ for having highest priority)
1 PT0 Timer 0 interrupt priority bit
(Set to ‘1’ for having highest priority)
0 PX0 External interrupt 0 priority bit
(Set to ‘1’ for having highest priority)
*Note:
1. The priority bit may be set to 1 (highest) or 0 (lowest).
o 0 → Low priority
o 1 → High priority
2. Bit addresses: B8H to BFH
Fig. 4.8.1(b)
Bit Symbol Function
7 EA Set to ‘0’ to disable all interrupts.
Set to ‘1’ to control all the interrupts individually.
6 - Not used (reserved for future).
5 ET2 Not used (reserved for future).
4 ES SET to ‘1’ to enable Serial port interrupt.
3 ET1 SET to ‘1’ to enable Timer 1 interrupt.
2 EX1 SET to ‘1’ to enable External interrupt 1.
1 ET0 SET to ‘1’ to enable Timer 0 interrupt.
0 EX0 SET to ‘1’ to enable External interrupt 0.
Fig. 4.8.3(a)
Symbol Function
GATE Gate control.
Fig. 4.8.3(b)
Bit Symbol Function
7 TF1 Timer 1 Overflow Flag. This bit is automatically set by hardware on
the Timer 1 overflow. Cleared when interrupt processed.
6 TR1 Timer 1 Run control bit.
Set/cleared by software to turn timer/counter on/off.
5 TF0 Timer 0 Overflow Flag. This bit is automatically set by hardware on
the Timer 0 overflow. Cleared when interrupt processed.
4 TR0 Timer 0 Run control bit. Set/cleared by software to turn
timer/counter on/off.
3 IE1 Interrupt 1 Edge Flag. Set by hardware when INT1 pin detects high to
low transition. Cleared when interrupt processed.
2 IT1 Interrupt 1 type control bit. Set /cleared by software to specify
falling edge/low level triggered external interrupts.
1 IE0 Interrupt 0 Edge Flag. Set by hardware when INT0 pin detects high to
low transition. Cleared when interrupt processed.
0 IT0 Interrupt 0 type control bit. Set /cleared by software to specify
falling edge/low level triggered external interrupts.
*Note:
1. All flags can be set by the indicated hardware action.
2. The flags are cleared when interrupt is serviced by the processor.
4.8.4. Serial Port SFRs
a) SCON (Serial Control) SFR
b) SBUF (Serial Data Buffer) SFR
c) PCON (Power Control) SFR
Fig. 4.8.4(a)
Bit Symbol Function
7 SM0 Serial port Mode control bit 0. Set/Cleared by software.
6 SM1 Serial port Mode control bit 1. Set/Cleared by software.
5 SM2 Serial port Mode control bit 2. It also known as multiprocessor
communication enable bit. For most applications 8051 is not used in
multiprocessor environment. Hence SM2=0.
4 REN Receiver Enable control bit. Set/Cleared by software to enable/disable
serial data reception.
3 TB8 Transmit Bit 8. It is the 9th bit that will be transmitted in modes 2 and 3.
It is set or cleared by software as desired.
2 RB8 Receiver Bit 8. In modes 2 and 3 this is the 9th bit that was received
(Cleared by hardware if 9th bit received is logic 0. Set by hardware if 9th
bit received is logic 1). In mode 1 if SM2 = 0, RB8 is the stop bit that was
received. In mode 0 RB8 is not used.
1 TI Transmit interrupt flag. Set by hardware when byte transmitted.
Cleared by software after servicing.
0 RI Receive interrupt flag. Set by hardware when byte received. Cleared
by software after servicing.
*Note: Bit address 98H to 9FH
b) SBUF (Serial Data Buffer) SFR
• Computers must be able to communicate with other computers in modern
multiprocessor distributed systems. One cost-effective way to communicate is to
Fig. 4.8.4(b)
Bit Symbol Function
7 SMOD Double baud rate bit.
6,5,4 - Not implemented.
3 GF1 General purpose user flag bit 1 set/clear by program.
2 GF0 General purpose user flag bit 0 set/clear by program.
1 PD Power down bit.
0 IDL Idle mode bit.
*Note: Bit addresses C8H to CFH
4.8.5. Parallel I/O Ports SFRs
1. In 8051 microcontroller, there are 4 ports for I/O operation. They are Port 0, Port 1,
Port 2 and Port 3.
2. Each I/O port takes 8 pins. In the pin diagram of 8051 microcontroller, a total of 32
pins (4 × 8 = 32) are occupied by the 4 I/O ports.
3. All 4 ports are bidirectional, i.e., each pin will be configured as input or output (or
both) under software control.
4. The functions of 4 ports are listed in below table.
Port Functions
Port • Used as an I/O Port
0 • Used as a bi-directional low-order address and data bus for external
memory
Port • Used as an input/output Port
1
Port • Used as an input/output port
2 • Used as a higher-order address bus for external memory.
Port • Used as an input/output Port or used for alternate function as shown
3 below.
P3.0 – RXD : Serial data input
P3.1 – TXD : Serial data output
P3.2 – INTO : External interrupt 0
P3.3 – INT1 : External interrupt 1
P3.4 – T0 : External timer 0 input
P3.5 – T1 : External timer 1 input
P3.6 – WR : External memory write signal
P3.7 – RD : External memory read signal
Additional Information
Special Function Registers
SFRs along with their direct addresses are listed in table.
Sl.No. Symbol Name Address
1. *ACC Accumulator 0E0H
2. *B B Register 0F0H
3. *PSW Program Status Word 0D0HS
4. SP Stack Pointer 81H
5. DPL Data pointer low byte 82H
4.9. Draw the pin diagram of 8051 micro controller and specify
the purpose of each pin.
1. The 8051 is packaged in a 40-pin DIP (Dual in line package).
2. It is important to note that many pins of 8051 are used for more than one function.
3. The alternative functions of pins are shown in parenthesis.
4. The 16 pins are used for single function. The remaining 24 pins may be used for
one of two entirely different functions (24 x 2 =48 functions) as shown in fig. 2.85.
5. Fig. 4.9 shows the pin diagram of 8051.
Fig. 4.9(a)
The description of each pin as follows.
Pin 1 - 8: Port 1
• A total of 8 pins named as P1.0 to P1.7 are belonging to port1.
• These pins can be used as input or output.
Pin 9: Reset input (RST)
• A logic one (high) on this pin resets the microcontroller.
• By applying logic zero to this pin, the program starts execution from the beginning.
Pin 10 - 17: Port 3
• Similar to port 1, each of these pins can serve as general input or output.
• In addition these lines provide alternative functions as listed in below table.
Maanya’s M.G.B Publications Microcontrollers
4‐26 8051 Microcontroller
Pin No. Designation Function
10 P3.0 (RXD) Serial data input.
11 P3.1 (TXD) Serial data output.
12 P3.2 (INT0) External interrupt 0 input.
Pin 31: EA
• External Access is an active low input pin.
• EA = 0 : Program code bytes can be fetched exclusively from an external ROM
memory addresses 0000H to FFFFH.
• EA = 1 : Program code from address 0000H to 0FFFH is fetched from internal ROM
and remaining code where address is 1000H to FFFFH is from external ROM
memory.
Pin 32 - 39: Port 0
• Similar to P2, if external memory is not used, these pins can be used as general
inputs/outputs.
• Otherwise this port provides low-order address (A7 - A0) and data bus (D7 – D0)
for external memory.
Pin 40: VCC
• 8051 operates on d.c power supply of +5V with respect to ground.
• The +5V is to be connected to pin VCC.
Additional Information
Pin diagram of 8051
The fig. (a) shows pin diagram of 8051.
• The description of each pin as follows:
1. Power Supply Pins
• Pin number 40 and 20 are used as power supply pins.
• In which VCC (+5) is given to the 40th pin and ground (VSS) is connected to 20th
pin.
Fig. 4.9(b)
Maanya’s M.G.B Publications Microcontrollers
4‐28 8051 Microcontroller
2. Crystal Pins
• The 8051 has an on-chip oscillator but it needs an external clock to activate it. A
quartz crystal is connected between XTAL1 (19) and XTAL2 (18). These two pins
are input pins.
3. Port Pins
• The 8051 consists four ports and each port needs 8-pins. The ports are Port 0,
Port1, Port 2 and Port 3. The function of each port is given below.
• Port 0: Pin No. 32 to 39 is assigned to port 0. These pins are bi-directional pins
(can be used as input or output). Port 0 is also the multiplexed low-order address
and data bus during accesses to external code and data memory. This port needs
pull-up resistors.
• Port 1: Port 1 requires 8 pins (pins 1 to 8). It can be used for input or output. This
port need not require pull-up resistors.
• Port 2: Pin No.21 to 28 is assigned to port 2. It can be used for input or output.
This port does not require any pull-up resistors. Port 2 is also used for higher order
address pins (A8-A15)
• Port 3: Pin No.10 to 17 assigned to port 3. These pins can use for input or output.
Port 3 has the additional function of providing some external important signals
such as P3.0 and P3.1 are used for the RXD and TXD. These two pins are used for
serial communication signals. P3.2 and P3.3 are used for external interrupts (INT0
and INT1). Pins P3.4 and P3.5 are used for timers (T 0and T1). Finally, P3.6 and
P3.7 are used to provide control signals RD and WR .
4. RESET
• Pin no.9 is used as reset pin. It is an input pin. When this pin active high, the
controller reset and terminate all activities. Hence this is referred as a power-on
reset.
5. EA
• EA means external access.
• The pin no. 31 is assigned for EA .
• It is an input pin and it must be connected to either Vcc or GND.
• If this pin is connected to the Vcc, the controller access the internal ROM, if this pin
is connected to the GND, the controller access the external ROM.
6. PSEN
• PSEN means “program strobe enable”.
• This is an output pin.
• Pin no.29 is assigned to PSEN .
Maanya’s M.G.B Publications Microcontrollers
8051 Microcontroller 4‐29
• This pin is used to control the external memory.
• This pin is connected to OE pin of the ROM.
7. ALE
• Pin no.30 is assigned to ALE.
• ALE means address latch enable.
• This is an output pin.
• This pin is used to de-multiplex the data line from the address lines.
Fig 4.10
• A functioning computer must require memory for program and data. ROM is used
for program code and data could be stored in RAM.
• Code memory is the memory that holds the actual program that is to be run. Data
memory is the memory space where data are stored.
• The 8051 has internal RAM and ROM memory for these functions. Additional
memory can be added externally using suitable circuits.
4.10.1. Internal Memory (or) On-chip memory
• 8051 has 128 bytes of RAM for data memory and 4K bytes of ROM for code
memory inside the IC chip.
• These memories are called internal memory or on chip memory.
FIG 1.10.1(a)
• The fig. 4.10.1(a) shows the organization of internal RAM.
• The 128-bytes of internal RAM with address space from 00H to 7FH is divided into
three groups as follows:
a) Working registers (32)
• The lowest 32 bytes (address 00H to 1FH) of the internal RAM form 4 banks of 8
registers each.
• The 4 register banks are numbered 0 to 3.
• Each bank is made up of 8 registers named R0 to R7.
• Only one register bank can be accessed by 8051 at a time.
• Default bank (On reset) is bank 0 (locations 00 – 07H).
• The bits RS1 (PSW.4) and RS0 (PSW.3) of program status word (PSW) are used to
select any one of the four register banks.
b) Bit Addressable area
• The 8051 provides 16 bytes of a bit-addressable area.
• It occupies RAM byte addresses from 20H to 2FH, forming a total of 128 (16 x 8)
addressable bits.
Fig 4.10.1(b)
2. Internal ROM (i-ROM)
• The 8051 has 4 Kbytes of internal ROM with address space from 0000H to 0FFFH.
• This is used to store final version of the program codes hence the name program
memory.
Note: It is programmed by manufacturer when the chip is built. This part cannot be
erased or altered after fabrication.
Fig 4.10.1(c)
EA = VCC EA = 0
Fig. 4.11
• Let EA pin is at VCC means that upon reset the 8051 access the on-chip RAM first
(0000 H – 0FFF H) and when reaches (to 0FFF H) end of the on-chip ROM it
switches to off-chip ROM (1000H – FFFFH). This type of accessing is widely used in
practice.
• If EA pin is grounded then only external (off-chip) ROM of 64 KB space can be
accessed by the Program Counter.
Do You Know?
External data memory:
1. To access external data memory , microcontroller use the Pins RD or WR.
2. The external data memory is accessed by using DPTR.
3. The external data memory is accessed using indirect addressing mode only.
4. To access the external data memory MOVX instruction is used.
5. MOVX means Move External. This instruction will transfer data between the
accumulator and a byte of external data memory.
6. Access to external data memory can use either one byte address [@ Ri where Ri can
be either R0 or R1 of the selected register bank] or two byte address [@ DPTR].
7. Accumulator is always the destination or source during external data RAM accesses.
External program memory:
Fig 4.12
Note: The ports P1, P2 and P3 do not require any pull-up resistors since they already
have pull-up resistors internally.
2. Port 1 (P1)
• It is designated as P1; the bits of port 1 are designated as P1.0 to P1.7.
• Port 1 has mono function i.e. it is used as input or output port.
Fig 4.13(a)
1. 8051 has two 16-bit timer/counters, designated as Timer 0(T0) and Timer 1(T1).
They can be used as timers or counters.
2. Since the 8051 is an 8-bit controller, the 16-bit timer/counter registers are divided
into two 8-bit registers called the timer low (TL0, TL1) and high (TH0, TH1) bytes.
Fig.4.13(b)
Fig. 4.13(c)
3. A timer always counts up. It doesn’t matter whether the timer is being used as a
timer, a counter, or a baud rate generator: A timer is always incremented by the
microcontroller.
4. Two SFRs TMOD (Timer Mode Control SFR) and TCON (timer control SFR) are
used to monitor the nature of working of timer/counters.
5. The timers can be configured to operate either as timers or event counters. The
timer/counter mode is selected by the control bit C/ T in the special function
register TMOD.
6. Counters are used to count the events where as timers are used to maintain time
delays between the actions.
7. In timer mode, it will count the internal clock frequency of the 8051 divided by 12 d.
The register is incremented for every machine cycle.
8. In counter mode, the register is incremented in response to 1 to 0 transition at its
corresponding external input pins T0 and T1 .
Fig. 4.13.1(a)
• Internal clock pulses are divided by 12 counter and fed to one input of AND gate.
• Whenever TR is set by the program the counting starts. When the count reaches
1FFFH (8192D) the overflow flag sets.
Note: If a 13-bit timer has an initial value of 0000, the overflow flag will set to 1 after
8192 machine cycles.
b) Mode 1 Operation
• In this mode timer can acts as 16 bit timer.
• Here both the registers TL and TH are used to hold the 16-bit value. TL is
incremented from 0 to 255. When TL reaches 255 it resets and causes TH to be
incremented by 1. Thus both the registers are used effectively to hold 16-bit value.
Fig. 4.13.1(b)
TF will set when timer rolls over from FFFF to 0000.
c) Mode 2 Operation
Fig. 4.13.1(c)
• The mode 2 operation is called 8-bit auto reloaded mode. In this mode TH holds the
“reloaded value” and TL is the timer itself.
• The timer register TL is loaded with value between 00 and FF. Once TL is loaded
with a value, a copy of this value will be stored in TH.
• When the timer operation starts TL starts counting up from the set value. Once TL
value reaches FF, the initial value stored in TH will be automatically reloaded into
the TL register and the TL starts its operation once again automatically.
Fig 4.15
OBJECTIVES
5.1 State the need for an instruction set.
5.2 Describe the instruction format of 8051.
5.3 Explain fetch cycle, execution cycle and instruction cycle.
5.4 Define the terms machine language, assembly language, and mnemonics.
5.5 Give the difference between machine level and assembly level programming.
5.6 List the major groups in the instruction set along with examples.
5.7 Explain the terms operation code, operand and illustrate these terms by writing an instruction.
5.8 Explain the data manipulation functions data transfer, arithmetic, logic and branching.
5.9 Classify the 8051 instructions into one byte, two byte and three byte instructions.
5.10 Describe the five addressing modes of 8051.
5.11 Explain data transfer instructions of 8051.
5.12 Explain the arithmetic instructions and recognise the flags that are set or reset for given data
conditions.
5.13 Explain the logic instructions and recognize the flags that are set or reset for given data conditions.
5.14 Explain unconditional and conditional jump and how flags are used to change the sequence of
program.
5.15 Write programs of instructions to perform single byte, double byte and multi byte addition and
subtraction.
5.16 Illustrate the application of jump instruction in the program.
5.17 Define a subroutine and explain its use.
5.18 Explain the sequence of program when subroutine is called and executed.
5.19 Explain how information is exchanged between the program counter and the stack and identify the
stack pointer register when a subroutine is called.
5.20 Write program to perform Single byte & Multi byte addition.
5.21 Write program to sum up given N‘numbers.
5.22 Write program to multiply two 8-bit numbers using MUL‘instruction.
5.23 Write program to find biggest data value in given Data array.
5.24 Write program to convert a given HEX‘ number to BCD‘ number.
5‐2 8051 Instruction set & Programming
5.2. Write the instruction format of 8051 & illustrate these terms
by writing an instruction.
• A set of rules defining the way the operands, data and addresses are arranged in an
instruction is known as instruction format.
• An instruction is a command given to the microcontroller to perform a given task on
specified data. Each instruction has two parts as shown in fig. 5.2.
• Each instruction has two parts consists of two parts, they are opcode and operand
.The fig. shows the general format of the instruction.
Fig. 5.2
1. Operation code (op-code)
• The part of the instruction that specifies the operation to be performed is called the operation
code or op-code.
2. Operand
• The data on which the operation is to be performed is called the operand.
• Operand consists two fields, such as Destination and Source.
• The destination field may be a register or an address or port /port address or a
memory location. Destination field specifies the destination for the data that is
being copied form the source.
• Let us consider an instruction CPL A. The meaning of this instruction is
complementing the contents of accumulator. In this instruction CPL is op-code and
accumulator (A) is an operand.
Fig. 5.3.(a)
*Note:
The fetch operation can be explained by a simple algorithm.
1. Begin
2. Send the address of an instruction to memory.
3. Read the instruction from the memory.
Maanya’s M.G.B Publications Microcontrollers
5‐4 8051 Instruction set & Programming
4. Transfer the instruction to the microcontroller.
5. END
2. Execute Cycle (EC)
It is the total time required to decode the instruction fetched and execute it. If the
operand is in memory, execution is immediately performed.
Note that if an instruction contains data or operand and address which are still in
the memory, the processor has to perform one or two memory read operations to get the
desired data. A typical execute cycle is shown in the fig. 5.3(b).
The time required for an execute cycle will depends on the instruction type.
Fig. 5.3(b)
*Note:
It is explained by a simple algorithm.
1. Begin
2. Decode the instruction fetched
3. If operand is in memory, fetch the operand
4. Execute the instruction
5. End.
3. Instruction Cycle
It is the time required for the microcontroller to complete the execution of an
instruction. An instruction cycle consists of a fetch cycle and execute cycle i.e. IC = FC +
EC.
Fig. 5.3(c)
Fig. 5.4
The time taken to execute a machine cycle is 12 clock periods and so time taken to
execute an instruction is obtained by multiplying the number of machine cycles of that
instruction by 12 clock periods.
1
Instruction execution time = C × 12 × T = C × 12 ×
f
Where, C = Number of machine cycles of an instruction
T = Time period of crystal frequency in seconds
f = Crystal frequency in Hz.
*Note:
1. If the oscillator frequency is 12 MHz, then the time taken to execute one
machine cycle is 1 microsecond.
2. An 11.0592 MHz crystal yields a cycle frequency of 921.6 kilohertz, which can
be divided evenly by the standard communication baud rates of 19200, 9600,
4800, 2400, 1200 and 300 Hz.
3. The 8051 microcontroller has four machine cycles and they are:
a) External program memory fetch cycle
b) External data memory read cycle
c) External data memory write cycle
d) Port operation cycle.
Maanya’s M.G.B Publications Microcontrollers
5‐6 8051 Instruction set & Programming
4. In fig. 5.4 there are two ALE pulses per machine cycle. The ALE pulse, which is
primarily used as a timing pulse for external memory access, indicates when
every instruction byte is fetched.
5. Two bytes of single instruction may thus be fetched, and executed, in one
machine cycle.
6. Single byte instructions are not executed in a half cycle, however single byte
instructions “throw away” the second byte (which is first of the next
instruction). The next instruction is then fetched in the following cycle.
5.5. Explain the timing diagram for memory write & Memory read
operation of 8051
1. Interfacing External Program (ROM) Memory
In 8051 when the EA pin is connected to VCC , program fetches to addresses
0000H through 0FFFH are directed to the internal ROM and program fetches to
addresses 1000H through FFFFH are directed to external ROM/EPROM. On the other
hand when EA pin is grounded, all addresses (0000H to FFFFH) fetched by program are
directed to the external ROM/EPROM. The PSEN signal is used to activate output
enable signal of the external ROM/EPROM, as shown in fig. 5.5(a).
Fig. 5.5(a)
Fig. 5.5(b)
Maanya’s M.G.B Publications Microcontrollers
Instruction set of 8051 Microcontroller 5‐7
The circuit arrangement for connecting external program memory is shown in fig.
5.5(a).
• The port 0 is used as a multiplexed address/data bus. It gives lower order 8-bit
address in the initial T-cycle and later it is used as a data bus.
• The 8-bit address is latched using external latch and ALE signal generated by 8051.
• The port 2 provides the higher order 8-bit address.
• The timing waveforms for external memory read cycle, is as shown in fig. 5.5(b).
2. Interfacing External Data (RAM) Memory
Fig. 5.5(c)
Fig. 5.5(d)
8051 can address up to 64 Kbytes of external data memory. The “MOVX” instruction is
used to access the external data memory.
The circuit arrangement for connecting external data memory is shown in fig.
5.5(c).
• The multiplexed address/data bus provided by port 0 is demultiplexed by external
latch and ALE signal.
Maanya’s M.G.B Publications Microcontrollers
5‐8 8051 Instruction set & Programming
• Port 2 gives the higher order address bus.
• The RD and WR signals from 8051 selects the memory read and memory write
operation, respectively.
• Now the timing waveforms of external data memory for read and write cycles are as
shown in fig. 5.5(d) and fig. 5.5(e) respectively.
Fig. 5.5(e)
5.9. Explain the terms operation code and operand and illustrate
these terms by writing an instructions
Explain one byte, two byte and three byte instructions of 8051.
• The fig. shows the general format of the instruction. It consists of two parts, they
are opcode and operand.
Fig.
• Opcode: operation code. It is the portion of a instruction, that specifies the
operation to be performed.
• Operand: The second part of the instruction is called the operand, indicating the
information needed by the instruction in carrying out its task. Operand consists
two fields, such as Destination and Source.
• The destination field may be a register or an address or port /port address or a
memory location. Destination field specifies the destination for the data that is
being copied form the source.
• Based on length the 8051 instruction set is classified into the following 3 groups:
1. One byte instructions - 49
2. Two byte instructions - 45
3. Three byte instructions - 17
a. One byte instructions
A one byte instruction includes the op-code and operand in the same byte. Thus
these instructions occupy one memory location as shown in fig. 5.9(a).
Fig. 5.9(a)
Fig. 5.9(b)
The first byte always indicates the op-code and second byte is either immediate
data, direct address, bit address or relative address of the operand. This is illustrated as
shown in the below table.
Two byte instruction format Examples
Fig. 5.9(c)
The first byte indicates the op-code. The second and third bytes may indicate
address and/or data. This is illustrated as shown in the below table.
Three byte instruction format Examples
Op-code immed data15-8 immed data7-0
MOV DPTR,#data16
*Note:
Except arithmetic group of instructions, remaining group of instructions will have
one or two instructions of three byte length.
PUSH It is an invalid instruction because push and pop Instructions must be use
A direct addressing mode. PUSH 05EH is the valid instruction.
3. Register Addressing mode
Register addressing mode involves the use of registers to hold the data to be
manipulated.
In this mode the data, which the instruction operates on, A,B, DPTR or is in one of
eight registers labeled R0 to R7 (Rn, in general).
Additional Information
THE 8051 INSTRUCTION SET
The Inlet 8051 has excellent and most powerful instruction set offers possibilities in
control area, serial I/O, arithmetic, byte and bit manipulation. It has 111 instructions.
The most widely used registers of the 8051 are A (address is 0E0, B (0F0), R0, R1,
R2, R3, R4, R5, R6, R7. DPTR [DPH (83). DPL (82)]. PC (No address) SP (81).
Based on the operation performed by the instruction, the instruction set is divided
into (i) Data move (ii) Arithmetic, (iii) Logic (iv) Call and Jump.
The following symbols and abbreviations are used in the subsequence description
of 8051 instructions.
Acronyms
Rn Register R7-R0 of the currently selected Register Bank.
direct 8-bit internal data location’s address. This could be an Internal Data RAM
location (0-127) or a SFR [i.e.,I/O port, control register, status register, etc.,
(128-255)].
@Ri 8-bit internal data RAM location (0-255) addressed indirectly through
register R1 or R0
#data 8-bit constant included in instruction
# data 16 16-bit constant included in instruction
addr 16 16-bit destination address. Used by LCALL & LJMP. A branch can be
anywhere within the 64K- byte Program Memory address space.
addr 11 11-bit destination address. Used by ACALL & AJMP. The branch will be
within the same 2K-byte page of program memory as the first byte of the
following instruction.
rel Signed (two’s complement) 8-bit offset byte. Used by SJMP and all
conditional jumps. Range is-128 to +127 bytes relative to first byte of the
following instruction.
1. This group of instructions copies data from one location called source to another
location called destination.
2. In this operation source content remains unchanged and destination contains the
content of source.
Maanya’s M.G.B Publications Microcontrollers
5‐18 8051 Instruction set & Programming
3. The data transfer may be between A ↔ registers, register ↔ internal memory
location, external data (immediate data) to a register or internal memory location
and A ↔ external memory location.
4. During the execution of data transfer instructions “no flags are affected”.
1. MOV A, Rn
Description • Move the contents of register Rn into accumulator.
• Rn presents register from R0 to R7 of currently selected bank.
Operation (A) ← (Rn)
Example MOV A, R2
2. MOV A, # data
Description Move an immediate data into accumulator.
Note:
1. Immediate data is always presented by # in the instruction
2. The immediate data in this instruction is always an 8-bit.
3. Immediate data can never be a destination.
Operation (A) ← data
Example
3. MOV A, direct
Description The content of memory location (internal RAM/SFR) whose
address is specified directly in the instruction is moved to
accumulator.
Operation (A)←(direct = 8-bit address of i-RAM/SFR)
Example MOV A, 40H: The contents of memory location 40 H are moved
into accumulator.
4. MOV A, @Ri
Description Move the contents of internal RAM memory location whose
address is specified by Ri to accumulator.
Note: In this indirect addressing mode, the registers used are
only R0 and R1.
Operation (A)←((Ri))
Example
5. MOV Rn, A
Description Move the contents of accumulator into register Rn.
Operation (Rn) ← (A)
Example MOV R5, A
Maanya’s M.G.B Publications Microcontrollers
5‐20 8051 Instruction set & Programming
8. MOV direct, A
Description The content of accumulator is moved to a memory location
(internal RAM/SFR) whose address is directly specified in the
instruction.
Operation (direct) ← (A)
Example MOV 40H, A: The content of accumulator is moved to memory
location 40 H.
9. MOV direct,Rn
Description The content of register Rn is moved to memory location (internal
RAM/SFR) whose address is directly specified in the instruction.
Operation (direct) ← (Rn)
Examples MOV 55H, R7
Note:
1. When stack reaches to FF H it rolls over to 00 H.
2. RAM ends at 7F. So pushing the stack contents above 7F
will result in errors.
3. Stack should be normally initialized above the register
banks.
4. Only address and not register names are used in the
instruction.
Operation (direct) ← ((SP))
(SP) ← (SP ) − 1
Example POP 0E0H
This instruction copies the contents of the internal RAM location
addressed by the stack pointer to the accumulator (0E0H). Then
the stack pointer is decremented by one.
24. XCH A, Rn
Description • XCH stands for exchange.
• Exchange data bytes between register Rn and A.
Note: All exchanges are internal to 8051 and with register A only
(accumulator)
Operation (A) ↔ (Rn)
Example Initially assume Accumulator = 20H and Register R5=45H. After
executing the instruction XCH A, R5 the accumulator =45H and
Instructions Description
Operation
2. ADD A, #data
Description An immediate 8 bit number is added with contents of accumulator and
result is stored in accumulator.
Operation (A) ← (A) + data
Example
3. ADD A, direct
Description The contents of internal RAM location specified is added with
accumulator and result is stored in accumulator
Operation (A) ← (A) + (direct)
Example
4. ADD A, @Ri
Description The content of memory location whose address is in register Ri is added
with contents of register A and result is stored into accumulator.
Operation (A) ← (A) + ((Ri))
Example
5. ADDC A, Rn
Description Add contents of accumulator, register Rn and carry and store the result
into accumulator.
Operation (A) ← (A) +(CY)+ (Rn)
Example ADDC A, R5
6. ADDC A, # data
Description Add accumulator, immediate data (8-bit) and carry and store the result
into accumulator.
Operation (A) ← (A) + (CY) + data
Example ADDC A, #5
A = 01000001
CY = 0
R5 = 0000 0101
After executing the instruction: ADDC A, #5
A = 01000110; CY = 0
7. ADDC A, direct
Description Add accumulator, contents of RAM address () and carry and store the
result into accumulator.
Operation (A) ← (A) + (CY) + (direct)
Example ADDC A, 41H
A = 00100011; CY = 1;
Location 41H = 00100000
After executing the instruction: ADDC A, 41H
A = 01000100 = 44H; CY = 0
9. SUBB A, Rn
Description The contents of register Rn and a carry (borrow) flag are subtracted from
accumulator and result is stored in accumulator.
Operation (A) ← (A) - (CY) - (Rn)
Example
INC A
Description The content of accumulator is incremented by 1.
Operation (A) ← (A) + 1
Example: 1 Assume initially A = 40H. After executing INC A instruction, the
accumulator will contain 41H.
INC Rn
Description The content of specified register is incremented by 1.
Operation (Rn) ← (Rn) + 1
Example Assume initially R5 = 22H. After executing INC R5, the register R5 will
have 23H.
INC direct
Description The content of RAM/SFR (whose address is directly given in the
instruction) is incremented by 1.
Operation (direct) ← (direct) + 1
Example Assume initially the internal memory location 40H contains data 50H.
After executing INC 40H, the location will have the updated value 51H.
INC @Ri
Description The content of memory location whose address is stored in Ri is
incremented by 1.
Operation ((Ri)) ← ((Ri)) +1
Example Assume the pointer R0 contains 23H and the internal memory location
23H contains 40H. After executing INC@R0, the internal memory location
23H will have the updated value 41H.
INC DPTR
Description The content of 16-bit DPTR (Data Pointer) is incremented by 1.
Operation (DPTR) ← (DPTR) +1
Example: 1 Registers DPH and DPL contain 14H and FFH respectively. The execution
of INC DPTR instruction will change DPH and DPL to 15H and 00H
respectively.
DCR A
Description The content of accumulator is decremented by 1.
Operation (A) ← (A) -1
Example Let us assume A = 42H. After executing the instruction DEC A, the
accumulator will contain 41H.
DCR Rn
Description The content of specified register is decremented by 1.
Operation (Rn) ← (Rn) - 1
Example Let us assume R5 contains 45H. After executing the instruction DEC R5,
the register R5 will contain 44H.
DCR direct
Description The content of RAM/SFR (whose address is directly given in the
instruction) is decremented by 1.
Operation (direct) ← (direct) -1
Example Assume initially the internal memory location 41H contains data 51H.
After executing DEC 41H, will have the location will have the updated
value 50H.
DCR @Ri
Description The content of memory location whose address is stored in Ri is
decremented by 1.
Operation ((Ri)) ← ((Ri)) - 1
Example Assume the pointer R1 contains 24H and the internal memory location
24H contains 56H. After execution of DEC@R1, the internal memory
location 24H will have the updated value 55H.
MUL AB
Description Multiply two unsigned numbers stored in accumulator and register B.
Register B is for such purposes only. The lower byte of the result is
stored in accumulator and higher byte in register B.
Operation Low order byte in A←(A) × (B)
High order byte in B
Example 1: MOV A, # 5d
MOV B, # 7d
MUL AB ; A = 35d = 23H, B= 0 0
MOV A, #25H
Example 3: MOV B, # 78H
MUL AB ; A = 58H B=11H, CY = 0 and OV = 1
; (25H x 78H = 1158H)
MOV A, #100d
Example 4: MOV B, #200d
MUL AB ; A = 20H B=4EH, OV = 1 and CY = 0
; (100 x 200 = 20,000 = 4E20H)
Note:
1. When A = FFH; B = FFH. The largest possible product is obtained
i.e., FE01H.
2. Register A contains 01H and register B contains FEH after
multiplication of FFH by FFH.
3. The OV flag is set to al that register B contains the high-order-byte
of the product, the Carry flag is 0.
4. There is no comma between A and B in the MUL mnemonic.
DIV AB
Description The unsigned number in accumulator is divided by the unsigned
number in register B. The integer part of the quotient is in register A and
integer part of remainder is in B.
Note:
1. The Carry flag (CY) is always cleared.
2. The overflow flag (OV) is set if division by 0 was attempted,
otherwise it is cleared.
Operation AB ← (A)/(B)
(A) ← Quotient
(B) ← Remainder
Example 1: MOV Ax 35d
MOV B, #10d
DIV AB ; A = 3d and B = 5d
DAA
Description Decimal Adjust Accumulator after addition. When we add two numbers
the result is in binary, to adjust the result in BCD (Decimal) DAA is used
followed by ADDI or ADDC. As we know that after adding, if the lower
four bits is greater than 09 then 06 is added, if upper four bits greater
than 09 then 60 is added to adjust the result in BCD
(Take example of 8085 DAA instruction) CY, AC and OV flag area
affected.
Operation If (A3-0)>9 or (AC)=1 then(A3-0) ← (A3-0) + 06
If (A7-4)>9 or (CY)=1 then(A7-4) ← (A7-4) + 06
Example 1: Example (i): If A = 23; B = 14. After addition the result is 37. In this case
BCD addition and hexadecimal addition results are same.
IF A = 23; B = 48. After addition the result is 6B. This is the hexadecimal
Example 2: result where as BCD result is 7.
In order to obtain BCD result there is an instruction available labeled as
DAA instruction. A DAAA instruction is useful in decimal arithmetic.
The performance of DAA instructions is given below.
Maanya’s M.G.B Publications Microcontrollers
Instruction set of 8051 Microcontroller 5‐39
Addition Instructions
1. ADD A,Rn (A) ← (A)+(Rn) Add register to Accumulator.
2. ADD (A) ← (A) + data Add immediate data to Accumulator.
A,#data
3. ADDC A,Rn (A) ← (A) +(CY)+ (Rn) Add register to Accumulator with carry.
Subtraction Instructions
4. SUBB A,Rn (A) ← (A) - (CY) - (Rn) Subtract Register from ACC with borrow.
5. SUBB (A) ← (A) - (CY) - data Subtract immediate data from ACC with
A,#data borrow.
Incrementing & Decrementing Instructions
6. INC A (A) ← (A) + 1 Increment Accumulator.
7. DEC A (A) ← (A) - 1 Decrement Accumulator.
8. INC DPTR (DPTR) ← (DPTR) +1 Increment Data Pointer.
Multiplication & Division Instructions
9. MUL AB Low order byte in A←(A) × (B) Multiply A and B.
High order byte in B
5.13. Explain the logical instructions and recognize the flags that
are set or reset for given data conditions.
1. This group of instructions perform various logical operations AND, OR, EX-OR,
rotate, clear and complement.
2. For the instructions RLC, RRC, and CPL only carry flag is affected.
3. For the instruction CLR the carry flag is set to 0.
4. These instructions use one of the four addressing modes each: register, immediate,
direct and indirect.
5. The A register or a direct address in internal RAM is the destination of the logical
operation result.
6. Keep in mind that all such operations are done using each individual bit of the
destination and source bytes. These operations, called byte-level Boolean operations.
ANL A, Rn
Description The content of register Rn is ANDed with accumulator and result is
stored in accumulator.
Operation (A) ← (A) AND (Rn)
Maanya’s M.G.B Publications Microcontrollers
5‐42 8051 Instruction set & Programming
Example: 1 A = 00100010; R0 = 00100010
ANL A, R0
After execution A ⇐ 00100000
Example: 2
ANL A, direct
Description The content of accumulator is ANDed with the content of RAM location
whose address is specified in the instruction. The result is stored in
accumulator.
Operation (A) ← (A) AND (direct)
Example: 1 A = 00100010 & Location 41 = 00110100
ANL A, 41H
After execution A ⇐ 00100000
The content of location 41H remains same.
Example: 2
ANL A, @ Ri
Description The content of accumulator is ANDed with the content of memory
location whose address is stored in either R0 or R1 register. The result is
stored in accumulator.
Operation (A) ← (A) AND ((Ri))
Example A = 00110010
ANL A, # data
Description An immediate data is ANDed with contents of accumulator and result is
stored in accumulator.
Operation (A) ← (A) AND data
Example A =32H= 00110010
Data =31H= 00110001
ANL A, #31H
After execution of this instruction, A ⇐ 00110000 ⇐ 30H
ANL direct, A
Description The content of accumulator is ANDed with direct memory location
contents and result is stored in direct memory location.
Operation (direct) ← (direct) AND (A)
Example Acc = 01000001 = 41H;
Location 23 = 00110010 = 32H
ANL 23H, A
After execution of this instruction, the location 23H contains zero.
In this instruction, the specified internal memory location 23H is the
destination.
ORL A, Rn
Description The content of register Rn is ORed with accumulator and result is stored
in accumulator.
Operation (A) ← (A) OR (Rn)
Example Acc = 01011010
R1 = 10110000
ORLA, R1
After execution of this instruction Acc = 11111010 = FA.
ORL A, direct
Description The content of accumulator is ORed with the content of RAM location
whose address is specified in the instruction. The result is stored in
accumulator.
Operation (A) ← (A) OR (direct)
Example Acc = 01011100 = 5C
Direct = 43
Content of 43 = 00001100 = 0C
ORLA, 43
After execution of this instruction A becomes 0101100 = 5C.
ORL A, @ Ri
Description The content of accumulator is ORed with the content of memory location
whose address is stored in either R0 or R1 register. The result is stored in
accumulator.
Operation (A) ← (A) OR ((Ri))
Example: 1 Acc = 10101010; R0 = 43
Content of RAM address 43 = 00001000
ORLA, @ R0
After execution of this instruction, A=10101010 = AA
Example: 2
ORL A, # data
Description An immediate data is ORed with contents of accumulator and result is
stored in accumulator.
Operation (A) ← (A) OR data
Example: 1 Acc = 01001100 = 4C
Data n = 01000011 = 43
ORL A, # 43
After execution of this instruction A becomes 01001111 = 4F
Example: 2
ORL direct, A
Description The content of accumulator is ORed with direct memory location
contents and result is stored in direct memory location.
Operation (direct) ← (direct) OR (A)
Example A = 67H = 0110 0111
RAM address F0 content = 44H = 0100 0100
ORL F0, A
After executing this instruction F0 = 01100111 = 67H
XRL A, Rn
Description The content of register Rn is XORed with accumulator and result is
stored in accumulator.
Operation (A) ← (A) XOR (Rn)
Example XRL A, R5
XRL A, direct
Description The content of accumulator is XORed with the content of RAM location
whose address is specified in the instruction. The result is stored in
accumulator.
Operation (A) ← (A) XOR (direct)
Example XRL A, 75H
XRL A, @Ri
Description The content of accumulator is XORed with the content of memory
location whose address is stored in either R0 or R1 register. The result is
stored in accumulator.
Operation (A) ← (A) XOR ((Ri))
Example XRL A, @R0
XRL A, # data
Description An immediate data is XORed with contents of accumulator and result is
stored in accumulator.
Operation (A) ← (A) XORD data
Example XRL A, 0FH
XRL direct, A
Description The content of accumulator is XORed with direct memory location
contents and result is stored in direct memory location.
Operation (direct) ← (direct) XOR (A)
Example
CLR A
Description • Clear accumulator.
• All eight bits of accumulator are cleared to zero.
Operation (A) ← 0
Example Suppose A = 52 H, then after CLR A, A = 00 H
RL A
Description • Rotation accumulator left.
• Rotate one bit in A (i.e. accumulator) left. This means A7 comes in
A0, A0 in A1, A1 in A2 and so on.
RLC A
Description • Rotate accumulator left through the carry flag.
• Rotate accumulator left by one bit through carry. This means A7 goes
into carry, carry into A0, A0 to A1, A1 to A2 and so on.
RR A
Description • Rotate accumulator right.
• Rotate register A contents by one bit towards right. This means A0
goes into A7, A7 into A6, A1 into A0 and so on.
RRC A
Description • Rotate accumulator right through carry flag.
• Rotate contents of accumulator right by one bit through carry. A0
goes into carry; carry into A7, A7 to A6 and so on.
SWAP A
Description • Swap nibbles within the accumulator.
• Interchange the upper 4 bits (nibbles) and lower four bits in the
accumulator. This means D7 to D4 will go at D3 to D0 and D3 to D0
will go in place of D7 to D4.
Operation (A3-0) ↔ (A7-4)
Example: 1 Suppose A = 7D then after swap A = D7
Example: 2
13. XRL A,Rn (A) ← (A) XOR (Rn) Exclusive-OR register to Accumulator.
14. XRL A,direct (A) ← (A) XOR (direct) Exclusive-OR direct byte to Accumulator.
15. XRL A,@Ri (A) ← (A) XOR ((Ri)) Exclusive-OR indirect RAM to Accumulator.
1. In 8051, these instructions are more powerful and can perform clear, complement,
set, move operations on bit wise rather than on type of given data.
2. Certain internal RAM (bytes 20 to 2F) and SFRs (A, B, IE, IP, P0, P1, P2, P3, PSW,
TCON, and SCON) can be addressed by their byte addresses or by the address of
each bit within a byte.
3. The bit-level Boolean logical op-codes operate on any addressable RAM or SFR bit.
4. The carry flag (CY) in the PSW special function register is the destination for most of
the op-codes because the flag can be tested and the program flow is changed using
instructions.
7. ANL C,bit (C) ← (C) AND (bit) AND direct bit to carry.
8. ANL (C) ← (C) AND bit AND complement of direct bit to
C, bit carry.
9. ORL C,bit (C) ← (C) OR (bit) OR direct bit to carry.
10. ORL C, (C) ← (C) OR bit OR complement of direct bit to
bit carry.
11. MOV C,bit (C) ← (bit) Move direct bit to carry.
12. MOV bit,C (bit) ← (C) Move carry to direct bit.
13. JC rel (PC) ← (PC)+2 Jump if carry is set.
If C= 1 then,
(PC) ← (PC)+rel
14. JNC rel (PC) ← (PC)+2 Jump if carry not set.
If C= 0 then,
(PC) ← (PC)+rel
15. JB bit,rel (PC) ← (PC)+3 Jump if direct bit is set.
If (bit)= 1 then,
(PC) ← (PC)+rel
16. JNB bit,rel (PC) ← (PC)+3 Jump if direct bit is not set.
If (bit)= 0 then,
(PC) ← (PC)+rel
17. JBC bit,rel (PC) ← (PC)+3 Jump if direct bit is set and clear
If (bit)= 1 then, bit.
(bit) ← 0
(PC) ← (PC)+rel
CLR Bit
• Description Clear the addressed bit to 0. This works only with bit oriented register.
• Example CLR P 2.0 bit 0 in port 2 cleared.
Eg. CLR 7F, means clear Bit 7 of RAM byte 2F from 16 bytes of Bit
addressable RAM area.
SETB C
• Description Set Carry flag to 1.
SETB bit
• Description Set the addressed bit to 1.
• Example SETB 01, i.e. Bit 1 of RAM byte to 20 H.
SETB P1.2 i.e. bit D2 in port 1 is set to 1.
MOV C, b
• Description Copy the addressed bit in C.
• Example MOV C, PO.1
Copy bit 1 port 0 to Carry flag.
The contents of bit remains uncharged.
MOV b, C
• Description Move (copy) the carry to the addressed bit.
CPL bit
ANL C, bit
• Description AND the carry bit and the addressed bit and put the result in carry bit.
• Example ANL C. 03
AND carry with bit 3 in 20 H RAM location.
ANL C, bit
• Description /bit means complement of bit. The carry bit and complement of
addressed bit is ANDed and result is stored in carry
• Example ANL C, P1.2 ; the second bit in port 1 is complemented and then
ANDed with carry bit.
ORL C, bit
• Description OR carry with the addressed bit and result of ORing stored in carry.
• Example ORL C, 07, i.e. bit 07 in location 20 H
ORL C, / bit
• Description Carry is logically ORed with complement of addressed bit and result
is back stored in C.
• Example ORL C, 07
Additional Information
Branch Control group of instructions of 8051
1. These instructions are used to change the normal sequence of a program, either
unconditionally or under certain test conditions.
Maanya’s M.G.B Publications Microcontrollers
5‐56 8051 Instruction set & Programming
2. These instructions sometimes make use of the flags to change the sequence of
program.
3. Flags are not affected by any instruction.
4. For the instruction CJNE the carry flag is only affected.
5. The branch group mnemonics (17) are grouped in to the following types.
6. Jump and Call Instructions:
• The jump and call instructions are decision control group of instructions that
alter the flow of the program.
• A jump or call instruction can replace the contents of the program counter with
a new program address that causes program execution to begin at the code at
the new address.
• A jump permanently changes the contents of the program counter if certain
program conditions exist.
• A call temporarily changes the program counter to allow another part of the
program to run.
• Program has the following types of decision op-codes while writing program
to do certain task using microcontroller 8051.
o Jump on bit conditions
o Compare bytes and jump if not equal
o Decrement byte and jump if zero
o Jump unconditionally
o Call a subroutine
o Return from a subroutine
7. Range:
The difference, in bytes, between new address from the address in the program
where the jump or call is located is called the range of the jump or call.
Example: For example, if a jump instruction is located at program address 0100H, and
the jump causes the program counter to become 0130H, then the range of the jump is
30H bytes.
8. Classification based on range:
o Relative range: Maximum of +127d, -128d bytes from the instruction following the
jump or call instruction
o Absolute range: Maximum of 2 K bytes from +127, -128d bytes from the
instruction following the jump or call instruction.
o 3) Long range: Maximum of 64K byte (any address from 0000H to FFFFH). Figure
3.1 shows the relative range of all the jump instructions.
Jump Instructions
6. LJMP addr16 (PC) ← addr16 Long jump.
7. SJMP rel (PC) ← (PC)+2 Short jump (relative addr).
(PC) ← (PC)+offset
8. JMP @A+DPTR (PC) ← ((A)+(DPTR)) Jump indirect relative to the
DPTR.
9. JZ rel (PC) ← (PC)+2 Jump if Accumulator is
If (A)=0 then, zero.
(PC) ← (PC)+rel
10. JNZ rel (PC) ← (PC)+2 Jump if Accumulator is not
If (A) ≠ 0 then, zero.
(PC) ← (PC)+rel
LJMP address 16
Description Jump to long address range (i.e. 16bit address). This is an unconditional
jump. This is three byte instruction. First is op-code followed by lower
and higher bytes of 16 bit address.
Operation (PC) ← addr16
SJMP rel
Description Jump to relative address (in the range of + 127 to – 128 ) specified in the
instruction. This is an unconditional jump and two byte instruction. First
is opcode and another specifies the address often called as short jump.
Operation (PC) ← (PC)+2
(PC) ← (PC)+offset
JMP @ A + DPTR
Description Jump to the address formed by adding A to the DPTR. This is single byte
unconditional jump. The address can be anywhere in program memory
(i.e. 16 bit add).
In this instruction neither A nor DTPR in changed.
Operation (PC) ← ((A)+(DPTR))
JZ rel.
Description Jump to the relative address if a content of register A is 00H
(Accumulator is zero).
Operation (PC) ← (PC)+2
If (A)=0 then,
(PC) ← (PC)+rel
JNZ rel
Description Jump to the relative address if accumulator is not zero.
Operation (PC) ← (PC)+2
If (A) ≠ 0 then,
(PC) ← (PC)+rel
Additional Information
Conditional Jump Instructions
Instruction Action
JZ Jumps if A = 0
JNZ Jumps if A ≠ 0
DJNZ Decrement and Jumps if register ≠ 0
CJNE A, data Jumps to the target address if A ≠ 0
CJNE reg, #data Jumps if byte ≠ #data
JC Jumps if CY = 1
JNC Jumps if CY = 0
JB Jumps if bit = 1
JNB Jumps if bit = 0
JBC Jumps if bit = 1 and clear bit
• All conditional jumps are short jumps
• It must be noted that all conditional jumps are short jumps, meaning that the
address of the target must be within -128 to +127 bytes of the contents of the
program counter (PC). This very important concept is discussed at the end of this
section.
Stop
b) Program
Label Instructions Comments
Program 2: Write a program to ADD given two 8-bit numbers and store the result in a
specified external memory location.
Label Instructions Comments
Stop
b) Program
Label Instructions Comments
Program 6: Subtract an 8-bit number from another 8-bit number and store the
result in memory
Label Instructions Comments
MOV DPTR, #8500H Initialize the data pointer.
MOVX A,@DPTR Data 1 is loaded into accumulator.
MOV R2,A Data 1 is copied into R2 register.
INC DPTR Update the pointer.
MOVX A,@DPTR Data 2 is loaded into accumulator.
b) Program
Label Instructions Comments
MOV DPTR,#2200H Initialize memory pointer.
MOVX A,@DPTR Get the count.
MOV R0,A Initialize the iteration counter.
INC DPTR Initialize pointer to array of numbers.
MOV R1,#00H Result = 0.
UP MOVX A,@DPTR Get the number.
ADD A,R1 (A)← Result + (A).
MOV R1,A Result ← (A).
INC DPTR Increment the array pointer.
DJNZ R0,UP Decrement iteration count if not zero repeat.
MOV DPTR,#2300H Initialize memory pointer.
MOV A,R1 Get the result.
MOVX @ DPTR,A Store the result.
STOP SJMP STOP Stop execution.
Fig. 3.9
• A set of instructions written separately from the main program to perform a
function that occurs repeatedly in the main program is called subroutine or
subprogram or routine or procedure or function.
• The subroutine may be required by the main program or another subroutine as
many times as required.
• Subroutine can be called from the main program either conditionally or
unconditionally.
• Subroutine is called to main program by the CALL instructions. As an when the
subroutine is called, the execution of main program is stopped and the program
counter is loaded with starting memory address of the subroutine and the
Stop
b) Program
b) Program
Label Instructions Comments
MOV DPTR,# 2000 Initialize pointer to memory where numbers are stored.
MOV R0,#0AH Initialize counter.
MOV R3,#00H Maximum=0.
AGAIN MOVX A,@DPTR Get the number from memory.
b) Program