Download as pdf or txt
Download as pdf or txt
You are on page 1of 260

i

2nd Edition AP EE-505 DEMC

Master’s Edition

Digital Electronics & Microcontrollers

N.DHANANJAYA
P. SRINIVAS
D.V. RAMANA

Price: 230

M.G.B Publications
Hyderabad & Tirupati. Cell: 9000 3050 79
ii

Digital Electronics & Microcontrollers

Second Edition: June – 2020

© All Rights Reserved

Printing of books passes through many stages–writing, composing, proof


reading, printing etc. We try our level best to make the book error-free. If any
mistake has inadvertently crept in, we regret it and would be deeply indebted to
those who point it out. We do not take any legal responsibility.
No part of this book may be reproduced, stored in any retrieval system or
transmitted in any form by any means - electronic, mechanical photocopying,
recording or otherwise without the prior written, permission of the author and
publishers.

For Copies Please Contact


M.G.B Publications
Cell: 9000305079 & 9700705030
Also Available at All Leading Book Shops
iii

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.

Author - T. Murali Krishna


iv

Table of Contents

Chapter Name ……. Page.No


1. Basics of Digital Electronics. ……. 1-1 to 1-62

2. Combinational Logic circuits. ……. 2-1 to 2-23

3. Sequential Logic Circuits. ……. 3-1 to 3-53

4. 8051 Microcontroller ……. 4-1 to 4-42

5. 8051 instruction set and programming ……. 5-1 to 5-76

DEDICATED

TO

My Daughter MAANYA

Author
N. Dhananjaya
1
Basics of Digital Electronics

OBJECTIVES

1.0 Understand the basics of Digital Electronics


1.1. Explain Binary, Octal, Hexadecimal number systems
1.2. Compare the above number systems with Decimal number system.
1.3. Convert a given decimal number into Binary, Octal, and Hexadecimal
numbers and vice versa.
1.4. Convert a given binary number into octal and hexadecimal number system and vice versa.
1.5. Perform binary addition, subtraction, Multiplication and Division.
1.6. Write 1’s complement and 2’s complement numbers for a given binary number.
1.7. Perform subtraction of binary numbers in 2’s complement method.
1.8. Compare weighted and Un-weighted codes.
1.9. State the use of codes in digital electronics.
1.10. Write Binary equivalent number for a number in 8421, Excess-3 and Gray Code and vice-
versa.
1.11. Mention the use of alphanumeric codes (ASCII & EBCDIC)
1.12. State the importance of parity Bit.
1.13. State different postulates in Boolean algebra.
1.14. Explain the basic logic gates AND, OR, NOT gates with truth tables.
1.15. Explain the working of universal logic gates (NAND, NOR gates) with truth tables.
1.16. Explain the working of an exclusive – OR gate with truth table.
1.17. State De-Morgan’s theorems.
1.18. Realize AND, OR, NOT operations using NAND, NOR gates.
1.19. Apply De-Morgan’s theorems and other postulates to simplify Boolean expressions (up to
three variables).
1.20. Explain standard representations for logical functions (SOP and POS form)
1.21. Write Boolean expressions from the given truth table.
1.22. Explain how to simplify Boolean Expression using Karnaugh map (up to 3 variables only)
1‐2 Basics of Digital Electronics

1.0 Understand the basics of Digital Electronics


Digital electronics is the foundation of modern computers and digital
communications. Massively complex digital logic circuits with millions of gates can now
be built onto a single integrated circuit such as a microprocessor and these circuits can
perform millions of operations per second.
Digital techniques are useful because it is easier to get an electronic device to
switch into one of a number of known states than to accurately reproduce a continuous
range of values.
An advantage of digital circuits when compared to analog circuits is that signals
represented digitally can be transmitted without degradation due to noise.
Information storage can be easier in digital systems than in analog ones. The
noise-immunity of digital systems permits data to be stored and retrieved without
degradation. In a digital system, as long as the total noise is below a certain level, the
information can be recovered perfectly.
Digital electronic circuits are usually made from large assemblies of logic gates,
simple electronic representations of Boolean logic functions. In this chapter, we are going
to study about the basic logic gates and Boolean functions.

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.

There are mainly two types of numbering systems:


1. Non- positional number system
2. Positional number system
1. Non-positional Number System
• An example of non-positional number system is Ancient Roman number system.
• In this roman system letters are converted into numerical digits as shown below:
I = 1, V = 5, X = 10, L = 50, C = 100, D = 500, M = 1000
• They do not follow the arrangement of digits in positions to express a numerical
value as in our Arabic system i.e. decimal system.

Maanya’s M.G.B Publications Digital Electronics


Basics of Digital Electronics 1‐3

2. Positional Number System


• The most widely used number system is positional number system.
• In this number system, the position of a digit indicates the significance to be
attached to that digit.
• There are many positional number systems are in practice. Each system differs one
by the other in number of digits available in that system.
• The commonly used positional number systems are:
1. Decimal number system
2. Binary number system
3. Octal number system
4. Hexadecimal number system
3. Base or Radix
• The number of symbols that can be used in a number system is known as the base
or radix of the system.
• It is always one more than the highest digit of the system.

1.1. Explain Binary, Octal, Hexadecimal number systems


1.1.1. Binary Number System
• The number system with base (or radix) 2 is known as the binary number system.
• Binary means two.
• Thus in binary number system the two symbols available are 0 and 1.
• The symbol 0 or 1 is called a bit instead of calling a binary digit.
• Examples: 1011, 1010, 1000, 11110000 etc.
• The binary system is also a positional value system, where in each binary digit has
its own value or weight expressed as a power of 2.
1.1.2. Octal Number System
• The number system with base (or radix) 8 is known as octal number system.
• In this system, eight symbols 0, 1, 2, 3, 4, 5, 6 and 7 are used to represent numbers.
Thus any octal number is a combination of these digits.
• Examples: 13, 143, 2437 etc.
• It is used in many computers and microcomputers for entering data.
• Since its base 8 = 23 , every 3-bit group of binary can be represented by an octal
digit. An octal number is, thus, 1/3rd the length of the corresponding binary
number.

Maanya’s M.G.B Publications Digital Electronics


1‐4 Basics of Digital Electronics
• Similar to decimal and binary number systems, it is also a positional number
system.
1.1.3. Hexadecimal Number
• The number system with base or radix sixteen (16) is known as hexadecimal
number system.
• The hexadecimal number system having 16 distinct symbols to represent the
numbers.
• These are numerals 0 to 9 and alphabets A to F. Thus any hexadecimal number is a
combination of these digits.
• Examples: 2009, 924, 8180, 962 etc.
• As numbers and alphabets are used to represent the digits in hexadecimal number
systems, hence it is also known as alphanumeric number system.
• Hexadecimal number system is very popular in computer usage and more so in
microprocessor work.

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.

1.2 Compare the above number systems with Decimal


number system
1. Decimal Number System
• Decimal means 10.
• Definition: The number system with base (or radix) 10 is known as the decimal
number system.
• The decimal number system contains ten different symbols. These are: 0, 1, 2, 3, 4, 5,
6, 7, 8 and 9.

Maanya’s M.G.B Publications Digital Electronics


Basics of Digital Electronics 1‐5
• Any decimal number is given as a combination of only ten digits.
• Examples: 19,108, 2014, 143.79, etc.
2. Comparison of different number systems
Sl.No. Number system Digits/Symbols Base/Radix
1 Binary 0, 1 2
2 Octal 0, 1, 2, 3, 4, 5, 6, 7 8
3 Decimal 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 10
4 Hexadecimal 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F 16

1.3 Convert a given decimal number into Binary, Octal, and


Hexadecimal numbers and vice versa.
1.3.1 Decimal to Binary Conversion
• The given decimal number consists of both integer and fractional parts, then each
part should be converted to its equivalent binary separately.
Case-1: Decimal Integer Part
• To convert a decimal integer part to binary number, the decimal number is
successively divided by 2 and the remainders are recorded after each division until
quotient is zero.
• Lest us consider a decimal number say, 13.
2 13
2 6 1 (LSB)
2 3 0
2 1 1
0 1(MSB)
• Dividing the given decimal number 13, we get the quotient 6 and remainder 1.
• The 6 is then divided by 2, giving a quotient 3 and remainder 0.
• This process is carryout until the quotient becomes zero.
• Note that the first remainder is the LSB and last remainder is the MSB.
• The binary equivalent of the decimal number 1310 = 11012.
Case-2: Decimal Fractional Part
• Let us consider a decimal fractional part 0.875.
• Multiplying it by 2, we get the result 1.750.
• Only fractional part of the result should be multiplied by 2 to get the next binary bit
(0.75 x 2 = 1.5).
• This process is carryout until we get zero.
Maanya’s M.G.B Publications Digital Electronics
1‐6 Basics of Digital Electronics
0.875 x 2 = 1.750 With a carry of 1
0.75 x 2 = 1.50 With a carry of 1
0.5 x 2 = 1.0 With a carry of 1
• Reading carries downward we get, binary fraction 0.1112 which is equivalent to
0.87510 decimal.
NOTE: In certain cases we may not get a value of 0 after repeated multiplication by 2.
Then we shall try to end the process after a specified number of binary places.
Example 1: Convert 0.95 decimal number to its binary equivalent.
Solution:
0.95 x 2 = 1.9 1
0.9 x 2 = 1.8 1
0.8 x 2 = 1.6 1
0.6 x 2 = 1.2 1
0.2 x 2 = 0.4 0
0.4 x 2 = 0.8 0
0.8 x 2 = 1.6 1
∴ Binary equivalent of decimal number 0.9510 = 0.11110012
In this case, 0.8 is repeated and if we multiply further, we will get repeated
sequence.
NOTE:
1. If we stop here, we get 7 binary digits, 1111001. This answer is an approximate
answer.
2. To get more accurate answer we have to continue multiplying by 2 until we have
as many digits as necessary for our application.
Example 2: Convert decimal number 37 to its binary equivalent.
Solution:
2 37
2 18 1 (LSB)
2 9 0
2 4 1
2 2 0
2 1 0
0 1 (MSB)
∴ Binary equivalent of decimal number 3710 = 1001012

Maanya’s M.G.B Publications Digital Electronics


Basics of Digital Electronics 1‐7

Example 3: Convert the decimal number 157 into binary.


March/April - 2009
Solution:
2 157
2 78 1 (LSB)
2 39 0
2 19 1
2 9 1
2 4 1
2 2 0
2 1 0
0 1 (MSB)
∴ Binary equivalent of decimal number 15710 = 100111012
Example 4: Carry out the following conversion.
(125)10 = __________ 2
March/April – 2007
Solution:
2 125
2 62 1 (LSB)
2 31 0
2 15 1
2 7 1
2 3 1
2 1 1
0 1 (MSB)
∴ Binary equivalent of decimal number 12510 = 11111012
Example 5: Convert 0.8125 decimal number to its binary equivalent.
Solution:
0.8125 x 2 = 1.625 1
0.625 x 2 = 1.25 1
0.25 x 2 = 0.5 0
0.5 x 2 = 1.0 1
∴ Binary equivalent of decimal number 0.812510 = 0.11012

Maanya’s M.G.B Publications Digital Electronics


1‐8 Basics of Digital Electronics
Example 6: Convert (26.15)10 into binary number.
March/April - 2006
Solution:
Separate the integer part and fractional part. Integer part: 26, Fractional part: 0.15.
Step 1: Find equivalent binary number for integer part.
2 26
2 13 0 (LSB)
2 6 1
2 3 0
2 1 1
0 1 (MSB)
2610=110102
Step 2: Find equivalent binary number for fractional part.
0.15 x 2 = 0.3 0
0.3 x 2 = 0.6 0
0.6 x 2 = 1.2 1
0.2 x 2 = 0.4 0
0.4 x 2 = 0.8 0
0.8 x 2 = 1.6 1
0.1510=0.0010012
∴Binary equivalent of decimal number 26.15 = 11010.0010012
Example 7: Convert the decimal number 41.7 to its binary equivalent.
Solution: Integer part = 41, Fractional part = 0.7.
Step 1: Find equivalent binary number for integer part.
2 41
2 20 1 (LSB)
2 10 0
2 5 0
2 2 1
2 1 0
0 1 (MSB)
4110=1010012
Step 2: Find equivalent binary number for fractional part.

Maanya’s M.G.B Publications Digital Electronics


Basics of Digital Electronics 1‐9

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

Maanya’s M.G.B Publications Digital Electronics


1‐10 Basics of Digital Electronics
Example 10: Convert decimal number 214 to its octal equivalent.
Solution:
8 214
8 26 6 (LSD)
8 3 2
0 3 (MSD)
∴Octal equivalent of decimal number 21410 = 3268
Example 11: Convert a given decimal number 753 in to octal number system.
Oct/Nov-2013
Solution:
8 753
8 94 1 (LSD)
8 11 6
8 1 3
0 1 (MSD)
∴Octal equivalent of decimal number 75310 = 13618
Example 12: Convert a given decimal number 908 in to octal number system.
Oct/Nov-2013
Solution:
8 908
8 113 4 (LSD)
8 14 1
8 1 6
0 1 (MSD)
∴Octal equivalent of decimal number 90810 = 16148
Example 13: Convert decimal number 0.640625 to its octal equivalent.
Solution:
0.640625 x 8 = 5.125 5(MSD)
0.125 x 8 = 1.0 1(LSD)
∴Octal equivalent of decimal number 0.64062510 = 0.518

Maanya’s M.G.B Publications Digital Electronics


Basics of Digital Electronics 1‐11

Example 14: Convert decimal number 0.65 to its octal equivalent.


Solution:
0.65 x 8 = 5.2 5
0.2 x 8 = 1.6 1
0.6 x 8 = 4.8 4
0.8 x 8 = 6.4 6
0.2 x 8 = 1.6 1
∴Octal equivalent of decimal number 0.6510 = 0.514618
Example 15: Convert decimal number 467.43 to its octal equivalent.
Solution:
8 467
8 58 3 (LSD)
8 7 2
0 7 (MSD)
46710 = 7238
0.43 x 8 = 3.44 3
0.44 x 8 = 3.52 3
0.52 x 8 = 4.16 4
0.16 x 8 = 1.28 1
0.28 x 8 = 2.24 2
0.4310 = 0.334128
∴Octal equivalent of decimal number 467.4310 = 723.334128
1.3.3 Decimal to Hexadecimal Conversion
• For conversion from decimal to hexadecimal, the procedure used in binary as well
as octal system is applicable, using 16 as the dividing factor (for Integer part) and
multiplying factor (for fractional part).
Example 16: Convert a given decimal number 76 in to hexadecimal number system.
Oct/Nov-2013
Solution:
16 76
16 4 C (LSD)
0 4 (MSD)
∴Hexadecimal equivalent of decimal number 7610 = 4C16

Maanya’s M.G.B Publications Digital Electronics


1‐12 Basics of Digital Electronics
Example 17: Convert a given decimal number 753 in to hexadecimal number
system.
Oct/Nov-2013
Solution:
16 753
16 47 1 (LSD)
16 2 F
0 2 (MSD)
∴Hexadecimal equivalent of decimal number 75310 = 2F116
Example 18: Convert a given decimal number 908 in to hexadecimal number
system.
Oct/Nov-2013
Solution:
16 908
16 56 C (LSD)
16 3 8
0 3 (MSD)
∴Hexadecimal equivalent of decimal number 90810 = 38C16
Example 19:Convert decimal number 22.64 to hexadecimal number.
Solution:
Separate the integer part and fractional part. Integer part: 22, Fractional part: 0.64
Step 1: Find equivalent hexadecimal number for integer part.
16 22
16 1 6 (LSD)
0 1 (MSD)
2210=1616
Step 2: Find equivalent hexadecimal number for fractional part.
0.64 x 16 = 10.24 A
0.24 x 16 = 3.84 3
0.84 x 16 = 13.44 D
0.44 x 16 = 7.04 7
0.6410=A3D716
∴Hexadecimal equivalent of decimal number 22.6410 = 16.3D716

Maanya’s M.G.B Publications Digital Electronics


Basics of Digital Electronics 1‐13

1.3.4 Binary to Decimal Conversion


Following procedure is adopted to convert a given binary number into its
equivalent decimal number.
Step 1: Write the given number i.e., all its digits in a row.
Step 2: Write the positional weights of each bit working from right to left.
Step 3: Multiply each bit by its weight written under it.
Step 4: Add the resulting products to get the decimal equivalent.
The above steps can be illustrated using following examples.
Example 20:: Convert binary 10011 to its decimal equivalent.
Step 1: 1 0 0 1 1

Step 2: 24 23 22 21 20

Step 3: (24x1) (23x0) (22x0) (21x1) (20x1)


↓ ↓ ↓ ↓ ↓
16 0 0 2 1
Step 4: 16 + 0 + 0 + 2 + 1 = 19
∴ (10011)2 = 1910
Example21:: Convert binary fraction 0.1111 into decimal number.
Step 1: 0 . 1 1 1 1

Step 2: 2-1 2-2 2-3 2-4

Step 3: (2-1x1) (2-2x1) (2-3x1) (2-4x1)


↓ ↓ ↓ ↓
0.5 0.25 0.125 0.0625
Step 4: 0.5 + 0.25 + 0.125 + 0.0625 = 0.9375
∴ (0.1111)2 = 0.937510
Example 22: Convert (1010.10)2 to its decimal equivalent.
Solution:
1 0 1 0 . 1 0
−1
23
2 2 1
2 2 0
2 2− 2
= 8 + 0 + 2 + 0 + 0.5 + 0 = 10.5
Decimal equivalent of binary number 1010.102 = 10.510
Maanya’s M.G.B Publications Digital Electronics
1‐14 Basics of Digital Electronics
Example 23: Convert the following number as specified. (1010111)2=( )10
Oct/Nov-2010
1 0 1 0 1 1 1
6 5 4 3 2 1
2 2 2 2 2 2 20
= (26 × 1) + (25 × 0) + (24 × 1) + (23 × 0) + (22 × 1) + (21 × 1) + (20 × 1)
= 64 + 0 + 16 + 0 + 4 + 2 + 1 = 87
∴Decimal equivalent of binary number 10101112 = 8710
Example 24: Convert the following binary number into decimal. 11010.101
Oct/Nov-2010
Solution:
1 1 0 1 0 . 1 0 1
2 4 23 2 2 21 2 0 2 −1 2 − 2 2−3
= ( 2 4 × 1) + ( 2 3 × 1) + ( 2 2 × 0) + ( 21 × 1) + ( 2 0 × 0) + ( 2 −1 × 1) + ( 2 −2 × 0) + ( 2 −3 × 1)
= 16 + 8 + 0 + 2 + 0 + 0.5 + 0.125 = 26.625
∴Decimal equivalent of binary number 11010.1012 = 26.62510
Example 25: Convert the following binary number into decimal. 101010.1011
Oct/Nov-2010
Solution:
1 0 1 0 1 0 . 1 0 1 1
−1
25 2 4 23 22 21 20 2 22 2−3 2− 4
= 2 5 × 1 + 2 4 × 0 + 2 3 × 1 + 2 2 × 0 + 21 × 1 + 2 0 × 0 + 2 −1 × 1 + 2 −2 × 0 + 2 −3 × 1 + 2 −4 × 1
= 32 + 0 + 8 + 0 + 2 + 0 + 0.5 + 0 + 0.125 + 0.0625 = 42.6875
∴Decimal equivalent of binary number 101010.10112 = 42.687510
1.3.5 Octal to Decimal Conversion
• Any octal number can be converted into its equivalent decimal number in the
same manner as the binary to decimal conversion.
• The only difference is that power of 8 is used in place of 2.
Example 26: Convert (143.25)8 to its decimal equivalent.
Solution:
Step 1: 1 4 3 . 2 5

Step 2: 82 81 80 8-1 8-2

Maanya’s M.G.B Publications Digital Electronics


Basics of Digital Electronics 1‐15

Step 3: (82x1) (81x4) (80x3) (8-1x2) (8-2x5)


↓ ↓ ↓ ↓ ↓
64 32 3 0.250 0.078
Step 4: 64 + 32 + 3 + 0.250 + 0.078 = 99.328
∴Decimal equivalent of octal number 143.258 = 99.32810
Example 27: Convert octal 776 into decimal number.
March-2008
Solution:
7 7 6
82 81 80
= (82 × 7) + (81 × 7) + (80 × 6) = 448 + 56 + 6 = 510
∴Decimal equivalent of octal number 7768 = 51010
Example 28: Convert octal 257 into decimal number.
March-2008
Solution:
2 5 7
82 81 80
= (82 × 2) + (81 × 5) + (80 ×) = 128 + 40 + 7 = 175
∴Decimal equivalent of octal number 2578 = 17510
Example 29: Convert octal 22.4 into decimal number.
March-2008
Solution:
2 2 . 4
81 0
8 8−1
= (81 × 2) + (80 × 2) + (8−1 × 4) = 16 + 2 + 0.5 = 18.5
∴Decimal equivalent of octal number 22.48 = 18.510
1.3.6 Hexadecimal to Decimal Conversion
• Any hexadecimal number can be converted into its equivalent decimal number in
the same manner as the binary / octal to decimal conversion.
• The only difference is that power of 16 is used instead of 2/8.

Maanya’s M.G.B Publications Digital Electronics


1‐16 Basics of Digital Electronics
Example 30: Convert BED.15 to its Decimal equivalent.
Solution:
Step 1: B E D . 1 5

Step 2: 162 161 160 16-1 16-2

Step 3: (162x11) (161x14) (160x13) (16-1x1) (16-2x5)


↓ ↓ ↓ ↓ ↓
2816 224 13 0.063 0.020
Step 4: 2816 + 224 + 13 + 0.063 + 0.020 = 3053.083
∴Decimal equivalent of hexadecimal number,
BED.1516 = 3053.08310
Example 31: Convert the hexadecimal 3A1F to Decimal equivalent.
Nov/Dec-2009
Solution:
3 A 1 F
16 16 16 160
3 2 1

= (163 × 3) + (16 2 × 10) + (161 × 1) + (160 × 15) = 14879


∴Decimal equivalent of hexadecimal number,3A1F16 = 1487910
Example 32: Convert the hexadecimal 6F14 to Decimal equivalent.
Nov/Dec-2009
Solution:
6 F 1 4
163 162 161 160
= (163 × 6) + (162 × 15) + (161 × 1) + (160 × 4) = 28436
∴Decimal equivalent of hexadecimal number, 6F1416 = 2843610

1.4 Convert a given binary number into octal and


hexadecimal number system and vice versa.
• The below table gives hexadecimal numbers and their binary equivalents for
decimal numbers 0 to 15.

Maanya’s M.G.B Publications Digital Electronics


Basics of Digital Electronics 1‐17

Decimal Binary Hexadecimal


0 0 0 0 0 0
1 0 0 0 1 1
2 0 0 1 0 2
3 0 0 1 1 3
4 0 1 0 0 4
5 0 1 0 1 5
6 0 1 1 0 6
7 0 1 1 1 7
8 1 0 0 0 8
9 1 0 0 1 9
10 1 0 1 0 A
11 1 0 1 1 B
12 1 1 0 0 C
13 1 1 0 1 D
14 1 1 1 0 E

15 1 1 1 1 F

1.4.1 Binary to Octal Conversion


• Binary numbers can be converted into equivalent octal numbers by making
groups of 3-bits.
• The groups should be considered from the binary point towards the LSB and MSB
separately.
Example 33: Convert (11101.0101)2 to octal equivalent.
Solution:
Binary number : 0 1 1 1 0 1 . 0 1 0 1 0 0
↓ ↓ ↓ ↓
Octal number : 3 5 . 2 4
∴Octal equivalent of binary number 11101.01012 = 35.248

Maanya’s M.G.B Publications Digital Electronics


1‐18 Basics of Digital Electronics
1.4.2 Binary to Hexadecimal Conversion
• Binary numbers can be converted into equivalent hexadecimal numbers by
making groups of 4-bits.
• The groups should be considered from the binary point towards the LSB and MSB
separately.
Example 34: Convert binary number 11101100 into hexadecimal number.
Solution:
Binary number : 1 1 1 0 1 1 0 0
↓ ↓
Hexadecimal number : E C
∴Hexadecimal equivalent of binary number, 111011002 = EC16
Example 35: Convert 0.0101012 into hexadecimal number.
Solution:
Binary number : 0 . 0 1 0 1 0 1 0 0
↓ ↓
Hexadecimal number : 0 . 5 4
∴ (0.010101)2 = (0.54)16
∴Hexadecimal equivalent of binary number, 0.010101002 = 0.5416
Example 36: Convert binary number 10100010 into hexadecimal number.
April/May-2011
Solution:
Binary number : 1 0 1 0 0 0 1 0

↓ ↓

Hexadecimal number : A 2

∴Hexadecimal equivalent of binary number, 101000102 = A216


Example 37: Convert binary number 00111011 into hexadecimal number.
April/May-2011
Solution:
Binary number : 0 0 1 1 1 0 1 1

↓ ↓

Hexadecimal number : 3 B

∴Hexadecimal equivalent of binary number, 001110112 = 3B16

Maanya’s M.G.B Publications Digital Electronics


Basics of Digital Electronics 1‐19

1.4.3 Octal to Binary Conversion


• Octal numbers can be converted into equivalent binary numbers by replacing each
octal digit by its 3-bit equivalent binary.
Example 38: Convert octal number 3570 into binary.
Solution: Oct/Nov-2007
Octal number : 3 5 7 0
↓ ↓ ↓ ↓
Binary number : 0 1 1 1 0 1 1 1 1 0 0 0
∴Binary equivalent of octal number 35708 = 011 101 111 0002
Example 39: Convert octal number 7000 into binary.
Solution: Oct/Nov-2007
Octal number : 7 0 0 0
↓ ↓ ↓ ↓
Binary number : 1 1 1 0 0 0 0 0 0 0 0 0
∴Binary equivalent of octal number 70008 = 111 000 000 0002
Example 40: Convert (630.15)8 to binary.
Solution:
Octal number : 6 3 0 . 1 5
↓ ↓ ↓ ↓ ↓
Binary number : 1 1 0 0 1 1 0 0 0 . 0 0 1 1 0 1
∴Binary equivalent of octal number 630.158 = 110 011000.001 1012
1.4.4 Hexadecimal to Binary Conversion
• Hexadecimal numbers can be converted into equivalent binary numbers by
replacing each hexadecimal digit by its 4-bit equivalent binary.
Example 41: Convert (ABCD)16 to binary.
Solution:
Hexadecimal number : A B C D
↓ ↓ ↓ ↓

Binary number : 1 0 1 0 1 0 1 1 1 1 0 0 1 1 0 1

∴Binary equivalent of hexadecimal number, ABCD16 = 10101011110011012

Maanya’s M.G.B Publications Digital Electronics


1‐20 Basics of Digital Electronics
Example 42: Convert fraction 0.BF3 hexadecimal into binary number.
Solution:
Hexadecimal number : 0 . B F 3
↓ ↓ ↓
Binary number : 0 . 10 1 1 1 1 1 1 0 0 1 1
∴Binary equivalent of hexadecimal number, 0.BF316 = 0.1011111100112
1.4.5 Octal to Hexadecimal Conversion
The easiest way to convert octal number to hexadecimal number is given below.
Step 1: Convert octal number to its binary equivalent.
Step 2: Convert binary number to its hexadecimal equivalent.
Example 43: Convert (615)8 to its hexadecimal equivalent.
Solution:
Step 1: Octal to binary
Octal number : 6 1 5
↓ ↓ ↓
Binary number : 1 1 0 0 0 1 1 0 1
∴ Binary number = 1100011012
Step 2: Binary to hexadecimal
Binary number : 0 0 0 1 1 0 0 0 1 1 0 1
↓ ↓ ↓
Hexadecimal number : 1 8 D
∴ Hexadecimal number = 18D16

1.4.6 Hexadecimal to Octal Conversion


The easiest way to convert hexadecimal number to octal number is given below.
1. Convert hexadecimal number to its binary equivalent.
2. Convert binary number to its octal equivalent.
Example 44: Convert (25B)16 to its octal equivalent.
Solution:
Step 1: Hexadecimal to binary

Maanya’s M.G.B Publications Digital Electronics


Basics of Digital Electronics 1‐21

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.5 Perform binary addition, subtraction, Multiplication and


Division.
1. Binary Addition
Binary addition is performed in the same manner as decimal addition. The
addition is carried out from the least significant bits and preceded to higher significant
bits, adding the carry resulting from previous addition each time. The following are the
rules for binary addition.
A + B Carry Sum
0+0 0 0
0+1 0 1

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:

Maanya’s M.G.B Publications Digital Electronics


1‐22 Basics of Digital Electronics
Explanation:
In the example given above, first the two LSB’s (1 and 1) are added giving sum
bit 0 and a carry bit 1. The carry bit is carried over to the next higher significant position
and added to the binary digits (0 and 0) resulting in a sum bit 1 and carry bit 0. This
process is carried on until the MSB’s are added.
*Note:
1. While adding the two binary numbers one should align the LSB’s and then added.
2. In case the numbers have binary points, then the alignment should be made with
respect to the binary points.
3. The following example illustrates this point.
Example 46: Add 10101.101 and 110.1
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

Example 47 Subtract (0101)2 from (1011)2


Solution:

Maanya’s M.G.B Publications Digital Electronics


Basics of Digital Electronics 1‐23
Explanation:
Step 1: In the first column, we have 1 – 1 the result is 0.
Step 2: In the second column, we have 1 – 0, the result is 1.
Step 3: In the third column, we have 0 – 1, and then borrow 1 from next column
making a 10 in this column, hence 10 – 1 = 1.
Step 4: In the fourth column, when a 1 is already borrowed, a 0 is left, hence 0 – 0 = 0.
Example 48: Subtract (0010)2 from (1100)2
Solution:

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

Maanya’s M.G.B Publications Digital Electronics


1‐24 Basics of Digital Electronics
A Decimal check may be performed by converting binary numbers to decimal.
(110)2 = (6)10 (101)2 = (5)10 (11110)2 = (30)10
We know 6 x 5 = 30, thus checked.
b) Here 11.01 is multiplicand and 10.1 is multiplier

∴ 11.01 2 × 10.1 2 = 1000.0012


Binary point is placed just like in decimal multiplication, after three bits from right
multiplicand has two bits after binary pointed multiplier has one bit after binary point.
4. Binary Division
Binary division is carried out in the same manner as the decimal division. The
following are the rules for binary addition.
1) 0 ÷ 1 = 0
2) 1 ÷ 1 = 1
The following examples illustrate the binary division.
Example 50: Divide the following binary number
1011 ÷ 10
Solution:

Maanya’s M.G.B Publications Digital Electronics


Basics of Digital Electronics 1‐25

Example 51: Divide 1110101 by 1001


Solution:

1.6 Write 1’s complement and 2’s complement numbers for


a given binary number.
1.6.1. 1’s complement of a binary number
The 1’s complement of a binary number is got by changing all 1’s to zeros and
the zeros to 1’s.
Example 52:
1 1 0 1 ← Number
0 0 1 0 ← 1’s complement
Example 53:
1 0 1 1 1 0 0 1 ← Number
0 1 0 0 0 1 1 0 ← 1’s complement
1.6.2. 2’s complement of a binary number
The 2’s complement of a binary number is got by adding 1 to the 1’s
complement. It is given as
∴ 2’s complement = 1’s complement +1
Example 54:
1 0 0 1 ← Number
0 1 1 0 ← 1’s complement
+ 1
0 1 1 1 ← 2’s complement
Example 55:
1 0 1 0 0 0 1 1 ← Number
0 1 0 1 1 1 0 0 ← 1’s complement
+ 1
0 1 0 1 1 1 0 1 ← 2’s complement

Maanya’s M.G.B Publications Digital Electronics


1‐26 Basics of Digital Electronics

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

Maanya’s M.G.B Publications Digital Electronics


Basics of Digital Electronics 1‐27

c) Advantages of 1’s complement subtraction


1. The 1’s complement subtraction can be accomplished with a binary adder.
Therefore, this method is useful in arithmetic logic circuits.
2. The 1’s complement of a number is easily obtained by inverting each bit in the
number.

1.7 Perform subtraction of binary numbers in 2’s


complement method.
The 2’s complement subtraction of binary numbers is illustrated in the following
two cases
Case – 1: Subtraction of smaller number from larger number
1. Determine the 2’s complement of a smaller number (Subtrahend).
2. Add the 2’s complement to the larger number (minuend).
3. Discard the carry.
Example 58: Subtract the binary number 100100 from 110011 using the 2’s
complement method.
Solution:

∴ 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:

∴ Answer in true form = - 0 0 1 1 1 0

Maanya’s M.G.B Publications Digital Electronics


1‐28 Basics of Digital Electronics

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.

Weighted codes Un-Weighted codes


1. In weighted codes each digit 1. In un-weighted codes each digit position is
position is assigned with certain not assigned with certain weight.
weight.
2. Examples: 8421, 2421 and 5211 2. Examples: Gray code, Excess – 3 code.
codes.
3. These codes are used for I/O 3. These codes are used to perform arithmetic
operations in digital circuits, data operations in digital systems computers, shaft
manipulation during arithmetic position encoders, where error susceptibility
operations etc. increases etc.

1.9 State the use of codes in digital electronics.


1. The weighted codes are used for I/O operations in digital circuits, data manipulation
during arithmetic operations etc.
2. The un-weighted codes are used to perform arithmetic operations in digital systems
computers, shaft position encoders, where error susceptibility increases etc.

Maanya’s M.G.B Publications Digital Electronics


Basics of Digital Electronics 1‐29

1.10 Write Binary equivalent number for a number in 8421,


Excess-3 and Gray Code and vice-versa.
1.10.1 BCD 8421 code
• BCD is an abbreviation for Binary Coded Decimal. BCD is a numeric code in which
each decimal digit is represented by a separate group of bits.
• The most common BCD code is 8421 code, in which each decimal digit is
represented by a 4- bit binary number. It is called 8421 code because the weights
associated with 4 bits are 8, 4, 2, 1 from left to right.
• It is a weighted code.
• To express any decimal number in 8421 code, simply replace each decimal digit by
the appropriate 4 - bit code.
• The below table shows that the 4-bit 8421 code to represent a single decimal digit.
Decimal Digit 8421 Code
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

Example 60: Convert each of the following decimal number into BCD.
(a) 10.45 (b) 27.19 (c) 10.24
Solutions:

Maanya’s M.G.B Publications Digital Electronics


1‐30 Basics of Digital Electronics

1.10.2 Gray Code


• Definition: The code which exhibits only single bit change from one code word to
the next is known as gray code.
• Gray code is an un-weighted code, which means that there are no specific weights
assigned to the bit positions.
• It is known as cyclic code or unit distance code.
• A cyclic code is one, in which all successive code words differ in only one bit
position.
• This may be verified from the following table, which shows gray code and its
equivalent decimal and binary codes.
Decimal Number Binary Number Gray Code
0 0 0 0 0 0 0 0 0
1 0 0 0 1 0 0 0 1
2 0 0 1 0 0 0 1 1
3 0 0 1 1 0 0 1 0
4 0 1 0 0 0 1 1 0
5 0 1 0 1 0 1 1 1
6 0 1 1 0 0 1 0 1
7 0 1 1 1 0 1 0 0
8 1 0 0 0 1 1 0 0
9 1 0 0 1 1 1 0 1
• The code word for decimal 0 is 0000 and code word for decimal 1 is 0001. These
two code words differ in only right most bit position.

Maanya’s M.G.B Publications Digital Electronics


Basics of Digital Electronics 1‐31
• Similarly, code word of decimal 2 is 0011 which differ with code word of decimal
1 in only bit position second from right. Like this all code words differ in only one
bit position.
1.10.2.1. Binary to Gray Conversion
The given binary number can be converted into gray code by using the following
steps:
Step 1: Keep the MSB as it is.
Step 2: Add MSB to the next LSB.
Step 3: If there is carry neglect it.
Step 4: Write the sum as next bit in gray code.
Step 5: Continue this till LSB is reached.
The following examples, illustrates the process to convert the binary number
into the gray code.
Example 61: Convert the binary number 10110 to gray code.
Solution:
Step 1: The left-most Gray digit is the same as the left-most binary digit.
(MSB) (LSB)
Binary: 1 0 1 1 0

Gray: 1
Step 2: Add the left-most binary digit to the adjacent one.

Binary : 1 + 0 1 1 0

Gray : 1 1

Step 3: Add the next adjacent pair.

Binary : 1 0 + 1 1 0

Gray: 1 1 1

Maanya’s M.G.B Publications Digital Electronics


1‐32 Basics of Digital Electronics
Step 4: Add the next adjacent pair and discard carry.

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:

1.10.2.2. Gray to Binary Conversion


To convert from gray code to binary, a similar procedure is used, but there are
some differences carryout the following steps to convert the given gray code into binary
code.
Step 1: Keep MSB of the given number as it is. This is also the MSB of the binary
number.
Step 2: Add this MSB to the next bit of the gray code to get the second bit of the binary
code. Neglect if there is carry.
Step 3: The process is to be continued till the LSB is reached.

Maanya’s M.G.B Publications Digital Electronics


Basics of Digital Electronics 1‐33
The following examples, illustrates the process to convert the gray code into the
binary number.
Example 63: Convert the gray code number 11101 to binary
Solution:
Step 1: The left-most digits are the same.
(MSB) (LSB)
Gray : 1 1 1 0 1

Binary : 1

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.

Maanya’s M.G.B Publications Digital Electronics


1‐34 Basics of Digital Electronics
Example 64: Convert the following Gray code numbers to binary numbers.
(a) 11011110 (b) 10011011
Solutions:

1.10.3 Excess-3 Code


1. The Excess-3 is a digital code that is derived by adding 3 to each decimal digit and
then converting the result to four-bit binary.
2. Since no definite weights can be assigned to the four digit positions, Excess-3 is an
un-weighted code.
3. For instance, the Excess-3 code for the decimal 2 is,
2
+3
………
5 → 0101

4. The Excess-3 code for the decimal 9 is,


9
+3
………
12 → 1100
5. The Excess-3 code for each decimal digit is found by the same procedure, and the
entire code is shown in below table.

Maanya’s M.G.B Publications Digital Electronics


Basics of Digital Electronics 1‐35

Decimal Digit Excess-3 Code


E3 E2 E1 E0
0 0 0 1 1
1 0 1 0 0
2 0 1 0 1
3 0 1 1 0
4 0 1 1 1
5 1 0 0 0
6 1 0 0 1
7 1 0 1 0
8 1 1 0 1
9 1 1 0 0
*Note:
1. The Excess-3 code, also called XS-3.
2. It is a self-complementing code. Therefore, subtraction by the method of
complement addition is more direct in XS-3 code than that in 8421 code.
3. The XS-3 code has six invalid states 0000, 001, 0010, 1101, 1110 and 1111.
*Note: Self-Complementing property of XS-3 code
1. The key feature of the Excess-3 code is that it is self-complementing. That is, the 1’s
complement of an Excess-3 number is the Excess-3 code for the 9’s complement of
the corresponding decimal number. For example, the Excess-3 code for decimal 4 is
0111. The 1’s complement of this is 1000, which is the Excess-3 code for the decimal
5 (and 5 is the 9’s complement of 4).
2. It should be noted that the 1’s complement is easily produced with digital logic
circuits by simply inverting each bit. The self-complementing property makes the
Excess-3 code useful in some arithmetic operations, because subtraction can be
performed using the 9’s complement method.
3. The below table shows that the 1’s complement of each Excess-3 number is the
Excess-3 code for the 9’s complement of the corresponding decimal digit.

Maanya’s M.G.B Publications Digital Electronics


1‐36 Basics of Digital Electronics
Excess-3 1’s Complement Decimal Digit 9’s Complement
0011 1100 0 9
0100 1011 1 8
0101 1010 2 7
0110 1001 3 6
0111 1000 4 5
1000 0111 5 4
1001 0110 6 3
1010 0101 7 2
1011 0100 8 1
1100 0011 9 0
Example 65: Convert each of the following decimal numbers to Excess-3 code:
a) 23 b) 246 c) 78 d) 910
Solutions:
First, add 3 to each digit in the decimal number, and then convert each resulting
sum to its equivalent binary code.

Maanya’s M.G.B Publications Digital Electronics


Basics of Digital Electronics 1‐37

Note: 9’s & 10’s Complements


• The 9’s complement of decimal number is found by subtracting each digit in the
number from 9.
• The 10’s complement of a decimal number is equal to the 9’s complement plus 1.
Table: 1.6
Digit 9’ s Complement 10’ s Complement
0 9 10
1 8 9
2 7 8
3 6 7
4 5 6
5 4 5
6 3 4
7 2 3
8 1 2
9 0 1

1.11 Mention the use of alphanumeric codes (ASCII &


EBCDIC)
1. A binary code constructed to represent the 10 decimal digits, 26 alphabets certain
number of arithmetic symbols (+, - , * , /) relational operators (< , > , =) etc is
alphanumeric code.
2. In other words, the codes which consist of both numbers and alphabetic characters
are called alphanumeric codes.
3. Most of these codes, however, also represent symbols and various instructions
necessary for conveying intelligible information.
4. The most commonly used alphanumeric codes are:
a. ASCII (American Standard Code for Information Interchange)
b. EBCDIC (Extended Binary Coded Decimal Interchange Code) and
c. Hollerith code

Maanya’s M.G.B Publications Digital Electronics


1‐38 Basics of Digital Electronics
1.11.1. ASCII
• ASCII stands for American Standard Code for Information Interchange.
• ASCII code is widely used for the transmission of decimal digits, letters of the
alphabets, number of special characters and instructions to a teletype writer.
• It is a 7-bit code in which the decimal digits are represented by the BCD code
preceded by 011.
• Using 7 bits of the code, 128 (27) different items of information can be transmitted.
• In this 95 characters represent graphic symbols that include upper case and lower
case letters, numerals (0 to 9) punctuation marks and special symbols. 23 characters
represented for controlling the layout of printing or display devices such as carriage
return, like feed horizontal tabulation and back space. The other 10 characters are
used to direct the data communications flow and report its status.
LSB’s 000(0) 001(1) 010(2) 011(3) 100(4) 101(5) 110(6) 111(7)
0000(0) NUL DLE SP 0 @ P p
0001(1) SOH DC1 ! 1 A Q a q
0010(2) STX DC2 “ 2 B R b r
0011(3) ETX DC3 # 3 C S c s
0100(4) EOT DC4 $ 4 D T d t
0101(5) ENQ NAK % 5 E U e u
0110(6) ACK SYN & 6 F V f v
0111(7) BEL ETB ’ 7 G W g w
1000(8) BS CAN ( 8 H X h x
1001(9) HT EM ) 9 I Y i y
1010(A) LF SUB * : J Z j z
1011(B) VT ESC + ; K [ k {
1100(C) FF FS , < L \ l |
1101(D) CR GS - = M ] m }
1110(E) SO RS . > N ↑ n ~

1111(F) SI US / ? O ← o del

Maanya’s M.G.B Publications Digital Electronics


Basics of Digital Electronics 1‐39

*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.

1.12 State the importance of parity Bit


A parity bit is an extra bit included with a message to make the total number of 1’s
either even or odd. Consider the following two characters and their even and odd
parity:
With even parity With odd parity
ASCII A=1000001 01000001 11000001
ASCII T= 1010100 11010100 01010100

Maanya’s M.G.B Publications Digital Electronics


1‐40 Basics of Digital Electronics
In each case, we insert an extra bit in the leftmost position (MSB) of the code to
produce an even number of 1’s in the character for even parity or an odd number of 1’s
in the character for odd parity. In general, one or the other parity is adopted, with even
parity being more common.
The parity bit is helpful in detecting errors during the transmission of information
from one location to another. This is handled by generating an even parity bit in the
sending end for each character. The 8-bit characters that include parity bits are
transmitted to their destination. The parity of each character is then checked in the
receiving end. If the parity of the received character is not even, it means that at least
one bit has changed value during the transmission.
*Note:
This method detects one, three, or any odd combination of errors in each character
that is transmitted. An even combination of errors is undetected. Additional error
detection codes may be needed to take care of an even combination of errors.

1.13 State different postulates in Boolean algebra.


Boolean algebra may be defined with a set of elements, a set of operators and a
number of postulates Axioms. Anything which is not proved but assumed to be true is
known as postulate. The theorems of the Boolean algebra are derived from these
postulates.
A set of elements is a collection of objects having common property. If N is a set
and x and y are the two objects, then the notation x∈N denotes that x is a member of the
set N and y∉N denotes that y is not a member of the set N. The most common
postulates used to formulate various algebraic structures are as follows.
Postulate 1:
a) An operation + is defined such that if z = x + y then z∈N for every pair of
elements x, y ∈ N .
b) An operation • is defined such that if w = x • y then w∈N for every pair of
elements x, y ∈ N .
Postulate 2:
a) There exists an element 0 in N such that x + 0 = x for every element x∈N.
b) There exists an element 1 in N such that x • 1= x for every element x∈N.
Postulate 3:
A binary operator + or • on a set N is said to be commutative whenever,
x+y=y+x
x•y = y•x For all x, y ∈ N

Maanya’s M.G.B Publications Digital Electronics


Basics of Digital Electronics 1‐41
Postulate 4:
A binary operator + or • on a set N is said to be associative whenever,

(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 9 A=A NOT law

Maanya’s M.G.B Publications Digital Electronics


1‐42 Basics of Digital Electronics
The table below summarizes the multivariable Boolean laws.
Law 10 A + B=B+ A
Commutative laws
Law 11 A . B = B. A

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

Law 18 A+B = A⋅B


De-Morgan’s laws
Law 19 A⋅B = A + B

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:

Maanya’s M.G.B Publications Digital Electronics


Basics of Digital Electronics 1‐43

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)

Maanya’s M.G.B Publications Digital Electronics


1‐44 Basics of Digital Electronics
1.14.2 OR Gate
An OR gate performs logical addition, more commonly known as the OR
function. It has two or more inputs and one output. The standard logic symbol for an
OR gate with two inputs A and B and the output Y is shown in fig. 1.14.2(c). The
Boolean expression for OR function is Y = A + 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.

Maanya’s M.G.B Publications Digital Electronics


Basics of Digital Electronics 1‐45
2. If a low level is applied to the input, a high level appears on the output.
Thus the output of a NOT gate is always complement of the input. The truth
table for a NOT gate is shown in fig. 1.14.3(f).
Inputs Output
A Y=A
0 1

1 0
Fig. 1.14.3(f)

1.15 Explain the working of universal logic gates (NAND,


NOR gates) with truth tables.
1. The NAND and NOR gates are very popular logic gates because they have a
universal function, i.e., they can be used to construct an AND gate, an OR gate, a
NOT gate or any combination of these functions.
2. Hence NAND and NOR gates are known as universal gates.
1.15.1. The NAND Gate
1. A NAND stands for NOT-AND.
2. The NAND gate has two or more inputs but only one output.
3. If the output of AND gate is connected to the input of NOT gate, a NAND gate will
be formed, as shown in fig. 1.15.1(a).
4. Thus a NAND gate is nothing but AND gate followed by NOT gate.
5. The Boolean expression for the two input NAND function is Y = A.B , which is the
complement of the AND function.
6. The fig. 1.15.1(b) shows standard logic symbol of two input NAND gate

Fig. 1.15.1(a) Fig. 1.15.1(b)


The truth table for a two input NAND gate is shown in fig.1.15.1(c).
Inputs Output
A B Y = A.B
0 0 1
0 1 1

Maanya’s M.G.B Publications Digital Electronics


1‐46 Basics of Digital Electronics
1 0 1
1 1 0
Fig. 1.15.1(c)
*Note: The truth table for NAND gate is developed by inverting the output of the AND
gate.
Working
1. The output of NAND gate is high if any one input is low or all inputs are low.
2. The output of NAND gate is low if all inputs are high.
1.15.2. NOR Gate
1. NOR stands for NOT-OR.
2. The NOR gate has two or more inputs but only one output.
3. If the output of OR gate is connected to the input of NOT gate, a NOR gate will be
formed as shown in fig. 1.15.2(d).
4. Thus a NOR gate is nothing but OR gate followed by NOT gate.
5. The Boolean expression for the two input NOR function is Y = A + B , which is the
complement of the OR function.
6. The fig. 1.15.2(e) shows standard logic symbol of two input NOR gate.

Fig. 1.15.2(d) Fig. 1.15.2(e)


7. The truth table for a two input NOR gate is shown in fig. 1.15.2(f).
Inputs Output
A B Y=A+B
0 0 1
0 1 0
1 0 0
1 1 0

Fig. 1.15.2(f)
*Note: The truth table for NOR gate is developed by inverting the output of the OR
gate.

Maanya’s M.G.B Publications Digital Electronics


Basics of Digital Electronics 1‐47

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.

1.16 Explain the working of an exclusive – OR gate with


truth table.
1. The Exclusive OR is abbreviated as EX-OR or X-OR.
2. EX-OR gate has two inputs and one output.
3. It can be constructed by using either basic gates or universal gates.
4. Fig. 1.16(a) shows one way to construct an EX-OR gate.
5. The output expression for this gate is, Y = AB + AB .
6. The symbol used for EX-OR operation is ⊕ .
7. Fig. 1.16(b) shows standard logic symbol for EX-OR gate.

Fig. 1.16(a) Fig. 1.16(b)


Working
1. The output of an EX-OR gate is high for odd number of 1’s at the input.
2. The output of an EX-OR gate is low for even number of 1’s at the input.

The truth table for an EX-OR gate is shown below.


Inputs Output
A B Y = AB + AB
0 0 0

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.

Maanya’s M.G.B Publications Digital Electronics


1‐48 Basics of Digital Electronics

1.17 State De-Morgan’s theorems


Augustus De-Morgan used the Boolean algebra and discovered two important
theorems known widely as De-Morgan’s theorems as follows.
1. First Theorem
The complement of sum is equal to the product of the individual complements.
i.e., A + B = A. B
It really says that the complement of two or more variables ORed is the same as
the AND of the complements of each individual variable.
∴ A + B + C + D + ........ = A ⋅ B ⋅ C ⋅ D ⋅ ........
2. Second Theorem
The complement of product is equal to the sum of the individual complements.
i.e., A.B = A + B
It really says that the complement of two or more variables ANDed is the same as
the OR of the complements of each individual variable.
∴ A ⋅ B ⋅ C ⋅ D ⋅ ........ = A + B + C + D + ........

1.18 Realize AND, OR, NOT operations using NAND, NOR


gates.
1.18.1. AND, OR, NOT using NAND gate
A NAND gate is known as universal gate because it can 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 NAND Gate
A NOT gate may be obtained from NAND gate by connecting all of its inputs of a
NAND gate together as shown in fig. 1.18(a).

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)

Maanya’s M.G.B Publications Digital Electronics


Basics of Digital Electronics 1‐49

c) OR Gate by using NAND Gates


An OR gate can be obtained by connecting the outputs of two NOT gates (using
NAND) to the inputs of a NAND gate is shown in fig. 1.18(c).

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.

Maanya’s M.G.B Publications Digital Electronics


1‐50 Basics of Digital Electronics

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

Maanya’s M.G.B Publications Digital Electronics


Basics of Digital Electronics 1‐51

1.19 Apply De-Morgan’s theorems and other postulates to


simplify Boolean expressions (up to three variables).
The following procedure illustrates the application of De-Morgan’s theorems
using a specific expression:

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

Example 71: Simplify the following Boolean function

(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.

Maanya’s M.G.B Publications Digital Electronics


1‐54 Basics of Digital Electronics
1. In the saturated bipolar logic families, the transistor in the IC is driven into
saturation, whereas in the case of the non-saturated bipolar logic families the
transistors are driven into saturation.
2. NMOS stands for N-type metal–oxide–semiconductor logic. In this logic family, N-
channel MOSFETs are used.
3. PMOS stands for P-type metal–oxide–semiconductor logic. N-channel MOSFETs
are used in this uni-polar logic family.
4. (CMOS) stands for complementary metal–oxide semiconductor logic. Both N-
channel and P-channel MOSFETs are used here.
5. BiCMOS stands for Bipolar complementary metal–oxide–semiconductor logic.

1.21. List the important characteristics of Digital ICs of


different logic families.
The important characteristics of digital ICs are listed below;
1. Input and output logic levels
2. Propagation delay
3. Noise Margin
4. Noise immunity
5. Fan In
6. Fan Out
7. Power dissipation
8. Operating temperature
9. Power supply requirements

1.22. Explain logic levels and Voltage requirements of TTL


and CMOS ICs.
1 Logic Levels of CMOS ICs
CMOS ICs are widely available in two versions. They are +5V ICs and +3.3V ICs.
The logic levels of both the ICs are listed below.
a) +5V CMOS IC
• The input and output logic levels for a +5V CMOS IC is shown in the fig. 1.22(a).
• The ranges of input voltages (VIL) that can represent a valid LOW (logic 0) are from
0 to 1.5V.

Maanya’s M.G.B Publications Digital Electronics


Basics of Digital Electronics 1‐55
• The ranges of input voltages (VIH) that can represent a valid HIGH (logic 1) are
from 3.5V to 5V.
• The ranges of output voltages (VOL) that represents a valid LOW (logic 0) are from 0
to 0.33V.
• The ranges of output voltages (VOH) that represents a valid HIGH (logic 1) are from
4.4V to 5V.

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.

2 Logic Levels of TTL ICs


• The input and output logic levels for a CMOS IC is shown in the fig. 1.22(c).

Fig. 1.22(c)

3 power supply requirements of TTL and CMOS ICs


• The nominal values of the dc supply voltages for TTL devices are +5V.
• CMOS (complementary metal-oxide semiconductor) devices are available in two
different supply voltage categories, +5V and +3.3V, the latter is known as low
voltages CMOS.

1.23. Define the terms: propagation delay, Noise margin,


Fan-in, Fan-out, Power dissipation of digital ICs.
1. Logic levels of digital ICs
a) High-level input voltage (VIH (min)): It is the minimum voltage level required
for a logical 1 at an input. Any voltage below this level will not be accepted as a
high by the logic circuit.
b) Low-level input voltage (VIL (max)): It is the maximum voltage level required
for a logic 0 at an input. Any voltage above this level will not be accepted as a
LOW by the logic circuit.
Maanya’s M.G.B Publications Digital Electronics
Basics of Digital Electronics 1‐57
c) High-level output voltage (VOH (min)): It is the minimum voltage level at a
logic circuit output in the logical 1 state under defined load conditions.
d) Low-level output voltage (VOL (max)): It is the maximum voltage level at a
logic circuit output in the logical 0 state under defined load conditions.
e) High-level input current (IIH): It is the current that flows into an input when
a specified high-level voltage is applied to that input.
f) Low-level input current (IIL): It is the current that flows into an input when a
specified low-level voltage is applied to that input.
g) High-level output current (IOH): It is the current that flows from an output in
the logical 1 state under specified load conditions.
h) Low-level output current (IOL): It is the current that flows from an output in
the logical 0 state under specified load conditions.
*Note: The current directions shown in the fig. 1.23 may be opposite to those shown
depending on the logic family.

Fig. 1.23(a) & (b)


2. Propagation Delay
• It is defined as the time taken by a signal to propagate from input to output when
the input signals changes.
• In other words, it is the time interval between a change in input state and the
resulting change in the output state of an IC.
• Shorter the propagation delay, higher is the speed of the circuit and vice versa.

Maanya’s M.G.B Publications Digital Electronics


1‐58 Basics of Digital Electronics
3. Noise Immunity
• The noise immunity of a logic circuit refers to the circuit’s ability to tolerate noise
without causing spurious changes in the output voltage.
4. Noise margin
• It is the maximum external noise voltage that can be added to an input signal so
that it does not cause an undesirable change in the circuit output.
• A quantitative measure of noise immunity is called noise margin.
5.Fan-in
• The maximum number of inputs that can be connected to a logic circuit is called
fan-in of the logic circuit.
6. Fan-out
• The fan-out of a logic circuit is defined as the maximum number of outputs the
circuit may have and still operate properly.
• It is also called loading factor.
• It is a limiting factor which determines the number of loads that the given circuit
can drive.
7. Power Dissipation
• Every IC requires a certain amount of electrical power for its operation.
• The power taken by a logic gate during its operation is called power dissipation.
• This parameter is very important because if the power dissipation is very large,
then the life period of the IC is reduced.
• The power dissipation of a logic gate is given by,
PD = VCC x IC
Where, PD = power dissipation of the gate
VCC = DC supply voltage
IC = average supply current

1.24. Explain the working of open collector TTL NAND gate


with circuit diagram.
1. Circuit details
• The basic circuit of TTL family is the NAND gate.
• The circuit diagram of a TTL open collector NAND gate is shown in the fig. 1.24.1.

Maanya’s M.G.B Publications Digital Electronics


Basics of Digital Electronics 1‐59
• Q1 is a multiple emitter transistor and each emitter of the transistor Q1 acts as the
input of the NAND gate.
• Since nothing is connected at the collector of the output transistor (Q4), it is known
as open collector output. This collector terminal is available as IC pin.

+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.

Maanya’s M.G.B Publications Digital Electronics


1‐60 Basics of Digital Electronics
• This permits current through R 1 and the base-collector junction of Q1 into base
of Q 2 .
• Thus transistor Q 2 is turned ON.
• Due to Q 2 is ON, Q 4 gets sufficient base driven voltage from VCC and enters into
saturation. Thus the output goes to low level.

1.25. Explain the working of Totem pole output TTL NAND


gate with circuit diagram.
1. Circuit details
• A 2-input standard TTL NAND gate with totem-pole output is shown in fig. 1.25.1.
• Q1 is a multiple-emitter transistor for the inputs A and B.
• The output transistors Q 3 , Q 4 and diode D form a totem pole arrangement.
• It is specially designed to reduce propagation delay (and hence increases the
operating speed) and to increase the output current capability of this circuit.

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.

Maanya’s M.G.B Publications Digital Electronics


Basics of Digital Electronics 1‐61
• 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 collector of Q 2 is high and turns Q 3 into saturation. The output is thus high.
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.
• This permits current through R 1 and the base-collector junction of Q1 into base of
Q2 .
• Thus transistor Q 2 is turned ON. As a result Q 4 is turned ON by Q 2 , and
producing low output which is near ground potential.
• At the same time, the collector of Q 2 is sufficiently at low voltage level to keep Q 3
in OFF.
*Note:
1. In the high output state Q 3 is ON and Q 4 is OFF. In the low output state Q 3 is
OFF and Q 4 is ON. But Q 3 and Q 4 are never simultaneously ON.
2. The purpose of diode D is it does not allow the base-emitter junction of Q 3 to be
forward-biased and thus ensures that Q 3 remains OFF when Q 4 is ON.

1.26. Compare the TTL, CMOS and ECL logic families.


Logic families
Parameters
Standard TTL ECL CMOS

1. Basic gate NAND OR-NOR NOR or NAND

2. Fan out 10 25 >50

3. Power dissipated per gate(mw) 10 40-55 0.01 static at 1MHz

4. Noise immunity Very good Good Very good

5. Propagation delay per gate(ns) 10 1-2 70

6. Clock rate(MHz) 15-60 60-400 5

Maanya’s M.G.B Publications Digital Electronics


1‐62 Basics of Digital Electronics

1.27. List IC numbers of two input Logic gates.


Sl.No. IC Description
Number
1 7400 Quad 2-input NAND (i.e. it contains 4 independent two-input
NAND-gates).
2 7402 Quad 2-input NOR (i.e. it contains 4 independent two -input
NOR gates).
3 7408 Quad 2-input AND (i.e. it contains 4 independent two -input
AND gates).
4 7432 Quad 2-input OR (i.e. it contains 4 independent two -input OR
gates).
5 7486 Quad 2-inputs EX-OR (i.e. it contains 4 independent two -
input EX-OR gates).

Maanya’s M.G.B Publications Digital Electronics


2
Combinational Logic circuits

OBJECTIVES
Upon completion of the chapter the student should be able to Understand
the working of Combinational Logic circuits.

2.1 Give the concept of combinational logic circuits.


2.2 Draw the Half adder circuit and verify its functionality using truth table.
2.3 Realize a Half-adder using NAND gates only and NOR gates only.
2.4 Draw the full adder circuit and explain its operation with truth table.
2.5 Realize full-adder using two Half-adders and an OR – gate and write truth table
2.6 Draw and explain a 4 Bit parallel adder using full – adders.
2.7 Draw and Explain 2‘s compliment parallel adder/ subtractor circuit.
2.8 Explain the working of a serial adder with a Block diagram.
2.9 Compare the performance of serial and parallel adder.
2.10 Draw and explain the operation of 4 X 1 Multiplexers
2.11 Draw and explain the operation of 1 to 4 demultiplexer.
2.12 Draw and explain 3 X 8 decoder.
2.13 Draw and explain BCD to decimal decoder.
2.14 List any three applications of multiplexers and decoders.
2.15 Draw and explain One bit digital comparator.
2‐2 Combinational Logic circuits

2.1. Explain the concept of combinational logic circuits.


• Any digital circuits can be classified into two types
1. Combinational logic circuits
2. Sequential logic circuits
a) Combinational logic circuits:
• In these circuits, the outputs at any instant of time depend on the inputs present at
that instant only.
• The block diagram of a combinational logic circuit is shown in the fig. 2.1(a).

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.

Maanya’s M.G.B Publications Digital Electronics


Combinational Logic circuits 2‐3

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)

2.3. Explain Half-adder using i) NAND gates only and ii)


NOR gates only.
2.3.1. Half Adder using NAND Gates only
The fig. 2.3(a) shows the construction of half adder using 6 NAND gates.

Case – 1: Refer to fig. 2.3(a), Sum = A AB B AB

Maanya’s M.G.B Publications Digital Electronics


2‐4 Combinational Logic circuits
⎛ ⎞ ⎛ ⎞
( ) (
= ⎜ A AB ⎟ + ⎜ B AB ⎟ = A A + B + B A + B )
⎝ ⎠ ⎝ ⎠
= A A + A B + AB + BB
∴ Sum = A B + AB
or Sum = A ⊕ B
Case – 2: Refer to fig. 2.3(a), Carry = AB = AB

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)

Case – 1: Refer to fig. 2.3(b), Sum= 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 (Q A ⋅ A = B ⋅ B = 0 )

Maanya’s M.G.B Publications Digital Electronics


Combinational Logic circuits 2‐5

∴ Sum = A B + AB = A ⊕ B
Case – 2: Refer to fig. 2.3(b), Carry = A + B = A B = AB

2.4. Explain the operation of Full adder circuit with truth


table using basic gates.
• Full adder is also a combinational logic circuit.
• The full adder accepts three inputs and generates a sum and a carry output. In
simply it can add three bits at a time and produce sum and carry.
• The logic symbol for a full adder is shown in fig. 2.4(a).
A Sum
Full
B
Adder Carry
C

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 )

Maanya’s M.G.B Publications Digital Electronics


2‐6 Combinational Logic circuits

( )
= 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)

2.5. Explain realization of full-adder using two Half-adders


and an OR gate
• The logic diagram for the full adder can also be implemented with two half adders
and one OR gate as shown in fig. 2.5(a).
• The actual circuit of a full adder is shown in fig. 2.5(b) in which the two half adders
are replaced by their EX-OR and AND gates.
• The sum output from the second half adder is the EX-OR of C and the sum output
of the first half adder, giving

Maanya’s M.G.B Publications Digital Electronics


Combinational Logic circuits 2‐7
Sum = (A ⊕ B) ⊕ C
• The carry output is,
Carry = AB + (A ⊕ B)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

Maanya’s M.G.B Publications Digital Electronics


2‐8 Combinational Logic circuits

2.6. Explain the working of 4 Bit parallel adder using full


adders.
• A digital circuit that adds two binary numbers at a time and produce sum and carry
is called parallel adder.
• For the addition of two n bits of data, n numbers of full adders can be cascaded.
• The 4-bit parallel adder or ripple carry adder is constructed by cascading 4-full
adders (FA) blocks in series as shown in fig. 2.6.
• Let A 3A 2 A1A 0 and B3B2 B1B0 be the two binary numbers to be added. In the given
numbers, A 3 and B 3 are the MSBs and A 0 and B 0 are the LSBs.
• From the fig. 2.6, it can be seen that all the bits are given at a time to the adder and
the result is get immediately. Hence, it is known as the parallel adder.
• When we want to add two numbers, first we should add the LSBs (A0 and B0)
giving us the sum (S0 ) and the carry (C 0 ) .
• This carry has to be added to the next higher significant position bits ((A1 and B1)
giving us the sum (S1 ) and carry(C1 ) .

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.

Maanya’s M.G.B Publications Digital Electronics


Combinational Logic circuits 2‐9

Additional Information

N-Bit Parallel Adder


1. The Full Adder is capable of adding only two single digit binary number along
with a carry input. But in practical we need to add binary numbers which are much
longer than just one bit.
2. To add two n-bit binary numbers we need to use the n-bit parallel adder.
3. It uses a number of full adders in cascade. The carry output of the previous full
adder is connected to carry input of the next full adder.
4. The most basic arithmetic operation is the addition of two binary digits, i.e. bits.
5. A combinational circuit that adds two bits, according the scheme outlined below, is
called a half adder.
6. A full adder is one that adds three bits, the third produced from a previous
addition operation.

2.7. Explain 4 bit parallel adder cum 2’s compliment


subtractor circuit.
2.7.1. Circuit Details

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).

Maanya’s M.G.B Publications Digital Electronics


2‐10 Combinational Logic circuits
• When A/S =1, the circuit acts as a Subtractor, the number B3B2B1B0 is subtracted
from A3A2A1A0 and gives Difference (D3D2D1D0).
2.7.2. Working
Case - I: Adder
• When A/S =0, the output of EX-OR gates will be same as the B data bits. The carry
input to the first full adder is 0 (because A/S =0).
• Hence A and B bits are applied as the two inputs to the parallel adder.
• Therefore the full adders produce the sum S = A + B. Note that the final carry
output (C3) has no significance here.
Case - II: Substractor
• When A/S =1, then the output of the EX-OR gates will be complemented of B data
bits. The carry input to the first adder is now 1 (because A/S =1).
• The addition of 1 to the 1’s complement of B bits results 2’s complement of B. Hence
A and 2’s complemented B bits are applied as the inputs to the parallel adder.
• The circuit performs the operation A plus the 2’s complement of B, i.e. A-B. Note
that the final carry output from the last full adder is neglected.
• Thus the circuit can be used as an adder as well as a Subtractor. This simplifies the
overall hardware required to implement the arithmetic circuit.

2.8. Explain the working of a serial adder with block


diagram.
• The circuit diagram of a serial adder by using only one full adder (FA) as shown in
fig. 2.8.

Fig. 2.8

Maanya’s M.G.B Publications Digital Electronics


Combinational Logic circuits 2‐11
• The numbers to be added are applied at the input bit by bit and added.
• This means that the first two LSBs (A0 & B0) are applied at the inputs of the FA.
• All the operations have to be carried out in synchronization with a clock pulse.
• Initially the circuit is reset, by applying low input at CLR .
• When the first clock pulse is given the LSBs of the two numbers to be added (i.e. A0
& B0) and we get the sum bit (S0) and the carry bit (C0).
• The sum bit is taken out and stored.
• The carry bit is given to the input of a delay circuit (D Flip-Flop).
• When the next clock pulse is given, this carry bit (C0) is applied at the third input of
the full adder along with the two bits of the binary numbers to be added (i.e. A1 &
B1).
• These three bits are now added and the sum (S1) and carry bit (C1) got.
• This process is repeated until all the bits of the given numbers are added.

2.9. Compare the performance of serial and parallel adder.


The comparison between parallel adder and serial adder is shown in the form of
a table.
Sl.No Parallel Adder Serial Adder
1 All the bits of two numbers are added The bits are added serially
simultaneously. one after the other.
2 The number of full adders in the parallel adder The serial adder requires
equal to the number of bits in the binary only one full adder.
numbers.

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.

6 Excluding the registers, the parallel adder is a The serial adder is a


purely combinational logic circuit. sequential logic circuit.

Maanya’s M.G.B Publications Digital Electronics


2‐12 Combinational Logic circuits

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)

2.10. Draw and explain 4x1 Multiplexer.


• The logic symbol of a 4 X 1 multiplexer is shown in the fig. 2.10(a) and the logic
circuit of a 4 X 1 multiplexer is shown in the fig. 2.10(b).
• The logic level applied to the control inputs determines which AND gate is enabled
so that its data input passes through the OR gate to output Y.
• When S0 S1 = 00, the upper AND gate is enabled but all other AND gates are
disabled. Therefore, data bit D0 is transmitted to the output, giving, Y = D0.
• If the control word is changed to S0 S1 = 11, the bottom gate is enabled and all other
gates are disabled. In this case: Y = D3.

Maanya’s M.G.B Publications Digital Electronics


Combinational Logic circuits 2‐13
• In the same manner the other combinations of the truth table can be verified. The
truth table for 4 x 1 MUX is shown in fig. 2.10(c).

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)

Maanya’s M.G.B Publications Digital Electronics


2‐14 Combinational Logic circuits

2.12. Mention any 3 applications of multiplexers.


The following are the important multiplexer applications:
1. The logic function generator.
2. The seven segment display multiplexer.
3. Digital counter with multiplexed display.
4. Data selector.
5. Parallel to serial convertor.
6. Waveform generator.

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.

2.13. Draw and explain 1x4 De-multiplexer


• The logic symbol and circuit diagram of 1 x 4 De-MUX is shown in fig. 2.13(a) & (b).
• It has 1 input and 4 output lines. It accepts a single input and sends it to one out of
4 output lines which are selected by control lines S0 and S1. It may be noted that the
data input line goes to all of the AND gates.

Maanya’s M.G.B Publications Digital Electronics


Combinational Logic circuits 2‐15
• When both the selection lines (S0 & S1) are at 0, the first AND gate is enabled and all
the other AND gates are disabled.
• The output of the first AND gate is 0 if Din is 0 and 1 if Din is 1.
• Hence it can be seen that the output of the AND gate is same as Din.
• In the same manner the other combinations of the truth table can be verified.
• The truth table for 1 x 4 De-MUX is shown in fig. 2.13(c).

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)

Maanya’s M.G.B Publications Digital Electronics


2‐16 Combinational Logic circuits

2.15. Mention any 3 applications of De-multiplexers.


De-multiplexers are used in
1. Boolean function implementation
2. Data transmission
3. Combinational logic circuit design
4. Generate enable signals in microprocessor systems
5. Communication systems
6. Arithmetic and Logical Units
7. Serial to Parallel converters
8. Decoder circuits

2.16. Explain the working of 3x8 decoder circuit.


2.16.1. Introduction
A decoder is a combinational circuit that converts binary information from n input
lines to a maximum of m ( ≤2n) output lines. In other words a decoder converts binary
data into other form viz. decimal, octal, hexadecimal etc.
Fig. 2.16. shows the block diagram of a decoder with n inputs and m ( ≤2n)
outputs. Examples: a) 2 x 4 decoder b) 3 x 8 decoder c) 4 x 10 decoder d) 4 x 16 decoder,
etc.

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.

Maanya’s M.G.B Publications Digital Electronics


Combinational Logic circuits 2‐17

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.

Maanya’s M.G.B Publications Digital Electronics


2‐18 Combinational Logic circuits
1. The inputs to the top AND gate are A, B, and C .
∴ Y0 = A B C
2. The inputs to the next AND gate are A, B and C.
∴ Y1 = A B C
3. Analyzing the remaining gates gives:
Y2 = A B C Y3 = A B C Y4 = A B C Y5 = A B C
Y6 = A B C and Y7 = A B C

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.

Maanya’s M.G.B Publications Digital Electronics


Combinational Logic circuits 2‐19

2.17. Mention any 3 applications of decoders


The decoders are used in the following applications:
1. Binary to octal conversion.
2. BCD to decimal conversion.
3. Hexadecimal to binary conversion.
4. BCD to gray code or gray to BCD conversion.

2.18. Explain the working of BCD to decimal decoder


1. Circuit Details
The decoder which converts BCD to decimal is known as BCD to decimal (4 x 10
decoder) decoder. Fig. 2.18.(a)&(b) shows logic symbol and circuit diagram of 4 x 10
decoder. The circuit of this example is also called a 4 line to 10 line decoder because
there are 4 input lines and 10 output lines. Another name for it is a 1 of 10 decoder
because only 1 of 10 output lines is activated at a time.
Each AND gate forms the logical product of its input signals. The inputs to the top
AND gate are A, B , C and D ; therefore;
Y0 = A B C D
The input to the next AND gate are A, B , C and D ; this means that,
Y1 = A B C D
Analyzing the remaining gates gives,
Y2 = A B C D
Y3 = A B C D
Y4 = A B C D
Y5 = A B C D
Y6 = A B C D
Y7 = A B C D
Y8 = A B C D
Y9 = A B C D

Maanya’s M.G.B Publications Digital Electronics


2‐20 Combinational Logic circuits

Fig. 2.18.(a)

Fig. 2.18(b)

Maanya’s M.G.B Publications Digital Electronics


Combinational Logic circuits 2‐21

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.

2.15 Draw and explain One bit digital comparator.


• A digital magnitude comparator is a combinational logic circuit that compares two
binary numbers and determines whether the two numbers are equal or not. It can
also determine which one is larger and which one is smaller, when two given
numbers are not equal.

Maanya’s M.G.B Publications Digital Electronics


2‐22 Combinational Logic circuits
• The EX-OR gate is a basic comparator because its output is 1, if its two input bits
are not equal and 0 if the inputs are equal. Fig. 2.25 shows the EX-OR gate as a 1-bit
digital comparator.

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)

Maanya’s M.G.B Publications Digital Electronics


Combinational Logic circuits 2‐23
Case – 5:
• Using the logic expressions (1), (2) and (3), single bit comparator can be realized
using logic gates as shown in fig. (c).

Fig. (c)

Maanya’s M.G.B Publications Digital Electronics


3
Sequential Logic Circuits

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

3.1. Explain the concept of Sequential logic circuits


1. In the sequential logic circuits the output not only depends on the present inputs,
but also on the previous outputs.
2. Gates and flip-flops are required for constructing sequential logic circuits.
3. Examples: Shift registers, counters, serial adder, RAM etc.
4. Memory element is required to store the past history of input variables in
sequential logic circuits. Flip-flops are used as memory elements.
5. Fig. 3.1 shows the block diagram of sequential logic circuit. As shown in fig. 3.1
memory elements are connected to the combinational circuit as a feedback path.

Fig. 3.1
6. Sequential logic circuits are slower than the combinational logic circuits.

Additional Information

Types of sequential logic circuits


The sequential logic circuits can be classified in to two types
1. Synchronous sequential logic circuits
2. Asynchronous sequential logic circuits
1. Synchronous sequential logic circuits
Synchronous sequential logic circuits use pulsed or level inputs and a clock input
to drive the circuit.

Fig. 3.1(a)

Maanya’s M.G.B Publications Digital Electronics


Sequential Logic Circuits 3‐3
2. Asynchronous sequential logic circuits
Asynchronous sequential circuits do not use a clock signal as synchronous circuits
do. Instead the circuit is driven by the pulses of the inputs only.

Fig. 3.1(b)

3.2. Explain NAND and NOR latches with truth tables


3.2.1. R-S Latch using NOR gates
Fig. 3.2(a) shows the R-S latch using two NOR gates. As shown in fig. 3.2(a), the
two NOR gates are cross coupled so that the output of NOR gate 1 is connected to one
of the inputs of NOR 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(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.

Maanya’s M.G.B Publications Digital Electronics


3‐4 Sequential Logic Circuits
2. When R = 0 and S = 1, the output of latch is set to logic 1, i.e. Q = 1.
3. When R = 1 and S = 0, the output of latch is reset to logic 0 i.e. Q = 0.
4. When R and S inputs both are high, output is unpredictable. This is called
forbidden state, invalid state, not used state or prohibited state.
5. The table shown in fig. 3.2(c) illustrates the operation of R-S latch using NOR gates.
Inputs Outputs
State
R S Q Q

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).

Maanya’s M.G.B Publications Digital Electronics


Sequential Logic Circuits 3‐5

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)

3.3. State the necessity of clock


• A clock plays very important role in digital circuits.
• It is used to open and close digital paths, allow or stop a process and in general
provide timing for the circuit.

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).

Maanya’s M.G.B Publications Digital Electronics


3‐6 Sequential Logic Circuits
*Note: In asynchronous system, the outputs of logic circuits can change state any time,
when one or more of the inputs change. An asynchronous system is difficult to design
and troubleshoot.

3.4. Differentiate between level clocking and edge triggering


• The flip-flops using the clock signal are called the clocked flip-flops.
• Clocked flip-flops have a clock input that is typically labeled CLK or CP.
• Clocked flip-flops may be level clocked or edge triggered.
3.4.1. Concept of Level Clocking
• The flip-flops will responds to the changes in inputs only when the clock is high or
low are called level clocked flip-flops.
• Fig. 3.4(a) shows logic symbols of positive level clocked flip-flops.

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.

Maanya’s M.G.B Publications Digital Electronics


Sequential Logic Circuits 3‐7

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.

3.5. Explain clocked SR flip flop using NAND gates


Computers use thousands of flip-flips. To coordinate the overall action, a square
wave signal called the clock is sent to each flip-flop. This signal prevents the flip-flops
from changing states until the right time.

Fig. 3.5(a) Fig. 3.5(b)

Maanya’s M.G.B Publications Digital Electronics


3‐8 Sequential Logic Circuits
Positive level clocked S-R flip-flop and its symbol are shown in fig. 3.5(a) & (b)
respectively. NAND gates 1 and 2 form a basic latch, NAND gates 3 and 4 are called
steering gates or control gates, because they are used to drive a NAND latch.
1. Working
Case - I
As long as the clock is at low level (i.e. CLK =0), the NAND gates 3 and 4 are
disabled irrespective of S and R inputs. Thus the output remains in its previous state i.e.
Q = Q0 .
Case - II
When clock is at high level (i.e. CLK = 1), the inputs control the output (Q) as
follows:
1. If S = 0 and R = 0, then the outputs of NAND gates 3 and 4 at high due to this, the
latch will not change its output state i.e. Q = Q0.
2. If S = 0 and R = 1, then S = 1 and R = 0 . With these inputs, the latch is reset to 0
irrespective of its previous state.
3. If S = 1 and R = 0, then S = 0 and R = 1 . With these inputs, the latch is set to 1
irrespective of its previous state.
4. The case S = 1 and R = 1 is not used as it is ambiguous or invalid state.
Inputs Outputs
State
CLK S R Q
0 x x Q0 No change
1 0 0 Q0 No change
1 0 1 0 Reset
1 1 0 1 Set
1 1 1 * Forbidden
Fig. 3.5(c)
2. Timing Diagram

Fig. 3.5(d)

Maanya’s M.G.B Publications Digital Electronics


Sequential Logic Circuits 3‐9
Fig. 3.5(d) shows the timing diagram for clocked S-R flip-flop. Q goes high when S
is high and CLK goes high. Q returns to the low state when R is high and CLK goes
high. Using a common CLK signal to drive many flip-flops allows us to synchronize the
operation of the different sections of a computer.

3.6. State the need for preset and clear inputs


1. The asynchronous inputs can be used to set the flip-flop to the logic 1 state or reset
the flip-flop to the logic 0 state at any time regardless of the conditions at the other
inputs.
2. In other words, we can say that the asynchronous inputs are the override inputs,
which can be used to override all the other inputs in order to place the flip-flop in
one state or the other.
3. They are normally labeled PRE(preset) and CLR(clear).
4. An active level on the preset input will set the flip-flop and an active level on the
clear input will reset it.
5. If the asynchronous inputs are active-low, the same is indicated by a small bubble
at the input terminals. The inputs in that case are labeled as PRE and CLR .

3.7. Explain level clocked JK flip flop (using S-R flip-flops)


with truth table and race around condition
3.7.1 JK flip-flop
The invalid state that occurred in S-R flip-flop when S = 1 and R = 1, can be
eliminated by J-K flip-flop. The S-R flip-flop can be converted to J-K flip-flop by making
S = J ⋅ Q and R = K ⋅ Q as shown in fig. 3.7(a). Inputs J and K behave like inputs S and R
to set and reset the flip-flop.
The J-K flip-flop differs from S-R flip-flop in that the outputs Q and Q are
connected back to the input AND gates in a cross coupled fashion. Thus the input for
AND gate 1 is J and Q and to the AND gate 2 is K and Q .

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.

Maanya’s M.G.B Publications Digital Electronics


Sequential Logic Circuits 3‐11
The truth table shown in fig. 3.7(c) summarizes how the J-K flip-flop responds to
the positive clock pulse for each combination of J and K. Notice that the truth table is the
same as for the clocked S-R flip-flop except for the J = K = 1 condition.
Inputs Output
State
CLK J K Q
0 x x Q0 No change

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.

3.8. Explain the master slave JK flip flop with necessary


diagrams
1. The master slave J-K flip-flop avoids the race around problem.
2. It consists of a clocked J-K flip-flop as a master and clocked S-R flip-flop as a slave.

Maanya’s M.G.B Publications Digital Electronics


3‐12 Sequential Logic Circuits
3. The output of the master flip-flop is fed as an input to the slave flip-flop. The
output of the slave flip-flop is connected as a third input of the master J-K flip-flop.

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

Maanya’s M.G.B Publications Digital Electronics


Sequential Logic Circuits 3‐13
0 0 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.

Maanya’s M.G.B Publications Digital Electronics


3‐14 Sequential Logic Circuits
5. As shown in fig. 3.9(a) D input goes directly to the S input, and its complement is
applied to the R input through NAND gate 5. Therefore, only two input conditions
exists, either S = 0 and R = 1 or S = 1 and R = 0.

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.

Maanya’s M.G.B Publications Digital Electronics


Sequential Logic Circuits 3‐15
b) Timing Diagram

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).

Maanya’s M.G.B Publications Digital Electronics


3‐16 Sequential Logic Circuits
Inputs Output
State
CLK T Qn+1
0 x Qn No change
1 0 Qn No change
1 1 Qn Toggle

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)

Maanya’s M.G.B Publications Digital Electronics


Sequential Logic Circuits 3‐17

3.10. Give the concept of edge triggering using RC


differentiator
3.10.1 Concept of edge triggering using RC differentiator
• A clock pulse goes from 0 to 1 then returns from 1 to 0.
• The fig. 3.10(a) shows the two transitions and they are defined as the positive edge
(0 to 1 transition) and the negative edge (1 to 0 transition).
• The term edge-triggered means that the flip-flop changes its state only at either the
positive or negative edge of the clock pulse.

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.

Maanya’s M.G.B Publications Digital Electronics


3‐18 Sequential Logic Circuits
3.10.2. Positive edge triggering D latch

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.

3.11. Give only the truth tables of edge triggered D and T


flip flops with symbols
3.11.1. Truth Table of Edge Triggered D Flip-flop
Case – 1: The table shown in fig. 3.11(a) summarizes the operation of the positive edge
triggered D flip-flop.
Inputs Output
State
CLK D Q
0 x Q0 No change
1 x Q0 No change
↓ x Q0 No change
↑ 0 0 Reset
↑ 1 1 Set
Fig. 3.11(a)

Maanya’s M.G.B Publications Digital Electronics


Sequential Logic Circuits 3‐19
Case – 2: The table shown in fig. 3.11(b) summarizes the operation of the negative edge
triggered D flip-flop.
Inputs Output
State
CLK D Q
0 x Q0 No change

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)

3.12. List the applications of flip flops


There are large numbers of applications of flip-flops. Some of the basic
applications are:
1. SR flip flops
• Switch debouncers
• Memory storage devices
• counters
2. JK flip flops
• Shift registers
• Binary counters
• Frequency dividers
• Sequence detectors
• Serial-to-parallel conversion
• Parallel-to-serial conversion
3. D flip flops
• Data latches
• Delay circuits
• Frequency dividers
4. T flip flops
• General binary addition devices
• Frequency dividers
• Binary counters

3.13. Give the definition of modulus of a counter


3.13.1. Modulus of a Counter
The modulus of a counter is the number of output states it has. For example mod-
8 counter is having 8 different states (000 to 111). The number of flip-flops (n) required
to construct mod-N counter can be obtained from the following formula:
2 n −1 < N ≤ 2 n

Maanya’s M.G.B Publications Digital Electronics


Sequential Logic Circuits 3‐21
Example: Three flip-flops are required to construct mod-8 counter. A decade counter is
also called Mod-10 or ÷ 10 counter require 4 flip-flops. Any binary counter can be a
modulus counter where as the modulus counter need not be a binary counter.
The output signal frequency of mod-N counter is 1/ N th of the input clock
frequency. Hence that counter is also called ÷ N counter.
*Note: A counter having n flip-flops can have 2 n output states i.e. it can count 2 n clock
pulses (0 to 2 n -1). The largest binary number that can be represented by an n-bit
counter has a decimal equivalent of ( 2 n -1).

3.14. Draw and explain the working of 4-bit asynchronous


counter with Timing diagram
• A 4-bit asynchronous counter (Mod-16) is constructed using four negative edge
triggered J-K flip-flops as shown in fig. 3.14(a).
• The clock drives the first flip-flop. The normal output of each flip-flop is connected
to clock input of the next flip-flop.
• All J and K inputs are tied to supply voltage so that J=K=1. With this connection all
J-K flip-flops operates as toggle flip-flop. Here each flip-flop changes state (toggle)
at the negative going edge of the input clock pulse.

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 .

Maanya’s M.G.B Publications Digital Electronics


3‐22 Sequential Logic Circuits
• After the first clock pulse the output is 0001 as shown in count sequence table of
fig. 3.14(b).
Case - II:
• When second clock pulse is given to the first flip-flop it toggles the output from 1 to
0.
• Since the output changes from 1 to 0, it constitutes a negative change and is applied
as clock pulse for second flip-flop, and hence it toggles the output from 0 to 1.
• It constitutes positive change and hence the third and fourth flip-flops are not
affected. The output after the second clock pulse is 0010.
• This continuous for each clock pulse such that for the 15th clock pulse the output is
1111.
Case - III:
• When the 16th clock pulse is applied to the first flip-flop it toggles the output from 1
to 0.
• This negative change in the output of the first flip-flop causes the second, third and
fourth flip-flops to toggle its output from 1 to 0.
• Thus the flip-flops are reset once again and the output becomes 0000 after 16th clock
pulse.
• This cycle continues for every 16 clock pulses.
The table shown in fig. 3.14(b) summarizes the operation of mod-16 ripple
counter.
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

Maanya’s M.G.B Publications Digital Electronics


Sequential Logic Circuits 3‐23

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)

3.15. Draw and explain the working of asynchronous


decade counter with Timing diagram
• The decade counter is nothing but a mod-10 counter. This counter counts from 0000
to 1001(0 to 9) and again starts with 0000.
• This counter is very helpful in counting the decimal numbers. The fig. 3.15(a)
shows decade counter.
• In order to count from 0 to 9, a counter with 3 flip-flops is not sufficient. With 4 flip-
flops one can count from 0 to 15 (16 states). Out of those 16 states, we should skip
any 6 states.
• The fig. 3.15(b) shows the truth table of the decade counter skipping the last 6 states
(from 10 to 15).

Maanya’s M.G.B Publications Digital Electronics


3‐24 Sequential Logic Circuits
• The timing diagram is also shown in fig. 3.15(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

Maanya’s M.G.B Publications Digital Electronics


Sequential Logic Circuits 3‐25

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)

3.16. Draw and explain the working of 4-bit synchronous


counter with Timing diagram
The fig. 3.16(a) shows 4-bit synchronous counter. The counting sequence for this
counter is shown in fig. 3.16(b).
In synchronous counter, we must note the following:
1. The clock signal is applied to all the flip-flops simultaneously.
2. The J and K inputs of the first flip-flop are connected to high.
3. The first flip-flop would toggle for each clock pulse, because its J-K inputs are
connected to high.
4. A flip-flop in any other position is complemented with a pulse provided all the bits
in the lower order positions are equal to 1.

Maanya’s M.G.B Publications Digital Electronics


3‐26 Sequential Logic Circuits

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

Maanya’s M.G.B Publications Digital Electronics


Sequential Logic Circuits 3‐27

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)

3.17. Distinguish between synchronous and asynchronous


counters

Sl.No.
Asynchronous Synchronous

1. In this type of counter flip-flops are In this type there is no connection


connected in such a way that the output between output of first flip-flop
of first flip-flop drives the clock for the and clock input of the next flip-
next flip-flop. flop.

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.

Maanya’s M.G.B Publications Digital Electronics


3‐28 Sequential Logic Circuits
5. Its speed is lower than that of Its speed is higher than that of
synchronous counters. asynchronous counters.
6. These are also known as ripple or serial These are also known as parallel
counters. counters.

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.

3.18.1. State the necessity of Registers and classify


registers basing on data I/O
The two basic functional characteristics of a register are:
• Temporary storage
• Shifting capability
a) Temporary Storage

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.

Maanya’s M.G.B Publications Digital Electronics


Sequential Logic Circuits 3‐29
4. Fig. 3.18.1 illustrates the concept of storing the binary information either 0 or 1 in a
flip-flop.
5. A binary 1 is applied to the input as shown in fig. 3.18.1(a), and a clock pulse is
applied that stores the 1 by setting the flip-flop.
6. When the 1 on the input is removed, the flip-flop remains in the set state, thereby
storing the 1.
7. The same procedure is applied to the storage of a 0, as illustrated in fig. 3.18.1(b).
b) Shifting Capability

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

Maanya’s M.G.B Publications Digital Electronics


3‐30 Sequential Logic Circuits
3. Parallel In and Serial Out (PISO) shift register
4. Parallel In and Parallel Out(PIPO) 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.

Maanya’s M.G.B Publications Digital Electronics


Sequential Logic Circuits 3‐31
3. Parallel In Serial Out (PISO) Shift Register
In this case, the data is loaded simultaneously (i.e. all the bits of a word are loaded
in single clock pulse) whereas the data shifted out is bit by bit (or serially) as shown in
fig. (c).
*Note:
1. The PISO shift register is very useful in transferring data from a high speed device
to a low speed device.
2. Similarly the SIPO shift register arrangement can transfer data from a low speed
device to a high speed device.
3. The IC 74165 is an example of PISO 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.

Maanya’s M.G.B Publications Digital Electronics


3‐32 Sequential Logic Circuits
1. 4-bit Shift Right Register
1. The shift register that shifts the data by one bit to the right is known as the shift
right register.
2. The fig. (a) shows the logic diagram of a 4-bit shift right register using positive edge
triggered D flip-flops. It may be noted that shift right register is an example for
serial in serial out shift register.

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

Maanya’s M.G.B Publications Digital Electronics


Sequential Logic Circuits 3‐33
2. The next data bit to be stored is 0. This is now applied at D 0 . At the positive-going
edge of the second clock pulse, the value present at Q 0 is sent to the output of the
second flip-flop and stored in it. The data bit present at D 0 is stored in the first
flip-flop. Therefore,
Q0 = 0, Q1 = 1, Q2 = 0 and Q3 = 0,
or
Q = Q0 Q1 Q2 Q3 = 0100 after the second clock pulse
3. Thus the bits have moved to right by one bit position. The same continues till all
the data bits are stored in the flip-flops.
4. Fig. (b) shows the data bits stored in each flip-flop for each clock pulse. It can be
seen that to transfer all the data bits, 4 clock pulses are required.

Fig. (b)

Fig. (c)

Maanya’s M.G.B Publications Digital Electronics


3‐34 Sequential Logic Circuits
Method - II
Let Din = 1and Q = 0000.
1. All data inputs except the one on the left are 0s. The first positive clock edge sets
the left flip-flop and the stored word becomes, Q = 1000
2. With the appearance of this word, D0 and D1 are 1’s. The second positive clock
edge gives, Q = 1100
3. The third clock pulse gives, Q = 1110
4. The fourth clock pulse gives, Q = 1111
Timing diagram:
Fig. (c) shows the timing diagram which gives pictorial description of the above
explained operation of the shift right register.
2. 4-bit Shift Left Register
1. The shift register that shifts the data by one bit to the left is known as the shift left
register.
2. Fig. (d) shows 4-bit shift left register using positive edge trigger D flip-flops. It is
also an example for SISO shift register.
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 D3 input of the last flip-flop through the input Din.
4. The Q3 output of the last flip-flop is connected to the D2 input of the third flip-flop,
the Q2 output of the third flip-flop is connected to the D1 input of the second flip-
flop and the Q1 output of the second flip-flop is connected to the D0 input of the
first flip-flop.
5. The data is outputted from the Q0 terminal of the last flip-flop.

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 .

Maanya’s M.G.B Publications Digital Electronics


Sequential Logic Circuits 3‐35
1. At the positive-going edge of the first clock pulse, the MSB bit 1 sent to the output
of the last flip-flop and hence Q 3 becomes 1. Therefore,
Q0 = 0, Q1 = 0, Q2 = 0 and Q3 = 1,
or
Q = Q0 Q1 Q2 Q3 = 0001 after the first clock pulse
2. The next data bit to be stored is 1. This is now applied at D 3 . At the positive going
edge of the second clock pulse, the value present at Q 3 is sent to the output of the
third flip-flop and stored in it. The data bit present at D 3 is stored in the last flip-
flop. Therefore,
Q0 = 0, Q1 = 0, Q2 = 1 and Q3 = 1,
or
Q = Q0 Q1 Q2 Q3 = 0011 after the second clock pulse
3. Thus the bits have moved to left by one bit position. The same continues till all the
data bits are stored in the flip-flops.
4. Fig. (e) shows the data bits stored in each flip-flop for each clock pulse. It can be
seen that to transfer all the data bits, 4 clock pulses are required.

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

Maanya’s M.G.B Publications Digital Electronics


3‐36 Sequential Logic Circuits
4. The fourth positive clock edge gives, Q = 1111.
Timing Diagram:
The working of the 4-bit shift left register is illustrated in the timing diagram shown in
the fig. (f).

Fig. (f)

3.20. Draw and explain the working of 4-bit bi-directional


shift register with timing diagram
The shift register that allows shifting of data either to the left or to the right side is
known as bi-directional shift register.
It can be implemented by using logic gate circuitry that enables the transfer of
data from one stage to the next stage to the right or to the left, depending on the level of
a control line. Fig. (a) shows a 4-bit bi-directional shift register. The Right/ Left is the
control input signal which allows data shifting either towards right or towards left.
1. Operation
Case - I
When Right/ Left signal is high, the AND gates 2, 4, 6 and 8 are disabled and the
AND gates 1, 3, 5 and 7 are enabled. The state of the Q output of each flip-flop is passed
through the D input of the following flip-flop. When a positive going edge of clock
pulse arrives, the data are shifted one place to the right.

Maanya’s M.G.B Publications Digital Electronics


Sequential Logic Circuits 3‐37

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

Maanya’s M.G.B Publications Digital Electronics


3‐38 Sequential Logic Circuits
applied at the serial data input from intervals t1 to t10 . At interval t11 and onwards a
logic 0 is applied at the serial data input.

3.21. Draw & Explain parallel in parallel out shift register


with timing diagram
The fig. (a) shows parallel in parallel out shift register. In parallel in parallel out
shift register, there is simultaneous entry of all bits and appear on parallel outputs
simultaneously as shown in fig. (a).

Fig. (a)

Fig. (b)

Maanya’s M.G.B Publications Digital Electronics


Sequential Logic Circuits 3‐39
In order to understand the working of the parallel in parallel out shift right register, let
us assume that we want to store the data 1001 in the register. Therefore, the word to be
stored is ABCD=1001. When the clock is applied, the input word appears at the output
as shown in the timing diagram (fig. (b)).

3.22. Explain the working of ring counter and list its


applications
• The basic ring counter using negative edge triggered D flip-flops is shown in fig.
(a).
• The flip-flops are arranged as in a normal shift register, i.e. the Q output of each
stage is connected to the D input of the next stage, but the Q output of the last flip-
flop is connected back to the D input of the first flip-flop such that the array of flip-
flops is arranged in a ring and therefore the name ring counter.
*Note:
1. The Q0 output sets up the D1 input, the Q1 output sets up the D2 input, and so on.
Therefore, a ring counter resembles a shift right register because the bits are shifted
right one position per positive clock edge.
2. But the circuit differs because the final output is fed back to the D0 input. The kind
of action is called rotate right; bits are shifted right and fed back to the input.

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

Maanya’s M.G.B Publications Digital Electronics


3‐40 Sequential Logic Circuits
Fig. (b)

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,

Maanya’s M.G.B Publications Digital Electronics


Sequential Logic Circuits 3‐41
Q = 0001
5. The fourth negative clock edge starts the cycle over because the rotate right
produces,
Q = 1000
6. The stored 1 bit follows a circular path, moving right thought the flip-flop until the
final flip-flop sends it back to the first flip-flop. This is why the circuit is called a
ring counter.
*Note:
1. Add more flip-flops and you can build a ring counter of any length.
2. With six flip-flops we get a 6-bit ring counter.
3. Therefore, the successive ring words are:
Q = 100000 (0)
Q = 010000 (1)
Q = 001000 (2)
Q = 000100 (3)
Q = 000010 (4)
Q = 000001 (5)
Each of the foregoing words has only 1 high bit.
4. The number of distinct states in the ring counter, i.e. the mod of the ring counter is
equal to the number of flip-flops used in the counter.
5. An n-bit ring counter can count only n bit whereas an n-bit ripple counter can count
2n bits.
6. So, the ring counter is uneconomical compared to a ripple counter, but has the
advantage of requiring no decoder, since we can read the count by simply noting
which flip-flop is set.
Applications:
1. In successive approximation register
2.

3.23. Classify different types of memories


The memories can be classified in many ways. Some of them are listed below.
1. According to the Functional Relationship with CPU
Based on functional relationship with CPU, the memories are classified into two
types:
1. Primary or Main Memory
2. Secondary or Auxiliary Memory
Maanya’s M.G.B Publications Digital Electronics
3‐42 Sequential Logic Circuits
(a) Primary or Main Memory
1. The memory, which is directly accessible to the CPU, is called main memory.
2. Examples: ROM and RAM.
3. Semiconductor memories are used as main or internal memory of a computer.
4. It stores programs and data that are being processed by the CPU.
5. Semiconductor memories are well suited as the main memory because of the high
speed of operations. Such a high speed of operation is possible by decreasing the
propagation delay and increasing the switching speed of the memory elements. But
this increases the cost of the memory.
6. In simply we can say that, these memories are faster, smaller, and lighter and
consume less power.
*Note:
1. The semiconductor memories are available in the form of ICs (integrated circuits)
with different memory capacities.
2. The capacity of a memory IC is represented by 2n×m, where ‘2n’ represents number
of memory locations available and ‘m’ represents number of bits stored in each
memory location.
3. Example: 210 × 8 = 1024 × 8.
4. The number of address bits required to identify 2n memory locations are “n”.
Example: To identify one out of 1024 (210) memory locations, 10-address bits are
required.
5. To increase the bit capacity or length of each memory location, the memory ICs are
connected in parallel and the corresponding memory location of each IC must be
selected simultaneously.
Example: 1024 × 8 memory capacity can be obtained by using four memory ICs of
memory capacity 1024 × 2.
2. According to Active device used
Based on the active device used the memories are classified into two types:
1. Bipolar Memories
2. MOS Memories
(a) Bipolar Memories
1. In these bipolar memories the multi-emitter transistors are used to store the data.
2. Bipolar memories are faster than MOS memories.
(b) MOS Memories
1. In this, the uni-polar devices such as metal oxide semiconductor field effect
transistors (MOSFET) are used to store the data.

Maanya’s M.G.B Publications Digital Electronics


Sequential Logic Circuits 3‐43
2. These have high package density as compared to the bipolar memories.
3. According to the movement of Data
Based on the movement of data the memories are classified into two types:
1. Static Memory
2. Dynamic Memory
(a) Static Memory
Semiconductor memory devices in which the stored data will remain permanently
stored as long as power is applied, without the need for periodically rewriting the data
into memory.
(b) Dynamic Memory
Semiconductor memory devices in which the stored data will not remain
permanently stored, even with power applied, unless that data are periodically
rewritten into memory. The latter operation is called a refresh operation.
Most of the static RAMs are built using MOS technology, but some are built using
bipolar junction technology. In this section we will see both the types.
(c) Secondary or Auxiliary Memory
1. The memory, which is not directly accessible to the CPU, is called secondary
memory. Examples: Hard disk, Floppy disk, Magnetic tape etc.
2. The secondary or auxiliary memory is also known as peripheral memory because it
is connected as peripheral device to the computer.
3. The auxiliary memories are used for bulk storage of data and information.
4. Auxiliary memory operates at a much slower speed than internal memory.
5. It stores programs and data that are not currently being used by the CPU. This
information is transferred to the internal memory when the computer needs it.
6. Auxiliary memories are cheaper than semiconductor memories.
*Note:
In semiconductor memories the information is stored in magnetic or optical form.
But in semiconductor memories the information is stored in voltage or charge form.
4. According to the method of Access
Based on method of access the memories are classified into three types:
1. Random Access Memory
2. Sequential Access Memory
3. Direct Access Memory
(a) Random Access Memory
1. The term random access means that any storage location in the memory can be
accessed in any order to read or write data.
Maanya’s M.G.B Publications Digital Electronics
3‐44 Sequential Logic Circuits
2. In other words, the access time is the same for any address in memory.
3. Actually, most ROMs are also random access devices, so the terminology is
somewhat misleading.
4. More precisely, the RAM is a read/write random access memory, and the ROM is a
read only random access memory.
5. Most semiconductor memories are RAMs.
(b) Sequential Access Memory
1. In sequential access memory the memory locations are accessed in a sequential or
serial fashion for the purpose of reading or writing.
2. In this memory the access time is not constant, but varies depending on the address
location.
3. Thus the time required for accessing a memory location for writing into or reading
from is different for different locations.
(b) Direct Access Memory
1. Some memory devices such as magnetic disks or drums contain a large number of
independent rotating tracks.
2. If each track has its own read-write head, the tracks may be accessed randomly,
although access within each track is serial.
3. In such cases the access mode is sometimes called as semi random or direct access.
5. According to Period of Storage
Based on the period of storage the memories are classified into two types:
1. Volatile Memory
2. Non-volatile Memory
(a) Volatile Memory
1. In this type of memory, the stored information is dependent on power supply i.e.
the stored information will remain as it is as long as power is applied.
2. If the electrical power is removed, all information stored into the memory will be
lost.
3. Example: RAM
(b) Non-volatile Memory
1. In this type of memory, the stored information is independent on power supply. i.e.
the stored information will present as it is even if the power fails.
2. Examples: ROM, PROM, EPROM, EEPROM etc.
*Note: Many semiconductor memories are volatile, while all magnetic memories are
nonvolatile, which means they can store information without electrical power.

Maanya’s M.G.B Publications Digital Electronics


Sequential Logic Circuits 3‐45
6. According to type of Accessing allowed
Based on the type of accessing allowed the memories are classified into two types:
1. Read Only Memory
2. Read/Write Memory(RAM)
(a) Read Only Memory
1. The data stored can be read from but not written in to the read only memory
(ROM).
2. We can only read from this memory and not written in to it.
(b) Read/Write Memory (RAM)
1. This is the memory in which we can read as well as write the data.
2. The main memory, magnetic disks and magnetic tapes are all read/write
memories.

3.24. Explain the terms memory read operation, write


operation, access time, memory capacity, and address lines
and word length
In the following sections we define some of the common terms used in memory
systems.
1. Memory Cell: A device or electrical circuit used to store a single bit (0 to 1).
Examples of memory cells include a flip-flop, a charged capacitor, and a single spot
on magnetic tape or disk.
2. Memory Location: A group of memory cells is referred to as a memory location.
For example a register consisting of 8 flip-flops can be considered as a memory
location whose length is 8 bits.
3. Memory Word: A group of bits (cells) in a memory that represents instructions or
data of some type. For example, a register consisting of eight flip-flops can be
considered to be memory that is storing an 8-bit word. Word sizes in modern
computers typically range from 4 to 64 bits, depending on the size of the computer.
4. Byte: A special term used of a group of 8 bits. Bytes always consist of 8 bits. Word
sizes can be expressed in byte as well as in bits. For example, a word size of 8 bits is
also a word size of one byte; a word size of 16 bites is two bytes, and so on.
5. Capacity: The term specifies the number of bits that can be stored in a particular
memory device or complete memory system. To illustrate, suppose that we have a
memory which can store 4096 twenty-bit words. This represents a total capacity of
81,920 bits. We could also express this memory’s capacity as 4096 ×20. When
expressed this way, the first number (4096) is the number of words and the second
number (20) is the number of bits per word (word size).

Maanya’s M.G.B Publications Digital Electronics


3‐46 Sequential Logic Circuits
Example: 1 A certain semiconductor memory chip is specified as 2K × 8. How many
words can be stored on this chip? What is the word size? How many total bits can this
chip store?
Solution:
2K = 2× 1024 = 2048 words
Each word is 8 bits (one byte). The total number of bits is therefore
2048 × 8 = 16,384 bits
Example: 2 Which memory stores the most bits: a 5 M × 8 memory, or memory that
stores 1M words at a word size of 16 bits?

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.

Maanya’s M.G.B Publications Digital Electronics


Sequential Logic Circuits 3‐47

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.

Maanya’s M.G.B Publications Digital Electronics


3‐48 Sequential Logic Circuits

3.26. Explain working of diode ROM

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

Maanya’s M.G.B Publications Digital Electronics


Sequential Logic Circuits 3‐49
• When the switch is moved to position 1, the diode in the R1 register conducts,
forcing D3 to go high. Because all other diodes are off, the output from the ROM
becomes, D = 1000.
• So the contents of memory location 1 are 1000.
Case - III
• As you move the switch to other positions, you will read the contents of the other
memory locations. Table shows these contents, which you can check by analyzing
fig. (b).
• With discrete circuits we can change the contents of a memory location by adding
or removing diodes. With integrated circuits, the manufacturer stores the words at
the time of fabrication.
• In either case, the words are permanently stored once the diodes are wired in place.
Register Address Word
R0 0 0111
R1 1 1000
R2 2 1011
R3 3 1100
R4 4 0110
R5 5 1001
R6 6 0011
R7 7 1110
Fig. (b)

3.27. Distinguish between EEPROM and UVEPROM


Sl.No. UVEPROM EEPROM
1. These devices can be erased and These devices can be erased
programmed with ultraviolet light. and programmed with
electrical signals.
2. The photo current from the insulated gate The voltage on the floating gate
structure permits the storage. structure permits the storage.
3. It is not possible to erase individual bytes. It is possible to erase and write
individual bytes.
4. Difficult to construct. Easy to construct.
5. Speed of operation is less. Speed of operation is more.

Maanya’s M.G.B Publications Digital Electronics


3‐50 Sequential Logic Circuits
6. Economical. Expensive.
7. High density. Low density.
8. This memory is ideally suitable for product This memory is suitable for
development, experimental projects and field and remote control
college laboratories. applications.

3.28. Explain the working of basic dynamic MOS RAM cell

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.

Maanya’s M.G.B Publications Digital Electronics


Sequential Logic Circuits 3‐51
• To write a 0 into the cell, a LOW is applied to the DIN line. The capacitor remains
uncharged, or if it is storing a 1, it is discharges as indicated in fig. (b).
When the ROW line is made LOW, the transistor turns off and disconnects the
capacitor from the data line, thus storing the charge (either 1 or 0) on the
capacitor.

Fig. (a) Fig. (b)

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)

3.29. Compare static RAM and dynamic RAM


Sl.No Static RAM Dynamic RAM
1. Fabricated using bipolar and
Fabricated using MOS technology only.
MOS technology.
2. Static RAM contains less Dynamic RAM contains more memory cell as
memory cell per unit area. compared to static RAM per unit area.
3. It has less access time hence
Its access time is greater than static RAMs.
faster memories.
4. Static RAM consists of number Dynamic RAM stores the data as a change on
of flip-flop. Each flip-flop the capacitor. It consists of MOSFET and the
stores one bit. capacitor for each cell.
5. Refreshing circuitry is not Refreshing circuitry is required to maintain
required. the charge on the capacitors after every few
milliseconds. Extra hardware is required to

Maanya’s M.G.B Publications Digital Electronics


Sequential Logic Circuits 3‐53
control refreshing. The makes system design
complicated.
6. Fast in operation. Moderate in operation.
7. Power consumption is more. Power consumption is less.
8. Cost is more. Cost is less.

Maanya’s M.G.B Publications Digital Electronics


4
8051 Microcontroller

OBJECTIVES

4.1 Explain the concept of Micro controllers.


4.2 Compare Embedded with External memory devices.
4.3 List the three commonly used Commercial Microcontroller Device families.
4.4 Draw the block diagram of a microcontroller and explain the function of each block.
4.5 Describe the register structure of 8051.
4.6 Explain the functions of various special function registers.
4.7 Draw the pin diagram of 8051 micro controller and specify the purpose of each pin.
4.8 Describe internal memory, external memory and ports of 8051.
4.9 Describe counters & timers in 8051
4.10 Explain serial input/output of 8051
4.11 Explain interrupts in 8051.
4.12 Describe the four timer modes in 8051.
4‐2 8051 Microcontroller

4.1. Draw the block diagram of a microcomputer and explain the


function of each block.
• A digital computer in which one Microprocessor has been provided to act as a CPU
is called micro computer.
• It is the smallest and least expensive general purpose computer.
• The fig. 4.1 shows the block diagram of Microcomputer.
• It mainly consists of four sections namely:
1. Microprocessor
2. Input device
3. Output device
4. Memory

Fig. 4.1(a) Block diagram of Microcomputer

Fig. 4.1(b) Block diagram of Microcomputer (additional fig)


1. MPU (Microprocessor Unit)
• The CPU of a microcomputer is a microprocessor. The major components of a
microprocessor are: ALU, register array and timing and control unit.
• The heart of a microcomputer is microprocessor unit (MPU).

Maanya’s M.G.B Publications Microcontrollers


8051 Microcontroller 4‐3
• The MPU is a VLSI device.
• It is a general purpose processing unit, built into a single integrated circuit.
• It is a decision making unit, which decides the task to be performed by the system.
• It may be 8-bit or 16-bit or 32-bit processor.
• The MPU executes instructions of the program and process data.
• It is responsible for performing all arithmetic operations and making the logical
decision initiated by the computer’s program.
2. Memory Unit
• The memory unit is used to store data, address, or instructions in hexadecimal
form.
• The memory unit is subdivided into two parts as Primary memory and Secondary
memory or auxiliary memory.
• The primary memory is also called as semiconductor memory.
• The primary memory is again divided into number of sub units in which RAM and
ROM are important.
• The RAM is used to store the temporary data whereas ROM is used to store
permanent data.
• The secondary or auxiliary memory is a magnetic memory. It is used to store large
amount of data. Example for secondary memory is hard disk.
3. Input Device
• The computer receives data and instructions through input devices.
• An input devices converts instructions, input data and signals into proper binary
form suitable for a digital computer.
• A keyboard and simple switches are used as input devices.
4. Output Device
• The computer sends the processed data (Results) to the output devices.
• An output device may store, print, display or send electrical signal to control
certain equipment.
• The examples of simple output devices are LEDs, CRT, D/A converter, Video
Monitor and Printers etc.

4.2. List the features of micro controllers.


Definition: The special digital system contains a microprocessor, memory
(RAM/ROM), I/O ports and timer on a single chip is known as Microcontroller.
The main features of Microcontroller are:
• A microcontroller is a single chip computer.
Maanya’s M.G.B Publications Microcontrollers
4‐4 8051 Microcontroller
• It is used to control electronic or electro-mechanical devices. It is also called as
dedicated computer or embedded microcontroller.
• It has the specified computational capabilities. In compare with general purpose
microprocessor, it is less cost and more effective.
• It is designed for a specific task.
• It is designed with VLSI technique.
• It is highly integrated chip that consists all the components comprising a controller.
Basically it contains a CPU, RAM, ROM, I/O ports, and timers.
• Specifically it is designed for a single task to control one device. It is small in size.
Microcontrollers are selected based on bus width,
• Internal logic technique (VLSI or VHDL or Verilog).
• Present 8, 16 and 32bit microcontrollers are in use.

4.3. Compare Microprocessors and Microcontrollers


S.No. Microprocessor (MP) Microcontroller (MC)
1 Microprocessor contains ALU, Microcontroller contains microprocessor,
control unit (clock and timing), memory (ROM and RAM), I/O ports and
different registers and interrupt peripheral devices such as A/D converter,
circuit. timer, counter etc.
2 Microprocessor must have many Microcontroller can function as a
additional parts to function as a computer with the addition of no external
computer. parts.
Microprocessor based system Microcontroller based system requires less
3 requires more hardware. hardware reducing PCB size and
increasing the reliability.
Microprocessor based system is Less flexible in design point of view.
4 more flexible in design point of
view.
5 It has single memory map for It has separate memory map for data and
data and code. code.
6 Very few pins are programmable. Most of the pins are programmable that is,
capable of having several functions.
7 Time taken to complete a process Time taken to complete a process is less.
is more.
8 It has many instructions to move It has one or two instruction to move data
data between memory and CPU. between memory and CPU.
9 It may have one or two bit It may have many bit handling
handling instructions. instructions.
Maanya’s M.G.B Publications Microcontrollers
8051 Microcontroller 4‐5

4.4. State the details of INTEL microcontroller family chips.


1. In 1976, Intel introduced 8-bit microcontroller 8048. It is also known as MCS-48. The
same company continued to drive the evolution of single chip microcontrollers.
2. In the year 1980, Intel introduced MCS-51 (8051 family) microcontroller, with higher
performance than 8048.
3. The below table shows the main features of Intel 8051 family.
Features 8051 8052 8031

1. ROM (on-chip) 4K bytes 8K bytes 0

2. RAM (on-chip) 128 bytes 256 bytes 128 bytes


3. Timers 2 3 2
4. I/O Pins 32 32 32
5. Serial ports 1 1 1
6. Interrupt sources 6 8 6
4. In 1983, Intel introduced a 16-bit single chip microcomputer series called MCS-
96(8096 family). It contains 16-bit CPU, 8 K ROM, 232 bytes RAM, timer/counter,
parallel I/O, 10-bit A/D, high control such as missile guidance and control, complex
instrumentation system, control of sophisticated machines etc.
Note: The 8051 is the original member of the 8051 family. It is an 8-bit microcontroller
designed by Intel in the year 1981.

4.5. State the features of Intel 8051 Micro Controller.


The features of Intel 8051 are listed below.
• The 8051 is an 8-bit microcontroller
• It was designed by Intel in the year 1981.
• It is a 40 pin IC chip.
• It operates with 12 MHz clock and a single +5 V supply.
• Its internal ROM (on–chip program memory) capacity is 4K bytes.
• It’s internal RAM (on – chip data memory) capacity is 128 bytes.
• It has external data memory of 64Kbytes and external program memory of
64Kbytes.
• It has two 16-bit Timers/Counters.
• It has 32 bidirectional I/O lines organized as four 8-bit ports (P0-P3).

Maanya’s M.G.B Publications Microcontrollers


4‐6 8051 Microcontroller
• It has one serial port and four 8-bit I/O ports.
• It has four bank registers.
• It has five interrupts, 2 from external devices, 2 from timer/counters and 1 from
serial port.
• It has 111 instructions: 49 single byte, 45 two byte and 17 three byte.

4.6. Draw the functional block diagram of 8051 microcontroller


Fig. 4.6(a) shows the block diagram of a microcontroller. A detailed functional
block diagram of 8051(or) the internal architecture of 8051 is shown in fig. fig. 4.6(b).
The functional description of each block is presented briefly below.

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.

Maanya’s M.G.B Publications Microcontrollers


8051 Microcontroller 4‐7

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.

Maanya’s M.G.B Publications Microcontrollers


4‐8 8051 Microcontroller
5. Timers and Counters
8051 has two 16-bit timers: timer-0 and timer-1. They can be used either as timers
or event counters. Counters are used to count the events where as timers are used to
maintain time delays between the actions.
6. Interrupt control
Interrupt is a signal send by an external device or from internal unit to the
microcontroller so as to request the controller to perform a particular task or function.
The interrupt control unit transfers the microcontroller attention from one task to other.
7. Oscillator
This circuit generates the basic timing clock signal for the operation of the circuit
using crystal oscillator. The oscillator circuit generates a train of pulses at a frequency of
the crystal.
Note: The machine cycle is group of six states (or 12 crystal pulses). A state is the basic
time period for the microcontroller to fetching an op-code, decoding an op-code,
executing an op-code or writing data byte.
8. Bus control
A control bus is used by microcontroller for communicating with other devices
within the system. Basically Bus is a collection of wires which work as a communication
channel or medium for transfer of Data. Buses are of three types: i) Address ii) Bus Data
Bus and iii) Control bus.
9. Special Function Registers (SFRs)
The 8051 has 21 Special Function Registers. These are: A, B, DPTR, PSW, IP, IE,
TCON, SCON, PCON, P0, P1, P2, P3, SBUF etc. All these are byte addressable and some
of them are bit addressable also. The SFRs can be accessed by their names or by their
addresses. These are useful in accessing I/O ports, timers/counters, UART, power
Controlling etc.
10. Timing and Control Unit
This unit synchronizes all the operations with the clock and generates control
signals for proper functioning.
11. Instruction Register
It is an 8-bit register. When instruction is fetched from memory it is loaded in the
instruction register. It holds the instruction which is to be decoded. It determines the
operation to be followed in executing the entire instruction.
12. Temporary Registers
There are two 8-bit temporary registers TMP1, TMP2. They are also integral part
of ALU. Comparisons and certain other operations use the contents of these registers.

Maanya’s M.G.B Publications Microcontrollers


8051 Microcontroller 4‐9

4.7. Draw the register structure of 8051and explain.


Registers are used to store the data temporarily. The data may be of op-codes or
operands or address of a memory location or address of a peripheral. The 8051 has wide
range of registers.
These are classified as:
1. General purpose registers
2. Special purpose registers(MCU related registers)
3. Special function registers

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.

Maanya’s M.G.B Publications Microcontrollers


4‐10 8051 Microcontroller

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)

Maanya’s M.G.B Publications Microcontrollers


8051 Microcontroller 4‐11
are used to select any one of the four register banks as follows.
RS1 RS0 Bank selection
0 0 Bank 0: 00- 07H
0 1 Bank 1: 08- 0FH
1 0 Bank 2: 10- 17H
1 1 Bank 3: 18- 1FH
• Default bank (On reset) is bank 0 (locations 00 – 07H).
2. A A (Accumulator)
• It is an 8-bit register.
• It is used for all arithematic and logical opeations.
• The A-register must be used as destination for all addition and
subtraction operations.
• For logical operations this can be used as a source or a destination.
• There are some specific operations like clear, complement, rotate and
swapping the data must be reside in A-register only.

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.

Maanya’s M.G.B Publications Microcontrollers


8051 Microcontroller 4‐13

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.

Maanya’s M.G.B Publications Microcontrollers


4‐14 8051 Microcontroller
4. Parity Flag (P)
• The parity flag is set if the accumulator contains an odd number of 1s.
• The parity flag is reset if the accumulator contains an even number of 1s.
• It is mainly used during data transmit and receive via serial communication.
b) Register Bank Select bits (RS1 and RS0)
• These two bits are used to select one of four register banks of RAM.
• Below table shows the address ranges of four register banks along with RS1 & RS0
bits.
RS1 RS0 Register bank selected Address range in the on chip RAM
0 0 Bank 0 00-07 H
0 1 Bank 1 08-0F H
1 0 Bank 2 10-17 H
1 1 Bank 3 18-1FH
c) F0
• F0 is available to user as a general purpose flag. This flag can be set/cleared by
software, or its status can be observed by software.
• The user can define its role.
*Note:
1. PSW is a bit addressable register.
2. Each of the PSW bits is referred as PSW.X.
3. Thus, PSW.0 is the least significant bit (LSB), which is a parity flag, and the
most significant bit (MSB) PSW.7 is the carry flag.
4.7.3. Special function registers
SFRs Function
1. TMOD • TMOD (Timer Mode) register is used to specify the mode of
operation of timers/counters of 8051.
• It is dedicated particularly to the two timers T0 and T1.
2. TCON • TCON (Timer Control) register is used to control the entire
operation of the timer/counter of 8051.
3. IE • IE (Interrupt Enable) register is used to enable/disable all or any
of the interrupts of 8051.
4. IP • IP (Interrupt Priority) register is used to alter the priority (high

Maanya’s M.G.B Publications Microcontrollers


8051 Microcontroller 4‐15
or low) among the interrupts of 8051.
5. SCON • SCON (Serial Control) register is used to specify the mode in
which the serial port is to work.
6. PCON • PCON (Power Control) register is used to control the baud clock
generated from the timer.
7. SBUF • SBUF is physically two registers.
• One is write only and is used to hold data to be transmitted out
of the 8051 via TXD.
• The other is read only and holds received data from external
sources via RXD.
8. P0, P1, • These registers specify the value to be output on an output port
P2,& P3 or the value read from an input port.
• They are also bit addressable. Each port is connected to an 8-bit
register in the SFR.

4.8. Explain the function of various special function registers.

Fig. 4.8

Maanya’s M.G.B Publications Microcontrollers


4‐16 8051 Microcontroller
Some of the special function registers are discussed in the previous section. Let
us consider the remaining SFRs.
4.8.1. Interrupts control SFRs
The 8051 have five interrupts. There are two registers to handle interrupts. These
are:
a) IP (Interrupt Priority) SFR
b) IE (Interrupt Enable) SFR
a) IP (Interrupt Priority) SFR
• It is an 8-bit SFR.
• It is used to alter the priority (high or low) among the interrupts of 8051.
• If the bit is 0, the corresponding interrupt has a lower priority and if the bit is 1, the
corresponding interrupt has a higher priority.
• Bit addressing is allowed with this register also.

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

Maanya’s M.G.B Publications Microcontrollers


8051 Microcontroller 4‐17
3. Upon the reset the 8051 the priorities among the five interrupts are listed in below
table.
Interrupt Priority
External interrupt 0 (Highest)
Timer 0 interrupt
External interrupt 1
Timer 1 interrupt
Serial port interrupt (Lowest)

b) IE (Interrupt Enable) SFR


• It is an 8-bit bit addressable SFR.
• It is used to enable/disable all or any of the interrupts of 8051.
• If the bit is 0, the corresponding interrupt is disabled.
• If the bit is 1, the corresponding interrupt is enabled.
• Upon reset, all interrupts are masked (disabled).
• Fig. 4.8.1(b) shows the bit arrangement of IE register.

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.

Maanya’s M.G.B Publications Microcontrollers


4‐18 8051 Microcontroller
*Note:
1. EA - Global interrupt enable/disable.
a. 0 - Disables all interrupt requests.
b. 1 - Enables all individual interrupt requests.
2. Bit address: A8H to AFH.
1.8.2. TL0, TH0, TL1, TH1 SFRs
The 8051 contain two 16 bit timer/counters named as T0 and T1 . Since the 8051
is a 8 bit microcontroller, these 16 bit registers are divided into two 8 bit registers. TL0
and TH 0 and TL1 and TH1 .
TL0 : Timer0 Low byte
TL1 : Timer1 Low byte
TH0 : Timer0 High byte
TH1 : Timer1 High byte
4.8.3. Timer/Counter control SFRs
a) TMOD (Timer/Counter Mode Control) SFR
b) TCON (Timer/Counter Control) SFR
a) TMOD (Timer/Counter Mode Control) SFR
• TMOD is an 8-bit SFR.
• It is dedicated particularly to the two timers T0 and T1.
• This register is used to select the operating mode and the timer/counter operation
of the timers.
• Fig. 4.8.3(a) shows the bit assignment for TMOD SFR.
• As shown in fig. 4.8.3(a) the lower four bits are used to control the timer 0 and the
upper four bits are used to control the timer 1.

Fig. 4.8.3(a)

Maanya’s M.G.B Publications Microcontrollers


8051 Microcontroller 4‐19

Symbol Function
GATE Gate control.

C/ T Timer or counter selector.

M1 & M0 Timer modes select bits.


• GATE:
o 1 → The timer operation is controlled by external sources through the
INT0 / INT1 pins available in port 3 of the microcontroller 8051. This is
hardware method of controlling the timer.
o 0 → The timer operation is controlled by software commands such as SETB
TR, CLR TR. This is software method of controlling the timer.
• C/ T :
o 1 → T1/T0 will work as counters
o 0 → T1/T0 will work as timers
• M1, M0: These two bits will decide the mode of operation of the timer/counter.
M1 M0 Operating Mode
Mode 0: 13-bit timer
0 0
(TL-5 bits and TH-8 bits)
Mode 1: 16-bit timer
0 1
(TL-8 bits and TH-8 bits).
Mode 2: 8-bit auto-reload
1 0 8-bit counter (TL-8 bit) overflow from TL not only sets TF, but also reloads
TL with the contents of TH.
Mode 3: Split mode
1 1 In this mode, real timer0 (TL0 & TH0) alone is being used independently as
8-bit timers.
b) TCON (Timer/Counter Control) SFR
• TCON is called timer/counter control register.
• It is also an 8-bit register.
• This register contains timer overflow flags, timer run control bits, external interrupt
flags and external interrupt type control bits.
• Fig. 4.8.3(b) show the bits arrangement of TCON register.
• The higher nibble used to turn ON or OFF the specific timer.

Maanya’s M.G.B Publications Microcontrollers


4‐20 8051 Microcontroller
• The lower nibble is meant for controlling the interrupts.

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

Maanya’s M.G.B Publications Microcontrollers


8051 Microcontroller 4‐21
a) SCON (Serial Control) SFR
1. It is an 8-bit bit addressable SFR.
2. It is a serial port mode control register and is used to control the operation of
the serial port.
3. This register contains not only the mode selection bits, but also the specific bit
for transmit and receive; and the serial port interrupt bits.
4. Fig. 4.8.4(a) shows the bits arrangement of SCON 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

Maanya’s M.G.B Publications Microcontrollers


4‐22 8051 Microcontroller
send and receive data bits serially. The 8051 has a serial data communication circuit
that uses register SBUF to hold data.
• SBUF is physically two registers. One is write only and is used to hold data to be
transmitted out of the 8051 via TXD.
• The other is read only and holds received data from external sources via RXD. Both
mutually exclusive registers use address 99H.
c) PCON (Power Control) SFR
1. PCON is a power control register of 8-bit length.
2. Its vectored address is 87H.
3. It is a byte addressable register.
4. It is used for power control and baud rate selection.
5. The PCON register also contains two general purpose flags and a double baud
rate bit.
6. The fig. 1.8.4(b) shows the bit patterns for PCON SFR.

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.

Maanya’s M.G.B Publications Microcontrollers


8051 Microcontroller 4‐23

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

6. DPH Data pointer high byte 83H

7. *P0 Port 0 80H


8. *P1 Port 1 90H

Maanya’s M.G.B Publications Microcontrollers


4‐24 8051 Microcontroller
9. *P2 Port 2 0A0H
10. *P3 Port 3 0B0H
11. *IP Interrupt priority Control 0B8H
12. *IE Interrupt Enable Control 0A8H
13. TMOD Timer/Counter Mode Control 89H
14. *TCON Timer/Counter Control 88H
15. TH0 Timer/Counter 0 High Byte 8CH
16. TL0 Timer/Counter 0 Low Byte 8AH
17. TH1 Timer/Counter 1 High Byte 8DH
18. TL1 Timer/Counter 1 Low Byte 8BH
19. * SCON Serial Control 98H
20. SBUF Serial Data Buffer 99H
21. PCON Power Control 87H
*Note:
1. * = Bit Addressable.
2. Any address used in the program must start with a number. Thus, the
addresses E0, F0, A0 etc., are specified as 0E0, 0F0, 0A0, etc.
3. Failure to use this number convention will result in an assembler error when
the program is assembled.
2. The following two points should be noted about the SFR addresses.
1. The SFRs have addresses between 80H and FFH.
2. Not all the address space of 80H to FFH is used by the SFRs. The unused
locations are reserved and must not be used by the 8051 programmer.

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.

Maanya’s M.G.B Publications Microcontrollers


8051 Microcontroller 4‐25

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.

13 P3.3 (INT1) External interrupt 1 input.

14 P3.4 (T0) External timer 0 input (or) Counter 0 clock input.


15 P3.5 (T1) External timer 1 input (or)
Counter 1 clock input.
16 P3.6 ( WR ) External memory write signal.

17 P3.7 ( RD ) External memory read signal

Pin 18-19: XTAL 1 and XTAL2


• The 8051 has on chip oscillator but requires an external clock to run it. Most often a
quartz crystal oscillator is connected at these two pins.
• If you decide to use a frequency source other than a crystal oscillator, such as a TTL
oscillator, it will be connected to XTAL1, XTAL2 is left unconnected.
Pin 20: VSS
• This is power supply ground.
Pin 21 - 28: Port 2
• If there is no intention to use external memory then these port pins (P2.0 to P2.7) are
configured as general inputs/outputs.
• In case external memory is used, the higher address (A8-A15) will appear on this
port.
Pin 29: PSEN
• Program Store Enable is the active low output pin.
• If external ROM is used for storing program then a logic zero (0) appears on it
every time the microcontroller reads a byte from memory.
Pin 30: ALE
• Address Latch Enable is an active high output pin. It is used for de-multiplexing the
address and data bus.
• This pin is also the program pulse input (PROG) during EPROM programming.

Maanya’s M.G.B Publications Microcontrollers


8051 Microcontroller 4‐27

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.

4.10. Explain internal memory Organization in 8051

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.

Maanya’s M.G.B Publications Microcontrollers


4‐30 8051 Microcontroller
1. Internal RAM (i-RAM)

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.

Maanya’s M.G.B Publications Microcontrollers


8051 Microcontroller 4‐31
c) General purpose
• The last 80 bytes (30H to 7FH) of 8051 internal RAM is called general purpose RAM
or scratch pad memory locations.
• The scratch pad area is used for general purpose storage i.e. to store data, results,
constants and intermediate results.

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)

4.11. Explain external memory access in 8051


• The 8051 can address external memory if there is not enough internal RAM and/or
ROM.
1. External RAM or (Off-chip RAM)
• The 8051 has external RAM of 64k bytes.
• The range of external data memory is from 0000H to FFFFH.
• This memory space is used for storing data so frequently called as data memory.
2. External ROM or (Off-chip ROM)
• The 8051 MC has 64k bytes of external program memory.

Maanya’s M.G.B Publications Microcontrollers


4‐32 8051 Microcontroller
• Fig. 4.11 shows the map of 8051 program memory.
• The control pin EA (pin 31) determines the accessing of either internal or external
ROM.

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:

Maanya’s M.G.B Publications Microcontrollers


8051 Microcontroller 4‐33
8. The MOVC instructions can be used to access the internal or external program
memory.
9. It is accessed by the DPTR and PC only
10. It is accessed by using indexed addressing mode.
11. The External Code memory can be used not only for storing the program (code), but
also for lookup table of different functions required for specific task. Mathematical
functions such as Square root, Sine, quadratic equations etc. can be stored in the
code memory (internal or External) and these functions can be accessed by using
MOVC instruction.
12. To access external code (program) memory PSEN (program store enable) is used as
read strobe.
13. External program memory (ROM) is accessed under two conditions:
a) Whenever EA = 0 (or)
b) Whenever PC contains a number that is larger than 0FFFH.

4.12. Explain various ports of 8051.


• The 8051 has four input /output ports.
• The 8051 has a group of 32 I/O pins configured as four 8- bit parallel ports named
as P0, P1, P2 and P3 .
• All four ports are bidirectional, i.e, each pin will be configured as input or output
(or both) under software control.
• Each port consists of a latch, an output driver and an input buffer.
• All the ports are configured as input upon the RESET of 8051 and ready to be used
as input ports.
• In order to change them as an output ports, they must be programmed. In addition
to the I/O nature, these lines also have some specific functions.
• The following two instructions are used to make the Port 0 as an output port.
MOV A, #00H 00 H loaded in to A-register.
MOV P0, A Making Port 0 as an output port while writing 0s (0000 0000).
• The following two instructions are used to make the Port 0 as an input port.
MOV A, #0FFH FF H loaded in to A-register.
MOV P0, A Making Port 0 as an input port while writing 1s (1111 1111).

Maanya’s M.G.B Publications Microcontrollers


4‐34 8051 Microcontroller
1. Port 0 (P0)
• It is designated as P0; the bits of port 0 are designated as P0.0 to P0.7.
• It is a bit as well as byte addressable register.
• Port 0 pins are also named as AD0-AD7 i.e. the pins of Port 0 allowed to use as
address as well as data pins depends upon the status of the ALE signal.
• It has dual function.
• The internal RAM address of Port 0 is 80H.
• Port 0 is used as:
o Input port for transferring data from peripherals to microcontroller.
(or)
o Output port for transferring data from microcontroller to peripherals.
(or)
o Lower order address bus (A0 to A7) for external memory.
(or)
o Data bus (D0 to D7) for external memory.
*Note: To use the pins of port 0 as both input and output, each pin must be connected
externally to a 10 kΩ pull-up resistor.

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.

Maanya’s M.G.B Publications Microcontrollers


8051 Microcontroller 4‐35
• It is a bit as well as byte addressable register.
• The internal RAM address of Port 1 is 90H.
• It can be accessed by direct or indirect address modes.
• The port 1 does not need any pull-up resistors since it already has built in pull up
resistors internally.
• When the microcontroller is reset, port 1 is configured as an input port.
3. Port 2 (P2)
• It is designated as P2, the bits of port2 are designated as P2.0 to P2.7.
• Port 2 has dual function, i.e. Port 2 can be used as I/O port as well as higher order
address bus (A8 to A15) .
• When external memory is interfaced with 8051, Port2 pins can’t be used as I/O port
it works as higher order address bus (A8-A15).
• It is a bit as well as byte addressable register.
• The internal RAM address of Port 2 is 0A0H.
• It can be accessed by direct or indirect address modes.
• The port2 does not need any pull-up resistors since it already has built in pull up
resistors internally.
• All the pins of port2 are compatible with TTL circuits.
• When the microcontroller is reset, port 2 is configured as an input port.
Note: Port 2 is used as:
o Input port
(or)
o Output port
(or)
o High-order address bus (A8 to A15) for external memory.
4. Port 3 (P3)
• It is designated as P3; the bits of port3 are designated as P3.0 to P3.7.
• Port 3 also have dual functions.
• Similar to the Port 1 and Port 2, the Port -3 also can be used as input or output.
However Port-3 lines are commonly used for special functions. The special function
of Port-3 was mentioned in below table.
• It is a bit as well as byte addressable register.
• The internal RAM address of Port 3 is 0B0H.
• It can be accessed by direct or indirect address modes.

Maanya’s M.G.B Publications Microcontrollers


4‐36 8051 Microcontroller
• The port3 does not need any pull-up resistors since it already has built in pull up
resistors internally.
P3.0 RXD Serial data input
P3.1 TXD Serial data output
P3.2 INT0 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


Note:
1. RxD and TXD pins are used as input and output pin if 8051 is operated in serial data
transmission mode.
2. Through T0 and T1 pins the external clock pulse are given to counter 0 and counter
1 respectively for counter operation.

4.13. Explain counters & timers in 8051

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)

Maanya’s M.G.B Publications Microcontrollers


8051 Microcontroller 4‐37

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 .

Additional fig:4.13(d) Timer /Counter control logic circuit


4.13.1. Operational Modes of Timers
Timer has four modes of operation. The modes of operation of the timer is
depends on the bits of the TMOD register.
a) Mode 0 Operation
• In this mode timer can acts as 13 bit timer.
• Here 8 bits of TH0 and 5 bits of TL0 or 8 bits of TH1 and 5 bits of TL1 are alone
used. TL (TL0 or TH0) will count from 0 to 31.
• For the next count, it will “reset” to 0 and increment TH. Thus, effectively, only 13
bits of the 16 bit timer are being used: bits 0-4 of TL and bits 0-7 of TH.
Maanya’s M.G.B Publications Microcontrollers
4‐38 8051 Microcontroller

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.

Maanya’s M.G.B Publications Microcontrollers


8051 Microcontroller 4‐39
• TF goes high when timer rolls over from FFH to 00H.
d) Mode 3 Operation
• In this mode timer 0 is configured as two separate 8 bit timers and timer-1 is
stopped.
• The TL0 is functioned as 8 bit timer and controlled by timer 0 control bits i.e., TR 0
and TF0 of TCON.
• The TH 0 is functioned as 8 bit timer and controlled by timer 1 control bits TR1 and
TF1 of TCON.
Do You Know?
1. Since the timer T0 is virtually 16-bit register, the largest value it can store is 65 535.
In case of exceeding this value, the timer will be automatically cleared and counting
starts from 0. This condition is called an overflow.

4.14. Explain serial input/output of 8051


• The 8051 is a 8 bit microcontroller and it transfer 8-bit data simultaneously. This is
the parallel I/O mode. It is an expensive method as it requires eight transmission
lines between sender and receiver.
• Particularly it is very difficult to adopt this parallel data transfer mode in long
distance communication.
• In situations, the Serial Input and Output mode is used, whereby one bit at a time is
transferred over a single line.
• The serial input output port provides serial data transfer. Serial data
communication is one of the effective ways of transmitting and receiving data bits
between computer systems.
• The serial port of 8051 provides full duplex data communication.
• It can transmit and receive data simultaneously.
• The 8051 has a serial data communication circuit (UART: Universal Asynchronous
Transmitter and Receiver) that uses;
o Register SBUF to hold data.
o Register SCON controls data communication.
o Register PCON controls data rates.
o Pins RXD (P3.0) and TXD (P3.1) connect to the serial data network.
• The 8051 provides four programmable modes for serial data communication.
• A particular mode can be selected by setting the SM0 and SM1 bits in SCON. The
mode selection also decides the baud rate.

Maanya’s M.G.B Publications Microcontrollers


4‐40 8051 Microcontroller
SM0 SM1 Mode Function
0 0 0 Shift register, baud rate = f/12
0 1 1 8-bit UART, baud rate= variable
1 0 2 9-bit UART; baud rate = f/32 or f/64
1 1 3 9-bit, UART : baud rate =variable

4.15. Explain interrupts in 8051


• An interrupt is a signal either from hardware or from a program that interrupts the
normal processing of the CPU.
• Upon receiving an interrupt signal, the microcontroller interrupts whatever it is
doing and serves the device.
• The program associated with the interrupt is called interrupt service routine (ISR)
or interrupt handler.
• When an interrupt is invoked, the microcontroller runs the interrupt service
routine.
• The 8051 provides 5 interrupts.
• 2 interrupts are external interrupts and the remaining 3 are internal interrupts.
These are :
o External Interrupts: INT0 & INT1 .
o Internal Interrupts: Timer 0, Timer 1 serial communication.

Fig 4.15

Maanya’s M.G.B Publications Microcontrollers


8051 Microcontroller 4‐41
1. External interrupts
o The external interrupts are provided by the external circuitry connected between
the pins INT0 and INT1 of the 8051.
o Memory locations 0003H and 0013H in the interrupt vector table are assigned to
INT0 and INT1 respectively.
2. Internal interrupts
a) Timer interrupts:
o Two interrupts are set aside for the timers: one for Timer 0 and one for Timer1.
o Timer 0 and Timer 1 interrupts are generated by TF0 and TF1 in their respective
timer/counter registers.
o Memory locations 000BH and 001BH in the interrupt vector table belong to timer0
and timer 1 respectively.
b) Serial port interrupt:
o Serial communication has a single interrupt that belongs to both receive and
transmit.
o The serial port interrupt is generated by the logical OR of RI and TI.
o The interrupt vector table location 0023H belongs to this interrupt.
3. Vector address and the Priorities of the interrupts
• Each interrupt has its own program, known as ISR, which was stored from a
predetermined location.
• All the interrupts of 8051 are vectored interrupts. That means, when an interrupt
occurs, the control of microcontroller is automatically transferred to its service sub
routine (ISR).
• There are five vector addresses for five interrupts fixed by the manufacturer.
• If more than one interrupts are occurred simultaneously, the microcontroller will
service the interrupts according to their priority.
• The vector address and the priorities of the interrupts of 8051 are listed in table.
Interrupt Vector address Priority

External interrupt 0 ( INT0 ) 0003H (Highest)

Timer 0 interrupt (TF0) 000BH

External interrupt 1 ( INT1 ) 0013 H

Timer 1 interrupt (TF1) 001BH


Serial port interrupt (RI OR TI) 0023H (Lowest)

Maanya’s M.G.B Publications Microcontrollers


4‐42 8051 Microcontroller
*Note:
1. For example, if INT0 and TF1 are activated at a time then the 8051 will
respond to INT0 initially as it has higher priority than TF1.
2. The priorities of the interrupts can also be altered by programming the IP
register.
3. The external interrupts can be programmed to be either level active or
transition activated by setting or clearing the bit IT1 or IT0 in register TCON.
4. All the interrupts of 8051 are maskable interrupts. It means that all the
interrupts of 8051 can be individually enabled or disabled by setting or
clearing a bit in IE SFR. The IE register contains also an interrupt disable bit
EA, which disables all interrupts when it is 0.
5. In order to handle five interrupts the 8051 use SFRS, such as IE, IP and TCON.
Review Questions
1. Draw the pin-out diagram of 8051 and label the names.
[April -2012]
2. List any six special function registers of 8051 microcontroller.
[April/May-2015, April -2008]
3. Explain input/output ports of 8051
[Oct/Nov-2011]
4. List the interrupts in 8051 microcontroller
[March/April-2014]
5. Draw the functional block diagram of 8051. Explain the functions of each block.
[Apirl/May-2015, 2014, 2013; Oct/Nov-2013]

6. Explain memory organization of 8051


[Apirl-2012; Oct/Nov-2011]
7. Explain the internal RAM-organization in 8051
[April-2012]
8. Explain the pin diagram of 8051 microcontrollers.
[April-2008, 2009]
9. Explain the internal memory organization of 8051.
[April-2012]
10. Explain the function of various special function registers.
[Oct/Nov-2011]
11. Explain the following:
a) Timers/ Counters of 8051
b) Input/ Output ports
c) Interrupts of 8051
[March/April-2013]

Maanya’s M.G.B Publications Microcontrollers


5
8051 Instruction set & Programming

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.1. State the need for an instruction set.


• Instruction set is also called a command set. Instruction set means a group of
instructions that a microcontroller understand.
• The instruction set of a microcontroller used to allow efficient control of both its
internal devices and the surrounding infrastructures (i.e. those devices are
connected to the ports of the microcontroller).
• Control of the register set of the microcontroller in an easy manner.
• Ability to access ports and other peripheral control and status registers.
• Ability to be able to access individual bits of a port or special function registers.
• Ability to access internal and external memory.

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.

Maanya’s M.G.B Publications Microcontrollers


Instruction set of 8051 Microcontroller 5‐3
Examples:
Instruction
Comment
Op-code Operand
ADD A,Rn Add register Rn to Accumulator.
MOV A,#data Move immediate data to Accumulator.
MOV direct,Rn Move register Rn to direct byte.
CLR bit Clear direct bit.
SJMP rel Jump if Carry is set.

5.3. Explain fetch cycle, execution cycle and instruction cycle.


The total time required to perform the execution of an instruction is known as the
instruction cycle. Every instruction is consists of two parts i.e., opcode and operand.
Therefore the instruction cycle is broken into two time intervals known as Fetch cycle
and Execute cycle.
1. Fetch cycle
The time taken by the 8051 for fetching an opcode is known as Fetch cycle. During
the fetch cycle, an instruction byte containing the operation code is brought into the
microcontroller from memory.
(or)
It is the time required to bring an instruction byte containing the operation code
from the memory into the microcontroller. A typical fetch cycle is shown in the fig.
5.3.(a).
The entire operation of fetching an op code takes three states (S) or six clock
cycles (P).

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)

Maanya’s M.G.B Publications Microcontrollers


Instruction set of 8051 Microcontroller 5‐5

5.4. Distinguish between machine cycle and T-state.


The external basic operations performed by the microcontroller are called machine
cycles. The 8051 microcontroller takes one to four machine cycles to execute an
instruction.
The basic timing of the 8051 machine cycle is shown in fig. 5.4. The entire timing of
a machine cycle of 8051 is divided into 6-states and they are denoted as S1, S2, S3, S4, S5
and S6. The timing of each state is two clock periods and they are denoted as P1 and P2.
A state in a machine cycle is a basic time interval for discrete operation of the
microcontroller such as fetching an op-code byte, decoding an op-code, excluding an op-
code, writing a data, etc.

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.6. Define the terms machine language, assembly language,


and mnemonics.
When anyone wants to communicate to any other person then a language is
followed. So if we want to communicate with computer system then we have to use
computer language.
The microcomputer programming languages can be divided into the following
three types.
1. Machine language
2. Assembly language
3. High Level language
*Note: The machine language and assembly language are considered as low level
language.
1. Machine language
A program written in the form of 0s & 1s is called machine language also known
as binary language as only two bits are being used. To write a program in machine
language programmer has to memorize the hundreds of binary instruction codes for a
processor. This task is difficult and error prone.
For example, for INTEL 8051 to add the contents of register A and register R7, the
binary code is 0010 11112(2FH). To move the content of register R7 to register A the
binary code is 1110 11112(EFH). Thus by observing the two examples, it is very difficult
for the programmers, to write a program in machine language.

Maanya’s M.G.B Publications Microcontrollers


Instruction set of 8051 Microcontroller 5‐9
However this language is preferred for simple programs and control applications
where less computation is required..
Characteristics:
1. Easily understood by the computer system.
2. Very fast in execution and no need of hardware/ circuits/ Compiler/
Assemblies.
3. Difficult to write and understand.
4. Difficult to debug.
5. Very slow to enter.
6. Not readable.
7. Machine dependent.
2. Assembly language
A program written in mnemonics is known as assembly language program.
Mnemonic is a group of alphabets which suggest operation to be performed by that
instruction. A few examples are ADD for addition, XCH for exchange, and MOV A, Rn
transfer data from register Rn to accumulator.
The writing of program in assembly language is much easier as compared to the
writing of a program in machine language. The machine and assembly language
instructions are specific to each microcontroller; they are not transferable to other
microcontroller. In some times it is also called as one to one language since each
mnemonic refers to unique operation.
Characteristics:
1. Very easy to read
2. Easy to debug
3. Easy to write
4. It is not portable i.e. the programs written for one microcomputer cannot be
used for any other because each computer has its own assembly language.
5. It is machine dependent
6. It requires assembler to convert it into machine language.
3. High level language
The disadvantages of assembly languages are overcome using high level
languages. Instructions written in high level language are called statements rather
mnemonics. In a high level language statements more clearly resemble English and
Mathematics than mnemonics. FORTRAN, BASIC, COBOL, C, C++, PASCAL are some of
the examples of high level languages.
Programs written in this language for one type of computer can easily be used for
any other type of computer. Thus the programs written in high languages are portable.
The programs writing in these languages are easier and faster because one statement of

Maanya’s M.G.B Publications Microcontrollers


5‐10 8051 Instruction set & Programming
high level language correspond too many instructions of assembly languages. The
programs written in high level languages are converted to machine level before
executing them. This is done by either compilers or interpreters.
Compiler: A program that translates a high level language program into a
machine language program is called a compiler. A compiler is more powerful than
assembler.
Interpreter: It is also a translator. It translates a high level language program into
object codes, statement wise. It does not translate the entire program at a time. It takes
up one statement of a high level language program at a time, translates it and then
executes it. It is slower than a compiler.

5.7. Write the differences between machine level and assembly


level programming.
S.No. Machine Language Assembly Language High-Level Language

1 Language consists of binary Language consists of Language consists of


codes which specify the mnemonics which specify English-like statements
operation. the operation. which specify more-than
one operations.
2 Processor dependent and Processor dependent and Independent of
hence requires knowledge hence requires knowledge processor.
of internal details of of internal details of
processor to write a processor to write a
program. program.
3 Programs require less Programs require less Programs require more
memory. memory. memory.

4 Programs have less Programs have less Programs have more


execution time. execution time. execution time.
5 Program development is Program development is Program development is
difficult. similar than machine easy.
language.
6 It is not user friendly. It is less user friendly. It is user friendly.

5.8. Classify the instruction set of 8051.


Different microcontroller architecture use different instruction sets. But all
members of the MCS-51 family execute the same instruction set. The MCS-51 instruction
set is optimized for 8-bit control applications. The 8051 has 111 instructions: 49 single
byte, 45 two byte and 17 three byte.

Maanya’s M.G.B Publications Microcontrollers


Instruction set of 8051 Microcontroller 5‐11
Based on function the 8051 instruction set is classified into the following 5 groups:
1. Data transfer group - 28
2. Arithmetic group - 24
3. Logical group - 25
4. Branch group - 17
5. Boolean group - 17

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)

Maanya’s M.G.B Publications Microcontrollers


5‐12 8051 Instruction set & Programming
In this format operand may be register/registers and in some cases it may be
absented or implicited.
Examples: INC A; ADD A,Rn; NOP.
b. Two Byte Instructions
As the name indicates these instructions occupy two memory locations as shown
in fig. 5.9(b).

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

Op-code immediate data ADD A,#data

Op-code direct address ADD A,direct

Op-code bit address ANL C,bit

Op-code relative address DJNZ Rn,rel


c. Three Byte Instructions
This type of instructions occupy three bytes i.e. three memory locations as shown
in fig. 5.9(c).

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

Op-code addr15 - addr8 Addr7 - addr0 LCALL addr16

Maanya’s M.G.B Publications Microcontrollers


Instruction set of 8051 Microcontroller 5‐13
Op-code dir addr(dest) dir addr(scr) MOV direct,direct

Op-code direct address Immediate data MOV direct,#data

Op-code immediate data Relative address CJNE A,#data,rel

Op-code direct address Relative address CJNE A,direct,rel

Op-code bit address Relative address JB blt,rel

*Note:
Except arithmetic group of instructions, remaining group of instructions will have
one or two instructions of three byte length.

5.10. List the various addressing modes of 8051and Explain with


examples.
Each instruction requires certain data on which it has to operate. The data could be
in register/registers, in memory or an immediate value. The various ways of accessing
data are called the addressing modes. The 8051 microcontroller has the following
addressing modes.
1. Immediate addressing mode
2. Direct addressing mode
3. Register addressing mode
4. Register indirect addressing mode
5. Indexed addressing mode
6. Register specific addressing mode
1. Immediate Addressing mode
In this addressing mode the source operand is a constant, i.e. immediate data (8-bit
or 16-bit).
Examples:
MOV A,#47 H Load 47H in to A.
MOV R4,#67H Load 67H in to R4.
MOV DPTR,#8500H DPTR = 5800H.
*Note:
1. The immediate data must be preceded by the pround sign #.
2. Using this addressing mode we can load data into any of the registers, including
the DPTR.
3. This is the easiest addressing mode to transfer the data to destination.

Maanya’s M.G.B Publications Microcontrollers


5‐14 8051 Instruction set & Programming
4. The general format of immediate addressing mode is
MOV A, #nH : Where ‘n’ is the 8-bit data
MOV DPTR, #nnH : Where ‘nn’ is the 16-bit data
MOV R3, It is illegal instruction because source and destination sizes are mismatched i.e.
#125H R3 is a 8-bit register data is 12-bits
MOV 35H, It is illegal instruction because source should not be register it should be data.
#R2
MOV R0, It is not the example of immediate addressing mode instruction because there is
33H missing of symbol ‘#’.

2. Direct Addressing mode


In direct addressing the operand is specified by an 8-bit address filed in the
instruction.
This mode is used to access data either in the internal RAM (128 bytes) or SFRs.
Examples:
MOV R0,40H Save content of RAM location 40H in R0.
MOV 56H,A Save content of A in RAM location 56H.
MOV R4,7FH Move contents of RAM location 7FH to R4.
*Note:
1. Only internal data RAM and SFRs can be directly addressed. Although the entire
128 bytes of RAM can be accessed using direct addressing mode, it is most often
used to access RAM locations 30-7FH.
2. This is due to the fact that register bank locations are accessed by the register
names of R0-R7, but there is no such name for other RAM locations 30-7FH.
3. Stack in 8051 uses only direct addressing modes i.e. Only direct addressing mode
is allowed for pushing or popping the stack
4. The general format of the Direct Addressing Mode is

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).

Maanya’s M.G.B Publications Microcontrollers


Instruction set of 8051 Microcontroller 5‐15
Examples:
MOV A,R0 Copy the contents of R0 in to A.
MOV R2,A Copy the contents of A in to R2.
ADD A,R7 Add the contents of R7 to contents of A.
Do You Know?:
1. The source and destination registers must match in size.
2. The movement of data between Rn registers is not allowed.
3. The general format of Register addressing mode is
4. MOV Rd, Rs where Rd is destination register and Rs is source register
5. MOV DPTR,A; Illegal instruction because the source and destination registers are
mismatched in size
6. MOV R1,R2 ; Illegal instruction because, The movement of data between
Registers of Register bank is not allowed
4. Register Indirect Addressing mode
In indirect addressing, the instruction specifies a register which contains the
address of the operand. Both internal and external RAM can be indirectly addressed.
Examples:
MOV A,@R0 Move contents of RAM location whose Address is held by R0 into
A.
MOV @ R1,0F0H Move contents of B (0F0)into RAM location whose address is held
by R1.
MOVX @ DPTR,A Writes the contents of the accumulator to the address held by the
DPTR register.
*Note:
1. If the data is inside the CPU, only registers R0 and R1 are used for this purpose.
When R0 and R1 are used as pointers, that is, when they hold the address of
RAM location, there must be preceded by the @ sign. In the absence of the “@”
sign, MOV will be interpreted as an instruction moving the contents of register
R0 to A, instead of the contents of the memory location pointed to by R0
2. The external RAM can be addressed indirectly through DPTR.
5. Indexed Addressing mode
Only program memory can be accessed with indexed addressing. Either the DPTR
or PC can be used as an index register.
Examples:
MOVC A, @A+DPTR Copy the code byte found at the ROM address formed by
adding A and the DPTR to A.

Maanya’s M.G.B Publications Microcontrollers


5‐16 8051 Instruction set & Programming
MOVC A, @A+PC Copy the code byte found at the ROM address formed by
adding A and the (PC +1).
6. Reregister Specific Addressing mode
In this addressing mode the instructions always operates on implied register
such as A or DPTR. Therefore no operands have to be specified.
Examples:
RR This instruction operates only on the accumulator. This instruction when
A executed rotates the contents of accumulator one bit towards right.
DAA Decimal Adjustment Accumulator.

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.

Maanya’s M.G.B Publications Microcontrollers


Instruction set of 8051 Microcontroller 5‐17
bit Direct Addressed bit in Internal Data RAM or Special Functional Register.
CY The Carry flag
lsn Least significant nibble
msn Most significant nibble
[ ]: If the condition inside the brackets is true. THEN the action listed will
occur, ELSE go to the next instruction
∧ External memory location
() Contents of the location inside the parentheses
Flag Flag
Instruction Instruction
CY OV AC CY OV AC
ADD X X X CLR C 0
ADDC X XX X CPL C X
SUBB X X X ANL C, bit X
MUL 0 X ANL C,/bit X
DIV 0 X ORL C, bit X
DA X ORL C,/bit X
RRC X MOV C, bit X
RLC X CJNE x
SETB C 1

5.11. Explain data transfer instructions of 8051.

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

Note: The content of accumulator is changed after execution but


contents of R2 remain as it is. This means contents of destination
always change after data transfer instruction is executed.

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

Maanya’s M.G.B Publications Microcontrollers


Instruction set of 8051 Microcontroller 5‐19

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.

Note: Parenthesis () means ‘contents of’

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

6. MOV Rn, #data


• Description Move an immediate data into register Rn.
• Operation (Rn) ← data
• Example MOV R2, # 97 H

7. MOV Rn, direct


Description The content of memory location (internal RAM/SFR) whose address is
directly specified in the instruction is moved to register Rn, where Rn
is any one of the 8 register of the currently selected register bank.
Operation (Rn) ← (direct)
Examples • MOV R3, 42: Move the contents of memory location addressed 42
into R3 i.e. (42) → R3.

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

Maanya’s M.G.B Publications Microcontrollers


Instruction set of 8051 Microcontroller 5‐21
10. MOV direct, #data
Description An immediate data is moved into internal RAM memory location
whose address is specified directly in the instruction.
Note: The destination can be SFR also.
Operation (direct) ← data
Example MOV 38H, #90H: Move 90 H into RAM location having address
38 H.

11. MOV direct, direct


Description The content of one internal RAM/SFR is moved to another
internal RAM/SFR. The address of the source and destination
are directly specified in the instruction.
Operation (direct) ← (data)
Example MOV 0A8 H, 78H: Move the contents of RAM location 78H into
A8H which is Interrupt Enable (IE) register.

12. MOV direct, @Ri


Description The content of internal RAM whose address is specified by
Ri is moved to another internal RAM/SFR whose address is
directly specified in the instruction. The register Ri can be
either R0 or R1.
Operation (direct) ← ((Ri))
Example

Maanya’s M.G.B Publications Microcontrollers


5‐22 8051 Instruction set & Programming
13. MOV @Ri, A
Description The content of accumulator is moved to an internal RAM location whose
address is specified by Ri register. The register Ri can be either R0 or R1.
Operation ((Ri)) ← (A)
Example MOV @ R0,A

14. MOV @Ri, #data


Description The immediate data given in the instruction is moved to an
internal RAM location whose address is specified by Ri
register. The register Ri can be R0 or R1.
Operation ((Ri)) ← data
Example MOV @ R0,44H

15. MOV @Ri, direct


Description The content of internal RAM/SFR whose address is directly
specified in the instruction is moved another internal RAM
location whose address is specified by Ri register. The
register Ri can be either R0 or R1.
Operation ((Ri)) ← (direct)
Example

16. MOV DPTR, #16 bit data


Description Move 16 bit immediate data into data pointer register
(which is a 16 bit register).
Note: This is the instruction which deals with 16 bit data
transfer.
Operation (DPTR) ← 16 bit data
Example MOV DPTR, #8000H

Maanya’s M.G.B Publications Microcontrollers


Instruction set of 8051 Microcontroller 5‐23

17. MOVX A, @Ri


Description Move the contents of external RAM memory location whose
address is specified by register Ri into accumulator.
Note:
1. While accessing external RAM, Ri can address 256 bytes
and DPTR can address 64 K bytes.
2. MOVX instruction is used to access external RAM or I/O
addresses.
3. There are two sets of RAM addresses between 00H and
FFH, one for internal 8051 RAM and another for RAM
external to 8051.
Operation (A) ← ((Ri)) Ext
Example MOVX A, @R1

18. MOVX A, @DPTR


Description Move the contents of the external RAM memory location
whose address is specified by data pointer (DPTR) into
accumulator.
Note: In external moves the data transfer is only between
memory and accumulator.
Operation (A) ← ((DPTR))
Example

19. MOVX @ DPTR, A


Description Move the contents of accumulator into external RAM
memory location whose address is specified by data pointer
(DPTR).
Operation ((DPTR)) ← (A)
Example MOVX @ DPTR,A

Maanya’s M.G.B Publications Microcontrollers


5‐24 8051 Instruction set & Programming

20. MOVC A, @A + DPTR


Description Move the contents of the external ROM memory location
whose address is formed by adding the accumulator
contents with the contents of DPTR.
Note: This instruction is called as move code byte, the C in
MOVC indicates that it’s a code byte.
Operation A ← ((A + DPTR))
Example MOVC A, @ A + DPTR.
This instruction copies the code byte found at the external
ROM address formed by adding A and the DPTR, i.e. at an
address (1200H +61H) 1261 H to A.

21. MOV A,@ A+PC


Description Move the contents of the external ROM memory location
whose address is formed by adding the accumulator
contents with the contents of program counter (PC).
Note: Destination is always an accumulator.
Operation (PC) ←(PC)+1
(A) ← ((A)+(PC))
Example Let the contents of PC are 4000 H and contents of A are 50H.
MOVC A, @ A + PC.
This instruction copies the code byte found at the external
ROM address formed by adding A and the PC, i.e. at an
address (4000 H + 50 H) 4050 H to A.

Maanya’s M.G.B Publications Microcontrollers


Instruction set of 8051 Microcontroller 5‐25

22. PUSH direct


Description • The stack pointer is incremented by one.
• PUSH the contents of internal RAM memory
location/SFR on to the stack addressed by stack pointer
(SP).
Note:
1. The stack is internal RAM area only.
2. PUSH and POP instructions are used to move the data
between internal RAM stack and internal RAM.
Operation (SP) ← (SP ) + 1
((SP)) ← (direct )
Example PUSH 0F0H
This instruction increments the stack pointer by one and
stores the contents of register B (0F0H) to the internal RAM
location addressed by the stack pointer (SP).

Maanya’s M.G.B Publications Microcontrollers


5‐26 8051 Instruction set & Programming
23. POP direct
Description • This instruction moves the contents of stack addressed by
stack pointer (SP) to the internal RAM/SFR (Whose address
is given within the instruction).
• Stack pointer is decremented by one.

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

Maanya’s M.G.B Publications Microcontrollers


Instruction set of 8051 Microcontroller 5‐27
Register R5=20H.

25. XCH A, direct


Description Exchange the contents of direct address (8 bit) and accumulator.
Operation (A) ↔ (direct)
Example XCH A, 30 H: The contents of 30 H (address of internal RAM)
and accumulator are exchanged.

26. XCH A, @Ri


Description Exchange the contents of internal RAM location whose address is
stored either in Ri and accumulator.
Operation ((Ri)) ↔ (A)
Example XCH A, @R0

27. XCHD A, @Ri


Description Exchange the lower nibble (lower four bits) in accumulator and
lower nibble of contents of address in register Ri.
Note: The upper nibbles remain un-exchanged.
Operation ((Ri))0-3 ↔ A0-3
Example

Maanya’s M.G.B Publications Microcontrollers


5‐28 8051 Instruction set & Programming
Summary of Data transfer instructions of 8051

Instructions Description
Operation

Data Moving Instructions


1. MOV A,Rn (A) ← (Rn) Move register to Accumulator.
2. MOV A,direct (A) ← (direct) Move direct byte to Accumulator.
3. MOV A,@Ri (A) ← ((Ri)) Move indirect RAM to
Accumulator.
4. MOV A,#data (A) ← data Move immediate data to
Accumulator.
5. MOV Rn,A (Rn) ← (A) Move Accumulator to register.
6. MOV direct,@Ri (direct) ← ((Ri)) Move indirect RAM to direct byte.
7. MOV DPTR,#data16 (DPTR) ←data16 Load Data Pointer with a 16-bit
constant.
External Data Memory Accessing Instructions
8. MOVX A,@DPTR (A) ← ((DPTR)) Move external RAM (16-bit addr) to
A.
9. MOVX @DPTR,A ((DPTR)) ← (A) Move A to external RAM (16-bit
addr).

External Program Memory Accessing Instructions


10. MOVC A,@A+DPTR (A) ← ((A) + (DPTR)) Move Code byte relative to DPTR to
A.
11. MOVC A,@A+PC (PC) ← (PC)+1 Move Code byte relative to PC to A.
(A) ← ((A)+(PC))
PUSH & POP Instructions
12. PUSH direct (SP) ← (SP)+1 Push direct byte onto stack.
((SP)) ← (direct)
13. POP direct (direct) ← ((SP)) Pop direct byte from stack.
(SP) ← (SP)-1
Data Exchange Instructions
14. XCH A,Rn (A) ↔ (Rn) Exchange register with
Accumulator.
15. XCHD A,@Ri ((Ri0-3)) ↔ (A0-3) Exchange low-order digit indirect
RAM with A.

Maanya’s M.G.B Publications Microcontrollers


Instruction set of 8051 Microcontroller 5‐29

5.12. Explain the arithmetic instructions and recognize the flags


that are set or reset for given data conditions.

1. This group of instructions performs arithmetic operations such as addition, subtraction,


multiplication, division, increment, decrement and decimal adjustment operations on the 8-
bit data present in registers or in memory locations.
2. All the additions are done with the A register as the destination of the result.
3. Add, subtract, increment and decrement instructions use one of the four addressing modes
each: register, immediate, direct, and indirect.
4. There are two sets of addition instructions: add without carry and add with carry. There is
only one set of subtract with borrow, subtract without borrow can be done in two steps-CLR
C and then SBBB.
5. The 8051 has four arithmetic flags: the Carry (CY), Auxiliary Carry (AC), Overflow (OV)
and Parity (P). The CY, AC and OV flags are set to 1 or cleared to 0 automatically,
depending on the outcomes of the certain instructions.
6. The parity flag is affected by every instruction executed.
a) Parity flag will be set to 1 for odd number of 1’s in A register and will be set to 0
for even number of 1’s in A register.
b) All 0’s in A register is considered as even number of 1’s and set P flag to 0.
1. ADD A, Rn
Description The content of register Rn is added with contents of A and result is stored
in register A.
Note: Flags are affected.
Operation (A) ← (A)+(Rn)
Example

Maanya’s M.G.B Publications Microcontrollers


5‐30 8051 Instruction set & Programming

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

Maanya’s M.G.B Publications Microcontrollers


Instruction set of 8051 Microcontroller 5‐31

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

Maanya’s M.G.B Publications Microcontrollers


5‐32 8051 Instruction set & Programming
8. ADDC A, @Ri
Description Add accumulator, the contents of the address stored in register Ri, and
carry and sum is stored in accumulator.
Operation (A) ← (A) + (CY) + ((Ri))
Example ADDC A, @R0
A= 00110011; CY = 1; R0 = 45H;
@R0 = 01000000
After executing ADDC A; @ R0 instruction
A = 01110100; 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

10. SUBB A, #data


Description An immediate number is subtracted from accumulator with carry
(borrow) flag.
Operation (A) ← (A) - (CY) - data
Example SUBB A, #32H
Initially, A register = 85H and CY = 0. After executing SUBB A, # 32H the
accumulator will hold the result 53H, in it and the CY flag is made zero.
A=1 0 0 0 0 1 0 1
CY = 0
1 0 0 0 0 1 0 1
32H = 0 0 1 1 0 0 1 0
= 0 1 0 1 0 0 1 1 = 53H

Maanya’s M.G.B Publications Microcontrollers


Instruction set of 8051 Microcontroller 5‐33

11. SUBB A, direct


Description The contents of RAM location and a carry (borrow) flag are subtracted
from accumulator and result is stored in accumulator.
Operation (A) ← (A) -(CY)-(direct)
Example SUBB A, 32H
Let us assume that, A register = 86H and internal memory location 32H
contains 30H and CY = 1. After executing SUBB A, 32H instruction the
accumulator is loaded with 55H and the memory location content remains
same.
A=1 0 0 0 0 1 1 0
CY = 1
1 0 0 0 0 1 0 1
(32H) = 30H= 0 0 1 1 0 0 0 0
= 0 1 0 1 0 1 0 1 = 55H

12. SUBB A, @Ri


Description The contents of address which is stored in Ri and a carry (borrow) flag are
subtracted from accumulator and result is stored in accumulator.
Operation (A) ← (A) - (CY) - ((Ri))
Example SUBB A, @R0
Assume that, A register = 74H, CY = 1, R0 register = 23H, Internal
Memory (23H) = 32H. After executing SUBB A, @R0 the accumulator
contains 41H, the internal memory content remains same and CY flag is
cleared.
A = 74 H = 0 1 1 1 0 1 0 0
CY = 1
0 1 1 1 0 0 1 1
(R0) = (23) = 32 = 0 0 1 1 0 0 1 0
A = 0 1 0 0 0 0 0 1=41H

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.

Maanya’s M.G.B Publications Microcontrollers


5‐34 8051 Instruction set & Programming
Example: 2

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.

Maanya’s M.G.B Publications Microcontrollers


Instruction set of 8051 Microcontroller 5‐35
Example: 2

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.

Maanya’s M.G.B Publications Microcontrollers


5‐36 8051 Instruction set & Programming

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

Example 2: MOV A, # 10d


MOV B, #15d
MUL AB ; A = 150d = 96H, B = 0 0
Note: This instruction always clears the CY flag; however, OV is changed
according to the product. If the product is greater than FFH, OV = 1;
otherwise, it is cleared (OV = 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.

Maanya’s M.G.B Publications Microcontrollers


Instruction set of 8051 Microcontroller 5‐37

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

Example 2: MOV A, #97H


MOV B, #12H
DIV AB ; A = 8d and B = 7d
Notice in this instruction that the carry and OV flags are both cleared,
unless we divide A by 0, in which case the result is invalid and OV = 1 to
indicate the invalid condition.
A = 45H ; B = 00
Maanya’s M.G.B Publications Microcontrollers
5‐38 8051 Instruction set & Programming
Example 3: DIV AB
After executing the instruction the value in register A and B are
undefined and the OV flag is set to high to indicate an invalid result.
Notice that CY is always 0 in this instruction.
Note:
1. The original contents of A and B are lost
2. There is no comma between A and B in the DIV mnemonic

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

Maanya’s M.G.B Publications Microcontrollers


5‐40 8051 Instruction set & Programming

Summary of Arithmetic instructions of 8051

Instructions Operation Description

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

Maanya’s M.G.B Publications Microcontrollers


Instruction set of 8051 Microcontroller 5‐41
10. DIV AB Quotient in A←(A) ÷ (B) Divide A by B.
Reminder in B
Decimal Arithmetic Operation Instruction
11. DA A If (A3-0)>9 or (AC)=1 then(A3-0) ← Decimal Adjust Accumulator.
(A3-0) + 06
If (A7-4)>9 or (CY)=1 then(A7-4) ← (A7-
4) + 06

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

Maanya’s M.G.B Publications Microcontrollers


Instruction set of 8051 Microcontroller 5‐43
Content of R0 = 45
Content of RAM address at
45 = 00010010
ANL A, @ R0
After execution, A = 00010010
The content of memory location pointed by R0 remains same.

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.

ANL direct, # data


Description An immediate data is ANDed with direct memory location contents and
result is stored in direct memory location.
Operation (direct) ← (direct) AND data
Example Location 51H = 01000001 = 41;
data = 01000101 = 45
ANL 51H, #45
After execution of this instruction, the location 51H contains the result
41H.

Maanya’s M.G.B Publications Microcontrollers


5‐44 8051 Instruction set & Programming

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

Maanya’s M.G.B Publications Microcontrollers


Instruction set of 8051 Microcontroller 5‐45

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

Maanya’s M.G.B Publications Microcontrollers


5‐46 8051 Instruction set & Programming

ORL direct, # data


Description An immediate data is ORed with direct memory location contents and
result is stored in direct memory location.
Operation (direct) ← (direct) OR data
Example Assuming that RAM location 32H has the value 67H, ORL32H, #29
After execution: RAM location 32H: 6F

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

Maanya’s M.G.B Publications Microcontrollers


Instruction set of 8051 Microcontroller 5‐47

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

XRL direct, # data


Description An immediate data is XORed with direct memory location contents and
result is stored in direct memory location.
Operation (direct) ← (direct) XOR data
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

Maanya’s M.G.B Publications Microcontrollers


5‐48 8051 Instruction set & Programming
CPL A
Description • Complement accumulator.
• The contents of accumulator are complemented i.e. 0 to 1 and 1 to 0.
Here all eights bits are complemented.
Operation (A) ← ( A )
Example Suppose A = 55 H then after CPL, A = AA

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.

Operation (An+1) ← (An)n=0-6 (A0) ← (A7)


Example: 1 Suppose A = 1C = 0001 1100
After RL A = A = 0011 1000 = 38 H
Example: 2

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.

Maanya’s M.G.B Publications Microcontrollers


Instruction set of 8051 Microcontroller 5‐49

Operation (An+1) ← (An)n=0-6 (A0) ← (C) (C) ← (A7)


Example: 1 Suppose A = 3A = 0011 1010 and CY = 1
Then, After RLC A 0111 0101 and CY = 0
Example: 2

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.

Operation (An) ← (An+1)n=0-6 (A7) ← (A0)


Example: 1 Suppose A = 1C = 0001 1100
Then, after RR A, A = 0000 1110 = 0E H

Maanya’s M.G.B Publications Microcontrollers


5‐50 8051 Instruction set & Programming
Example: 2

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.

(An) ← (An+1)n=0-6 (A7) ← (C) (C) ← (A0)


Example: 1 Suppose A = 3A = 0 0 1 1 1010 and CY = 0
Then after RRC A = 0 0 0 1 1101 and CY = 0
Example: 2

Maanya’s M.G.B Publications Microcontrollers


Instruction set of 8051 Microcontroller 5‐51

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

Summary of Logic instructions of 8051


Instructions Operation Description

Byte level Logical Operations


1. ANL A,Rn (A) ← (A) AND (Rn) AND Register to Accumulator.
2. ANL A,direct (A) ← (A) AND (direct) AND direct byte to Accumulator.
3. ANL A,@Ri (A) ← (A) AND ((Ri)) AND indirect RAM to Accumulator.
4. ANL A,#data (A) ← (A) AND data AND immediate data to Accumulator.
5. ANL direct,A (direct)←(direct) AND (A) AND Accumulator to direct byte.
6. ANL direct,#data (direct) ← (direct) AND data AND immediate data to direct byte.
7. ORL A,Rn (A) ← (A) OR (Rn) OR register to Accumulator.
8. ORL A,direct (A) ← (A) OR (direct) OR direct byte to Accumulator.
9. ORL A,@Ri (A) ← (A) OR ((Ri)) OR indirect RAM to Accumulator.
10. ORL A,#data (A) ← (A) OR data OR immediate data to Accumulator.
11. ORL direct,A (direct) ← (direct) OR (A) OR Accumulator to direct byte.
12. ORL direct,#data direct) ← (direct) OR data OR immediate data to direct byte.

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.

Maanya’s M.G.B Publications Microcontrollers


5‐52 8051 Instruction set & Programming
16. XRL A,#data (A) ← (A) XORD data Exclusive-OR immediate data to Accumulator.
17. XRL direct,A (direct) ← (direct) XOR (A) Exclusive-OR Accumulator to direct byte.
18. XRL direct,#data direct) ← (direct) XOR data Exclusive-OR immediate data to direct byte.
19. CLR A (A) ←0 Clear Accumulator.

20. CPL A (A) ← ( A ) Complement Accumulator.


Rotate & Swap Instructions
21. RL A (An+1) ← (An)n=0-6 Rotate Accumulator left.
(A0) ← (A7)
22. RLC A An+1) ← (An)n=0-6 Rotate Accumulator left through the carry.
(A0)← (CY) & (CY) ← (A7)
23. RR A (An) ← (An+1)n=0-6 Rotate Accumulator right.
(A7) ← (A0)
24. RRC A (An) ← (An+1)n=0-6 Rotate Accumulator right through the carry.
(A7)←(CY) & (CY) ← (A0)
25. SWAP A (A3-0) ↔ (A7-4) Swap nibbles within the Accumulator.

5.15. Explain Boolean group of instructions.

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.

Maanya’s M.G.B Publications Microcontrollers


Instruction set of 8051 Microcontroller 5‐53
Mnemonic Operation Description
1. CLR C (C) ← 0 Clear carry.
2. CLR bit (bit) ← 0 Clear direct bit.
Note: bit=Address of particular bit of
RAM/SFR.
3. SETB C (C) ← 1 Set carry.
4. SETB bit (bit) ← 1 Set direct bit.
5. CPL C (C) ← ( C ) Complement carry.

6. CPL bit bit ← (bit ) Complement direct bit.

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

Maanya’s M.G.B Publications Microcontrollers


5‐54 8051 Instruction set & Programming
CLR C
• Description Clear carry bit, means carry flag is made 0.

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.

5.14. Explain bit-level logical instructions.


CPL C
• Description Complement the carry flag. If 1, make it 0 and vice versa.

CPL bit

Maanya’s M.G.B Publications Microcontrollers


Instruction set of 8051 Microcontroller 5‐55

• Description Complement the addressed bit.


• Example CPL P1.0 : Complements the bit 0 from port 1.

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.

Maanya’s M.G.B Publications Microcontrollers


Instruction set of 8051 Microcontroller 5‐57
Mnemonic Operation Description
CALL & Subroutines
1. ACALL addr11 (PC) ← (PC)+2 Absolute subroutine call.
(SP) ← (SP)+1
((SP)) ← PCL
(SP) ← (SP)+1
((SP))← PCH
(PC10-0) ← addr11
2. LCALL addr16 (PC) ← (PC)+3 Long subroutine call.
(SP) ← (SP)+1
((SP)) ← (PCL)
(SP) ← (SP)+1
((SP)) ← (PCH)
(PC) ← addr16
3. RET (PCH) ←((SP)) Return from subroutine.
(SP) ← (SP)-1
(PCL) ← ((SP))
(SP) ← (SP)–1
4. RETI (PCH) ←((SP)) Return from interrupt.
(SP) ← (SP)-1
(PCL) ← ((SP))
(SP) ← (SP)–1
5. AJMP addr11 (PC) ← (PC)+2 Absolute jump
(PC10-0) ← addr11

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

Maanya’s M.G.B Publications Microcontrollers


5‐58 8051 Instruction set & Programming
11. CJNE A,direct,rel (PC) ← (PC)+3 Compare direct byte to A
If (A) ≠ (direct) then, and jump if not equal.
(PC) ← (PC)+offset
If (A)<(direct) then, (C) ← 1
If (A)>(direct) then, (C) ← 0
12. CJNE A,#data,rel (PC) ← (PC)+3 Compare immediate to A
If (A) ≠ #data then, and jump if not equal.
(PC) ← (PC)+offset
If (A)<#data then, (C) ← 1
If (A)>#data then, (C) ← 0
13. CJNE Rn,#data,rel (PC) ← (PC)+3 Compare immediate to
If (Rn) ≠ #data then, register and jump if not
equal.
(PC) ← (PC)+offset
If (Rn)<#data then, (C) ← 1
If (A)>#data then, (C) ← 0
14. CJNE @Ri,#data,rel (PC) ← (PC)+3 Compare immediate to
If ((Ri)) ≠ #data then, indirect and jump if not
equal.
(PC) ← (PC)+offset
If ((Ri))<#data then, (C) ← 1
If (A)>#data then, (C) ← 0
15. DJNZ Rn,rel (PC) ← (PC)+2 Decrement register and
(Rn) ← (Rn)-1 jump if not zero.
If (Rn) ≠ 0 then,
(PC) ← (PC)+rel

16. DJNZ direct,rel (PC) ← (PC)+2 Decrement direct byte and


(direct) ← (direct)-1 jump if not zero.
If direct ≠ 0 then,
(PC) ← (PC)+rel

17. NOP (PC) ← (PC)+1 No operation.

5.16. Explain unconditional & conditional jump instructions.


5.16.1. Unconditional jump
The unconditional jump is a jump in which control is transferred
unconditionally to the target location.
In 8051 there are 4 types of unconditional jump instructions. These are:
• AJMP adddr(11)
• LJMP adddr(16)
Maanya’s M.G.B Publications Microcontrollers
Instruction set of 8051 Microcontroller 5‐59
• SJMP adddr(rel)
• JMP @A+DPTR
AJMP address 11
Description (Absolute jump) Jump to absolute (i.e in the range of 2k) as discussed
earlier. This jump is unconditional jump. This is two byte instruction in
which absolute address is written. After execution, program counter is
loaded by the jump address.
Operation (PC) ← (PC)+2
(PC10-0) ← addr11

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))

5.16.2. Conditional jump


JC rel
Description In the JC instruction, if CY = 1 it jumps to the target address. If CY = 0, it
will not jump but will execute the next instruction below JC.
Operation (PC) ← (PC)+2
If (CY)=1 then,
(PC) ← (PC)+rel

Maanya’s M.G.B Publications Microcontrollers


5‐60 8051 Instruction set & Programming
JNC rel
Description In the JNC instruction, if CY = 0 it jumps to the target address. If CY = 1,
it will not jump but will execute the next instruction below JNC.
Operation (PC) ← (PC)+2
If (CY)=0 then,
(PC) ← (PC)+rel

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

CJNE A, direct, rel.


Description Compare contents of accumulator with the contents of direct address and
if not equal, then jump to relative address. Here the status of carry flag is
checked. If accumulator contents are less than direct address contents,
then carry flag is set otherwise it is reset.
Operation (PC) ← (PC)+3
If (A) ≠ (direct) then,
(PC) ← (PC)+offset
If (A)<(direct) then, (C) ← 1
If (A)>(direct) then, (C) ← 0

CJNE A, # data, rel.


Description The contents of register A is compared with immediate data and if not
equal then jump to relative address specified in the instruction. Carry
flag as explained earlier.

Maanya’s M.G.B Publications Microcontrollers


Instruction set of 8051 Microcontroller 5‐61
(PC) ← (PC)+3
If (A) ≠ #data then,
(PC) ← (PC)+offset
If (A)<#data then, (C) ← 1
If (A)>#data then, (C) ← 0

CJNE Rn, # data, rel.


Description Compare the contents of register Rn (R0 – R7) with immediate data
specified in the instruction and if not equal jump to specified relative
address. The status of carry flag is as explained in previous instructions.

Operation (PC) ← (PC)+3


If (Rn) ≠ #data then,
(PC) ← (PC)+offset
If (Rn)<#data then, (C) ← 1
If (A)>#data then, (C) ← 0

CJNE @ Ri, # data, rel.


Description Compare contents of the address contained in Ri with immediate
number (data) specified in the instruction and if not equal then jump to
specified address (relative).
Operation (PC) ← (PC)+3
If ((Ri)) ≠ #data then,
(PC) ← (PC)+offset
If ((Ri))<#data then, (C) ← 1
If (A)>#data then, (C) ← 0

DJNZ Rn, rel.


Description Decrement the contents of register Rn (RO…. R7) specified in the
instruction by one and jump to the relative address if register is not zero.
Here decrement and jump is performed in a single instruction. Whereas
in processor like 8085 two instructions are written.
Operation (PC) ← (PC)+2
(Rn) ← (Rn)-1
If (Rn) ≠ 0 then,
(PC) ← (PC)+rel

Maanya’s M.G.B Publications Microcontrollers


5‐62 8051 Instruction set & Programming

DJNZ direct, rel.


Description Decrement the contents of direct address by one and if not zero jump to
relative address specified in the instruction.
Operation (PC) ← (PC)+2
(direct) ← (direct)-1
If direct ≠ 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.

Maanya’s M.G.B Publications Microcontrollers


Instruction set of 8051 Microcontroller 5‐63

5.17. Write programs of instructions to perform single byte,


double byte and multi byte addition and subtraction.
Single byte and Multi byte Addition
1. Addition of Two 8-bit (Two 1-byte) Numbers
Program 1: Write a program to add two 8-bit numbers 14H and 23H and store the
result in the location 8500H.
a) Flow chart
Start

Initialize data pointer

Bring data to register A

Add the data

Transfer the result into


external memory location

Stop

b) Program
Label Instructions Comments

MOV DPTR,#8500H Initialize the data pointer.

MOV A,#14H Move 14H to A.


ADD A,#23H Add A and 23H.
MOVX @DPTR,A Result is moved into memory location.
STO SJMP STOP Stop execution.
Maanya’s M.G.B Publications Microcontrollers
5‐64 8051 Instruction set & Programming

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

MOV DPTR,#8500H Initialize the data pointer.

MOVX A,@DPTR Data1 is copied into accumulator.

MOV R2,A Data1 is copied into R2 register.

INC DPTR Update the pointer.


MOVX A,@DPTR Data2 is copied into accumulator.
ADD A,R2 Data1 & Data2 are added
INC DPTR Update the pointer.
MOVX @DPTR,A Result is transferred into memory location.
STOP SJMP STOP Stop execution.

2. Addition of Three 8-bit (Three 2-byte) Numbers


Program 3: Write a program to find the sum of three data bytes and store the result in
a specified external memory location.
Label Instructions Comments
MOV DPTR,#8500H Initialize the data pointer
MOV A,#29H Move 29H to A.
ADD A,#35H Add A & 35H.
ADDC A,#47H Add A, 47H and CY.
MOVX @DPTR,A Result is moved into memory location.
STOP SJMP STOP Stop execution.

3. Addition of Two 16-bit (Two 2-byte) Numbers


Program 4: Write a program to ADD given two 16-bit numbers and store the result in
a specified external memory location.
Label Instructions Comments
CLR C Clear the C flag to zero.
MOV A,#DATA L1 Load DATA1 LSD to A.

Maanya’s M.G.B Publications Microcontrollers


Instruction set of 8051 Microcontroller 5‐65
MOV A,#DATA L2 Add the content of A and DATA 2 LSD.

MOV DPTR,#4500H Load DPTR = 4150H.


MOVX @DPTR,A Move the content in A to external memory whose address is 4150H.
INC DPTR Increment the DPTR.
MOV A,#DATA M1 Load DATA1 MSD to A.
ADDC A,#DATA M2 Add A, DATA 2 MSD and CY.
MOVX @DPTR,A Move the sum in A to external memory address 4151H.
HERE SJMP HERE Stay in this loop.

4. Subtraction of Two 8-bit Numbers


Program 5: Write a program to subtract two 8-bit numbers 23H and 14H and store
the result in the location 8500H.
a) Flow chart
Start

Initialize data pointer

Bring data to register A

Subtract the data

Transfer the result into


external memory location

Stop

b) Program
Label Instructions Comments

MOV DPTR, #8000H Initialize the data pointer.

MOV A,#23H Move 23H to A.

Maanya’s M.G.B Publications Microcontrollers


5‐66 8051 Instruction set & Programming
CLR C Clear carry flag.

SUBB A,#14H Subtract 14H from A.

MOVX @DPTR,A Result is moved into memory location.

STOP SJMP STOP Stop execution of the program.

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.

CLR C Clear the carry flag.

SUBB A,R2 Subtract data1 from data2.

JNC DOWN If no carry goto DOWN.

CPL A 1’s Complement

INC A 2’s Complement


DOWN INC DPTR Update the pointer
MOVX @DPTR,A Result is transferred into memory
STOP SJMP STOP Stop execution.

5. Subtraction of two 16-bit numbers


Program 7: Subtract a 16-bit data from another 16-bit data and store the result in
memory.
Label Instructions Comments
CLR C Clear the C (initial borrow as zero).

Maanya’s M.G.B Publications Microcontrollers


Instruction set of 8051 Microcontroller 5‐67

MOV A,#DATA Load DATA1 LSD to A.


L1
SUBB A,#DATA Subtract with borrow the content of A and Data L2.
L2

MOV DPTR,#4500 Load DPTR = 4500H.


MOVX @DPTR,A Move the content in A to external memory whose address
is 4500H.

INC DPTR Add one to DATA pointer.


MOV A,# DATA Load DATA1 MSD to A.
M1
SUBB A,#DATA Subtract with borrow the content of A DATA2 MSD.
M2

MOVX @DPTR,A Move the result in A into memory whose address is at


data pointer.

HLT SJMP HLT Stay in the loop.

5.18. Illustrate the application of jump instruction in the


program.
Program 1: Write a program to clear 16 RAM locations starting at RAM address 60H.
Label Instruction Comment
CLR A A=0
MOV R1,#60H Load pointer, R1=60H.
MOV R7,#10H Load counter, R7=16 (10 in hex).
UP MOV @R1,A Clear RAM location R1 points to.
INC R1 Increment R1 pointer.
DJNZ R7,UP Loop until counter=zero.
Program 2: Write a Program to sum of given N numbers (series of numbers). The
length of the series is in memory location 2200H and the series itself begins from
memory location 2201H. Assume the sum to be 8-bit number so you can ignore
carries. Store the sum at memory location 2300H.

Maanya’s M.G.B Publications Microcontrollers


5‐68 8051 Instruction set & Programming
a) Flow chart

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.

5.19. Define a subroutine and explain its use.


During the execution of a program, certain operations have to be repeated at
different times within the program operating on different parameters. This would be a
Maanya’s M.G.B Publications Microcontrollers
Instruction set of 8051 Microcontroller 5‐69
waste of memory space. Instead of such repeated operations, a min program may be
written for the operation as a sub-program or routine in the memory and it may be
called into main program whenever necessary. Then such sub-program is known as
‘subroutine’.

Fig. Structure of Subroutine


Thus a subroutine is defined as a group of instructions written separately from
the main program to perform a function that occurs repeatedly from the main program
to perform a function that occurs repeatedly in the main program.
Note that the concept of subroutine is mainly used to avoid the repetition of
small programs. The structure of the subroutine is shown in Fig. in which they are
called at various points of the main program by CALL instruction where ever they
required. The subroutine ends with RETURN instruction.
The concept of subroutine is mainly used to avoid repetition of smaller
programs. Subroutine are written separately and stored in the memory. They are called
at various points of the program using CALL instruction.

5.20. Explain the sequence of program when subroutine is called


and executed.
LCALL is a 3-byte instruction, in which one byte is the opcode, and the other two
bytes are the 16-bit address of the target subroutine. LCALL calls a program subroutine.
The following is the sequence of the LCALL instruction.
1. The content of PC is incremented by three
2. Stack pointer is incremented by one
3. The low order PC is pushed on to the stack.
4. Stack pointer is incremented by one
5. The high order PC is pushed on to the stack

Maanya’s M.G.B Publications Microcontrollers


5‐70 8051 Instruction set & Programming
6. The second byte of the instruction is loaded to PCH and the that byte of the
instruction is loaded to PCL
7. Now, the instruction at (PC) is executed.
8. The last instruction of the called subroutine must be RET. After executing the called
subroutine, the address from the stack is loaded into PC. The subroutine may
therefore begin anywhere in the full 64K byte program memory address space. No
flags are affected.

5.21. Explain information exchange between the program


counter and the stack and identification of stack pointer register
when a subroutine is called.

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

Maanya’s M.G.B Publications Microcontrollers


Instruction set of 8051 Microcontroller 5‐71
subroutine is running up to the RET instruction encountered by the controller, the
controller return to the main program and starts to execute the remaining program.
There are so many advantages to subroutine, they are
• The main program becomes simple.
• Execution speed increases.
• It takes less up space in the ROM.
• Debugging is very easy.
• Size of the main program is reduced.
• The disadvantage of the subroutine is that need of stack
• If a subroutine is made up of few instructions, it may be advisable not to create it,
since the call and return mechanism may make it slower execution instructions to
place directly in the main program.
The concept of subroutine can be understood from the following diagram.
• The main program starts at 2000 H; default value of the stack is 07H.
• The controller encounters the subroutine at 2004H in the main program.
• Immediately the program counter is loaded with the starting address of the
subroutine (6070H).
• The return address 2007H is loaded into the stack as the lower byte of the return
address is (07) at 08H location of the stack and higher byte (20) of the return address
is at 09H location of the stack.
• Now the subprogram at the location 6070H is starts to executed.
• Whenever the controller encounters the RET instruction in the subprogram, it will
return back to the return address 2007H of the main program , at the same time the
program counter content becomes 2007H. And controller starts to execute the
remaining program.
Program 8: Write a Program to multiply two 8-bit numbers using MUL instruction
and store the result in a specified external memory location.
a) Flow chart

Maanya’s M.G.B Publications Microcontrollers


5‐72 8051 Instruction set & Programming
Start

Initialize data pointer

Initialize Accumulator with


data 1, B register with data2

Multiply the data

Transfer the result into


external memory location

Stop

b) Program

Label Instructions Comments

MOV DPTR,#8500H Initializing the data pointer


MOV 0F0H,#02H Data1 is transferred into B register.

MOV A,#05H Data2 is transferred into A register.


MUL AB Multiplying the data1 & data2.
MOVX @DPTR,A Copy low order byte of result into 8800H.
INC DPTR Update the pointer to 8801H.

MOV A,0F0H Copy high order byte of result into A.

MOVX @DPTR,A Copy high order byte of result into 8801H.


STOP SJMP STOP Stop execution.

Maanya’s M.G.B Publications Microcontrollers


Instruction set of 8051 Microcontroller 5‐73
Program 3: Write a Program to find biggest data value in given data array.
a) Flow chart

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.

Maanya’s M.G.B Publications Microcontrollers


5‐74 8051 Instruction set & Programming
CJNE A, R3,NE Compare number with maximum number.
AJMP SKIP If equal to SKIP.
NE JC SKIP If not equal check for carry. If carry go to SKIP.
MOV R3,A Otherwise maximum=number
SKIP INC DPTR Increment memory pointer.
DJNZ R0,AGAIN Decrement count, if count = 0 stop.
Otherwise go to AGAIN.
STOP SJMP STOP Stop execution.

Program 4: Write a Program to find the sum of first N natural numbers.


a) Flow chart

Maanya’s M.G.B Publications Microcontrollers


Instruction set of 8051 Microcontroller 5‐75
b) Program
Label Instructions Comments
MOV A, #00H A is initialized with 00H.
MOV R0,#01H Register R0 is initialized with 01H.

MOV R2,#00H Register R2 is initialized with 00H.

Top1 MOV A,R2 Bring the partial result to accumulator.

ADD A,R0 Add the generated number to accumulator.

MOV R2,A Transfer the result to R2 register.

INC R0 Do the generated natural number is 11.

CJNE R0,#0BH,Top1 If not go to the label Top1.

MOVDPTR,5000H Move the address 5000H into DPTR.


MOVX @DPTR,A Result is moved into memory location.
STOP SJMP STOP Stop execution.

Note: Above program adds the generated natural numbers as follows 10 + 9 + 8 + 7 + 6 +


5 + 4 + 3 + 2 + 1 = 55
Program 12: Write a program to convert the given HEX number into its equivalent
BCD number.
a) Flow chart (See on next page)

Maanya’s M.G.B Publications Microcontrollers


5‐76 8051 Instruction set & Programming

b) Program

Label Instructions Comments

MOV A,#76H Load the binary number in A.


MOV B,#64H Load B with 100 decimal.
DIV AB Divide number with 100.
MOV R0,A Save the hundreds of the number.
(Quotient of the previous division)
MOV A,B Get the remainder.
MOV B,#0AH Load B with 10 decimal.
DIV AB Divide number with 10.
MOV R1,A Save the tens of the number.
MOV R3,B Save the ones of the number.
STOP SJMP STOP Stop execution.

Maanya’s M.G.B Publications Microcontrollers

You might also like