100% found this document useful (1 vote)
400 views

Microcontroller Architecture and Programming Electronics Paper I

Uploaded by

philiphazel0
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
400 views

Microcontroller Architecture and Programming Electronics Paper I

Uploaded by

philiphazel0
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 201

ELC - 231

MICROCONTROLLER
ARCHITECTURE &
PROGRAMMING
[2 Credits]
Electronics Paper-I
For
Second Year B.Sc. Computer Science - Semester-III
New Syllabus as per CBCS Pattern from June 2020

Dr. J. A. Bangali S. R. Chaudhari


M. Sc., M. Phil, Ph.D. M. Sc., M.Phil.
Head, Dept. of Electronic Science, Vice Principal and Head,
Coordinator, B.Sc. Computer Science, Dept. of Electronic Science,
Kaveri College of Arts, Science & Commerce, Modern College, Shivajinagar,
Pune - 411038 Pune – 411005.

Prof. (Dr.) P. B. Buchade Prof. (Dr.) M. L. Dongare


M.Sc., M.Phil., Ph.D. M.Sc., D.H.E., Ph.D.
Principal and Head, Head, Dept. of Electronic Science,
Dept. of Electronic Science, Rayat Shikshan Sanstha's,
Abasaheb Garware College, S. M. Joshi College,
Karve Road, Pune - 411004. Hadapsar, Pune – 411028.

Price ` 100.00

N5405
S.Y.B.Sc : Microcontroller Architecture & Prog. ISBN 978-93-90225-12-5
First Edition : July 2020
© : Authors
The text of this publication, or any part thereof, should not be reproduced or transmitted in any
form or stored in any computer storage system or device for distribution including photocopy,
recording, taping or information retrieval system or reproduced on any disc, tape, perforated media or
other information storage device etc., without the written permission of Authors with whom the rights
are reserved. Breach of this condition is liable for legal action.
Every effort has been made to avoid errors or omissions in this publication. In spite of this, errors
may have crept in. Any mistake, error or discrepancy so noted and shall be brought to our notice shall
be taken care of in the next edition. It is notified that neither the publisher nor the authors or seller shall
be responsible for any damage or loss of action to any one, of any kind, in any manner, therefrom.

Published By: Polyplate Printed By:


NIRALI PRAKASHAN RACHANA OFFSETS
Abhyudaya Pragati, 1312, Shivaji Nagar S. No. 15, Arihant Marg
Off J.M. Road, PUNE – 411005 Sukhsagar Nagar, Katraj
Tel - (020) 25512336/37/39, Fax - (020) 25511379 Tel - (022) 2778 2011
Email : [email protected]

DISTRIBUTION CENTRES
PUNE
Nirali Prakashan : 119, Budhwar Peth, Jogeshwari Mandir Lane, Pune 411002,
(For orders within Pune) Maharashtra, Tel : (020) 2445 2044, Mobile : 9657703145
Email : [email protected]
Nirali Prakashan : S. No. 28/27, Dhayari, Near Asian College Pune 411041
(For orders outside Pune) Tel : (020) 24690204; Mobile : 9657703143
Email : [email protected]
MUMBAI
Nirali Prakashan : 385, S.V.P. Road, Rasdhara Co-op. Hsg. Society Ltd.,
Girgaum, Mumbai 400004, Maharashtra;
Mobile : 9320129587 Tel : (022) 2385 6339 / 2386 9976,
Fax : (022) 2386 9976
Email : [email protected]
DISTRIBUTION BRANCHES
JALGAON
Nirali Prakashan : 34, V. V. Golani Market, Navi Peth, Jalgaon 425001,
Maharashtra, Tel : (0257) 222 0395, Mob : 94234 91860;
Email : [email protected]
KOLHAPUR
Nirali Prakashan : New Mahadvar Road, Kedar Plaza, 1st Floor Opp. IDBI Bank,
Kolhapur 416 012, Maharashtra. Mob : 9850046155;
Email : [email protected]
NAGPUR
Nirali Prakashan : Above Maratha Mandir, Shop No. 3, First Floor,
Rani Jhanshi Square, Sitabuldi, Nagpur 440012, Maharashtra
Tel : (0712) 254 7129;
Email : [email protected]
DELHI
Nirali Prakashan : 4593/15, Basement, Agarwal Lane, Ansari Road, Daryaganj
Near Times of India Building, New Delhi 110002
Mob : 08505972553, Email : [email protected]
BENGALURU
Nirali Prakashan : Maitri Ground Floor, Jaya Apartments, No. 99, 6th Cross,
6th Main, Malleswaram, Bengaluru 560003, Karnataka;
Mob : 9449043034
Email: [email protected]
Other Branches : Hyderabad, Chennai

[email protected] | www.pragationline.com
Also find us on www.facebook.com/niralibooks
Preface …
We have great pleasure to present this book on ‘Microcontroller
Architecture and Programming’ which is written as per the revised
syllabus of Savitribai Phule Pune University for S.Y.B.Sc. (Computer
Science), semester III course ELC 231 in the subject of Electronics. The
authors have sincerely put their efforts to give complete information of
the subject as prescribed in the syllabus in a simplified manner.
Electronics and computer science go hand in hand. In fact, electronics
is a backbone for understanding computer operations.
This text book has been prepared keeping in mind the need of
subject and syllabus specified by SPPU.
The First Chapter describes architecture and internal block diagram of
8051 microcontroller. Internal RAM and ROM organization of 8051 are
also explained in this chapter.
Second chapter gives details of instruction set of 8051
microcontroller. Assembly language programming of 8051 is also
elaborated in this chapter.
Timers/Counters, Interrupts of 8051 microcontroller are discussed in
detail in chapter three. In this chapter timer programming in ‘C’ language
is elaborated.
Chapter four describes interfacing of devices such as ADC, DAC,
Stepper motor, LCD. Serial communication using 8051 is also discussed in
detail in this chapter.
After studying all this, students will get a clear understanding of
computer organization. To give something extra to think and to explore
the topic, ‘Think over It’ element is added in this book. As per new choice
based credit based system syllabus of Savitribai Phule Pune University
questions like multiple choice, true/false, short answer, long answer type
questions are included at the end of each unit. Also two sample question
papers as per university guidelines are provided.
We are thankful to the publishers Shri Dineshbhai Furia and Shri
Jignesh Furia and the staff of Nirali Prakashan specially Mr. Ilyas Shaikh,
Mrs. Manasi Pingle, Ms. Chaitali Takle, Mr Ravindra Walodare for the
great efforts they have taken to publish this book in time.
All valuable suggestions from the readers of this book are always
welcome.

AUTHORS
Syllabus …

Unit 1 : Basics of Microcontroller & Intel 8051 Architecture


(8 L)
Introduction to Microcontrollers, Difference in Controller and
Processor. Architecture of 8051, Internal Block Diagram, Internal RAM
Organization, SFRS, Pin functions of 8051, I/O port structure and
Operation, External Memory Interface.
Unit 2 : Programming Model of 8051 (10 L)
Instruction classification, Instruction set, Addressing Modes :
Immediate, Register, Direct, Indirect and Relative, Assembler
Directives (ORG, END), Features with examples, I/O Bit and Byte
programming using assembly language for LED and Seven Segment
Display (SSD) interfacing.
Introduction to 8051 programming in C.
Unit 3 : Timer / Counter, Interrupts (10 L)
Timer / Counter : TMOD, TCON, SCON, SBUF, PCON Register, Timer
modes, Programming for time delay using mode 1 and mode 2.
Interrupts : Introduction to interrupt, Interrupt types and their vector
addresses, Interrupt enable register and interrupt priority register
(IE, IP)
Unit 4 : Interfacing Serial Communication (8 L)
Programming of serial port without interrupt, Serial Communication :
Synchronous and asynchronous serial communication, Use of timer
to select baud rate for serial communication. Interfacing : ADC, DAC,
LCD, Stepper motor.
Contents …

1. Basics of Microcontroller & Intel 8051 Architecture


1.1 − 1.32

2. Programming Model of 8051 2.1 − 2.48

3. Timer / Counter, Interrupts 3.1 − 3.62

4. Interfacing, Serial Communication 4.1 − 4.52

✍✍✍
Unit 1…
Basics of Microcontroller
and
Intel 8051 Architecture
The first microcontroller was invented in
Texas Instruments. During 1970-71, when
Intel was working on inventing the world’s
first microprocessor, Gray Boone of Texas
Instruments was working on similar concept
and invented microcontroller. Boone
designed a single integrated circuit (IC) chip

that could hold nearly all the essential circuits to form a calculator, only
the display and the keypad were not incorporated. The first IC was
named as TMS1802NC. This device has 5000 transistors providing 3000
bits of program memory and 128 bit of access memory. It can perform a
range of functions.

Introduction
• Microcontroller is one of the powerful control systems, which can be
used for the measurement and control applications in industry as
well as in a specific application. Microcontroller is sometimes
described as a “Computer on Chip” because it contains all the
features of full computer including Central Processing Unit (CPU),
inbuilt clock circuitry ROM, RAM, input and output ports with special
features such as serial communication, analog to digital conversion
and more recently signal processing.
• It is a general purpose device, which is meant to read data, perform
limited calculations on the data and control its environment based
on those calculations. The prime use of microcontroller is to control
the operation of machine using a fixed program that is stored in
ROM and that does not change over the lifetime of system.
(1.1)
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 1-2 Basics of Microcontroller ….

• A special application of microcontroller is well suited for data logging


as well as monitoring and recording the environmental, physical and
chemical parameters. In addition to the above home monitoring
systems, embedded processors are used in home appliances.
• Microcontrollers have many advantages as compared with the
microprocessors especially in the areas where high integration and
saving of space is required.
• The main difference between microprocessor and microcontroller is
given below:
Microprocessor Microcontroller
1. Microprocessor is used in 1. Microcontroller is used in
computer system. It is the embedded systems.
heart of computer.
2. It consists of Memory and I/O 2. Microcontroller consists of
components which are inbuilt internal memory and
externally connected to it. I/O ports.
3. Since memory and I/O are 3. Since memory and I/O are
externally connected, the inbuilt, the circuit is small.
circuit becomes large.
4. Cost of the entire system is 4. Cost of the entire system is
more. less.
5. Total power consumption is 5. Total power consumption is
high as compared with less and can be used in
microcontrollers, so cannot be battery operated devices.
used in battery operated
devices.
6. Most of the microprocessors 6. Most of the microcontrollers
do not have power saving have power saving modes
features. like idle mode and power
saving mode. This helps to
reduce power consumption
even further.
… (Contd.)
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 1-3 Basics of Microcontroller ….

Microprocessor Microcontroller
7. Since memory and I/O 7. Since components are in-
components are externally built, most of the operations
connected, each instruction are done by internal
will need external operation, instructions, hence speed is
hence it is relatively slower. fast.
8. Microprocessors are based on 8. Microcontrollers are based
Von-Neumann architecture on Harvard architecture
where program and data are where program memory and
stored in same memory Data memory are separate.
module.
9. Mainly used in personal 9. Mainly used in washing
computers. machine, MP3 players, stand
alone systems.
• More than ten times as many microcontrollers than microprocessors
are manufactured and sold in the world inspite of the high profile. In
recent time, eight bit microcontrollers have become the backbone
for the solution of application of intermediate complexity. Small size,
low power consumption and flexibility make these devices ideal.
Being software driven, microcontrollers greatly simplify the design of
sophisticated instrumentation and control circuitry.
• Now-a-days everyone is familiar with a term embedded system like
cell phones, remote controllers, washing machines, microwave ovens,
fax machines etc. The meaning of embedded system is the system
which performs some specific task.
• In other words, an embedded products uses microprocessor (or
microcontroller) to do one and only one task. These systems are not
generalized. They are assigned some specific sequence of operations
and requires sort of automation to control the sequence of
operations.
• The device which controls and sequences the flow of operation in a
specified manner is referred as microcontroller. Microcontrollers are
assigned with some specific task which they perform in a systematic
way.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 1-4 Basics of Microcontroller ….

1.1 Internal Block Diagram of 8051


Microcontroller
• Intel Corporation introduces an 8-bit microcontroller called 8051.
This microcontroller had 128 bytes of RAM, 4 k bytes of on-chip
ROM, two timers and four ports all on a single chip. The 8051 is
an 8-bit processor that means CPU can work on only 8 bits of
data at a time. It has four 8-bit ports.
ALU Time/Counter Parallel I/O
port
Accumulator
Register(s) Serial I/O
port

Internal
Internal ROM Interrupt
RAM circuits

Stack pointer Clock


circuit

Program counter

Fig. 1.1 : A internal block diagram of microcontroller


• Fig. 1.1 shows the block diagram of typical microcontroller. It is the
on chip computer. It contains all blocks such as RAM, ROM, parallel
I/O and serial I/O ports on a single chip. Along with this, it also has
ALU, SP and other registers to perform the features supported by
microprocessor.
• Microcontroller can perform simple arithmetic and logical operations.
The prime use of microcontroller is to control operations with proper
sequence.
• This chapter provides a broad overview of the architecture of the
8051. The 8051 introduced by Intel Corporations is also referred as
MCS-51.
Salient Features of 8051 Microcontroller :
The salient features of 8051 microcontroller are :
1. It is an 8-bit microcontroller, designed using NMOS technology.
2. 8-bit data bus, 16-bit address bus.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 1-5 Basics of Microcontroller ….

3. 4 k bytes – on-chip ROM.


4. 128 bytes – on-chip RAM.
5. 32 general purpose registers, each of 8-bit.
6. Operating frequency 12 MHz.
7. Multiply and divide instruction available.
8. It has a boolean processor and supports bitwise operations.
9. 16-bit address bus multiplexed with port 0 and port 2. Port 0 is
also 8-bit data bus.
10. Four I/O ports (8-bit).
11. 16-bit 2 timers.
11. One serial port.
12. Five interrupt sources.
Table 1.1 : MCS – 51 family members

Device One Chip One Chip No. of No. of Serial


Data Program Timers Interrupts Port
Memory Memory
Bytes

8031 128 None 2 5 1

8032 256 None 3 6 1

8051 128 4 k bytes 2 5 1


ROM

8052 256 8 k bytes 3 6 1


ROM

8751 128 4 k bytes 2 5 1


EPROM

8752 256 8 k bytes 3 6 1


EPROM
• The block diagram of the 8051 in Fig. 1.2 shows all of the features
which are unique to microcontrollers.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 1-6 Basics of Microcontroller ….

Arithmetic Special- I/O

Port 0
Latch
and PSW function
registers A0-A7
logic unit
RAM D0-D7

8-bit data and


A B address bus

Latch

Port 1
I/O

DPTR
PC DPH ROM

Latch

Port 2
DPL I/O
A8-A15
16-bit address bus
I/O

Port 3
Interrupt

Latch
Special- Counter
EA System
timing Byte/bit function Serial data
ALE registers RD - WR
PSEN addresses
XTAL1 System IE
XTAL2 interrupt Register
bank 3 IP
RESET timers
PCON
VCC Data buffers Register SBUF
GND Memory control bank 2 SCON
TCON
Register TMOD
bank 1 TL0

Register TH0
bank 0 TL1
TH1
(Internal RAM structure)

Fig. 1.2 : 8051 block diagram


• The basic architecture of 8051 microcontroller can be divided into
seven parts :
1. Processing unit
2. Register set
3. Memory units
4. I/O ports and serial port
5. Timing and control unit
6. Interrupt structures
7. Timers
• The programming model of 8051 is shown in Fig. 1.3. From the block
diagram and programming model registers, Special Function
Registers (SFR) and its memory location can be studied.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 1-7 Basics of Microcontroller ….

8 B8 * 8 A8 * 8 89 8 88 *
IP IE TMOD TCON FFF
Register Register Register Register
8 E0* 8 F0 *
Interrupt registers Timer/counter registers
A B
Register Register 8 8C 8 8A 8 8D 8 8B
Math registers TH0 TL0 TH1 TL1
Counter Counter Counter Counter
Timer/counter registers

8 98* 8 99 8 87 8 D0*
SCON SBUF PCON PSW
Register Register Register Register
7F Serial data registers Flags
General
purpose 8 81
area SP
30 Register
2F Bit 7F Stack pointer register
address Bit Addresses for This RAM Area Only
20 area
00
1F Register
bank
18 3
17
Register 8 83 8 82 16 No Address
bank Data pointer Program counter
10 2 DPH DPL
0F Register Data and code addressing registers
bank
08 1
07 R7 8 80* 8 90* 8 A0* 8 B0*
06 R6 Port 0 Port 1 Port 2 Port 3
Register 05 R5 Latch Latch Latch Latch
Bank 04 R4
0 03 R3
02 R2 Number of Direct byte address
01 R1 bits
00 R0 000
Byte Internal Internal
Addresses RAM ROM

Note : *lndicates bit addressable


Fig. 1.3 : 8051 programming model
1.2 Registers
• Fig. 1.3 shows the programming model having collection of 8-bit and
16-bit registers. These registers and memory locations can be made
to operate using the software instructions that are incorporated as a
part of the design. The program instructions are related with the
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 1-8 Basics of Microcontroller ….

control of registers and digital data paths that are physically


contained inside the 8051.
• In CPU, registers are used to store information temporarily. That
information could be a byte of data to be processed, or an address
pointing to the data to be fetched.
D7 D6 D5 D4 D3 D2 D1 D0
8-bit Register
• The 8-bit register is shown from the MSB (Most Significant Bit) D7 to
the LSB (Least Significant Bit) D0 with an 8-bit data type. 8051
contains large number of registers. Some of the widely used registers
are Accumulator A and B register, register R0, R1, R2, R3, R4, R5, R6, R7
as well as Data Pointer (DPTR), Stack Pointer (SP) and Program
Counter (PC). All the above registers are 8-bit except DPTR and PC.
• There are two math registers namely Accumulator A and B. These
registers (A and B) are mostly used in arithmetic and logical
operations as well as used in data transfer between 8051 and
external memory. B register is used with A for multiplication and
division operations. Some Special Function Registers (SFR) are also
there in 8051.
• Each register with an exception of Program Counter (PC) has an
internal 1 byte address assigned to it. Some registers marked with ‘*’
(asterisk sign) are both bit and byte addressable. i.e. the entire byte
of data at each register address may be read or altered, or individual
bits may be read or altered. Software instructions are generally able
to specify a register by its address, its symbolic name or both.

1.3 Flags and Program Status Word (PSW)


• The 8051 has a flag registers to indicate arithmetic condition. These
are 1-bit registers provided to store the results of certain program
instructions. Other instructions can test the condition of flags and
make the decisions based on the flag status.
• The flags may be conveniently addressed and they are grouped
inside the Program Status Word (PSW) and Power Control register
(PCON).
1.3.1 PSW (Program Status Word) Register
• The Program Status Word (PSW) Register is an 8-bit register. It is
referred as flag register.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 1-9 Basics of Microcontroller ….

• Although PSW is 8-bit wide register, only 6-bits of it are used by the
8051. The two unused bits are user definable flags.
• Four of the flags are called conditional or math flags, meaning that
they indicate some conditions that results after an instruction is
executed.
• These four flags are Carry (CY), Auxiliary Carry (AC), Parity (P) and
Overflow (OV).
CY AC F0 RS1 RS0 OV − P

Bit Symbol
PSW.7 CY Carry flag
PSW.6 AC Auxiliary carry flag
PSW.5 F0 User flag 0
PSW.4 RS1 Register bank select bit 1
PSW.3 RS0 Register bank select bit 0
PSW.2 OV Overflow flag
PSW.1 − Reserved for future use
PSW.0 P Parity flag
Carry flag (CY) : This flag is set if there is a carry out from D7 bit. This
flag bit is affected after an 8-bit addition or subtraction. It can be set
to 1 or 0 directly by an instruction “SETB C” and “CLR C”.
Auxiliary carry flag (AC) : If there is a carry from D3 to D4 during an
ADD or SUB operation, this bit is set, otherwise it is cleared. This flag
is used by instructions that perform BCD (Binary Coded Decimal)
arithmetic.
Parity flag (P): The parity flag reflects the number of 1s in A
(accumulator) register only. If register A contains an odd number of
1s then P = 1. Therefore, P = 0 if A has an even number of 1s.
Overflow flag (OV): This flag is set if the result of a signed number
operation is too large, causing the high order bit to overflow into the
sign bit. In general, the carry flag is used to detect errors in unsigned
arithmetic operations.
Register Bank Select Bits (RS0 and RS1) :
• The 128 bytes on chip memory location are divided into different
sections, out of which 00H to 1FH is divided into four register
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 1-10 Basics of Microcontroller ….

banks. Bit PSW.4 and PSW.3 are RS1 and RS0 respectively which
selects the register bank.
RS0 RS1 Register Bank Address
0 0 0 00H − 07H
0 1 1 08H − 0FH
1 0 2 10H − 17H
1 1 3 18H − 1FH

1.4 Data Pointer and Program Counter (PC)


• The 8051 contains two 16-bit registers, Data Pointer (DPTR) and
Program Counter (PC). Each is used to hold the address of a byte in
memory.
• DPTR is a 16-bit register consisting of two 8-bit DPH and DPL, which
are used for handling memory addresses from internal and external
code access or external data access.
• It is possible to use DPH (Data Pointer Higher byte) or DPL (Data
Pointer Lower byte) separately or single 16-bit DPTR in an
instruction.
• Program Counter (PC) is a 16-bit register which always points to the
next instruction to be fetched from ROM. And it is automatically
incremented after each instruction execution. PC gets affected by
JMP and CALL instruction.
• PC register does not have an internal address.
• PC is 16-bit wide, means it can fetch address 0000H to FFFFH, total
64 k byte of code.
1.5 The Stack and Stack Pointer (SP)
• Stack referred to an internal RAM is used to store and retrieve data
quickly.
• The 8-bit Stack Pointer (SP) register is used to hold an internal RAM
address which always points to top of the stack that is it holds the
address of memory location at which last byte in the stack is stored.
• When data is to be placed onto the stack, SP gets incremented by
one and then data is placed on the stack.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 1-11 Basics of Microcontroller ….

• While the data is retrieved from the stack, the data is taken out first
and then stack pointer is decremented.
• Fig. 1.4 shows the stack operations.
CPU CPU
action action

SP = 0A Store data Address 0A Get data SP = 0A

SP = 09 Store data Address 09 Get data SP = 09

SP = 08 Store data Address 08 Get data SP = 08

SP = 07 Address 07 SP = 07
Storing data Internal RAM Getting data
on the stack from the
(increment stack (get then
then store) decrement)

Fig. 1.4 : Stack operations


• On reset, stack pointer contains the value 07H.
• The stack size is limited to the size of internal RAM. The stack is
overlapped on bank 1 because they have same starting address, even
though bank 1 can be used separately by programmer. In this case,
the SP has to be initialized somewhere else by the programmer, so
that bank 1 and stack would not be overlapped.

1.6 Special Function Register (SFR)


• These 128 bytes are reserved for SFRs. From address 80H to FFH all
SFRs are addressed. Table 1.2 shows the list of SFRs which may be
addressed like internal RAM.
• Some of the SFRs marked with asterisk (*) are bit addressable. Not all
addresses from 80H to FFH are used for SFRs. Some of the addresses
are not defined. In the instruction, user can either specify the SFR
name or the address of the SFR.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 1-12 Basics of Microcontroller ….

• SFRs are named in certain opcodes by their functional names, such as


A or TH0, are referenced by other opcode by their address, such as
0E0H or 8CH.
• Note that, any address used in the program must start with a
number, thus address E0H for the SFR accumulator A begins with 0
i.e. 0E0H, otherwise there will be assembler error.
Table 1.2 : SFR names and equivalent internal RAM address

Name Function Internal RAM Address


(HEX)
A Accumulator 0E0
B Arithmetic 0F0
DPH Addressing external memory 83
DPL Addressing external memory 82
IE Interrupt enable control 0A8
IP Interrupt priority 0B8
P0 Input/Output port latch 80
P1 Input/Output port latch 90
P2 Input/Output port latch A0
P3 Input/Output port latch 0B0
PCON Power control 87
PSW Program status word 0D0
SCON Serial port control 98
SBUF Serial port data buffer 99
SP Stack pointer 81
TMOD Timer/counter mode control 89
TCON Timer/counter control 88
TL0 Timer 0 low byte 8A
TH0 Timer 0 high byte 8C
TL1 Timer 1 low byte 8B
TH1 Timer 1 high byte 8D
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 1-13 Basics of Microcontroller ….

1.7 I/O Pins


• The 8051 microcontroller has an important feature of in-built
Input/Output (I/O) ports. Through these ports, microcontroller can
directly interface with the outside world.
• The 8051 incorporate many functions which are economically and
technically feasible.
• The main constraint that limits numerous functions is the number of
pins available to the 8051 circuit designers.
Pin Diagram of 8051 and Its Description :
P1.0 1 VCC 40 + 5V

P1.1 2 39 (AD0)P0.0
P1.2 3 38 (AD1)P0.1
P1.3 4 37 (AD2)P0.2
P1.4 5 36 (AD3)P0.2
P1.5 6 (AD4)P0.4
35
P1.6 7 34 (AD5)P0.5
P1.7 8 (AD6)P0.6
33
RST 9 32 (AD7)P0.7
P3.0(RXD) 10
8051 31 (VPP)/EA
P3.1(TXD) 11
30 (PROG) ALE
P3.2(INT0) 12
29 PSEN
P3.3(INT1) 13
28 (A15)P2.7
P3.4(T0) 14
27 (A14)P2.6
P3.5(T1) 15
26 (A13)P2.5
P3.6(WR) 16
25 (A12)P2.4
P3.7(RD) 17
24 (A11)P2.3
XTAL2 18
23 (A10)P2.2
XTAL1 19 (A9)P2.1
22

Ground 20 VSS 21 (A8)P2.0

Fig. 1.5 : Pin diagram of 8051


S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 1-14 Basics of Microcontroller ….

• Fig. 1.5 shows pin diagram of 8051.


• It is a 40 pin IC, 32 pins are set aside for the 4 ports (P0 − P3), where
each port takes 8 pins. The rest of the pins are VCC, GND, XTAL1,
 
XTAL2, RST, EA, PSEN and ALE.
• Out of these 8 pins, six of them (VCC, GND, XTAL1, XTAL2, RST and

EA) are used by all members of 8051 and 8031 families. They must be
connected in order to work the system, regardless of whether the
microcontroller is from 8051 or 8031 family.

• The other two pins PSEN and ALE are mainly used in 8031 systems.
Below details of these six pins are given:
VCC : Pin 40 provides the supply voltage to the chip. The voltage
source is +5 V.
GND : Pin 20 is ground.
XTAL1 and XTAL2 : The 8051 has on-chip oscillator but requires an
external clock to run it. A crystal oscillator is connected between
these pins (pin no. 19 and 18). It also needs the capacitor between
them, of value 30 pF. One side of capacitor is connected to ground
as shown in Fig. 1.6.
30 pF
XTAL1

XTAL2
30 pF
Gnd

Fig. 1.6
Speed of 8051 refers to the maximum frequency of oscillator
connected to XTAL1 and XTAL2. For example, if 12 MHz is frequency
of oscillator then speed at 8051 is also 12 MHz.
RST : Pin 9 is the RESET pin. It is active high input pin. Upon applying
pulse to this pin, the microcontroller will reset and terminates all
activities. This is also referred as power-on-reset. Activating reset will
cause all values in registers to be lost.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 1-15 Basics of Microcontroller ….

Table 1.3 : On RESET values of some registers


Register Value
PC 0000
ACC 0000
B 0000
PSW 0000
SP 0007
Value of PC is reset to 0000H forcing microcontroller to fetch the first
instruction from ROM memory location 0000H. Therefore, we must
place the code at location 0000H.
Fig. 1.7 shows the reset circuit of 8051.
VCC

10 mf

30 pF EA/VPP
X1
11.052 MHz

X2
30 pF

RST

Fig. 1.7 : Reset circuit



EA : In 8051, it comes with on-chip ROM to store programs. To

access on-chip ROM, EA is connected to VCC. To access off-chip ROM,

EA pin has to be connected to ground to indicate that the code is
stored externally.
 
PSEN : This is an output pin which stands for Program Store
Enable. In case of off-chip ROM, this pin is connected to the OE pin

of ROM to read the ROM contents. It acts as RD in case of off-chip
ROM.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 1-16 Basics of Microcontroller ….

ALE : Address Latch Enable is an output pin and it is active high.


When 8051 is connected to external memory, port 0 provides both
address and data on multiplexed bus. In order to demultiplex it, ALE
signal is used.
1.8 Ports
• In 8051, there are four 8-bit ports P0, P1, P2 and P3.
• All the ports upon RESET are configured as inputs and are ready to
use as input ports. When the first 0 is written to a port, it becomes an
output. To reconfigure as an input port, logic ‘1’ must be sent to the
port.
• Each port has D type output latch for each pin. The SFR for each port
is made up of these eight latches which can be addressed at SFR
address for that port.
• The ports are not capable of driving loads that require currents in the
tens of milliamperes (mA).
1.8.1 Port 0
• Port 0 occupies a total of 8 pins (Pin 32 - Pin 39). It can be used for
input or output as shown in Fig. 1.8.
Port 0 → Address data lines
Enhancement
mode FET VCC
Control signals
Address/Data Two
1 oscillator
Read latch bit
periods Depletion
0 mode FET
VCC

Internal bus D Q Control Pin Internal FET Pull-up


Write to latch CL Q logic 0.X
Port SFR latch

Read pin data

Fig. 1.8 : Port 0 pin configuration


• Port 0 pins may serve as inputs, outputs or as a bidirectional low
order address and data bus for external memory when used
together.
• For example, when a pin is to be used as an input, a logic ’1’ must be
written to the corresponding port 0 latch by the program, thus
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 1-17 Basics of Microcontroller ….

turning both of the output transistors OFF which in turn causes the
pin to “float” in a high-impedance state and the pin is essentially
connected to the input buffer.
• When used as an output, the pin latches that are programmed to a 0
will turn ON the lower FET, grounding the pin. All latches that are
programmed to a 1 still float; thus, external pull-up resistors will be
needed to supply logic high when port 0 is used as an output.
• When port 0 is used as an address bus to external memory, internal
control signals switch the address lines to the gates of the Field
Effect Transistors (FETs). A logic ‘1’ on an address bit will turn the
upper FET ON and the lower FET OFF to provide a logic high at the
pin. When the address bit is zero, the lower FET is ON and the upper
FET is OFF to provide a logic low at the pin.
• After the address has been formed and latched into external circuits
by the Address Latch Enable (ALE) pulse, the bus is turned around to
become a data bus. Port 0 now reads data from the external memory
and must be configured as an input, so logic ‘1’ is automatically
written by internal control logic to all port 0 latches.
• Port 0 is different than other ports as it does not have the pull-up
registers, while all other ports have internal pull-up registers. In case
of port 0, external pull-up registers are provided as shown in the
Fig. 1.9.
VCC

10 kW

P0.0
P0.1 Port 0
P0.2
8051 P0.3
P0.4
P0.5
P0.6
P0.7

Fig. 1.9 : Port 0


• This is due to the fact that P0 is an open drain configuration. In order
to make it as input port, it must be programmed by writing 1 to all
the pins.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 1-18 Basics of Microcontroller ….

• Dual role of port 0 : Port 0 is also designed as AD0 - AD7, allows it to


be used as address bus or data bus. In case of external memory, port
0 provides both address and data. 8051 has a multiplexed lowered
order address bus (A0 - A7) and data bus (D0 - D7). ALE signal is used
for demultiplexing it.
1.8.2 Port 1
VCC
Read latch bit
Internal
FET
Pull-up
Internal bus D Q Pin
Write to latch 1.X
CL Q
Port SFR latch

Read pin data


Fig. 1.10 : Port 1 pin configuration
• Port 1 is a 8 pins port (Pin 0 - Pin 7). It can be used as input or
output. It has internal pull-up resistors. To configure it as input port
just write ‘1’ to all pins of port 1 as shown in Fig. 1.10. It has no dual
functioning.
1.8.3 Port 2
• It is again 8-bit port. It has an internal pull-up registers. To make it as
input port, it must be programmed by writing 1 to all its bits as
shown in Fig. 1.11.
Control signals
VCC
Address/Data
Read latch bit
Internal
FET
Pull-up
Internal bus D Q Pin
Control
Write to latch 2.X
CL Q logic
Port SFR latch

Read pin data


Fig. 1.11 : Port 2 pin configuration
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 1-19 Basics of Microcontroller ….

Dual role of port 2 : Port 2 also acts as a higher order address bus.
In case of accessing external memory (64 k bytes) it provides the higher
order address (A8 - A15).
1.8.4 Port 3
Port 3 is 8-bit port. It can be I/P or O/P port. By default it is output
port. It has internal pull-up registers. O/P port is commonly used for
some important signals. As this port is a dual functioning port, it is
normally used for the other purpose listed in Table 1.4. The Fig. 1.12
shows port 3 pin configuration.
Alternate output VCC
Read latch bit
Internal
FET
pull-up
Internal bus D Q Pin
Write to latch 3.X
CL Q
Port SFR latch

Read pin data


Alternate input

Fig. 1.12 : Port 3 pin configuration


Table 1.4
P3 bit P3⋅0 P3⋅1 P3⋅2 P3⋅3 P3⋅4 P3⋅5 P3⋅6 P3⋅7

Function R×D T×D   T0 T1 − −


INT0 INT1 WR RD

1.9 Internal Memory


• A functioning computer must have memory for program code bytes,
commonly in ROM and RAM memory for variable data that can be
altered as the program run. The 8051 has internal RAM and ROM
memory for these functions.
1.9.1 Internal RAM
• The 128 byte internal RAM, which is organized into three distinct
areas is shown in Fig. 1.13.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 1-20 Basics of Microcontroller ….

Byte
Byte Address
Address 7F

1F R7
1E R6
1D R5
Bank 3

1C R4
1B R3
1A R2
19 R1
18 R0
17 R7
16 R6
15 R5
Bank 2

14 R4
13 R3
12 R2
11 R1 Byte Bit
R0 Address Address
10
0F R7 2F 7F 78
0E R6 2E 77 70
0D R5 2D 6F 68
Bank 1

0C R4 2C 67 60
0B R3 2B 5F 58
0A R2 2A 57 50
09 R1 29 4F 48
08 R0 28 47 40
27 3F 38
07 R7
06 R6 26 37 30
05 R5 25 2F 28
Bank 0

04 R4 24 27 20
03 R3 23 1F 18
02 R2 22 17 10
01 R1 21 0F 08
00 R0 20 07 00 30
7 0
Working Registers Bit Addressable General Purpose
Fig. 1.13 : Internal RAM organization
• Thirty-two bytes from address 00H to 1FH that make up 32 working
registers are organized as four banks of eight registers each.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 1-21 Basics of Microcontroller ….

• The four register banks are numbered from 0 to 3 and are made up
of eight registers named R0 to R7.
• Each register can be addressed by name (when its bank is selected)
or by its RAM address. Thus, R0 of bank 3 is R0 (if bank 3 is currently
selected) or address 18H (whether bank 3 is selected or not).
• Bits RS0 and RS1 in the PSW determine which bank of registers is
currently in use at any time when the program is running.
• Register banks not selected can be used as general purpose RAM.
Bank 0 is selected on reset.
• A bit-addressable area of 16 bytes occupies RAM byte addresses 20H
to 2FH, forming a total of 128 addressable bits.
• An addressable bit may be specified by its bit address of 00H to 7FH,
or 8-bits may form a byte address from 20H to 2FH. Thus, for
example, bit address 4FH is also bit 7 of byte address 29H.
• Addressable bits are useful when the program need only to
remember a binary event (switch ON, light OFF etc.).
• A general purpose RAM and above bit area, from 30H to 7FH is
addressable as bytes.
1.9.2 Internal ROM
• The 8051 is organized so that data memory and program code
memory can be in two entirely different physical memory entities.
Each has the same address ranges.
• A corresponding block of internal program code, contained in an
internal ROM, occupies code address space 0000H to 0FFFH.
• The PC is ordinarily used to address program code bytes from
address 0000H to FFFFH. Program address higher than 0FFFH, which
exceed the internal ROM capacity will cause the 8051 to
automatically fetch code bytes from external program memory.
• Code bytes can also be fetched exclusively from an external memory,
address 0000H to FFFFH, by connecting the external access pin (EA
pin 31) to ground.
1.10 External Memory
• There are several reasons for adding an external memory with 8051.
When the project is in the prototype stage and internal ROM
memory is not enough, external memory is needed.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 1-22 Basics of Microcontroller ….

• External memory, which is accessed by the DPTR, may also be


needed when 128 bytes of internal data storage is not sufficient.
Fig. 1.14 shows the connections between 8051 and external memory.

• EA pin is connected to VCC to indicate that the program code is

stored in the on-chip ROM. If it is in off-chip (external ROM) then EA
is connected to GND.
• As PC of 8051 is 16-bit, it is capable of addressing 64 k bytes external
memory.
• P0 and P2 provide the 16-bit address to access external memory.
• P0 provides lower 8-bit address A0 - A7 and P2 provides higher 8-bit
address A8 - A15.
• P0 is also used to provide the 8-bit data bus D0 - D7.
• P0 port is multiplexed port and ALE signal of microcontroller is used
to demultiplex the port when ALE = 1. It is used for the address path.
• As a result to extract the address from P0 pins, P0 is connected to
74LS373 latch and ALE to enable the latch.
 
• PSEN : Another important signal for 8051 is PSEN (programme store
enable) PSEN which is connected to the OE pin of ROM containing
program code. It is important to emphasize on role of PSEN and EA
of 8051 when EA is connected to GND, the 8051 fetches opcode
from external ROM using PSEN signal. (Fig. 1.14 shows interfacing 8 k
ROM with 8051.)
(RD)
EA P3.7 Vcc
(WR)
P3.6
PSEN CE OE Vpp
P2.7 A12
A12 2864
(2764)
8051 P2.0 A8
ALE G A8
8k ´ 8
AD7 A7 program
P0.7 D Q A7
ROM
74LS373 A0 D7 D0
P0.0
AD0 OC A0

D7

D0

Fig. 1.14 : Interfacing of 8051 and external memory


S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 1-23 Basics of Microcontroller ….

• For 8 k bytes memory, 13 address lines are required, out of which


lower 8 lines are taken from P0 by demultiplexing it and higher 5-bits
are taken from port 2.
  
• PSEN is connected to OE and CE of memory to select it and as well to

read data from memory. Observe that EA is grounded, specifying it
as external memory.
1.10.1 8051 Data Memory Space
• Program counter in 8051 is 16-bit and therefore it can access upto
64 k bytes of program code. But in some examples data is placed in
code space and used the instruction “MOVCA, @ A+PPTR” to get the
data.
• In 8051 family, there is also separate data memory space. While
program space is accessed by PC, the data space is accessed by the
DPTR register and instruction called MOVX is used for that. MOVXA,
@ DPTR is widely used instruction to read external data memory.
1.10.2 Interfacing External ROM to 8051
• In Fig. 1.15, it is shown to interface 8 k bytes external data memory
(ROM) with 8051.
RD Vcc
EA P3.7
A15
P3.6 WR
PSEN A14 OE Vpp
CE
P2.7 A13
A12 A12
8051 P2.0
G A8 8k ´ 8
ALE Data
AD7 A7
P0.7 D Q ROM
74LS373 A0 D7 D0
P0.0
AD0 OC A0

D7

D0
Fig. 1.15 : Interfacing with data ROM
 
• In this case, RD is connected to OE of memory chip. While chip select
 
(CE) is connected to simple address decoding logic. PSEN is not used

as it is a data memory. Again EA is GND to show that it is external
memory.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 1-24 Basics of Microcontroller ….

• For 8 k bytes memory, 13 address lines are required, out of which


lower 8 lines are taken from P0 by demultiplexing it and higher 5-bits
are taken from port 2.
1.10.3 Interfacing External RAM to 8051
• MOVX A, @ DPTR is the instruction used to read external RAM. This
has many applications where collecting large number of bytes data is
required.
• Fig. 1.16 shows the interfacing external data RAM with 8051. Note
   
that RD and WR is connected to OE and WE of RAM. While chip

select (CE) is connected to simple address decoding logic.
8051
P3.7 (RD)
EA
P3.6 (WR) A15
PSEN A14 CE WE OE
P2.7 A13 A13
A12
A12
P2.0
G A8 8k ´ 8
ALE Data
AD7 A7
P0.7 D Q RAM
74LS373 A0 D7 D0
P0.0
AD0 OC A0

D7

D0

Fig. 1.16 : Interfacing with external data RAM


• Fig. 1.17 shows timing associated with an external memory access
cycle.
• During any memory access cycle, port 0 is time multiplexed. That is, it
first provides the lower byte of the 16-bit memory address and then
acts as a bidirectional data bus to write or read a byte of memory
data.
• Port 2 provides the high byte of the memory address during the
entire memory read/write cycle.
• The lower address byte from port 0 must be latched into an external
register to save the byte. Address byte save is accomplished by the
ALE clock pulse that provides the correct timing for 74373 to latch.
The port 0 pins then become free to serve as a data bus.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 1-25 Basics of Microcontroller ….

• If the memory access is for a byte of program code in the ROM, the

PSEN (Program Store Enable) pin will go low to enable the ROM to
place a byte of program code on the data bus. If the access is for a
 
RAM byte, the WR (write) or RD (read) pins will go low, enabling data
to flow between the RAM and data bus.

Port 0 A0-A7 D0-D7

Port 2 A8-A15

ALE Latch address


ALE pulse
External Memory Addressing

PSEN pulse
PSEN
Enable ROM
Reading ROM Using PSEN

Read pulse
RD
Enable read
Write pulse
WR
Enable write
Accessing RAM Using RD or WR

Fig. 1.17 : External memory timing


1.11 Oscillator and Clock
• The 8051 has on-chip oscillator but requires an external clock to run
it. It is a heart of the 8051 which generates the clock pulses by which
internal operations are synchronized. Most often a quartz crystal
oscillator is connected to inputs XTAL1 (pin 19) and XTAL2 (pin 18).
• The quartz crystal oscillator connected to XTAL1 and XTAL2 also
need two capacitors of 30 pF value. One side of capacitor is
connected to ground as shown in Fig. 1.18.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 1-26 Basics of Microcontroller ….

C1 = 30 PF
18 XTAL2

Crystal
or 8051
Ceramic resonator

19 XTAL1
C2 = 30 PF

Fig. 1.18 : XTAL connection to 8051


• The clock frequency f, establishes the smallest interval of time within
the microcontroller called the pulse P time. The smallest interval of
time to accomplish any simple instruction or part of a complex
instruction is a machine cycle. The machine cycle is itself made up of
six states.
• A state is a basic time interval for discrete operations of the
microcontroller such as fetching an opcode byte, decoding an
opcode, executing an opcode or writing a data byte. The oscillator
pulses define each state as shown in Fig. 1.19.
P2 P1 P2 P1 P2 P1 P2 P1 P2 P1 P2 P1 P2
Oscillator
frequency
(f)
State 1 State 2 State 3 State 4 State 5 State 6
One machine cycle

Address
Latch
Enable 8051 Timing
(ALE)
Fig. 1.19 : Oscillator timing
• Program instructions may require one, two or four machine cycles to
be executed, depending on the type of instruction.
• Instructions are fetched and executed by the microcontroller
automatically, beginning with the instruction located at ROM
memory address 0000H at the time, the microcontroller is first reset.
• Any particular instruction will take time to execute. The time to
execute that instruction is then found by multiplying C by 12 and
dividing the product by the crystal frequency.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 1-27 Basics of Microcontroller ….

C × 12
Tinst =
Crystal frequency
• For example, if the crystal frequency is 11.0592 MHz, then the time to
execute an ADD A1 R1 one byte instruction will take 1.085 µs.
• In above Fig. 1.19, 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.

Think Over It

• What will happen if pull-up resistances are not connected to


ports?
• How will you expand memory of 8051 microcontroller?

Points to Remember
• Microprocessor consists of Memory and I/O components which are
externally connected to it.
• Microcontroller consists of inbuilt internal memory and I/O ports.
• 8051 is 8-bit microcontroller. It has 128 bytes of RAM, 4 k bytes of
on-chip ROM, two timers and four 8-bit ports all on a single chip.
• The 8051 is an 8-bit processor, that means CPU can work on only
8-bits of data at a time.
• 8051 has 64 k bytes external ROM – Program memory, 64 k bytes
external RAM – Data memory, and Five – Interrupt sources.
• The 8051 has a flag registers to indicate arithmetic condition. These
are 1-bit registers provided to store the results of certain program
instructions.
• Stack referred to an internal RAM used to store and retrieve data
quickly.
• 128 bytes are reserved for SFRs. From address 80H to FFH all SFRs
are addressed.
• 8051 has four ports P0, P1, P2 and P3, each of 8-bit ports. All the
ports upon RESET are configured as inputs and are ready to use as
input ports.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 1-28 Basics of Microcontroller ….

• Port 0 is different than other ports as it does not have the pull-up
registers, while all other ports have internal pull-up registers.
−
• EA pin is connected to VCC to indicate that the program code is
−
stored in the on-chip ROM. If it is in off-chip (external ROM) then EA
is connected to GND.
• The 8051 has on-chip oscillator but requires an external clock to run
it.
• Most often a quartz crystal oscillator is connected to inputs XTAL1
(pin 19) and XTAL2 (pin 18).
Exercises
[A] True or False :
1. 8-bit microcontrollers are the most widely used microcontroller the
world over.
2. A microcontroller has on-chip I/O ports.
3. Microcontrollers are normally less expensive than microprocessors.
4. A microcontroller has a variable size of RAM on the chip.
5. The 8051 is manufactured by more than one manufacturer.
6. The 8051 has 16-bit address bus.
7. The 8051 has 128 k bytes of on-chip RAM.
8. The 8051 family has 32 pins for I/O.
9. The 8051 has 4 number of I/O ports.
10. The 8051 has 8-bit ALU.
[B] Fill in the Blanks :
1. Microcontroller consists of inbuilt …… and …….
2. 8051 is …… microcontroller.
3. …… bytes are reserved for SFRs.
4. ……does not have the pull-up registers.
5. 8051 has …… bytes of RAM.
6. 8051 has …… bytes of on-chip ROM.
[C] Multiple Choice Questions :
1. …… has in-built internal memory and I/O ports.
(a) Microprocessor (b) Microcontroller
(c) CPU (d) None of these
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 1-29 Basics of Microcontroller ….

2. 8051 microcontroller has …… on-chip ROM.


(a) 128 k bytes (b) 64 k bytes
(c) 256 k bytes (d) 1 MB
3. …… port of microcontroller does not have internal pull-up registers.
(a) P0 (b) P1
(c) P2 (d) P3
4. 8051 microcontroller has …… internal memory banks which consists
of 8 registers.
(a) 1 (b) 2
(c) 3 (d) 4
5. All ports of 8051 upon RESET are configured as …… ports.
(a) Input (b) Output
(c) Input/Output (d) None of these

6. In case of off-chip ROM connected to 8051, EA pin has to be
connected to ……
(a) VCC (b) Ground
(c) −VCC (d) None of these
7. …… register is referred as flag register.
(a) R1 (b) PC
(c) R7 (d) PSW
8. Program counter (PC) is a …… bit register.
(a) 8-bit (b) 16-bit
(c) 32-bit (d) none of these
9. …… port is used for dual function (It can be used as AD0 - AD7 also).
(a) P0 (b) P1
(c) P2 (d) P3
10. ALE (Address Latch Enable) is an …… pin.
(a) Output (b) Input
(c) Input/Output (d) None of these
[D] Short Answer Questions :
1. What is microcontroller ?
2. What do you mean by an embedded system ?
3. State salient features of microcontroller.
4. List the different types of register in 8051.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 1-30 Basics of Microcontroller ….

5. List the various types of SFR in a microcontroller.


6. What is difference between general purpose and SFR registers ?
7. What is the internal size of ROM in 8051 ?
8. What is the address of the stack when the 8051 is reset ?
9. How many register banks are in 8051 ? Write their addresses.
10. Which of the port is used for external memory access.
11. Which of the port in 8051 has no alternate functions ?

12. What is significance of EA pin of 8051 ?
13. State the significance of ALE pin of 8051.
14. What is crystal oscillator?
15. How many ports are available on 8051 ? Which of them have no pull
up registers internally ?
 
16. What is the role of WR and RD pin ?
17. Write the address ranges of the following :
(a) Register bank 0
(b) Register bank 1
(c) Register bank 2
(d) Register bank 3.
18. Which of the memory locations of internal RAM are available for bit
addressable ?
19. Write in short about internal ROM of 8051.
20. What is computer ?
21. What are alternate functions of port 0 and port 2 ?
22. Which are the mathematical registers in 8051 ?
23. What is the default value of SP after 8051 is reset ?
24. Draw bit format of PSW register and explain the function AC and OV
bit.

25. What is the function PSEN pin when we access external memory ?
[E] Long Answer Questions :
1. Differentiate between microprocessor and microcontroller.
2. Draw the block diagram of 8051 microcontroller and explain it.
3. What are SFRs ? Are SFR bits addressable ? How do you address
them ?
4. What is PSW ? Explain with suitable examples the functions of each
bit.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 1-31 Basics of Microcontroller ….

5. What are different ports available in 8051 microcontroller ? Explain


port 0 in detail.
6. Explain how external memory is interfaced with 8051 microcontroller.
7. Write detail notes on :
(a) Data pointer, (b) Stack pointer, (c) Program counter.
8. Explain the division of 128 bytes of internal RAM with proper
structure.
9. What is role of crystal oscillator ? Explain oscillator timing with timing
diagram.

Answers
[A] True or False :
(1) True (2) True (3) True
(4) False (5) False (6) True
(7) False (8) True (9) True
(10) True
[B] Fill in the blanks :
(1) Inbuilt internal memory and I/O ports, (2) 8-bit, (3) 128,
(4) Port 0, (5) 128, (6) 4K
[C] Multiple Choice Questions :
1. (b) 2. (a) 3. (a) 4. (d) 5. (a) 6. (b) 7. (d)
8. (b) 9. (a) 10. (a)

University Questions
April 2015
1. What are alternate functions of port 0 and port 2 ? (2M)
2. Which are the mathematical registers in 8051 ? (2M)
3. What is the default value of SP after 8051 is reset ? (2M)
4. Draw the internal structures of port pin and explain how read and
writes operation is carried out. (5M)
5. Draw bit format of PSW register and explain the function AC and OV
bit. (2M)
6. Explain with neat diagram how a 4k × 8 program ROM can be
connected to 8051. (5M)
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 1-32 Basics of Microcontroller ….

April 2016
1. What is the size of address and data bus of 8051? (1M)
2. What is the function of ALE pin? (1M)
3. Which port of 8051 requires external pull-up registers? (1M)
4. What is the dual role of port 0? (1M)
5. Draw simplified block diagram of 8051 microcontroller and explain
the function of any three components of it. (5M)
6. Differentiate between microprocessor and microcontroller. (5M)
7. Explain the function of Program Counter and Data pointer. (2M)

April 2017
1. What if the value of Program Counter after Power On reset? (1M)
2. What is the size of on-chip RAM of 8051? (1M)
3. Differentiate between microprocessor and microcontroller. (5M)
4. Explain RAM space allocation in 8051. (5M)
April 2018

1. Write the function of pin PSEN. (1M)
2. Which port of 8051 requires external pull-up registers? (1M)
3. What is the value of stack pointer after power on reset in 8051? (1M)
4. Draw block diagram of 8051 microcontroller architecture and explain
any three blocks of it. (5M)
5. Write any five differences between microprocessor and
microcontroller. (5M)

April 2019
1. What is meant by a microcontroller? (1M)
2. What is the capacity of internal ROM of 8051? (1M)
3. Explain the internal RAM organization of 8051 microcontroller. (5M)
4. Explain the function of following pins of 8051 microcontroller (5M)
(i) RESET
(ii) ALE

(iii) PSEN

(iv) EA
(v) XTAL 1 and XTAL 2
✍✍✍
Unit 2…
Programming Model of 8051
John Harrison Wharton was an American engineer
specializing in microprocessors and their
applications. Wharton designed the Intel MCS-51,
one of the most implemented instruction set
architectures of all time. J. H. Wharton was the
architect of the instruction set of the Intel MCS-51
commonly known as the 8051. John Wharton
graduated from Northwestern University with a
Bachelor's degree in electrical engineering in 1976 and a Master's degree
in computer science in 1977. He was hired by Intel at the instigation of
Tom Rolander, working there for 5 years before leaving to start his
consulting company, Applications Research. He was a founding member
of the editorial board of Microprocessor Report.

Introduction
• The MCS-51 family has extremely versatile I/O port structure. The
8051 has 32 I/O pins configured as four 8-bit parallel ports (P0, P1,
P2, P3).
• All four ports are bidirectional, i.e. each pin will be configured as
input and output or I/O under software control.
• Each port consists of a latch, an output driver and an input buffer.
• In the previous chapter, the details of the ports available with 8051
are discussed. These programmable port pins have completely
different alternative functions.
• The configuration of control circuitry between the output latch and
port pin determines the nature of any particular pin function.
• Port 1 cannot have alternate functions while port 0, port 2 and port 3
can be programmed for various functions.
• In this chapter, the instruction set of 8051 is described in detail along
with its classification. Also the assembly and ‘C’ programming for
8051 microcontroller are elaborated.

(2.1)
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 2-2 Programming Model of 8051

2.1 8051 Instruction Classification and


Instruction Set
• The instruction set of MCS-51 is classified into the following types :
1. Data transfer instructions.
2. Arithmetic instructions.
3. Logical instructions.
4. Boolean variable manipulation instructions.
5. Branching instructions.
• Again it is also possible to group them according to byte size. There
are some single byte, two byte and three byte instructions.
1. Data Transfer Instruction :
• The four distinct memory types of 8051 are :
(i) Internal RAM (ii) Internal special function register
(iii) External RAM (iv) Internal and external ROM.
• Following instructions are used for data transfer :
(1) MOV
(2) MOVX
(3) MOVC
(4) PUSH and POP
(5) XCH
• In case of data transfer instruction, data is moved from source to
destination without affecting the source contents.
(a) MOV A, #n − This instruction will copy the immediate data byte
to accumulator, e.g. MOV A, #55H.
(b) MOV A, Rn − Move register contents to accumulator e.g. MOV A,
R0
Before execution R0 = 55HA = 70H
After execution R0 = 55HA = 55H
(c) MOV A, direct address
Move the contents of direct address to accumulate
MOV A, 54H : Before execution (54H) ⇒ 60HA = 70H
After execution (54H) ⇒ 60HA = 60H
Other possible combinations under the same group.
MOV Rn, A : Move accumulator to register.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 2-3 Programming Model of 8051

Table 2.1
Description Byte Oscillator
Period
MOV A, @ Ri Move indirect RAM to 1 12
accumulator
MOV Rn, A Move accumulator to 1 12
register
MOV Rn, direct Move data from direct 2 12
address to register
MOV direct, A Move accumulator to direct 2 12
byte
MOV direct, Rn Move register to direct byte 2 24
MOV direct, direct Move direct byte to direct 3 24
MOV direct, @ Ri Move indirect RAM to direct 2 24
byte
MOV direct, #data Move immediate data to 3 24
direct byte
MOV @ Ri, A Move accumulator to 1 12
indirect RAM
MOV @ Ri, direct Move direct byte to indirect 2 24
RAM
MOV @ Ri, #data Move immediate data to 2 12
indirect RAM
MOV DPTR, #data Load data pointer with a 3 24
(16) 16-bit constant
MOVC A, @ A + DPTR : This instruction is used for code memory
read only data move.
When access to programmed mass of data is needed, tables of
predefined bytes (look-up table) are used.
• This type of data must be permanent and is stored in program
ROM. Access to this data is made possible by using indirect
addressing.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 2-4 Programming Model of 8051

• In this instruction, the number in register A is added to DPTR or


PC and from resulting address, data is fetched and that data byte
is moved to the accumulator.
Table 2.2
Description Byte Oscillator
Period
MOV A, @ A + Copy the code byte, found at 1 24
DPTR the ROM address formed by
adding A and DPTR to A
MOVC A, @ A Move code byte relative to 1 24
+ PC PC to A.
Examples :
(a) MOVX A, @ DPTR : In this instruction, the external RAM is
accessed using DPTR as a pointer. The data stored at external
RAM location pointed by DPTR is moved into A.
(b) MOVX @ Ri, A : Move the accumulator contents to external
RAM address location pointed by RI (8-bit address).
Table 2.3
Description Byte Oscillator
Period
MOVX A, @ Move external RAM (10- 1 24
DPTR bit address) to A
MOVX @ DPTR, Move accumulator to 1 24
A external RAM (10-bit
address)
MOVX A, @ Ri Move external RAM (8-bit) 1 24
to A
MOVX @ Ri, A Move A to external RAM 1 24
(8-bit)
PUSH direct Push direct byte onto 2 24
stack
POP direct Pop direct byte from stack 2 24
XCH A, Rn Exchange register with 1 12
accumulator
… (Contd.)
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 2-5 Programming Model of 8051

Description Byte Oscillator


Period
XCH A, direct Exchange direct byte with 2 12
A
XCH A, @ Ri Exchange indirect RAM 1 12
with A
XCHD A, @ Ri Exchange the low order 1 12
digit indirect RAM with A
2. Arithmetic Instructions :
• Microcontroller applications often involve applying mathematical
calculations on data.
• MCS-51 supports 8-bit arithmetic unsigned operations. It is also
possible to carry both signed and unsigned addition and
subtraction by using the OV flag.
• It performs unsigned multiplication and division operations also.
• Arithmetic operations are grouped into the following types :
(a) INC destination
(b) DEC destination
(c) ADD/ADDC destination, source
(d) SUBB destination, source
(e) MUL AB
(f) DIV AB
(g) DA A
• In case of arithmetic operations the C, AC and OV flags get
affected.
Various Arithmetic Instructions :
[I] Incrementing and Decrementing:
• These operations become very useful when coupled with the
ability to repeat the operation that is to increment or decrement
until desired result is reached. No flags are affected.
INC A : Adds 1 to accumulator.
Before execution, A = 05H, and After execution A = 06H
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 2-6 Programming Model of 8051

DEC A : Subtract 1 from accumulator.


Before execution A = 06H and After execution A = 05H.
Other mnemonics are listed in the following Table 2.4.
Table 2.4

Mnemonic Description Byte Oscillator

INC A Increment accumulator 1 12

INC Rn Increment register 1 12

INC direct Increment direct byte 2 12

INC @ Ri Increment indirect RAM 1 12

DEC A Decrement accumulator 1 12

DEC Rn Decrement register 1 12

DEC direct Decrement direct byte 2 12

DEC @ Ri Decrement indirect RAM 1 12

INC DPTR Increment data pointer 1 24

[II] Addition and Subtraction :


(i) Addition : All additions are done with a register as the
destination of the result. C, AC, OV flags are affected after the
exhibition of addition instruction.
Unsigned Addition : Unsigned numbers makes use of all 8-bits to
represent the magnitude of a number. It also uses CY flag to detect
the result of an ADD operation. CY will be set if the result is greater
than FFH. If carry is set to 1 after addition then program could add
nd
the carry flag to higher order byte to form a 2 byte of a number.
e.g. 95d 01011111b
+ 189d 10111101b

284d 1 00011100b
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 2-7 Programming Model of 8051

Signed Addition : Signed numbers makes use of lower 7-bits to


represent magnitude and 1-bit as signed bit. The possible range of
signed numbers is from − 128d to + 127b.
For example,
1. − 001d 11111111b
+ 027d 00011011b

+ 026d 1 00011010 = + 26d

Here carry flag is 1. There is also carry from bit 6, so OV flag = 0. No


action is taken to correct the sum.
2. − 030d 11100010b
− 050d 11001110b

− 080d 1 1 0 1 1 0 0 0 0 b = − 80 d

(ii) Subtraction : Subtraction can be done by taking the two’s


complement of the number to be subtracted, the subtrahend and
adding it to another number, the minuend. The instruction treats the
CY flag as borrow and always subtract the carry flag as part of the
operation.
Unsigned Subtraction :
015d 00001111b
100d 01100100b

− 085d 1 10101011b

The CY flag is set + 01 and OV flag is 0. The 2’s complement of the


result is 085d.
Signed Subtraction :
+ 100d 01100100b
+ 126d 01111110b

− 026d 1 1 1 1 0 0 1 1 0 b = − 026 d
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 2-8 Programming Model of 8051

Table 2.5 : List of the mnemonics for addition and


subtraction operations
Mnemonics Description Bytes Oscillator
Period
ADD A, Rn Add register to A 1 12
ADD A, direct Add direct byte to A 2 12
ADD A, @ Ri Add indirect RAM to A 1 12
ADD A, #data Add immediate data to A 2 12
ADDC A, Rn Add register to A with carry 1 12
ADDC A, direct Add direct byte to A with carry 2 12
ADDC D, @ Ri Add indirect RAM to A with 1 12
carry
ADDC A, Add immediate data to A with 2 12
# data carry
SUBB A, Rn Subtract register from A with 1 12
borrow
SUBB A, @ Ri Subtract indirect byte from A 1 12
with borrow
SUBB A, direct Subtract direct byte from A 2 12
with borrow
SUBB A #data Subtract immediate data from 2 12
A with borrow
[III] Multiplication and Division :
• The 8051 has the capability to perform 8-bit integer multiplication
and division using A and B register.
• Register B is solely used for these operations.
• Multiplication and division treats the numbers in A and B as unsigned
numbers.
(i) Multiplication : Multiplication uses register A and B as both
source and destination addresses for operation. The OV flag will get
set it A * B > FF. It does not mean error has occurred, it signals that
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 2-9 Programming Model of 8051

the programmer needs to observe the higher byte also. The carry
flags always cleared to 0.
MUL AB: Multiply A by B : Puts the lower byte of product into A,
higher byte in B. 1 byte instruction and 48 oscillators are required.
(ii) Division : The unsigned number in A is divided by number in B.
The OV flag is cleared to 0 unless B holds 00H before DIV. Then OV
flag is set to 1 to show division, e.g. 0. The carry flag is always reset.
213d
= 12 quotient and 9 remainder
017d
Here A = 005H = 12 = Quotient
B = 001H = 9 = Remainder
Quotient is stored into register A while remainder is stored into
register B.
DIV AB : Divide by B : Puts the quotient in register A and remainder
in B, 1 byte and 12 oscillator periods required.
DAA : This instruction adjusts the result of BCD addition in the
decimal, adjust A for addition.
DA A : Adjust the sum of two packed BCD numbers found in A
register, leave the adjusted number in A, 1 byte is required.
This instruction only works when used with ADD or ADDC opcodes.
SOLVED EXAMPLES

Example 2.1 : Add the numbers stored at location 25H, 26H and 27H
and put the result in RAM locations 31H (MSB) and 30H (LSB).
Solution : The largest number possible is FFH + FFH = 01FEH +
FFH = 02 FDH, so that 2 bytes will hold the largest possible number. The
MSB will be set to 0 and carry bit added to it for each byte addition. Use
an ADD instruction for each addition and ADDC to the MSB for each
carry that might be generated.
Program :
MOV 31H, #00H ; Clear MSB of result to 0
MOV A, 25H ; Get the first byte in A from 25H
ADD A, 26H ; ADD A with byte from 26H address
MOV R0, A ; Save sum of first two bytes in R0
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 2-10 Programming Model of 8051

MOV A, #00 ; Clear A to 00


ADDC A, 31H ; Add carry to the MSB
MOV 31H, A ; Store MSB
MOV A, R0 ; Get partial sum back into A
ADD A, 27H ; Add final number
MOV 30H, A ; Store LSB
MOV A, #00H ; Clear A for MSB addition
ADDC A, 31H ; Form final MSB
MOV 31H, A ; Store final MSB
Example 2.2 : Multiply the unsigned number in register R3 by
number on port 2 and put the result in external RAM location 10H (MSB)
and 11H (LSB).
Solution :
MOV A, P2 ; Move the port 2 data to A
MOV B, R3 ; Move the R3 register to B register
MUL AB ; Multiply A and B
MOV R0, #11H ; Set R0 to point to external RAM
location 11H
MOV @ R0, A ; Store LSB in RAM
DEC R0 ; Decrement R0 to point to 10H
MOV A, B ; Move B to A
MOV @ R0, A ; Store MSB in RAM
Example 2.3 : Write assembly language program to convert Hex
number to its decimal equivalent.
Solution : MOV A, #98H
MOV B, #0AH
DIV AB
MOV R0, B
MOV B, #0AH
DIV AB
MOV R1, B
MOV R2, A
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 2-11 Programming Model of 8051

3. Logical Instruction :
• In 8051, bitwise logical operations like AND, OR and XOR are
possible. These instructions requires two 8-bit operands.
• No flags are affected by AND, OR and XOR instructions.
• In logical instructions again there are two types (1) byte level logical
instructions and (2) bit level logical instructions.
• Certain internal RAM and SFRs can be addressed by their byte
address or bit address. Bit addressing is very convenient in case of
altering single bit.
Table 2.6 : Internal RAM bit address

Byte Address Check Bit Address Check

20 00 − 07
21 08 − 0F
22 10 − 17
23 18 − 1F
24 20 − 27
25 28 − 2F
26 30 − 37
27 38 − 3F
28 40 − 47
29 48 − 4F
2A 50 − 57
2B 58 − 5F
2C 60 − 67
2D 68 − 6F
2E 70 − 77
2F 78 − 7F

• The bit addressable instructions such as CLR, SETB, CPL; rotate


instructions RR, RRC, RL, RLC and swap instruction SWAP are
available.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 2-12 Programming Model of 8051

• CPL instruction complements the operand and rotate instruction


rotates the operand by 1 bit.
• SWAP instruction interchanges the lower and higher nibbles of
the accumulator.
• ANL A, Rn will AND the data stored in accumulator with the data
stored in register Rn.
For example, Before execution A = 0FFH R0 = 77H ANL A, RP;
After execution A = 0FFH R0 = 77H
• Same way ORL and XRL will perform the logical OR and XOR
operation respectively.
Table 2.7

Mnemonic Description Byte Oscillator


Period

ANL A, Rn AND register to accumulator 1 12

ANL A, direct AND direct byte to 2 12


accumulator

ANL A, @ Ri AND indirect RAM to 1 12


accumulator

ANL A, #data AND immediate data to 2 12


accumulator

ANL direct, A AND accumulator to direct 2 12


byte

ANL direct, AND immediate data to direct 3 24


#data byte

ORL A, Rn OR register to accumulator 1 12

ORL A, direct OR direct byte to accumulator 2 12

ORL A, @ Ri OR indirect RAM to 1 12


accumulator
… (Contd.)
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 2-13 Programming Model of 8051

Mnemonic Description Byte Oscillator


Period

ORL A, #data OR immediate data to 2 12


accumulator

ORL direct, A OR accumulator to direct byte 2 12

ORL direct, OR immediate data to direct 3 24


#data byte

XRL A, Rn Exclusive – OR register to 1 12


accumulator

XRL A, direct Exclusion OR direct byte to 2 12


accumulator

XRL A, @ Ri Exclusive OR indirect RAM to 1 12


accumulator

XRL A, #data Exclusive OR immediate data to 2 12


accumulator

XRL direct, A Exclusive OR accumulator to 2 12


direct byte

XRL direct, Exclusive OR immediate data to 3 24


#data direct byte

CLR A Clear accumulator 1 12

CPL A Complement accumulator 1 12

Rotate Instructions :
• The ability to rotate the data is useful for inspecting bits of a byte
without using individual opcodes. The A register can be rotated left
or right with or without including CY flag by one bit.
• SWAP instruction can be thought of as a rotation of nibbles in the
register.
• Fig. 2.1 shows the rotate and SWAP operations.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 2-14 Programming Model of 8051

7 6 5 4 3 2 1 0

RL A

C 7 6 5 4 3 2 1 0

Carry flag RLC A

7 6 5 4 3 2 1 0

RR A

7 6 5 4 3 2 1 0 C

RRC A Carry flag

7 6 5 4 3 2 1 0
High Nibble Low Nibble

SWAP A
Fig. 2.1 : Register A rotate operations
Table 2.8 : Mnemonics of rotate instruction
Mnemonic Description Byte Oscillator
Period
RL A Rotate accumulator left 1 12
RLC A Rotate accumulator left 1 12
through carry
RR A Rotate accumulator right 1 12
RRC A Rotate accumulator right 1 12
through carry
SWAP A Swap nibble within the 1 12
accumulator
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 2-15 Programming Model of 8051

SOLVED EXAMPLE

Example 2.4 : Write an ALP in 8051 to OR the contents of ports 1


and 2, put the result in external RAM location 0100H.
Solution : Assume that parts are configured as input parts.
MOV A, 90H ; Copy the data ROM part 1 to A

ORL A, 0A0H ; OR the contents of A with part 2

MOV DPTR, #0100H ; Initialize DPTR to 0100H address

MOVX @ DPTR, A ; Store the result

4. Boolean Variable Manipulation Instructions :


• Operands for Boolean variable manipulation are defined in these
instructions. Also OR, AND operations are possible with bit operands.
There are instructions that test a specified bit and transfer control to
the desired location.
• For example, JB P1.0 instruction will test P1.0 bit and will jump the
relative address given in the instruction.
• In case of JC, JNC, JB, JNB instructions, the testing bit is not modified
and no flags are affected. But in case of JBC instruction it transfers
the control to the given relative address and also clears the testing
bit.
5. Branching Instructions :
• In 8051 both conditional and unconditional branch operations are
possible.
• A JMP or CALL instruction replaces the contents of the program
counter with a new program address which causes program
execution to begin from new address.
• JMP or CALL instructions may have any of the three ranges : a
relative range of +127 to 128 bytes from instruction following the
JMP or CALL instruction, an absolute range on the same 2 k byte
page as the instruction following the JMP or CALL; or a long range of
any address from 0000H to FFFFH (64 k) anywhere in program
memory.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 2-16 Programming Model of 8051

• Fig. 2.2 shows the relative range of all JMP instructions.


Memory address (HEX)

FFFF
LADD limit

Next page

SADD limit

PC + 127d Relative limit


JC
JNC Bit
JB jumps
JNB
JBC
AJMP
PC Next opcode LJMP
CJNE
Jump opcode
DJNZ
JZ Byte
JNZ jumps

PC - 128d Relative limit SJMP

SADD limit
This page

LADD limit
0000
Fig. 2.2 : Jump instruction ranges
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 2-17 Programming Model of 8051

[I] Conditional Jump :


• In conditional JMP instruction again there are two types :
(i) Bit JMP : In this instruction bit JMP all operate according to the
status of the carry flag or the status of any bit addressable
location.
Table 2.9
Mnemonic Description Byte Oscillator
Period
JC rel add Jump to relative address if carry 2 24
flag is set.
JNC rel add Jump to relative address if carry 2 24
flag is cleared.
JB b, rel add Jump to relative address if 3 24
addressable bit is set.
JNB b, rel add Jump to relative address if 3 24
addressable bit is cleared.
JBC b, rel add Jump to relative address if 3 24
addressable bit is set and clear
the addressable bit to 0.
The following program example makes use of bit groups :
Table 2.10

Address Mnemonic Comment

LOOP : MOV A, #10h; ; A = 10h

MOV R0, A; ; R0 = 10h

ADDA : ADD A, R0 ; add R0 to A

JNC ADDA; ; if the carry flag is clear then

; ; JMP to ADDA; Jump until A

; ; FOh; the C flag is set to 1

; ; On the next ADD and no carry


… (Contd.)
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 2-18 Programming Model of 8051

Address Mnemonic Comment


; ; Is false; do the next instruction
MOV A, #10n ; A = 10h, do program again using JNB
ADDR : ADD A, R0 ; add R0 to A
JNB 0D7h, ADDR; ; D7 is address of carry flag.
JBC 0D7h, Loop; ; the carry bit is 1; then jump to
; ; loop is taken and carry flag
; ; is cleared to 0
(ii) Byte Jump : In this case, the byte data is tested and jump is
taken; according to byte condition. All byte jumps are relative to
the program counter.
The different byte jumps are discussed below :
1. CJNE source, data, radd : This instruction means compare and
jump if not equal.
The contents of destination register compared with the content
of source; if they are not equal then it jump to the relative
address. CY flag changes accordingly as shown in the following
table.
Table 2.11
Compare Carry flag
destination > source CY = 0
destination < source CY = 0

SOLVED EXAMPLE
Example 2.5 : Write a code to determine if A contains the value 99H.
If so, make Rl = FFh otherwise make Rl = 0
Solution :
MOV Rl, #0H ; clear Rl
CJNE A, #99H, next ; if not equal 99, then jump
MOV Rl, #0FFH ; else if equal make Rl = OFFH.
Another byte jump instruction is
2. DJNZ source, data, radd : Decrement the source operand by 1
and JMP to the relative address if result is not zero. No flags are
affected. This instruction is very useful in case of looping
operation.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 2-19 Programming Model of 8051

The following two instructions are also byte JMPs.


3. JZ, radd : Jump to relative address if accumulator is zero.
4. JNZ, radd : Jump to relative address if accumulator is not zero.
To summarize jump instructions with required number of bytes, a
table is given below.
Table 2.12
Mnemonic Description Byte Oscillator
Period
CJNE A, direct Compare direct byte to 3 24
rel accumulator and jump if not
equal.
CJNE A, #data, Compare immediate to 3 24
rel accumulator and jump if not
equal.
CJNE Rn, #data, Compare immediate to 3 24
rel register and jump if not equal.
CJNE @ Ri, Compare immediate to 3 24
#data rel indirect and jump if not equal.
DJNZ, Rn, rel Decrement register and jump 3 24
if not equal
DJNZ, direct, rel Decrement direct byte and 3 24
jump if not zero.
JZ, rel Jump if accumulator is zero 2 24
JNZ, rel Jump if accumulator is not 2 24
zero
[II] Unconditional JMPs :
• Unconditional Jumps do not test any bit or byte : the jump is always
taken. All jump ranges are found in this group.
(i) JMP@A + DPTR : JMP is taken to the address formed by adding
A register to DPTR. No flags are affected.
(ii) AJMP radd : Absolute JMP allows 11-bit address to be specified
in the instruction. The relative addresses must be within a range
of 2 k block of program memory from the next instruction
followed by AJMP.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 2-20 Programming Model of 8051

(iii) LJMP radd : Long jump allows 16-bit address to be specified in


the instruction and a JMP to anywhere within 64 k block of
program memory is possible.
(iv) SJMP radd : Short jump to the relative address, this is again
unconditional JMP and is always taken. For short JMP, the
possible range is between − 127 to + 128 bytes.
• Similar to AJMP and LJMP there are CALL instructions, ACALL and
LCALL respectively.
• In case of call instruction, the JMP is taken to the address specified in
the instruction.
• The address of the next instruction after CALL from the program
counter is pushed onto the stack and program counter gets loaded
with the address specified in the CALL instruction.
• At the end of each subroutine there is a RET instruction. Once
subroutine is executed, program counter gets loaded with the
contents from the stack, and resumes the execution from the
instruction immediately after CALL instruction.
• Fig. 2.3 shows the storing and retrieving the return address.

Program counter

PCH PCL

SP + 2 PCH SP + 2
SP + 1 PCL SP + 1
RET RETI

Stack area
PCH PCL SP SP

Program Counter
ACALL LCALL
Interrupt

Internal RAM
Fig. 2.3 : Storing and retrieving the return address
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 2-21 Programming Model of 8051

Table 2.13 : Summarize unconditional JMP and CALL instructions

Mnemonic Description Byte Oscillator


Period

ACALL addr 11 Absolute subroutine call 2 24

LCALL addr 16 Long subroutine call 3 24

RET Return from subroutine 1 24

RETI Return from interrupt 1 24

AJMP addr 11 Absolute Jump 2 24

LJMP addr 16 Long Jump 3 24

SJMP rel Short Jump 2 24

JMP (α)A + Jump Indirect relative to the 1 24


DPTR DPTR

SOLVED EXAMPLES

Example 2.6 : Write ALP in 8051, place any number in internal RAM
location 3CH and increment it until the number equals 2Ah.
Solution : The number can be incremented and then tested to see
whether it equals to 2Ah. If it does, then the program is over. If not, then
loop executes and increment the number again.
Address Mnemonic Comment
ONE : CLR C ; This program will use SUBB
to detect equality
MOV A, #2Ah ; Put the target number in A
SUBB A, 3Ch ; Subtract the content of 3
CH; C is clear
JZ DONE ; If A = 00h, then the
content of 3CH = 2 AH
INC 3CH ; If A is not 0, then loop
until it is
SJMP ONE ; Loop to try again
DONE : NOP ; When finished, jump here
and continue.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 2-22 Programming Model of 8051

Example 2.7 : Write ALP in 8051. The number A6h is placed


somewhere in external RAM between locations 0100h and 0200h. Find
the address of that location and put that address in R6 (LSB) and R7
(MSB).
Solution : The DPTR is used to point to the bytes in external memory
and CJNE is used to compare and jump until the match is found.
Address Mnemonic Comment
MOV 20h, ; Load 20h with the number to
#0A6h be found
MOV DPTR, ; Start the DPTR below the
#00FFH first address
MOR : INC DPTR ; Increment first and save a
jump
MOVX : MOVX A, ; Get the number from external
@ DPTR memory to A
CJNE A, ; Compare the number against
20h, MOR (20h) and loop to MOR if not
equal
MOV R7, 83h ; Move DPH byte to R7
MOV R6, 83h ; Move DPL byte to R6
; Finished

2.2 Addressing Modes : Immediate, Register,


Direct, Indirect and Relative
• Addressing modes are an integral part of each computer’s instruction
set. They basically specify the way of transferring the data.
Depending on the programming situation, addressing modes allow
specifying the source or destination of data in different ways.
• There are various addressing modes available in 8051 microcontroller
architecture as follows:
1. Register Addressing
2. Direct Addressing
3. Indirect Addressing
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 2-23 Programming Model of 8051

4. Immediate Addressing
5. Relative Addressing
6. Absolute Addressing
7. Indexed Addressing
• These modes are discussed below in detail.
1. Register Addressing:
• In 8051, there are 8 registers named from R0 to R7.
• Instructions using register addressing are encoded using the
three least-significant bits of the instruction opcode to specify a
register within this logical address space.
• The 8051 assembly language indicates register addressing with
the symbol Rn where n is from 0 to 7.
• For example, to add the contents of Register 7 to the
accumulator, the following instruction is used :
ADD A, R7
and the opcode is 00101111B. The upper five bits, 00101,
indicate the instruction, and the lower three bits, 111, the
register.
• Examples:
MOV A, RO ; copy the contents of R0 into A
MOV R2, A ; copy the contents of A into R2
ADD A, R5 ; Add contents of R5 to A
MOV R7, DPL ; load R7 with contents of DPL
2. Direct Addressing:
• Direct addressing can access any on-chip variable or hardware
register.
• Depending on the higher order bit of the direct address, one of
two on-chip memory is selected.
• When bit 7 = 0, the direct address is between 0 to 127 (00h-7FH)
and 128 low-order on-chip RAM locations are referenced.
• The direct and indirect addressing modes are used to distinguish
between the SFR space and data memory space.
• In direct addressing mode the data is in a RAM Memory location
whose addressing is known and this address is given as a part of
the instruction.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 2-24 Programming Model of 8051

• Although the entire 128 bytes of RAM can be addressed using


direct addressing mode, it is most often used to access RAM
locations 30-7FH
• Example:
MOV R0, 40H ; save content of RAM location 40H in R0
MOV 7FH, A ; save content of A into RAM location 7FH
MOV A, 07H ; same as move A, R7, it copies data in R7 from
A
MOV 03H, 04H ; it copies data in R4 from R3
3. Indirect Addressing:
• In 8051 assembly language, indirect addressing is represented by
a ‘@’ sign preceding R0 or R1.
• In the register indirect addressing mode, a register is used as a
pointer to the data. Only register R0 and R1 are used for internal
RAM indirect data transfer.
• Indirect addressing is essential for stepping through sequential
memory locations.
• Example :-
MOV, A, @R0 ; move contents of RAM location whose address
is provided By R0 into A
MOV @R1, B ; move contents of B into RAM location whose
Address is provided by R1.
4. Immediate Addressing:
• When a source operand is a constant rather than a variable, then
the constant can be incorporated into the instruction as a byte of
‘immediate’ data.
• In this addressing mode, the source operated is a constant i.e.
immediate data. Here, the immediate data must be preceded by
the sign"#".
• Example:
MOV A, #82H ; load 82H into A
MOV R4, #100 ; load the decimal value 100 into R4
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 2-25 Programming Model of 8051

MOV B, #40H ; load 40H into B


MOV P1, #55H ; load 55H directly to port 1
MOV DPTR , #4521 H ; DPTR = 4521 H
5. Relative Addressing:
• Relative addressing is used only with certain jump instructions. A
relative address is 8-bit signed value, which is added to the
program counter to form the address of the next instruction
executed. The range of for jumping is from −128 to +127
locations.
• The relative instructions are based on the value of the program
counter.
• Relative addressing offers the advantage of providing position-
independent code. But the disadvantage is that the jump
destinations are limited in range.
• Example:
SJMP THERE
6. Absolute Addressing:
• Absolute addressing is used only with the ACALL and AJMP
instructions. These 2-byte instructions allow within the current 2K
page of code memory by providing the 11 least significant bits of
the destination address in the opcode (A10-A8) and byte 2 of the
instruction (A7 - A0).
• The upper five bits of the destination address are the current
upper five bits in the program counter, so the instruction
following the branch instruction and the destination for the
branch instruction must be within the same 2K page, since A15 -
A11 do not change.
• For example, if the label THERE represents an instruction at
address 0F46H, and the instruction.
• Example:
AJMP THERE
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 2-26 Programming Model of 8051

7. Indexed Addressing:
• Indexed instructions use a calculation to generate the address
used as part of the instruction.
• The Indexed addressing is useful when there is a need to retrieve
data from a look-up table. A 16-bit register (data
pointer/program counter – DPTR/PC) holds the base address and
the accumulator holds an 8-bit displacement or index value.
• The sum of these two registers forms the effective address for a
JMP or MOVC Instruction.
• Example:
MOV A, #08H ; Offset from table start
MOV DPTR, #01F00H ; Table start address
MOVC A, @ A + DPTR ; Gets target value from the table start
address + offset and puts it in A.

2.3 Assembler Directives (Org, End), Features


with Example
• The 8051 microcontroller has 8-bit data type and each register is also
8-bit. Programmer or user has to break the data larger than 8-bits.
Directives used for Microcontroller :
1. DB (define byte)
• The DB directive is the most widely used data directive in the
assembler. It is used to define the 8-bit data.
• When DB is used to define data, the numbers can be in decimal,
binary, hex, or ASCII formats.
• Programmer has to mention ‘B’ for binary and ‘H’ For
hexadecimal to mention binary and hex numbers. Regardless of
which is used, the assembler will convert the numbers into hex.
• To indicate ASCII, characters must be written in quotation marks
(for eg. ‘Hello’). The assembler will assign the ASCII code for the
numbers or characters automatically.
• The DB directive is the only directive that can be used to define
ASCII strings larger than two characters; therefore, it should
be used for all ASCII data definitions.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 2-27 Programming Model of 8051

2. ORG (origin)
• The ORG directive is used to indicate the beginning of the
address.
• The number that comes after ORG can be either in hex or in
decimal.
• If the number is not followed by H, it is decimal and the
assembler will convert it to hex.
• Some assemblers use “. ORG” instead of “ORG”.
3. EQU (equate)
• This is used to define a constant without occupying a memory
location.
• The EQU directive does not set aside storage for a data item but
associates a constant value with a data label so that when the
label appears in the program, constant value will be substituted
for the label.
• The following uses EQU for the counter constant and then the
constant is used to load the R3 register.
CONST EQU 25H
MOV A, #CONST ; COPY 25 INTO A
4. END
• This indicates to the assembler the end of the source (asm) file.
• The END directive is the last line of an 8051 program, that is in
the source code anything after the END directive is ignored by
the assembler.
• Some assemblers use “. END” instead of “END”.
2.4 I/O Bit and Byte Programming Using
Assembly Language
• I/O bit manipulation is a powerful and widely used feature of 8051
family.
• I/O Ports and Bit Addressability : A powerful feature of 8051 I/O
ports is capability to access individual bits of the port without
altering the rest of the bits in that port.
• When accessing a port in single bit manner, the instruction, for
example, “SETB P1.3”, which set bit 3 of port 1 high is used.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 2-28 Programming Model of 8051

• Table 2.14 shows the bits of the 8051 I/O port.


Table 2.14 : Single bit addressability of port
P0 P1 P2 P3 Port Bit
P0.0 P1.0 P2.0 P3.0 D0
P0.1 P1.1 P2.1 P3.1 D1
P0.2 P1.2 P2.2 P3.2 D2
P0.3 P1.3 P2.3 P3.3 D3
P0.4 P1.4 P2.4 P3.4 D4
P0.5 P1.5 P2.5 P3.5 D5
P0.6 P1.6 P2.6 P3.6 D6
P0.7 P1.7 P2.7 P3.7 D7

• For example, the following code toggle bit P1.3 continuously.


HERE : CPL P1.3 ; complement P1.3 only
ACALL DELAY
SJMP HERE
; Another variation of the above program
follows :
AGAIN : SETB P1.3
; change only P1.3 = High
ACALL DELAY
CLR P1.3 ; change only P1.3 = Low
ACALL DELAY
SJMP AGAIN
2.4.1 Single Bit Instructions
Table 2.15
Instruction Function
SETB bit Set the bit (bit = 1)
CLR bit Clear the bit (bit = 0)
CPL bit Complement the bit (bit = NOT bit)
JB bit, target Jump to target if bit = 1 (Jump if bit)
JNB bit, target Jump to target if bit = 0 (Jump if not= bit)
JBC bit, target Jump to target if bit = 1 clear bit (Jump if
bit, then clear)
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 2-29 Programming Model of 8051

SOLVED EXAMPLES

Example 2.8 : Write the following program to create a square wave


of 50% duty cycle on bit 2 of port 1.
Solution :

8051

P1.2
T1 T2

T
T1 = T2
Fig. 2.4
The 50% duty cycle means that ‘ON’ and ‘OFF’ states are same.
Therefore, we get toggle P1.2 with time delay inbetween each state.
BACK : SETB P1.2 ; set high bit 2 of port 1
LCALL DELAY ; call delay subroutine
CLR P1.2 ; P1.2 = 0
LCALL DELAY
SJMP BACK
We can write the same program by another way.
BACK : CPL P1.2 ; complement bit 2 of port 1
LCALL DELAY ; call delay subroutine
SJMP BACK
Example 2.9 : Write a assembly language program to create a
square wave of 75% duty cycle on bit-3 of port 1.
TON
Solution : Duty cycle =
TON + TOFF
The 75% duty cycle means ‘ON’ state is thrice the ‘OFF’ state.

8051

P1.3

Fig. 2.5
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 2-30 Programming Model of 8051

AGAIN : SETB P1.3 ;


set port 1 bit-3 high
LCALL DELAY ;
call delay subroutine
LCALL DELAY ;
call delay subroutine again
LCALL DELAY ;
call delay subroutine again
CLR P1.3 ;
clear bit-3 of port 1
(P1.3 = Low)
LCALL DELAY ; call delay subroutine
SJMP AGAIN
Example 2.10 : Write a program to perform the following :
(i) Keep monitoring pin P0.2 until it becomes high.
(ii) When P0.2 becomes high, read the data from port 1.
(iii) Send a LOW to HIGH pulse on P0.3 to indicate that the data has
been read.
Solution :
SETB P0.2 ; make P0.2 an input
MOV P1, #0FFH ; make P1 input port
AGAIN : JNB P0.2, AGAIN ; check P0.2 high
MOV A, P1 ; if P0.2 is high take data
from P1
CLR P0.3 ; clear P0.3
SETB P0.3 ; set P0.3
2.4.2 Checking an Input Bit
• The JB (Jump if bit = 1) and JNB (Jump if not bit) are also used for
single bit operations. They monitor a bit and make a decision
depending on whether it is 0 or 1.
• Instructions JNB and JB can be used for any bits of I/O port 0, 1, 2
and 3, since all ports are bit addressable. However, most of the pins
of port 3 are used for interrupts and serial communication signals
and typically is not used for any I/O either single bit or byte wise.
• Instructions for reading an input ports are :
Table 2.16
Example Function
MOV A, P2 Bring into A the data at P2 Pin
JNB P 2.1, target Jump if pin P2.1 is low
JB P1.4 target Jump if pin P1.4 is high
MOV C, P2.5 Copy status of pin P2.5 to CY
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 2-31 Programming Model of 8051

SOLVED EXAMPLES

Example 2.11 : Assuming that bit P2.2 is an input and represent the
condition of water level indicator. If it is high means tank is full, then send
a high to low pulse to port P1.3 to turn on a buzzer.
Solution :

VCC

P2.2
8051
1k Buzzer
P1.3
7404

Fig. 2.6
JNB P2.2, HERE ; keep monitoring for high
SETB P1.3 ; set bit P1.3 = 1
CLR P1.3 ; make high to low
SJMP HERE
Example 2.12 : A switch is connected to P1.1 and an LED to pin P2.1.
Write a program to get the status of switch and send it to LED.
Solution :
SETB P1.1 ; make P1.1 as input
AGAIN : MOV C, P1.1 ; read switch position
MOV P2.1, C ; send switch status to LED
SJMP AGAIN ; keep repeating
Example 2.13 : A switch is connected to pin P1.5. Write a program to
check the status of switch and perform the following :
(a) If SW = 0 send letter ‘A’ to P2.
(b) If SW = 1 send letter ‘B’ to P2.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 2-32 Programming Model of 8051

Solution :
SETB P1.5 ; make P1.5 an input
AGAIN : JB P1.5, over ; jump if P1.5 = 1
MOV P2, #‘A’ ; SW = 0 issue ‘A’ to P2
SJMP AGAIN ; keep monitoring
MOV P2, #‘B’ ; SW = 1 issue ‘B’ to P2
SJMP AGAIN ; keep monitoring
Example 2.14 : A switch is connected to port pin P0.1, write a
program to check the status of the switch and perform the following :
(a) If switch = 1 send high to low pulse to activate a siren connected
to pin P1.7.
(b) Continue monitoring the pin status. Use the carry flag to check
the switch status.
Solution :
SETB P0.1 ; make P0.1 an input
AGAIN: MOV C, P0.1 ; read P0.1 in carry flag
JNC AGAIN ; if C is not high monitor
SETB P1.7 ; if C = high set P1.7 = H
CLR P1.7 ; make P1.7 H → L
SJMP AGAIN

2.5 LED and Seven Segment Display (SSD)


Interfacing
2.5.1 LED Interfacing
• Light Emitting Diodes are the most commonly used components in
many applications.
• The basic interfacing of LEDs to the 8051 family microcontrollers is
described in this section.
• LEDs can be directly interfaced to the ports of microcontroller.
• LEDs will have voltage drop of 1.7 V and current of 10 mA to glow at
full intensity. This is provided through the output pin of the
microcontroller.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 2-33 Programming Model of 8051

VCC
C11
LED 1
100n

40

20
U7
LED 2
VCC GND

17 P3.0 LED 3
P3.7/RD
P3.6/WR 16 P3.1
15 P3.2 R18
P3.5/T1 LED 4 2 1
P3.4/TD 14 P3.3
P3.4 3
13 4
P3.3/INT1
12 P3.5 5
VCC P3.2/INTD P3.6 LED 5 6
11
P3.1/TXD 7
10 P3.7 8
P3.0/RXD
LED 6 9
C13 0
SW 330E
10 mF LED 7
RST 9 8051
RST
R6 19 ALE 30 ALE LED 8
x1 29 PSEN
8 kW PSEN
x1 31 EA
18 x EA
2
10 MHz
C18 C19
22pF 22pF

Fig. 2.7
• The circuit mainly consists of 8051 microcontroller. There are four
ports designated as P0, P1, P2 and P3.
• In this circuit, LEDs are connected to the port P0. Port P0 will not
have internal pull-ups, while the other ports have internal pull-ups.
• The controller is connected with external crystal oscillator to pin 18
and 19 pins. Crystal pins are connected to the ground through
capacitors of 33pf.
• LED is directly connected to the AT8051 microcontroller. The
negative terminal of the LED is connected to the ground through a
resistor.
• Value of this resistor is calculated using the following formula.
R = (V − 1.7)/10mA, where V is the input voltage.
• Generally, microcontrollers output a maximum voltage of 5V. Thus,
the value of resistor calculated for this is 330 Ohms. Thus, this can be
connected either to the cathode or anode of the LED.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 2-34 Programming Model of 8051

• Anode of LED is connected to Microcontroller pin and cathode is


connected through a resistor to GND. So when the Port Pin is HIGH,
the LED is ON and when the Port Pin is LOW the LED is turned OFF.

• By outputting the logic ‘1’ or HIGH, LED will turn ON and sending
logic ‘0’ Led will be turned OFF.
• The assembly language program for blinking the LED is given below:

ORG 00H
CLR P3.0
CLR P2.0
AGAIN: JB P3.0,ON
SETB P2.0
ACALL DELAY
CLR P2.0
ACALL DELAY
SJMP AGAIN
ON: SETB P2.0
SJMP AGAIN
DELAY: MOV R0,#05H
GO: MOV R1,#0FFH
HERE: DJNZ R1,HERE
DJNZ R0,GO
RET
END
2.5.2 Seven Segment Display (SSD) Interfacing

• Seven segment LED display can display digits from 0 to 9.


• A seven segment display consists of seven LED segments.

• Different characters can be displayed by selectively glowing the


required LED segments.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 2-35 Programming Model of 8051

• Seven segment displays are of two types, common cathode and


common anode.
• The 7-segment display is shown in Fig. 2.8.

g f com a b

e d com c dot

Fig. 2.8 : 7 segment LED display


• Look-up table for 7-segment shown below.
Table 2.17

Digit a b c d e f g

0 1 1 1 1 1 1 0

1 0 1 1 0 0 0 0

2 1 1 0 1 1 0 1

3 1 1 1 1 0 0 1

4 0 1 1 0 0 1 1

5 1 0 1 1 0 1 1

6 1 0 1 1 1 1 1

7 1 1 1 0 0 0 0

8 1 1 1 1 1 1 1

9 1 1 1 1 0 1 1
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 2-36 Programming Model of 8051

Interfacing Seven Segment Display to 8051:


+ 5V + 5V - 5V

31 40
EA VCC
D1
Common
+ C3 cathode
S3 R3 seven segment
10 mF/10V P1.0 1 LED display
9 2 R4
RST P1.1 a
3 R5
b
P1.2
R2 R6 c
4
8.2 kW IC1 P1.3 d
AT89S51 5 R7 e
P1.4
6 R8 f
P1.5
R9 g
7
P1.6
R10 h com
C1 8
19 P1.7
XTAL1
33pF X1 R3 to R10 = 560 ohm

C2 11.0592 MHz
18 XTAL2
33 pF GND
20

Fig. 2.9 : Interfacing 7 segment display to 8051


• The numbers are displayed on 7-segment display as shown in
Fig. 2.9.
• The common cathode seven segment display D1 is connected to the
Port 1 of the microcontroller.
• R3 to R10 are current limiting resistors.
• S3 is the reset switch and R2, C3 forms a debouncing circuitry. C1, C2
and X1 are related to the clock circuit.

Program:
ORG 000H // initial starting address
START: MOV A,#00001001B // initial value of
accumulator
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 2-37 Programming Model of 8051

MOV B,A
MOV R0,#0AH // Register R0 counts from 10
to 0
LABEL: MOV A,B
INC A
MOV B,A
MOVC A,@A+PC // adds the byte in A to the
program
MOV P1,A
ACALL DELAY // calls the delay of the
timer
DEC R0 // Counter R0 decremented by
1
MOV A,R0 // R0 moved to accumulator to
check counter
JZ START // Checks accumulator for
zero and jumps
SJMP LABEL
DB 3FH // digit pattern for 0
DB 06H // digit pattern for 1
DB 5BH // digit pattern for 2
DB 4FH // digit pattern for 3
DB 66H // digit pattern for 4
DB 6DH // digit pattern for 5
DB 7DH // digit pattern for 6
DB 07H // digit pattern for 7
DB 7FH // digit pattern for 8
DB 6FH // digit pattern for 9
DELAY: MOV R4,#05H // subroutine for delay
WAIT1: MOV R3,#00H
WAIT2: MOV R2,#00H
WAIT3: DJNZ R2,WAIT3
DJNZ R3,WAIT2
DJNZ R4,WAIT1
RET
END
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 2-38 Programming Model of 8051

• In the program, initial value in A is 00001001B.


• Register R0 is used as a counter which counts from 10 down to 0.
This ensures that digits from 0 to 9 are continuously displayed on the
7 segment display.
• Register B is used as a temporary storage of the initial value of the
accumulator and the subsequent increments made to accumulator to
fetch each digit pattern one by one from the Look Up Table (LUT).

2.6 Introduction to 8051 Programming in C


• Programming in assembly language is tedious and time consuming
while programming in ‘C’ is easy and less time consuming. However,
size of hex file in ‘C’ is much larger than assembly language
programming.
• Following are some major reasons for writing program in C instead
of assembly.
1. Programming in C is easier and less time consuming than
assembly.
2. It is easier to modify and update.
3. One can use code available in function libraries.
4. C code is portable to other microcontroller with little or no
modification.
2.6.1 Simple C Programs for 8051 Microcontroller
• In ‘C’ programming, the header file ‘reg51’ have to be added at the
beginning of the program.
• To set any port pin, sbit statement is used. ‘sbit’ keyword allows the
access to the single bit of SFR register.
• The standard format of a ‘C’ program is explained by illustrating the
examples as follows:
[I] The program for LED blinking using ‘C’ language.
Solution:
#include<reg51.h>
//sbit pin=P1^0; // set pin 0 of port 1
void delay(int value)
{
int i,j;
for(i=0;i<=value;i++);
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 2-39 Programming Model of 8051

for(j=0;j<=5;j++);
}
void main()
{
while(1){
P1=0×01; // pin=1;
delay(500);
P1=0×00; // pin=0
pin=0;
delay(500);
}
}
[II] Write an 8051 C program to toggle all the bits of P1 continuously.
Solution:
//Toggle P1 forever
#include <reg51.h>
void main(void)
{
for (;;)
{
p1=0×55;
p1=0×AA;
}
}
[III] Write an 8051 C program to toggle bits of P1 continuously forever
with some delay.
Solution:
//Toggle P1 forever with some delay in between
//“on” and “off”
#include <reg51.h>
void main(void)
{
unsigned int x;
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 2-40 Programming Model of 8051

for (;;) //repeat forever


{
p1=0×55;
for (x=0;x<40000;x++); //delay size
//unknown
p1=0×AA;
for (x=0;x<40000;x++);
}
}
[IV] Write an 8051 C program to toggle bits of P1 ports continuously
with a 250 ms.
Solution:
#include <reg51.h>
void MSDelay(unsigned int);
void main(void)
{
while (1) //repeat forever
{
p1=0×55;
MSDelay(250);
p1=0×AA;
MSDelay(250);
}
}
void MSDelay(unsigned int itime)
{
unsigned int i,j;
for (i=0;i<itime;i++)
for (j=0;j<1275;j++);
}
[V] Write ‘C’ program to read status of port pin P1.3 and save it in bit
memory location 42h.
Solution :
#include <reg51.h>
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 2-41 Programming Model of 8051

void main(void)
{
sfr r0 = 0×42h;
unsigned char mybyte;
sbit mybit = P1^3; // read P1.3
ro = mybit; // store data from P1.3 to r0
location 42h
}

Think Over It

• What is LST file?


• Would the Reset circuit function properly if its 8.2K resistance is
replaced by 10K resistance?

Points to Remember
• The instruction set of MCS-51 is classified into the following types :
1. Data transfer instructions.
2. Arithmetic instructions.
3. Logical instructions.
4. Boolean variable manipulation instructions.
5. Branching instructions.
• Multiplication uses register A and B as both source and destination
addresses for operation.
• In DIV AB instruction, the unsigned number in A is divided by
number which is in B. The OV flag is cleared to 0 unless B holds 00H
before DIV.
• Quotient is stored into register A while remainder is stored into
register B.
• Unconditional Jumps do not test any bit or byte; the jump is always
taken.
• Absolute JMP allows 11-bit address to be specified in the instruction.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 2-42 Programming Model of 8051

• Long jump allows 16-bit address to be specified in the instruction


and a JMP to anywhere within 64 k bytes block of program memory
is possible.
• Short jump to the relative address, is again unconditional JMP and is
always taken. For short JMP, the possible range is between − 127 to
+ 128 bytes.
• Various addressing modes available in 8051 microcontroller
architecture are as follows:
1. Register Addressing : ADD A, R7
2. Direct Addressing : MOV R0, 40H
3. Indirect Addressing : MOV A, @R0
4. Immediate Addressing : MOV A, #82H
5. Relative Addressing : SJMP THERE
6. Absolute Addressing : AJMP THERE
7. Indexed Addressing : MOV A, #08H
• The DB directive is the most widely used data directive in the
assembler. It is used to define the 8-bit data.
• The ORG directive is used to indicate the beginning of the address.
• EQU is used to define a constant without occupying a memory
location.
• END indicates to the assembler the end of the source (.asm) file.
• A powerful feature of 8051 I/O ports is capability to access individual
bits of the port without altering the rest of the bits in that port.
• Single bit instructions: SETB bit, CLR bit, CPL bit.
• LEDs can be directly interfaced to the ports of microcontroller.
• Seven segment LED display can display digits from 0 to 9. A seven
segment display consists of seven LED segments. Different characters
can be displayed by selectively glowing the required LED segments.
• C is easier and less time consuming than assembly and have
following advantages:
It is easier to modify and update. One can use code available in
function libraries, C code is portable to other microcontroller with
little or no modification.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 2-43 Programming Model of 8051

Exercises
[A] True or False :
1. All additions are done with a register as the destination of the result.
2. DA A adjust the sum of two unpacked BCD numbers found in
register A.
3. CJNE instruction means compare and jump if number is equal.
4. The DB directive is the most widely used data directive in the
assembler.
5. In ‘C’ programming, the header file reg 51 have to be added at the
beginning of the program.
[B] Fill in the Blanks :
1. …… instruction is used for data transfer.
2. …… and …… instructions are used for stack operators.
3. …… directive is used to indicate the beginning of the address.
4. …… indicates to the assembler the end of the source (asm) file.
5. ANL A, Rn will …… the data started in accumulator with the data
stored in register Rn.
[C] Multiple Choice Questions :
1. MCS-51 supports …… arithmetic unsigned operations.
(a) 8-bit (b) 16-bit
(c) 32-bit (d) none of these
2. INC A adds …… to accumulator.
(a) 2 (b) 1
(c) 3 (d) 4
3. DIV AB puts ……
(a) quotient in register A and remainder in B
(b) quotient in register B and remainder in A
(c) quotient and remainder both in A
(d) none of these
4. …… instruction clears accumulator.
(a) CPL A (b) CLR A
(c) Clear A (d) None of these
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 2-44 Programming Model of 8051

5. …… is the example of unconditional jump.


(a) JNZ (b) CJNE
(c) JMP@A + DPTR (d) None of these
6. …… is used before data in immediate addressing.
(a) @ (b) #
(c) H (d) None of these
7. …… addressing is represented by @ sign preceding R0 or R1.
(a) Indirect (b) Direct
(c) Register (d) Immediate
8. …… directive is used to indicate the beginning of the address.
(a) EQU (b) ORG
(c) END (d) DB
9. …… is single bit instruction.
(a) SET B (b) CLR
(c) CPL (d) All of these
10. …… define a constant without occupying a memory location.
(a) ORG (b) DB
(c) EQU (d) END
[D] Short Answer Questions :
1. Which 8051 ports need pull-up resistor to function as I/O port ?
2. All the 8051 ports can be used for both input and output : Comment.
3. How many ports are available in 8051 ?
4. Write instructions to get the status of P2.7 and put it on P2.0.
5. Write an instruction to toggle both bits at P1.7 and P1.0
continuously.
6. “SETB P2.1” makes P2.1 HIGH, will it affect on other bits of P2 ?
7. Which ports of the 8051 are bit addressable?
8. What is the advantage of bit addressable facility for 8051 ports ?
9. What is the error in the following code :
(a) MOV P1, # 0FH
CPL P1
(b) MOV P2.7, P1.0
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 2-45 Programming Model of 8051

10. What is the dual role of port 2 ?


11. Why does port 0 need pull-up resistors ?
12. Which of the port of the 8051 does not have alternate function and
can be used solely for I/O ?
13. Which pins of port 3 cater to interrupt ?
[E] Long Answer Questions :
1. Explain in detail about port 0 and port 1.
2. Discuss in detail about the alternate facilities available on port 3.
3. How pull-up resistors are connected in port 0 ? Explain with figure.
4. Write a program to get 8-bit data from P1 and send it to P0, P2 and
P3.
5. Write a program to get 8-bit data from P2 and send it to port P0 and
P1.
6. Write a program to output 00H on port 0, 0FH on port 1, 0FFH on
port 3.
7. Write a program to toggle all the bits of P1 and P2 continuously :
(a) Using AAH and 55H.
(b) Using CPL instruction.
8. Write a program to take in data through P1.1 and send it out
through P2.7.
9. Write a program to monitor P2.7, when it is high send 55H to P1.
10. Write a program to monitor the P2.0 bit. When it is high, send 99H to
P1. If it is low send 66H to P1.
11. Write a program to generate a square wave of 25% duty cycle on pin
2.3.
12. A switch SW is connected to pin P1.4 write a program to output 00
on port 1 if SW = 0 and FFH on port 1 if SW = 1.
13. Write a program to get status of P1.3 and put it on P2.4.
14. Write a program to monitor the P1.5, when it is high make high to
low pulse on P1.3.
15. Write a short note addressing modes of 8051.
16. Explain interfacing of LEDs to 8051 with the help of diagram.
17. Draw and explain the interfacing of seven segment display to 8051.
18. State the advantages of ‘C’ programming.
19. Explain the directives used in 8051.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 2-46 Programming Model of 8051

Answers
[A] True or False :
(1) True (2) False (3) False
(4) True (5) True
[B] Fill in the blanks :
(1) MOV, (2) PUSH and POP, (3) ORG, (4) END, (5) AND.
[C] Multiple Choice Questions :
1. (a) 2. (b) 3. (a) 4. (b) 5. (c) 6. (b) 7. (a)
8. (b) 9. (d) 10. (c)

University Questions
April 2015
1. Differentiate between the following instructions : (2M)
MOV A, R0
MOV A, @R0
2. Give the function of ‘sbit’ data type in ‘C’ program. (2M)
3. Write the function of assembler directive ORG. (2M)
4. Write assembly language program to convert Hex number to its
decimal equivalent. (5M)
5. Identify the addressing modes of the following instructions : (5M)
(i) MOVC A, @A + DPTR, (ii) MUL AB, (iii) MOV a, @R0,
(iv) SETB P1.3, (v) MOV P1, #47h.
6. Write a ‘C’ program to toggle all bits of port 1 continuously. (3M)
October 2015
1. Which header file must be included while programming 8051 using
‘C’ ? (2M)
2. Give the function of assembler directive END. (2M)
3. Write assembly language program to accept data from port 2 and
check whether the data is 15h. If yes, transfer the data to port 3 else
stored it to RAM location 30h. (5M)
4. Explain the function of the following instructions : (5M)
(i) ORL a, # 02h
(ii) INC @ R0
(iii) CJNE a, # 20h, 2h
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 2-47 Programming Model of 8051

(iv) MOVX a, @ dptr


(v) MOVC, a, @ a + dptr.
5. Write ‘C’ program to read status of port pin P1.3 and save it in bit
memory location 42h. (3M)

April 2016
1. Explain the instruction LCALL 1300H. (1M)
2. Which port of 8051 requires external pull-up registers? (1M)
3. Explain indirect addressing mode of 8051. (1M)
4. Explain how LED can be interfaced to 8051 with neat diagram. Write
an assembly program to toggle LED connected to P2.4. (5M)
5. Explain the operation of following instructions. (5M)
(a) ACALL addr15
(b) RETI
(c) NOP
(d) DIV AB
(e) CLR bit
6. Write an assembly language program to convert hexadecimal
number to decimal number. (3M)

April 2017

1. Give two unconditional jump instructions with proper syntax. (1M)


2. What is the difference between instructions MOV R1, 41H and MOV
R1, #41H? (1M)
3. Explain the function of following instructions : (5M)
(a) MOV A, @R0
(b) ADD A, #80H
(c) RL A
(d) SWAP A
(e) CPL bit
4. Explain different addressing modes of 8051 with suitable example.
(5M)
5. Write an assembly language program to toggle all bits of port 2
continuously. (2M)
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 2-48 Programming Model of 8051

April 2018
1. Name the addressing mode for the instruction MOV DPTR, #300H
(1M)
2. What is the status of bit P1.1 after execution of instruction SET B
P1.1. (1M)
3. What will be the output of each of the following after execution?(5M)
(a) P1 = O × FO & O × 35
(b) P1 = O × FF ∧ O × 65
(c) P2 = −O × IF
(d) P2 = O × 36 | O × 65
(e) P1 = O × 12 >> 2
4. Explain the functions of following instructions :
(a) MOV A, 40H
(b) AIPM addr 11
(c) CPL A
(d) INC R1
(e) MUL AB
5. Explain how LED can be interfaced to 8051 with a neat diagram and
write a program to toggle LEDs connected to P1.3 continuously. (5M)

April 2019
1. Explain the term Assembler directive. (1M)
2. Identify the addressing mode of 8051 instruction MOV A, @R1. (1M)
3. Explain the operation of following 8051 instructions : (5M)
(a) CLR A
(b) ORL A, #75H
(c) LCALL 2000H
(d) SETB PSW.4
(e) MUL AB
4. Write an 8051 assembly language program for blinking 8 LEDs
connected to port 2. (5M)
5. Which port of 8051 requires pull up registers? Draw the block
diagram interfacing common anode SSD to port 2. (5M)

✍✍✍
Unit 3…
Timer/Counter, Interrupts
Introduction
• The timers and counters are used in many applications such as
counting of external events, frequency of a pulse train or the
generation of precise internal timer delay between the computer
actions.
• In the subsequent sections, how these timers are used to generate
time delays and how they are used as event counters is discussed.
• In the last section of this chapter, programming of timers using C
language is explained. Also Programming of serial port without
interrupt and use of timer to select baud rate for serial
communication is studied in the last section of this chapter.
3.1 Counters and Timers of 8051
Microcontroller
• Many microcontroller applications require the counting of external
events, such as the frequency of pulse train or generation of precise
internal time delays between computer actions.
• Both of these tasks can be accomplished using software techniques.
But software loops for counting or timing keep the processor
occupied so that other, perhaps more important functions are kept
on waiting.
• To relieve the processor burden, two 16-bit timers T0 and T1 are
provided in 8051. They can be used either as timers or as counters.
• Both timer 0 and timer 1 are 16-bit wide.
• Since, 8051 has 8-bit architecture, each 16-bit timer is accessed as
two separate register of low byte and high byte as shown in Fig. 3.1.
3.1.1 Timer 0 Register
TH0 TL0

D15 D14 D13 D12 D11 D10 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0

Fig. 3.1 : Timer 0 register


(3.1)
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 3-2 Timer/Counter, Interrupts

• The 16-bit register of timer 0 is accessed as low byte and high byte.
The low byte register is called TL0 (Timer 0 low byte) and high byte
register is referred as TH0 (Timer 0 high byte).
• These registers can be accessed as any other registers like A, B, R0,
R1 etc. For example, MOV TH0, # 9FH; moves the value 9FH into TH0.
3.1.2 Timer 1 Register
TH1 TL1

D15 D14 D13 D12 D11 D10 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0

Fig. 3.2 : Timer 1 register


• Timer 1 register is also 16-bits and is split into two bytes referred as
TL1 (Timer 1 low byte) and TH1 (Timer 1 high byte). These registers
are accessible in the same way as the registers of Timer 0 as shown in
Fig. 3.2.
3.1.3 TMOD (Time Mode) Register
• Timer 0 and 1 use the same register called TMOD to set the various
timer operation modes.
• TMOD is an 8-bit register in which the lower 4-bits are set aside for
timer 0 and upper 4-bits for timer 1.
• In each case lower 2-bits are used to set the timer mode and upper
2-bits are used to specify the operation.
• 8051 timer/counter has four modes and they can be programmed
using the SFR called as TMOD (Timer modes).
(MSB) (LSB)

GATE − M1 M0 GATE − M1 M0
C/T C/T
Timer 1 Timer 0

GATE : Gating control when set. Timer/counter is enabled only when


the INTX pin is high and the TRx control pin is set. When cleared, the
timer is enabled whenever the TRx control bit is set.

C/T : Timer or counter selected cleared for timer operation (input
from internal system clock). Set for counter operation (input from Tx
input pin).
M1 : Mode bit 1
M0 : Mode bit 0
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 3-3 Timer/Counter, Interrupts

M1 M0 Mode Operating Mode


0 0 0 13-bit timer mode
8-bit timer/counter THX with TLX as 5-bit
prescaler
0 1 1 16-bit timer mode
16-bit timer/counters THX and TLX are
cascaded; there is no prescalar
1 0 2 8-bit auto reload
8-bit auto reload timer/counter; THX holds a
value which is to be reloaded into TLX each time
it overflows.
1 1 3 Split timer mode
Fig. 3.3 : TMOD Register
• In this case, the lower 4-bits are assigned to timer 0 and upper 4-bits
for timer 1. M1 and M0, decides mode of timer/counter.
− − −
C/T : Decides whether to use it as a timer C/T = 0 or counter C/T = 1.
GATE : Every timer has some means of starting and stopping.
Sometimes it is done by hardware or software or sometimes by both.
The start and stop action of timers are controlled by TRx bit. If they
are set, the timer will start and if cleared it will be stopped. But it is
possible only when GATE = 0. In case of GATE = 1 hardware control
will start or stop timer. Pin P3.2 and P3.3 i.e. T0 and T1 of timer will
control the timer operations. It is like a simple switch to stop the
timer.
3.1.4 The TCON (Timer Controlled) Register
TCON.7 TCON.6 TCON.5 TCON.4 TCON.3 TCON.2 TCON.1 TCON.0

TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0


Fig. 3.4 : Timer control register (TCON) (Bit addressable)
TCON.7 TF1 Timer 1 overflow flag, set when timer/counter
overflows

TCON.6 TR1 Time 1 run control bit

TCON.5 TF0 Timer 0 overflow flag, set when timer/counter 0


overflows
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 3-4 Timer/Counter, Interrupts

TCON.4 TR0 Timer 0 run control bit

TCON.3 IE1 Interrupt 1

TCON.2 IT1 Timer interrupt 1

TCON.1 IE0 Interrupt 0 flag

TCON.0 IT0 Timer 0 interrupt, IT0 = 0, low level trigger, IT0 =


1, edge trigger (falling edge)
• If a counter is programmed to be a timer, it will count the internal
clock frequency of the 8051 oscillator divided by 12d. As an example,
if the crystal frequency is 12 MHz, then clocking period is 1 µs.

XTAL ..
oscillator 12

Fig. 3.5 (a)


• The resultant timer clock is gated to the timer by means of circuit
shown in Fig. 3.5 (b).
Timer C/T = 0
Oscillator frequency ¸ 12d (TMOD timer operation)

To timer
stages
Counter
T1/0 input pin C/T = 1
(TMOD counter operation)
TR1/0 Bit in TCON

Gate bit in TMOD


INT1/0 input pin

Fig. 3.5 (b) : Timer/counter control logic



• In order for oscillator clock pulse to reach the timer, the C/ T bit in
the TMOD register must be set to 0 (timer operation).
• Bit TRx in the TCON register must be set to 1 (timer run) and the gate

bit in the TMOD register must be 0 or external pin INTX must
be 1.
3.1.5 Timer Modes of Operation
• The timer may operate in any one of four modes that are determined
by mode bits. M1 and M0, in the TMOD registers.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 3-5 Timer/Counter, Interrupts

Types of Timer Modes :


Following are the four timer modes.
Timer Mode 0 :
• It is a 13-bit mode, can hold values from 0000H to 1FFFH in THX
− TLX.
• Lower 5-bits are stored in TLX while higher 8-bits are stored in
THX.
• Whenever the timer reaches its maximum value, it rolls over to
000H and TFX flag is set.

Pulse TLX 5-bits THX 8-bits TFX Interrupt


input

Fig. 3.6 : Timer mode 0, 13-bit timer/counter


Timer Mode 1 :
• It is same as mode 0 except that it is 16-bit timer.
• It allows values from 0000H to FFFFH to be loaded into TLx and
THx register.
• Once counter reaches its maximum at FFFFH it rolls over to
0000H and TFx flag is set.

Pulse TLX 8-bits THX 8-bits TFX Interrupt


input

Fig. 3.7 : Timer mode 1, 16-bit timer/counter


Timer Mode 2 :
• It is 8-bit auto reload mode. Values from 00H to FFH are
allowed to be loaded in THX.
• Once THX is loaded, 8051 copies the same value in TLX and timer
gets started by setting TRX bit. It counts up by incrementing TLX
register while THX contents remains as it is.
• When TLX counts upto FFH it rolls over to 00H and TFX flag gets
set and the TLX register is again gets reloaded by THX and THX
values.
• Only thing is important to clear the TFX bit before reloading
takes place and counting starts again.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 3-6 Timer/Counter, Interrupts

Pulse TLX 8-bits TFX Interrupt


input

Reload TLX

THX 8-bits

Fig. 3.8 : Timer mode 2, auto-reload of TL from TH


Timer Mode 3 :
• It is two 8-bit timers mode.
• Two 8-bit timers are available which counters from 00H to FFH.
• One of the 16-bit times is 0FF.
• If timer 0 is used as two 8-bit timers then TL0 will use all signals
like TF0 and TR0.
• While TH0 will use signals of timer 1 i.e. TF1 and TR1.
• Timer 1 may be programmed into mode 0, 1, 2 but will not
generate any interrupt as its interrupt flag is used by TH0
register.

Pulse TL0 8-bits TF0 Interrupt


input

f/12 TH0 8-bits TF1 Interrupt

TR1 bit
in TCON
Fig. 3.9 : Timer mode 3, two 8-bit timers using timer 0
Counting :
• The only difference between counting and timing is the source of
the clock pulses to the counters.
• When used as a timer, the clock pulses are sourced from the
oscillator through the divide by 12d circuit.
• When used as a counter, pin to (P3.4) supplies pulses to counter
0 and pin T1 (P3.5) to counter 1.

• The C/T bit in TMOD must be set to 1 to enable pulses from the
Tx pin to reach the control circuits.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 3-7 Timer/Counter, Interrupts

3.2 Programming Timers 0 AND 1 of 8051


Using ‘C’
• Programming in assembly language is tedious and time consuming
while with using C is less time consuming and easy to write, but size
of hex file is much larger than assembly language programming.
• However, the following are some major reasons for writing program
in C instead of assembly.
1. C is easier and less time consuming than assembly.
2. It is easier to modify and update.
3. One can use code available in function libraries.
4. C code is portable to other microcontroller with little or no
modification.
• In this section, how to access the 8051 timers directly using C
statements is discussed.
Assessing Timer Register in C : In 8051, the timer registers TH, TL
and TMOD using the reg51.h header file can be accessed. Following
example will explain it.
SOLVED EXAMPLE

Example 3.1 : Write a 8051 C program to toggle all the bits of port 1
continuously with some delay in between. Use timer 0, 16-bit mode to
generate the delay.
Solution :

8051

Port To LED
1

Fig. 3.10
# include < reg51.h >
void To Delay (void);
void main (void)
{
while (1) // repeat forever
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 3-8 Timer/Counter, Interrupts

{
P1 = 0×55; // toggle all bits of port 1
To Delay ( ); // unknown delay size
P1 = 0×AA // toggle all bits of port 1
To Delay ( );
}
}
void To Delay ( )
{
TMOD = 0×01; // timer 0, Mode 1
TL0 = 0×00; // load TL0
TH0 = 0×35; // load TH0
TR0 = 1; // turn ON T0
while (TF0 = 0); // wait for TF0 to roll over
TR0 = 0; // turn OFF T0
TF0 = 0; // clear TF0
}
FFFFH − 3500H
= CAFFH
= 51967 + 1 = 51968
∴ 51968 × 1.085 µs = 56384 ms is the approximate delay.
3.2.1 Calculating Time Delay with C
• There are two ways to create a time delay in 8051 C :
1. Using a simple for loop.
2. Using the 8051 timer.
• In either case, time delay can be written. To measure the duration of
time delay, oscilloscope is used. Time delays can be also created
using for loop.
• The delay length depends on three factors :
(a) The Crystal Frequency : The crystal frequency connected to the
X1 and X2 input pins. The duration of the clock period for the
machine cycle is a function of the crystal frequency.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 3-9 Timer/Counter, Interrupts

(b) The Number of Clock Per Machine Cycles : The original 8051
uses 1/12 of the crystal oscillator frequency as one machine
cycle. In other word, each machine cycle is equal to 12 clock
periods of the crystal frequency connected to X1 − X2 pin. Many
of the new generations of the 8051 uses few machine cycles. For
example, DS5000 uses 4 clock periods per machine cycles, while
DS89C420 uses only 1 clock per machine cycle. This is done in
order to maintain compatibility with the original 8051, since
many designers use timer to create time delay.
(c) Compiler Cycle : This factor affects the time delay. When we
program in assembly language, we can change or control the
exact instructions and their sequences used in the delay sub-
routine. In case of C program, it is a C compiler, the C statements
and functions are converted into assembly language instructions.
• There is a major difference between the AT89C51 and DS89C4x0
chips in term of the time, it takes to execute a single instruction. The
DS89C4x0 execute instructions 12 times faster than AT89C51 chip,
they both still use OSC/12 clock for their timers. The faster execution
time for the instructions will have an impact on your delay length.
• Following examples show 8051 C programming of timer 0 and timer
1 in mode 1 (16-bit non-auto reload).
SOLVED EXAMPLES

Example 3.2 : Write a C program to toggle only bit P1.5


continuously every 50 ms. Use Time 0, Mode 1 (16-bit) to create the
delay using (a) AT89C51, (b) DS89C420.
Solution :
# include < reg51.h >
void T0M1Delay (Void);
Sbit mybit = P1^5;
void main (Void)
{
while (1)
{
mybit = − mybit; // toggle P1.5
T0M1Delay ( ); // timer 0, mode 1 (16-bit)
}
}
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 3-10 Timer/Counter, Interrupts

(a) Program is tested for AT89C51 XTAL = 11.0592 MHz using


preview 32 compiler.
void T0M1Delay (Void)
{
TMOD = 0×01; // timer 0, Mode 1 (16-bit)
TL0 = 0×FD; // load TL0
TH0 = 0×4B; // load TH0
TR0 = 1; // turn ON T0
while (TF0 = = 0); // wait for TF0 to roll over
TR0 = 0; // turn OFF T0
TF0 = 0; // clear TF0
}
(b) Program is tested for DS // 89C420, XTAL = 11.0592 MHz
using the same compiler mentioned above i.e. preview 32 compiler.
void T0M1Delay (Void)
{
TMOD = 0×01; // timer 0, Mode 1 (16-bit)
TL0 = 0×FD; // load TL0
TH0 = 0×4B; // load TH0
TR0 = 1; // turn ON T0
while (TF0 = = 0); // wait for TF0 to roll over
TR0 = 0; // turn OFF T0
TF0 = 0; // clear TF0
}
FFFFH − 4BFDH = B402H
= 46082 + 1
= 46083
Timer delay = 46083 × 1.085
= 50 ms
Example 3.3 : Write an 8051 C program to toggle all bits of P2
continuously every 500 ms. User Timer 1, Mode 1 to create the delay.
Solution : // This program is tested for DS89C420, XTAL = 11.0592
MHz, using preview 32 compiler.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 3-11 Timer/Counter, Interrupts

# include < reg51.h >


void T1M1 Delay (Void);
void main (Void)
{
Unsigned char X;
P2 = 0×55;
While (1)
{
P2 = − P2;  Toggle all bits at port 2
for (X = 0; X < 20; (X++)
T1M1 Delay ( );
}
}
void T1M1 Delay (Void)
{
TMOD = 0×10; // timer 1, mode 1 (16-bit)
TL1 = 0×FE; // load TL1
TH1 = 0×A5; // load TH1
TR1 = 1; // turn ON T1
while (TF1 = = 0); // wait for TF1 to roll over
TR1 = 0; // turn OFF T1
TF1 = 0; // clear TF1
}
A5FEH = 42494D
65536 − 42494 = 23042
23042 × 1.085 µs = 25 ms
20 × 25 ms = 500 ms
Following examples show 8051 C programming of timer 0 and 1 in
mode 2 (8-bit auto reload).
Example 3.4 : Write an 8051 C program to create a frequency of
2500 Hz on P2.5. Use timer 1 mode 2 to create the delay.
Solution :
f = 2500 Hz
1 1
∴ T = = = 400 µs
f 2500
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 3-12 Timer/Counter, Interrupts

400 µs
∴ = 200 µs
2
200 µs
∴ = 184
1.085 µs

8051

P2.5
f = 2500 Hz

Fig. 3.11
// The program is tested for DS89C420, XTAL = 11.0592 MHz using
the preview 32 compiler.
# include < reg51.h >
void T1M2 Delay (Void);
Sbit mybit = P2^5;
void main (void)
{
unsigned char X;
while (1)
{
mybit = − mybit; // toggle P2.5
T1M2 Delay ( );
}
}
void T1M2 Delay (Void)
{
TMOD = 0×20; // timer 1, mode 2 (8-bit
auto reload)
TH1 = − 184; // load TH1
TR1 = − 1; // turn ON T1
While (TF1 = − 0); // wait for TF1 to roll over
TR1 = 0; // turn OFF T1
TF1 = 0; // clear TF1
}
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 3-13 Timer/Counter, Interrupts

Example 3.5 : Write an 8051 C program to toggle only pin P1.3


continuously every 250 ms. Use Timer 0, Mode 2 (auto reload) to create
the delay.
Solution : // The program is tested for DS89C420, XTAL = 11.0592
MHz using the preview 32 compiler.
# include < reg51.h >
void T0M2 Delay (Void);
Sbit mybit = P1^3;
void main (Void)
{
unsigned char x, y;
while (1)
{
mybit = − mybit; // toggle P1.3
++
for (x = 0; x = 250; X ) // due to for loop
overhead
for (y = 0; y = 36; Y++) // put 36
T0M2 Delay ( );
}
}
void T0M2 Delay (Void)
{
TMOD = 0×02; // timer 0, Mode 2
TMOD = − 23; // load TH0
TR0 = 1; // turn ON T0
while (TF0 = = 0); // wait for TF0 to roll over
TR0 = 0; // turn OFF T0
TF0 = 0; // clear TF0
}
256 − 23 = 233
23 × 1.085 µs = 25 µs
25 µs × 250 × 40 = 250 ms
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 3-14 Timer/Counter, Interrupts

Example 3.6 : A switch is connected to P1.5. Write an 8051 C


program to monitor SW and create the following frequencies on P1.7 :
(a) SW = 0 500 Hz
(b) SW = 1 750 Hz, use Time 0, Mode 1 for both of them.
Solution : // This program is tested for AT89C51 XTAL = 11.0592
MHz, using the preview 32 compiler.
# include < reg51.h >
Sbit my bit = P1^5;
Sbit SW = P1^7;
void T0M1 Delay (unsigned char);
void main (Void)
{
SW = 1; // make P1.7 an input
while (1)
{
mybit = − mybit; // toggle P1.5
If (SW = = 0) // check switch
T0M1 Delay (0);
else
T0M1 Delay (1);
}
}
void T0M1 Delay (unsigned char C)
{
TMOD = 0×01;
If (C = = 0)
{
TL0 = 0×67; // FC67
TH0 = 0×FC;
}
else
{
TL0 = 0×9A;
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 3-15 Timer/Counter, Interrupts

TH0 = 0×FD; // FD9A


}
TR0 = −1;
while (TF0 = = 0);
TR0 = 0;
TF0 = 0;
}
FC67H = 64615
65536 − 64615 = 921
921 × 1.085 µs = 999.285 µs
1
= 500 Hz
(999.285 × 2)
Example 3.7 : Write a C program to generate square wave on P2.2
having 2500 Hz frequency and 70% duty cycle. XTAL = 12 MHz.
Solution : Frequency given = 2500 Hz
1 1
T = = = 0.004 sec = 400 µ sec.
F 2500
Given duty cycle = 70%
TON
Duty cycle =
TTotal
TON
∴ 70% =
400 µ sec
∴ TON = 0.7 × 400 µ sec
∴ TON = 280 µ sec
∴ TTotal = TON + TOFF
∴ TOFF = TTotal + TON
TOFF = 400 − 280 = 120 µ sec
1
Clock frequency = × Crystal frequency
12
1 6 6
∴ F = × 12 × 10 = 1 × 10 MHz
12
1 1
∴ Time = = 6 = 1 µ sec
F 1 × 10
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 3-16 Timer/Counter, Interrupts

∴ Number of clock pulses required for TON = 280 µ sec are


280 µ sec
= 280 clock pulses
1 µ sec
Value to the loaded into timer register is
= 65535 − 280 + 1
= 65256
= (FEE8)H
∴ TH1 = (0FE)H
TL1 = (0E8)H
Number of clock pulses required for TOFF are 120 µ sec.
120 µ sec
= = 120
1 µ sec
∴ Value to be loaded into time-register
= 65535 − 120 + 1 = 65416 = (FF88)H
∴ TH1 = (0FF)H
TL1 = (088)H
TMOD = 00010000 = 10 H
Program :
#include <reg 51.h>
sbit F1 = P2^3;
void TON (void);
void TOFF (void);
void main (void)
{
while (1)
{
F1 = 1;
TON ( );
F1 = 0;
TOFF ( );
}
}
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 3-17 Timer/Counter, Interrupts

void TON ( )
{
TMOD = 0×10;
TL1 = 0×8E;
TH1 = 0×EF;
TR1 = 1;
while (TF1 == 0);
TR1 = 0;
TF1 = 0;
}
void TOFF ( )
{
TMOD = 0×10;
TH1 = 0×FF;
TL1 = 0×88;
TR1 = 1;
while (TF1 == 0);
TR1 = 0;
TF1 = 0;
}
C Programming of Timer 0 and Timer 1 as a Counter :
• As discussed in earlier section, in counter programming, how
timer 0 and 1 is used as event counter.
• A timer can be used as a counter if pulses are provided from
outside the chip instead of using the frequency of the crystal
oscillator as the clock source.
• By feeding pulse to the T0 (P3.4) and T1 (P3.5) pins, Timer 0 and
Timer 1 are turned into Counter 0 and Counter 1, respectively.
• The following examples illustrate about the Timer 0 and Timer 1,
programmed as counters using the C language.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 3-18 Timer/Counter, Interrupts

SOLVED EXAMPLES

Example 3.8 : Assume that 1 Hz external clock is being fed into pin
T0 (P3.4). Write C program for Counter 0 in Mode 1, to count the pulse
and display the TH0 and TL0 register on P2 and P1 respectively.
Solution :

8051
P1 P1
and and P2 to LED
P2

1 Hz P3.4

T0
Fig. 3.12
# include < reg51.h >
void main (Void)
{
T0=1; // make T0 an input
TMOD = 0×05; // mode 1
TL0 = 0; // set count to 0
TH0 = 0; // set count to 0
while (1) // repeat forever
{
do
{
TR0 = 1; // start timer
P1 = TL0; // place value in pins
P2 = TH0;
}
while (TF0 = = 0); // Wait here
TR0 = 0; // Stop timer
TF0 = 0;
}
}
Example 3.9 : Assume that 1 Hz external clock is being fed into pin
T1 (P3.5). Write C program for counter 1 in mode 2, (8-bit auto-reload) to
count up and display the state of the TL1 count on P1, start the count at
0H.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 3-19 Timer/Counter, Interrupts

Solution :
# include < reg51.h >
Sbit T1 = P3^5;
void main (Void)
{
T1 = 1; // make T1 an input
TMOD = 0×60; // select mode 2
TH1 = 0; // set count to 0
while (1) // repeat forever
{
do
{
TR1 = 1; // start timer
P1 = TL1; // place value in pins
}
while (TF1 = = 0); // wait here
TR1 = 0; // stop timer
TF1 = 0; // clear flag
}
}

8051

P To LED's

1 Hz P3.5

T1
Fig. 3.13
Example 3.10 : Assume that 60 Hz external clock is being fed into
pin T0 (P3.4). Write a C program for a Counter 0 in Mode 2 (8-bit auto
reload) to display the seconds and minutes on P1 and P2, respectively.
Solution :
# include < reg51.h >
void T0 time (unsigned char);
void main ( )
{
unsigned char val;
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 3-20 Timer/Counter, Interrupts

T0 = 0
TMOD = 0×06; // T0, mode 2 counter
TH0 = − 60; // sec = 60 pulse
while (1)
{
do
{
TR0 = 1;
Sec = TL0;
T0 Time (val);
}
while (TF0 = = 0);
TR0 = 0;
TF0 = 0;
}
}
void T0 time (unsigned char val)
{
unsigned char sec, min;
min = value/60;
sec = value % 60;
P1 = sec;
P2 = min;
}

8051
P1
and P1 and P2 to LED's
P2

P3.4

60 Hz T0
Fig. 3.14
Example 3.11 : Assume that 2 Hz external clock is being fed into pin
T1 (P3.5). Write C program for Counter 0 in Mode 2, to display the count
in ASCII. The 8-bit binary count must be converted to ASCII. Display the
ASCII digits on P0, P1 and P2 where P0 is the least significant digit. Set
the initial value of TH0 to 0.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 3-21 Timer/Counter, Interrupts

Solution : We must convert 8-bit binary data to ASCII to display the


TL count. The ASCII values will be shown in binary. For example, ‘5’ will be
shown as 00110101 on port.
# include < reg51.h >
void Bin T0 ASCII (unsigned char);
void main ( )
{
unsigned char value;
T1 = 1;
TMOD = 0×06;
TH = 0;
while (1)
{
do
{
TR0 = 1
Value = TL0;
Bin T0 ASCII (value);
}
While (TF0 = = 0);
TR0 = 0;
TF0 = 0;
}
}
void Bin T0 ASCII (unsigned char value)
{
unsigned char x, d1, d2, d3;
x = value/10;
d1 = value % 10;
d2 = x % 10;
d3 = x/10;
P0 = 30/d1;
P1 = 30/d2;
P2 = 30/d3;
}
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 3-22 Timer/Counter, Interrupts

3.3 Serial Data I/O Transfer


• Microcontroller must be able to communicate with other compatible
multiprocessor distributed systems. One of the cost effective way to
communicate is to send and receive data bit serially.
• When the microcontroller communicates with the outside world, it
sends a data in byte size format. In some cases such as printer an 8-
bit data is sent by processor and the complete 8-bit data is received
by the printer. This kind of communication is called as Parallel data
transfer.
• But it no longer works for large distance because long cables
diminished and even signal gets distorted. Also 8-bit path/cable is
expensive. For these reasons, serial communication is used for data
transfer for long distance.

Parallel transfer
D0
Serial
transfer
Sender Receiver
Sender Receiver

D7

Fig. 3.15 : Comparison between serial and parallel data transfer


• In serial communication, a single data line used makes it much
cheaper.
• For serial data communication, a byte of data must be connected to
serial bits using parallel in serial out shift register and then it can be
transferred over a single line. This also means that the receiving end
must have serial in parallel out shift register to receive the serial data
and connect it into the byte form.
• Serial data communication uses two methods, asynchronous and
synchronous. The synchronous method transfers a block of data
(character) at a time while asynchronous communication transfers a
single byte at a time.
• It is possible to write software for both methods but it will be very
tedious job. So to make it simple, special IC’s are manufactured
referred as an UART (Universal Asynchronous Receiver Transmitter)
and USART (Universal Synchronous Asynchronous Receiver
Transmitter).
• The 8051 has a built-in UART reflected as a serial port of 8051.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 3-23 Timer/Counter, Interrupts

Data Processing in Asynchronous Serial Communication :


• The data is transferred in the form of 0’s and 1’s.
• It is difficult to make sense of data unless the sender and receiver
agrees on some rules called as protocol on how data is packed, how
many bits constitute a character and when data begins and ends.
• Start and stop bits are widely used in asynchronous data transfer,
for character transmission, while the block transmission uses
synchronous method.
• In an asynchronous transmission, a character is placed between start
and stop bits. This is called as framing.
• The start bit is always one bit, but the stop bit can be one or two bits.
• The start bit is always ‘0’ (low) and stop bit (s) is 1 (high) e.g. ASCII ‘A’
(0100 0001) is framed as shown in diagram. In data transmission,
always LSB is sent out first as shown in Fig. 3.16.

Stop 0 1 0 0 0 0 0 1 Start
Space bit bit Mark

d7 d0
Goes out last Goes out first
Fig. 3.16 : Data to be transferred
Data Transfer Rate :
• The rate of data transfer is stated in bps (bits per second) referred as
baud rate. However, the baud and bps rates not necessary always
equal. The data transfer rate of a given system depends on
communication port used in that system.
8051 Serial Port Communication :
• The 8051 has one serial port.
• Port pins P3.0 (RXD) and 3.1 (TXD) used to receive and transmit the
data respectively.
• The data is transmitted through a register called as SBUF.
• SBUF is an 8-bit register used for serial communication in 8051.
• For a byte of data to be transferred via the TXD line, it must be
placed in the SBUF register. Similarly, SBUF holds the byte of data
when it receives by the RXD line of 8051. SBUF register can be
accessed like any other register in 8051.
• For example, MOV SBUF, # ‘A’ will load the (41H) ASCII character ‘A’
in the SBUF register.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 3-24 Timer/Counter, Interrupts

3.3.1 SCON (Serial Control) Register


• The SCON register is an 8-bit register used to control data
communication, means it can program the start bit, stop bit, and
data bits of data framing.
SM0 SM1 SM2 REN TB8 RB8 TI RI
Fig. 3.17 : SCON serial port control register (bit addressable)
SM0 SCON.7 Serial port mode specified.
SM1 SCON.6 Serial port mode specified.
SM2 SCON.5 Used for multiprocessor communication (Make it
0).
REN SCON.4 Set/cleared by software to enable/disable
reception.
TB8 SCON.3 Not widely used.
RB8 SCON.2 Not widely used.
TI SCON.1 Transmit interrupt flag. Set by hardware at the
beginning of the stop bit in mode 1. Must be
cleared by software.
RI SCON.0 Receive interrupt flag. Set by hardware halfway
through the stop bit time in mode 1. Must be
cleared by software.
Note : Make SM2, TB8 and RB8 = 0
• This is an 8-bit register used to program the start bit, stop bit and
data bit of data framing.
SM0, SM1 : These bits are used to determine the forming of data by
specifying the number of bits per character and start and stop bits.
Table 3.1
SM0 SM1 Mode Description Baud Rate
0 0 0 Shift register f/12
0 1 1 8-bit UART Variable
1 0 2 9-bit UART f/64 or f/32
1 1 3 9-bit UART Variable
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 3-25 Timer/Counter, Interrupts

SM2 : This bit enables multiprocessing capability of 8051. In mode 2


and 3 if this bit is set, it will enable multiprocessor communication.
REN : When this bit is high, it allows 8051 to receive data on the RxD
pin of 8051. That means if we want to have both transmit and receive
data REN must be 1.
If REN = 0 receiving is disabled.
TB8 (Transmit bit 8) : Use in mode 2 and 3.
RB8 (Receive bit 8) : In mode 1, this bit get a value of stop bit when
th
8-bit data is received. In mode 2 and 3, it is the 9 data bit that was
received and will be stored in RB8. In mode 0 it is not used.
TI (Transmit interrupt flag) : This is very important bit in SCON
register. When 8051 finishes the data it sets this flag to 1, indicating
that it is ready for another byte. This bit is set at the beginning of
stop bit.
RI (Receive interrupt flag) : When 8051 receives data serially RXD it
gets rid of the start and stop bits and places the byte in the SBUF
register. Then it sets the RI flag to indicate byte has been received
and should be picked up before lost. It is set halfway through the
stop bit.
3.3.2 Data Transmission
• Transmission of serial data bits begins at anytime data is written
to SBUF.
• T1 is set to ‘1’ when data has been transmitted and signifies
SBUF is empty and that another byte can be sent. If the program
fails to wait for the T1 flag and overwrites SBUF while a previous
data byte is in the process of being transmitted, the result will be
unpredictable.
3.3.3 Data Reception
• Reception of serial data will begin if the REN bit (receive enable)
in SCON is set to 1 for all modes. In addition for mode 0 only, RI
must be cleared to 0. Receiver Interrupt flag RI is set after data
has been received in all modes.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 3-26 Timer/Counter, Interrupts

• Setting REN is the only direct program control that limits the
reception of unexpected data, the requirement that RI also be ‘0’
for mode 0. It prevents the reception of new data until the
program has dealt with the old data and reset RI.
3.3.4 Serial Data Transmission Modes
The 8051 designer has included four modes of serial data
communication. Modes are selected by programmer by setting the
mode bits SM0 and SM1 in SCON.
Mode 0 :
• Referred as shift register mode.
• Only RXD is the pin used for both to send the data and to receive
the data.
• TXD pin is used to provide the shift clock pulses. The shift
frequency or the baud rate is fosc/12, where fosc is the clock
frequency.
Mode 1 :
• It is a standard 8-bit UART mode.
• In mode 1, 10-bits are transmitted through TXD and received the
RXD pin.
• There is one start bit, 8 data bits and one stop bit.
• Baud rate is variable generated by timer. Interrupt flat T1 is set
once all 10-bits have been sent. LSB outs first.
smod
2 OSC freq.
Baud Rate = ×
32d 12d × (256d − (TH))
For example, if TH1 = 230 D, SMOD = 0 and OSC freq. (fosc) = 12 Hz
then BR = 1204 i.e. 12 k approx.
Shift data out
S6P2

RXD D0 D1 D2 D3 D4 D5 D6 D7 External data bits shifted out


data out
1 2 3 4 5 6 7 8

TXD
clock 1 2 3 4 5 6 7 8
S3P1 S6P1 External data bits shifted in
RXD D0 D1 D2 D3 D4 D5 D6 D7
data in
S5P2
Shift data in

Fig. 3.18 : Shift register mode


S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 3-27 Timer/Counter, Interrupts

SMOD is control bit in PCON register and can be 0 or 1. In case of


smod
2
timer, 1 is in mode 1. Then, Baud rate = × (Timer 1 overflow
32d
freq.)
To receive the data in mode 1, RI bit in SCON is tested for 1 and REN
must be 1 in SCON.
Mode 2 : 9-bit UART : 11-bits are transferred with 1 start bit, 8 data
th
bits, 9 -bit and 1 stop bit.
th
Stop bit 9 bit MSB … LSB Start bit = 0
(1)
→
th
The 9 -bit is programmable. User can define it in TB8 of SCON. It
th
may be a parity of data byte. On reception, this 9 bit goes into RB8
in SCON.
fosc fosc
Baud rate in mode 2 is either or
64 32
Mode 3 : It is same as mode 2, only BR is variable in this mode.
PCON Register :
• Before going for multiprocessor communication, PCON register is
discussed here.
SMOD − − − GF1 GF0 PD IDL
Fig. 3.19 : PCON register
SMOD : Double the BR. If timer 1 is used to generate BR and
SMOD = 1 then BR is double in serial port mode 1, 2 or 3.
−
− not implemented for future use.
−
GF1
 General purpose bits.
GF0
PD : Power down bit. Setting this bit activates power down operation
in 8051.
IDL : Idle mode bit. Setting this bit activates IDLE mode operation in
8051.
• If there is a requirement of communication among many 8051
devices, SM2 bit of SCON register is used. It is supported in mode 2
and 3 only. Again 9-bits are transmitted or received. In case of
multiprocessor communication the controlling processor is a master
processor and all other are slave processors.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 3-28 Timer/Counter, Interrupts

• When master wants to send data to the slave, first it sends the
address byte to slave. How to distinguish between address byte and
th
data byte? The 9 -bit while transmitting the address byte, does this.
th
In case of address byte the 9 -bit is 1 and for data byte it is 0.
• For multiprocessor, SM2 bit of all slaves will be set to 1. Once master
sends an address byte, each slave will compare it with it’s own
address byte. If it is not its own address it is sent to next slave till the
specified slave is found. Once that is done, the addressed slave will
clear its SM2 bit and receive data bytes. Other slave will continue
their own operation.

3.4 Interrupts
• 8051 supports 5 vectored interrupts. These are external interrupt 0,
external interrupt 1, timer/counter interrupt 0, timer/counter
interrupt 1 and serial port interrupt.
3.4.1 Interrupt Execution in 8051
• Whenever an interrupt occurs in 8051 microcontroller, it finishes the
execution of the current instruction. Then it pushes the PC contents
onto the stack which is nothing but address of next instruction to be
executed. Vectored address means the address of interrupt service
routine. The vectored address of that particular interrupt is loaded
into PC. And hardware clears the flag related to that interrupt.
• In 8051 these flags are IE0, IE1, TF0, TF1, RI and TI. The program
starts executing from vector location. Each ISR ends with RETI
instruction. As only 8 bytes are given to each interrupt, it contains
jump instruction at that location. And actual ISR is stored elsewhere
in the memory. (The table 3.2 shows the vector address for each
interrupt).
• Once RETI is executed, the stack contents are poped into program
counter due to which program resumes the execution from where it
was interrupted.
Table 3.2
Interrupt Vector Location Priority
External hardware interrupt INT0 00003 H Highest
Timer 0 interrupt (TF0) 000BH
External hardware interrupt 00013 H
Timer 1 interrupt 0001BH
Serial com interrupt (RI and TI) 0023 H Lowest
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 3-29 Timer/Counter, Interrupts

• In case if two interrupts occurs at the same time then they will be
sensed according to default priority as shown in table. Default
priority can be changed using one of the SFR called as IP.
3.5 SFRS for Interrupt Structure of 8051
3.5.1 Enabling and Disabling Interrupts
• On reset, all interrupts are disabled. The interrupt must be enabled
by software. It is possible with the SFR register called as IE (Interrupt
enable).
• EA is a kind of global enable for all interrupts. If it is 1 then only
individual interrupts can be handled. For any interrupt the EA bit and
corresponding interrupt bit must be 1.
• ET2 is the interrupt for timer 2 which is applicable in case of 8052
only.
External Interrupts : There are two external interrupts which are
enabled with these two flags.
D7 D0
EA … ET2 ES ET1 EX1 ET0 EX0
Fig. 3.20 : IE (Interrupt enable register)
EA IE.7 Disables all interrupts. If EA = 0; no interrupt is
acknowledged.
If EA = 1, each interrupt source is individually
enabled or disabled by setting or clearing its enable
pin.
− IE.6 Not implemented, reserved for future use.*
ET2 IE.5 Enables or disables timer 2 overflow or capture
interrupt (8952).
ES IE.4 Enables or disables the serial port interrupt.
ET1 IE.3 Enables or disables timer 1 overflow interrupt.
EX1 IE.2 Enables or disables external interrupt 1.
ET0 IE.1 Enables or disables timer 0 overflow interrupt.
EX0 IE.0 Enables or disables external interrupt 0.
* User software should not write 1s to reserved bits. These bits may be used in
future flash microcontrollers to invoke new features.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 3-30 Timer/Counter, Interrupts

Timer Flag Interrupt : (ET0, ET1) : When a timer/counter overflows,


the corresponding timer flag TF0 or TF1, is set to 1. This flag is set to
0 when the resulting interrupt generates a program call to the
appropriate timer subroutine in memory.
Serial Port Interrupt (ES) : If a data byte is received, an interrupt bit
RI is set to 1 in SCON register. When data byte is transmitted then TI
is set in SCON. They are ORed together to provide a single interrupt
to the processor: the serial port interrupt. These bits are not cleared
when interrupt is generated. The programmer should take care of
resetting them in order to access next byte.
 
External Interrupt (EX0 and EX1) : Pins INT0 and INT1 are used by
external circuitry. Inputs on these pins set the interrupt flags IE0 and
IE1 in TCON register. These are two types of external interrupt; edge
triggered or level triggered. It is decided by TCON register itself. In
this case, flags IEx will be reset when interrupt is serviced for edge
triggered interrupt. While for level triggered interrupt, it is
responsibility of programmer to take out the low level before RETI is
executed.
Reset : It is considered to be the ultimate interrupt. It is type of non-
maskable interrupt. Unlike other interrupts, the PC is not stored for
later program resumption. It is set to 0000H.
3.5.2 Setting Interrupt Priority
Interrupt :
• In case if two interrupts occurs at the same time then higher priority
interrupt will get serviced first and then lower priority interrupt will
be serviced. It is possible to change the interrupt priority using SFR
called as IP (Interrupt Priority). IP register bits determine if any
interrupt is to have high or low priority. Bit set to 1 gives interrupt a
high priority and 0 is low priority.
• If the two interrupts with the same priority occurs then they are
handled in the following ranking :
(1) IE0
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 3-31 Timer/Counter, Interrupts

(2) TF0
(3) IE1
(4) TF1
(5) Serial interrupt
D7 D0
− − PT2 PS PT1 PX1 PT0 PX0
Priority bit = 1 assigns high priority. Priority bit = 0 assigns low priority.
Fig. 3.21 : Interrupt Priority (IP) register (Bit-addressable)
− IP.7 Reserved
− IP.6 Reserved
PT2 IP.5 Timer 2 interrupt priority bit (8052 only)
PS IP.4 Serial port interrupt priority bit
PT1 IP.3 Timer 1 interrupt priority bit
PX1 IP.2 External interrupt 1 priority bit
PT0 IP.1 Timer 0 interrupt priority bit
PX0 IP.0 External interrupt priority bit
* User software should never write 1s to unimplemented bits, since they may
be used in future products.

3.6 Programming Serial Port Without Interrupt


• The 8051 has a serial data communication circuit that uses register
SBUF to hold data.
• Register SCON controls data communication, register PCON controls
data rates and pins RXD (P3.0) and TXD (P3.1) connect to the serial
data network.
• SBUF are physically two registers. One is to write only and is used to
hold data to be transmitted out of the 8051 via TXD. The other is
ready only and holds received data from external sources via RXD.
• There are four programmable modes for serial data communication
such as:
(1) Serial data mode 0 (Shift register mode).
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 3-32 Timer/Counter, Interrupts

(2) Serial data mode 1 (Standard UART).


(3) Serial data mode 2 (Multiprocessor mode).
(4) Serial data mode 3 (Identical to mode 2).
Serial Data Interrupts :
• Serial data communication is a relatively slow process, occupying
many milliseconds per data byte to accomplish. The serial data flags
in SCON, TI and RI are set.
• Whenever, a data byte is transmitted (TI) or received (RI). These flags
are ORed together to produce an interrupt to the program. The
program must read these flags to determine which caused the
interrupt and then clear the flag.
Interrupts :
• A computer program has only two ways to determine the conditions
that exist in internal and external circuits.
• One method uses software instructions that jump to sub-routines
on the states of flags and port pins.
• The second method responds to hardware signals called interrupts
that force the program to call a subroutine.
• Interrupts may be generated by internal chip operations or provided
by external sources.
3.7 8051 Interrupts
• The difference between polling and various interrupts of 8051 is
discussed below.
3.7.1 Interrupts and Polling
• Microcontroller can serve several devices using two ways; interrupts
and polling.
• In the interrupt method, whenever any device needs service, the
device notifies the microcontroller by sending an interrupt signal.
Upon receiving an interrupt signal, the microcontroller stops
whatever it is doing and serves the device.
• The program which is associated with the interrupt is called Interrupt
Service Subroutine (ISR).
• In polling, the microcontroller continuously monitor the status of a
given device, when the condition met, it gives the service to that
device. After that, it again starts monitoring the next device until
everyone is serviced.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 3-33 Timer/Counter, Interrupts

• Polling is not efficient as far as microcontroller is concerned because


with the polling, it is not possible to assign priority since it checks all
devices in a round robin fashion.
• In polling method, microcontroller cannot ignore (mask) a device
request for service. So, with the polling method microcontroller’s
important time is wasted on the devices they do not need service.
• The advantage with the interrupt method is that, microcontroller can
serve many devices and each device can get the attention of the
microcontroller based on the priority assigned to it.
• More importantly, in the interrupt method, the microcontroller can
ignore (mask) the device request for service.
3.7.2 Interrupt Service Routine (ISR)
• For every interrupt, there must be an Interrupt Service Routine (ISR).
When an interrupt is invoked, the microcontroller runs the interrupt
service routine.
• For every interrupt, there is a fixed location in memory that holds the
address of its ISR.
• The group of memory locations set aside to hold the addresses of
ISRs is called interrupt vector table (IVT) as shown in Table 3.3.
Table 3.3 : Interrupt vector table for the 8051
Interrupt ROM Pin Flag Clearing
Location
(HEX)
Reset 0000 9 Auto
External hardware interrupt 0003 P3.2 Auto
0 (INT0) (12)
Timer 0 interrupt (TF0) 000B Auto
External hardware interrupt 0013 P3.3 Auto
1 (INT1) (13)
Timer 1 interrupt (TF1) 001B Auto
Serial COM interrupt (RI and 0023 Programmer
TI) clears it
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 3-34 Timer/Counter, Interrupts

3.7.3 Interrupts Available in 8051


• In reality, five interrupts are available to the user, but in
manufacturer’s data sheet there are six interrupts mentioned, Reset is
also included in the interrupt list.
• These six interrupts are allocated as follows:
1. Reset : When reset pin is activated, the 8051 jumps to address
location 0000. This is a power up reset.
2. Timer : Two interrupts are set aside for the timers one for timer
0 and one for timer 1. Memory location 000BH and 001BH in the
interrupt vector table belong to timer 0 and timer 1, respectively.
3. External Hardware interrupt: Two interrupts are set aside for
hardware external interrupts. Pin number 12 of 8051 is (P3.2) and
pin number 13 is (P3.3) used as external hardware interrupts
INT0 and INT1 respectively. These external interrupts are also
referred as EX1 and EX2. Memory locations 0003H and 0013H in
the interrupt vector table are assigned to INT0 and INT1
respectively.
4. Serial communication : Serial communication has a single
interrupt that belongs to both receive and transmit. The interrupt
vector table location 0023H belongs to this interrupt.
• As per the above Table 3.3, limited number of bytes are set aside for
each interrupt. For example, a total of 8-bytes from location 0003 to
000A are set aside for INT0, external hardware interrupt 0. Similarly,
8-bytes from location 000B to 0012H are reserved for TF0, timer 0
interrupt.
3.7.4 Interrupt Execution Steps
In the execution of an interrupt following steps are followed:
1. Firstly, it finishes the current instruction execution and save the
address of the next instruction (PC) on the stack.
2. It also saves the current status of all the interrupts internally.
3. It jumps to a fixed location in memory called the interrupt vector
table that holds the address of the interrupt service routine.
4. The microcontroller gets the address of the ISR from the
interrupt vector table and jumps to it.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 3-35 Timer/Counter, Interrupts

5. It starts to execute the interrupt service subroutine until it


reaches the last instruction of subroutine, which is RETI (return
from interrupt).
6. After execution of RETI instruction, the microcontroller returns to
the place where it was interrupted. First it gets the Program
Counter (PC) address from the stack by popping the top two
bytes of the stack and then it starts executing from that address.
3.7.5 Enabling and Disabling an Interrupt
• When the system is reset, all interrupts are disabled (masked) and
none will respond to the microcontroller, if they are activated.
• The interrupt must be enabled by software in order for the
microcontroller to respond to them.
• A register IE (Interrupt Enable) is responsible for enabling and
disabling interrupt. The structure of IE is already discussed in
previous Chapter.
3.8 Programming Timer Interrupts
• The use of timer 0 and timer 1 were discussed in previous section.
• The Timer Flag (TF) is raised when the timer rolls over.
• Instruction (JNB TF, Target) is used to monitor TF.
• In polling TF, one has to wait until the TF is raised and this process
waste valuable time of microcontroller. This problem is solved by
using interrupt.
• If the timer interrupt in the IE register is enable, whenever the timer
rolls over, then TF is raised and the microcontroller is interrupted in
whatever it is doing and jumps to the interrupt vector table to service
the ISR.
• In this way, the microcontroller can do other things until it is notified
that the timer has rolled over. It is illustrated below with the help of
proper example.
SOLVED EXAMPLES

Example 3.12 : Write a program that display the value of ‘M’ and ‘N’
at port 0 and port 2 respectively and also generates a square wave of 10
kHz with timer 0 in mode 2 at port pin P1.2, XTAL = 22 MHz.
Solution : This program is tested for AT89C51 with XTAL = 22 MHz.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 3-36 Timer/Counter, Interrupts

; ….. upon wake up, go to main, avoid using memory


space allocated to interrupt vector table.
ORG 0000H
LJMP MAIN ; bypass interrupt
vector table
; ….. ISR for timer 0 to generate square wave
ORG 000BH ; timer 0 interrupt
vector
CPL P1.2
RETI
; ….. the main program for initialization
ORG 0030H ; a location after
interrupt vector
MAIN : MOV TMOD, #02H ; timer 0 mode 2
MOV TH0, #0B6 ; more count value
into TH0
MOV IE, #82H ; enable interrupt
timer 0
SETB TR0 ; start timer 0
BACK : MOV P0, #‘M’ ; display ‘M’ at
port 0
MOV P2, #‘N’ ; display ‘N’ at
port 2
SJMP BACK ; keep doing until
interrupted
END
TF0 Timer 0 interrupt vector

1 000BH
Jump to

TF1 Timer 1 interrupt vector

1 001BH
Jump to
Fig. 3.22 : TF interrupt
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 3-37 Timer/Counter, Interrupts

Important points about the above program Example 3.12 are:


1. Avoid using the memory space allocated to the interrupt vector
table. Place all the initialization code in memory starting at 30H.
2. The LJMP instruction is the first instruction that the 8051 execute
when it is powered up. LJMP redirects the controller away from
the interrupt vector table.
2. The ISR for timer 0 is located starting at memory location 000BH
since it is small enough to fit the address space allocated to the
interrupt.
3. We enabled the timer 0 interrupt with “MOV IE, #10000010B” in
MAIN.
4. While the P0 data is brought in and issued to P1 continuously.
5. Whenever, timer 0 is rolled over, the TF0 flag is raised and the
microcontroller gets out of the “BACK” loop and goes to 0000BH
to execute the ISR associated with timer 0.
5. In the ISR for timer 0, notice that there is no need for “CLR TF0”
instruction before RETI instruction. This is because the 8051
clears the TF flag internally upon jumping to the interrupt vector
table.
Example 3.13 : Write a program to generate square wave one of
5 kHz at pin P1.2 and another frequency of 25 kHz at P2.3.
Assume XTAL = 22 MHz

P1.2 5 kHz

8051

P2.3 25 kHz

Fig. 3.23
Solution : This program is tested for AT89C51 with a crystal
frequency of 22 MHz.
ORG 0000H
LJMP MAIN
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 3-38 Timer/Counter, Interrupts

; ….. ISR for timer 0


ORG 000B ; interrupt vector for
timer 0
CPL P1.2
RETI
; ….. ISR for timer 1
ORG 001BH ; interrupt vector for
timer 1
CPL P2.3
RETI
; ….. main program for initialization
ORG 0030H
MAIN : MOV TMOD, #22H ; mode 2
MOV IE, #8AH ; enable timer 0 and
timer 1
MOV TH0, #048H ; count value for 5
kHz
MOV TH1, #0B6H ; count value for 25
kHz
SETB TR0 ; start timer 0
SETB TR1 ; start timer 1
WAIT : SJMP WAIT ; keep waiting for
roll off either
timer
END
Example 3.14 : Write a program to toggle pin P1.2 every second.
Solution : To get a large delay of 1 second, we use a register in
addition to a timer. Here register R0 is used along with timer 1 to get
large time delay.
ORG 0000H ; bypass interrupt
vector table
LJMP MAIN
; ….. ISR for timer 1 to generate square wave
ORG 001BH
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 3-39 Timer/Counter, Interrupts

DJNZ R0, START


CPL P1.2 ; toggle P1.2
MOV R0, #28 ; reload register
MOV TL1, #00H ; reload counter
MOV TH1, #00H ; reload counter
START : RETI
; ….. the main program initialization
ORG 0030H
MAIN : MOV TMOD, 10H ; timer 1 mode 1
MOV IE, #88H ; enable timer 1
interrupt
MOV R0, #28H
MOV TL1, 00H
MOV TH1, #00H
SETB TR1
HERE : SJMP HERE

3.9 Programming External Hardware Interrupts


• The 8051 has two external hardware interrupts i.e. INT0 and INT1.
• After activation of these pins, the 8051 gets interrupted and jumps to
the vector table to execute the interrupt service routine.
• In this section, two external hardware interrupts of the 8051 with
some examples are described.
External Interrupts INT0 and INT1 :
• The interrupts INT0 and INT1 are located at P3.2 and P3.3 with the
vector table locations 0003H and 0013H. These interrupts can be
enabled and disabled using IE register.
• There are two types of activation for the external hardware interrupts:
1. Level triggered interrupt.
2. Edge triggered interrupt.
1. Level Triggered Interrupt :
• In this type of interrupt, INT0 and INT1 pins are normally high
and if low level signal is applied to them, it triggers the interrupt.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 3-40 Timer/Counter, Interrupts

Then the microcontroller stops execution and jumps to the


vector table to give service to that interrupt. This is called as a
level activated interrupt or level triggered and is the default
mode upon reset of the 8051.
• The low level signal at the INT pins must be removed before the
execution of the last instruction of the interrupt service routine,
RETI, otherwise another interrupt will be generated.
• In other words, if the low levels interrupt signal is not removed
before ISR is finished; it is interpreted as another interrupt and
the 8051 jumps to the vector table to exercise the ISR again.
• Activation of INT0 and INT1 is illustrated in Fig. 3.24.
Level triggered

0
INT0 0003 H
P (3.2) 1

IE0
Edge triggered ICON.1

(a)
Level triggered

0
INT0 0013 H
P (3.2) 1

IE1
Edge triggered TCON.3

(b)
Fig. 3.24 : Activation of INT0 and INT1
SOLVED EXAMPLE

Example 3.15 : Two switches are connected to P3.2 and P3.3, when
switch is pressed, the corresponding line goes low, write a program to
1. Light all LEDs connected to port 0 if the first switch is pressed.
2. Light all LEDs connected to port 2, if second switch is pressed.
Solution : Whenever, the switch on INT1 (P3.3) is activated, the
microcontroller gets out of the loop and jumps to vector location 0013H.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 3-41 Timer/Counter, Interrupts

The ISR for INT1 turns ON the LED, keeps it ON for a while and turns it
OFF before returns. If by the time it execute the RETI instruction, the INT1
pin is still low, the microcontroller initiates the interrupt again. Therefore,
to end this problem, the INT1 pin must be brought back to high by the
time RETI is executed.
// The program is tested for AT89C51 with crystal frequency of 22 MHz.
; ….. upon wake up, go to main
ORG 0000H ; bypass interrupt
vector
LJMP MAIN
; ….. the ISR for interrupt INT0
ORG 0003H ; interrupt vector for
INT0
LED1 : MOV P0, #0FFH ; turn on LED of port 0
MOV R0, #255
DJNZ R0, LED1 ; keep the LED ON
RETI
; ….. the ISR for INT1
ORG 0013H ; interrupt vector for
INT1
LED2 : MOV P2, #0FFH ; turn ON LED of port 2
MOV R0, #255
DJNZ R0, LED2
RETI
; ….. main program for initialization
ORG 0030H
MAIN : MOV IE, #85H ; enable INT0 and INT1
LOOP : SJMP LOOP
END
2. Edge Triggered Interrupts :
• In the edge triggered interrupt, the external source must be held
high for at least one machine cycle and then held low for at least
one machine cycle to ensure that transition is seen by
microcontroller. To make the edge trigger interrupts, we must
program the bits of the TCON register.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 3-42 Timer/Counter, Interrupts

• We have already seen the framing of TCON register. The TCON.1


and TCON.3 bits hold the latch falling edge of INT0 and INT1
respectively. TCON.1 and TCON.3 are also called IE0 and IE1.
They function as interrupt in service flags.
• When interrupt in service flag is raised, it indicates that the
interrupt is being serviced and now there is no new interrupt on
this pin will be responded until this service is finished. This is just
like busy signal you get if calling a telephone number that is in
use.
• When the ISR are finished, TCON.1 (IE0) and TCON.3 (IE1) are
cleared, indicating that interrupt is finished and the 8051 is ready
to respond to another interrupt on that pin.
• For another interrupt to be recognized, the pin must go to logic
‘high’ state and brought back to ‘low’, to indicate an edge
triggering interrupt.
SOLVED EXAMPLE

Example 3.16 : Generate from all pins of port 0, a square wave,


which is half the frequency of the signal applied at INT0 (P3.2).
Solution : Every negative edge at pin 3.2 will cause INT0 (vector
location 0003) interrupt to be activated.
ORG 0000H
LJMP START
; ….. ISR for hardware interrupt INT0
ORG 0003H
CPL P0
RETI
ORG 0030H
START : SETB TCON.0 ; make INT0 edge
triggered interrupt
MOV IE, #8LH ; enable hardware
interrupt INT0
BACK : SJMP BACK
END
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 3-43 Timer/Counter, Interrupts

3.10 Programming the Serial Communication


Interrupts
• Mostly serial data communication uses polling method. In this
section, interrupt based serial communication is discussed.
• SBUF register is playing very important role with SCON register.
• Basically, SBUF register is used solely for serial communication in the
8051.
• When the SBUF register has a byte, RI is raised to indicate that the
received byte needs to be picked up before it is overrun (lost) by new
incoming serial data.
• In the interrupt method, when the 8051 has received a byte, or is
ready to send the next byte, other things can be done while the serial
communication needs are served.
• In the 8051, only one interrupt is set aside for serial communication.
This interrupt is used to both, send and receive data. If the interrupt
bit in the IE register (IE.4) is enabled, RI or TI is raised, the 8051 gets
interrupted and jumps to memory address location 0023H to execute
the ISR.
• Here the TI and RI flags must be examined to see which one cause
the interrupt and respond accordingly as shown in Fig. 3.25.

Fig. 3.25 : Single interrupt for TI and RI


SOLVED EXAMPLES

Example 3.17 : Write a program in which the 8051 reads data from
P2 and writes it to P1 continuously and copy of it is given to the COM
port. Assume that XTAL = 11.0592 MHz, set the baud rate at 4800.
Solution :
ORG 0000H
LJMP START
ORG 0023H
LJMP SERIAL ; jump to ISR
ORG 0030H
START : MOV P2, #OFF ; P2 as input port
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 3-44 Timer/Counter, Interrupts

MOV TMOD, #20 ; timer 1, mode 2


MOV TH1, #0F4 ; 4800 baud rate
MOV SCON, #50H ; 8-bit, 1 stop, REN
enable
MOV IE, #10010000B ; enable serial int.
SETB TR1 ; start timer 1
BACK : MOV A, P2 ; read from port 2
MOV SBUF, A ; give copy to SBUF
MOV P1, A ; send it to port 1
SJMP BACK ; stay in loop
; ….. serial port ISR
ORG 100H
SERIAL : JB TI, TRANS ; jump if TI is high
MOV A, SBUF ; otherwise receive
CLR RI ; clear RI
RETI ; return from ISR
TRANS : CLR TI ; clear TI
RETI ; return from ISR
END
In the above program, we get the role of TI and RI. When the byte is
written in SBUF, it is transferred serially. As a result, when the last bit
(stop bit) is transferred the TI is raised, which causes the serial interrupt
to be invoked since the corresponding bit in IE register is high.
Example 3.18 : Write a program in which 5-bytes of data stored in
RAM locations starting from 40H are transferred serially. At the end of
data transfer, the values of R0 are displayed on P1.
Solution :
ORG 0000H
LJMP MAIN
ORG 0023H ; jump to ISR
LJMP SERIAL
ORG 0030H
MAIN : MOV TMOD, #20H ; timer 1 in mode 2
MOV TH1, #0FAH ; decimal−6, baud
rate 9600
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 3-45 Timer/Counter, Interrupts

MOV SCON, #50H ; 8-bit 1 stop, REN


MOV IE, #90H ; serial interrupt
enable
SETB TR1 ; start timer 1
MOV R0, #05 ; counter for number
of byte
MOV R1, #40H ; R1 is pointer to
RAM
BACK : MOV A, @ R1 ; move data from RAM
to A
MOV SBUF, A ; data transmitted
into SBUF
DJNZ R0, BACK ; repeat till all
data is sent
HERE : SJMP HERE
; ….. serial port ISR
SERIAL : JNB TI, RECEIVE ; data received if or
MOV A, R0 ; if TI = 1, more R0
into A
MOV P1, A ; transfer to P1
CLR TI ; clear TI for next
transmission
RETI
MOV A, SBUF ; if reception, move
data to SBUF
CLR RI ; clear RI to enable
next
RETI
END
Clearing RI and TI before RETI Instruction :
• Before RETI instruction, in the program, RI and TI flags must be
cleared.
• This is necessary since there is only one interrupt for both receive
and transmit and 8051 does not know who has generated it.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 3-46 Timer/Counter, Interrupts

• Therefore, it is the job of the ISR to clear the flag.


• In serial communication, RI (or TI) must be cleared by the
programmer using software instructions such as “CLR TI” and “CLR
RI” in the ISR.
• The list of interrupt flag bit for the 8051 is given below.
Table 3.4 : Interrupt flag bits for 8051
Interrupt Flag SFR Register Bit
External 0 IE0 TCON.1
External 1 IE1 TCON.3
Timer 0 TF0 TCON.5
Timer 1 TF1 TCON.7
Serial port TI SCON.1
• Many jobs can be done simultaneously using interrupt facility
available in the microcontroller.
SOLVED EXAMPLE

Example 3.19 : Write a program for the following :


(a) Generate a square wave at P1.3 using timer 0 mode 1, interrupt
mode.
(b) Take data from port 2 and send it serially and continuously.
(c) When INT0 is activated, port 0 is made 0 for a short time, to
switch OFF the LEDs connected to it. The LED will also remain
OFF if the switch connected to INT0 (P3.2) is kept pressed.
Solution :

P1.3

Data on 8051 P0 To LEDs


P2

P3.2 Data

Fig. 3.26
ORG 0000H
LJMP MAIN
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 3-47 Timer/Counter, Interrupts

; ….. time 0 ISR


ORG 000BH ; ISR for timer 0
CPL P1.3 ; toggle pin P1.3
MOV TH0, #00H ; timer is in mode 1
RETI
; ….. INT0 interrupt vector
ORG 0003H ; ISR for INT0
SJMP LED
; ….. serial port interrupt vector
ORG 0023H
LJMP SERIAL
; ….. main program
ORG 0030H
MAIN : MOV P2, #0FFH ; make P2 an input
port
MOV TMOD, #21H ; timer 0 mode 1,
timer 1 mode 2
MOV TH1, #−6 ; baud rate
MOV TH0, #00H ; count loaded for T0
MOV TL0, #0F0 ; count loaded for T0
MOV SCON, #50H ; 8-bit 1 stop REN
enable
MOV IE, #93H ; timer 0 enable
SETB TR1 ; start timer 1
SETB TR0 ; start timer 0
BACK : MOV A, P2 ; move data P2 to A
MOV SBUF, A ; move data A to SBUF
SJMP BACK
; ….. serial port ISR
SERIAL : JNB T1, RECE ; if T1 is not high
CLR T1 ; if T1 = 1
RETI
RECE : MOV A, SBUF ; move data to A
CLR TI ; clear TI
RETI
; ….. ISR for INT0
LED : MOV P0, #00H ; move 0 to P0
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 3-48 Timer/Counter, Interrupts

MOV R0, #0FFH


HERE : DJNZ R0, HERE ; for delay
MOV P0, 0FFH ; light up LEDs again
RETI
END

3.11 Interrupt Priority in the 8051


• In this section, the concept of priority using proper examples is
illustrated.
• When the 8051 is powered up, the priorities are assigned according
to Table 3.5.
Table 3.5 : 8051 Interrupt priority upon reset
Highest to Lowest Priority
External Interrupt 0 INT0
Timer Interrupt 0 TF0
External Interrupt 1 INT1
Timer Interrupt 1 TF1
Serial Communication RI and TI
• From Table 3.5, for example, if external hardware interrupt 0 and 1
are activated at the same time, external interrupt 0 (INT0) is
responded first.
• After INT0 has been serviced, INT1 is serviced, since INT1 has the
lower priority.
• In reality, the priority scheme in the table is an internal polling
sequence in which the 8051 polls the interrupts in the sequence
listed in above table and respond accordingly.
• IP register (Interrupt priority) is used to set the priority with
programming.
• To give a highest priority to any of the interrupts, the corresponding
bit in IP register is made high.
• Recall the framing of IP as shown below:
D7 D6 D5 D4 D3 D2 D1 D0

− − PT2 PS PT1 PX1 PT0 PX0

Fig. 3.27 : IP (Interrupt priority) register


• If interrupts INT0, TF0 and INT1 are activated at the same time then
priorities are set by power up reset.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 3-49 Timer/Counter, Interrupts

• In certain situation, 8051 checks all five interrupts according to the


sequence listed in Table 3.5.
• If anyone is activated, it services it in sequence. Therefore, when the
above three interrupts are activated, IE0 (external interrupt 0) is
activated first, then timer 0 (TF0) and finally IE1 (external interrupt 1).
SOLVED EXAMPLES

Example 3.20 : Program the IP register to assign the highest priority


to INT1 (external interrupt 1).
Solution : MOV IP, # 00000100B
Bit number two i.e. IP.2 is assigned to INT1 higher priority. We can
use the alternate instruction for it as
“SETB IP.2”.
Example 3.21 : If IP contains 07h. Give the sentence of execution of
interrupts.
Solution : Sequence will be :
ext int0,
Timer 0,
ext int1,
Timer 1,
Serial communication
Example 3.22 : Discuss what happens if INT0, INT1 and TF0 are
activated at the same time. Assume that the interrupts are both edge
triggered and IP = 00000100B.
Solution : When INT0, INT1 and TF0 interrupts are activated at the
same time, the 8051 services INT1 first, then it services INT0, then TF0.
This is due to the fact that INT1 has higher priority than the other two
because of the instruction IP = 00000100B, makes INT0 and TF0 are low
and INT1 is high. As a result, the sequence in Table 3.5 is followed, which
gives higher priority to INT0 over TF0.
Example 3.23 : Assume that after reset, the interrupt priority is set
by the instruction
“MOV IP, # 00001100B”
Discuss the sequence in which the interrupts are serviced.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 3-50 Timer/Counter, Interrupts

Solution : With the instruction MOV IP, # 00001100, the external


interrupt 1 (INT1) and timer 1 (TF1) to the higher priority level compared
with rest of the interrupts. However, since they are all polled according to
priority table, they will have the following priority.
Source Priority Level
External interrupt 1 (INT1) Highest
Timer interrupt 1 (TF1)
External interrupt 0 (INT0)
Timer interrupt 0 (TF0)
Serial communication (RI and TI) Lowest
3.11.1 Interrupt Inside an Interrupt
• If the 8051 is executing an ISR belonging to an interrupt and another
interrupt is activated, in such case, high priority interrupt can
interrupt a low priority interrupt. This is referred as interrupt inside
interrupt.
• In the 8051, a low priority interrupt can be interrupted by higher
priority interrupt, but vice-versa is not allowed.
• Although all the interrupts are latched and kept internally, no low
priority interrupt can get the immediate attention of the CPU until
the 8051 has finished servicing the high priority interrupt.

3.12 Use of Timer to Select Baud Rate for


Serial Communication
• One of the 8051’s many powerful features is its integrated UART
which is known as a serial port.
• The values can be easily read or write to a serial port of 8051.
However, serial port’s operation mode and baud rate have to be
configured. Once they are configured, a value in SFR can be written
which can be read by serial port.
• The 8051 automatically finish the task of sending the character.
• To configure the baud rate of serial port, first each bit of SFR must be
studied which is given in below table.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 3-51 Timer/Counter, Interrupts

Table 3.6
Bit Name Bit Explanation of Function
Address
7 SM0 9Fh Serial port mode bit 0.
6 SM1 9Eh Serial port mode bit 1.
5 SM2 9Dh Mutliprocessor communications enable
(explained later)
4 REN 9Ch Receiver enable. This bit must be set in order
to receive characters.
th
3 TB8 9Bh Transmit bit 8. The 9 bit to transmit in
mode 2 and 3.
th
2 RB8 9Ah Receive bit 8. The 9 bit is received in mode
2 and 3.
1 TI 99h Transmit flag. Set when a byte has been
completely transmitted.
0 RI 98h Receive flag. Set when a byte has been
completely received.
• Additionally, it is necessary to define the function of SM0 and SM1
which is given in below table:
Table 3.7
SM0 SM1 Serial Mode Explanation Baud Rate
0 0 0 8-bit Shift Register Oscillator / 12
0 1 1 8-bit UART Set by Timer 1 (*)
1 0 2 9-bit UART Oscillator / 64 (*)
1 1 3 9-bit UART Set by Timer 1 (*)
(*) Note: The baud rate indicated in this table is doubled if PCON.7 (SMOD)
is set.
• The SCON SFR allows us to configure the Serial Port. The first four
bits (bits 4 through 7) are configuration bits as shown in Table 3.7.
• Bits SM0 and SM1 decide the serial mode as 0, 1, 2 or 3.
• In modes 0 and 2 the baud rate is fixed based on the oscillator
frequency.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 3-52 Timer/Counter, Interrupts

• In modes 1 and 3 the baud rate is variable based on how often Timer
1 overflows.
• The next bit, SM2, is a flag for "Multiprocessor communication".
• Generally, whenever a byte has been received, the 8051 will set the
"RI" (Receive Interrupt) flag. It gives the information about a byte that
it has been received and that it needs to be processed. However,
th
when SM2 is set, the "RI" flag will only be triggered if the 9 bit
th
received was a "1". If SM2 is set and a byte is received whose 9 bit is
clear, the RI flag will never be set.
• This can be useful in certain advanced serial applications.
• The next bit, REN, is "Receiver Enable". To receive data via the serial
port, this bit is set.
• The last four bits (bits 0 through 3) are operational bits. They are
used when actually sending and receiving data. They are not used
to configure the serial port.
• The TB8 bit is used in modes 2 and 3.
• In modes 2 and 3, a total of nine data bits are transmitted. The first 8
data bits are the 8 bits of the main value, and the ninth bit is taken
from TB8.
• If TB8 is set and a value is written to the serial port, the data bits will
be written to the serial line followed by a "set" ninth bit. If TB8 is
clear, the ninth bit will be "clear".
• The RB8 also operates in modes 2 and 3 and functions essentially the
same way as TB8, but on the reception side. When a byte is received
in modes 2 or 3, a total of nine bits are received.
• In this case, the first eight bits received are the data of the serial byte
received and the value of the ninth bit received will be placed in RB8.
• TI means "Transmit Interrupt". When a program writes a value to the
serial port, a certain amount of time will pass before the individual
bits of the byte are "clocked out" the serial port.
• If the program were to write another byte to the serial port before
the first byte was completely output, the data being sent would be
garbled.
• When the TI bit is set, the program may assume that the serial port is
"free" and ready to send the next byte.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 3-53 Timer/Counter, Interrupts

• Finally, the RI bit means "Receive Interrupt". It functions similarly to


the "TI" bit, but it indicates that a byte has been received. Whenever
the 8051 has received a complete byte it will trigger the RI bit to let
the program know that it needs to read the value quickly, before
another byte is read.
• Once the Serial Port Mode has been configured, we must configure
the serial ports baud rate.
• Timer 1 is usually used as baud rate generator because it is easy to
generate various baud rates using Timer 1.
Procedure of Generating the Baud Rate :
1. Timer 1 overflow interrupt must be disabled.
2. Timer 1 is set in auto-reload mode.
3. Depending on necessary baud rate, calculate the value that
should be loaded into the TH1 so that it causes Timer 1 to
overflow.
• There are many ways one can cause timer 1 to overflow at a rate
that determines a baud rate, but the most common method is to
put timer 1 in 8-bit auto-reload mode (timer mode 2) and set a
reload value (TH1) that causes Timer 1 to overflow at a frequency
appropriate to generate a baud rate.
• To determine the value that must be placed in TH1 to generate a
given baud rate, we may use the following equation (assuming
PCON.7 is clear).
TH1 = 256 − ((Crystal / 384) / Baud)
If PCON.7 is set then the baud rate is effectively doubled, thus
the equation becomes:
TH1 = 256 − ((Crystal / 192) / Baud)
For example, if we have an 11.059 MHz crystal and we want to
configure the serial port to 19,200 baud we try plugging it in the
first equation:
TH1 = 256 − ((Crystal / 384) / Baud)
TH1 = 256 − ((11059000 / 384) / 19200)
TH1 = 256 − ((28,799) / 19200)
TH1 = 256 − 1.5 = 254.5
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 3-54 Timer/Counter, Interrupts

• To obtain 19,200 baud on 11.059 MHz crystal we have to set TH1


to 254.5. If we set it to 254 we will have achieved 14,400 baud
and if we set it to 255 we will have achieved 28,800 baud.
• The below table shows the values of baud rate and hex number
which should be loaded into the TH1.
Table 3.8
Baud rate Oscillator frequency in Reload values SMOD
MHz in HEX in bit
timer 1
register

150 11.0592 40H 0


12 30H
14.7456 00H

300 11.0592 A0H 0


12 98H
14.7456 80H
16 BBH
20 A9H

1200 11.0592 E8H 0


12 E6H
14.7456 E0H
16 DEH
20 D5H

2400 11.0592 F4H 0


12 F3H
14.7456 F0H
16 EFH
20 EAH
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 3-55 Timer/Counter, Interrupts

Another Method of Generating Baud Rate :


• The another method of generating the baud rate is explained in
below section.
• As studied above, Timer-1 is used to generate baud rate for mode-1
serial communication by using overflow flag of the timer to
determine the baud frequency. Timer-1 is used in timer mode-2 as
an auto-reload 8-bit timer. The data rate is generated by timer-1
using the following formula.
SMOD
2 fosc
fbaud = ×
32 12 × [256 − (TH1)]
th
where, SMOD is the 7 bit of PCON register, fosc is the crystal
oscillator frequency of the microcontroller.
fosc
• It can be noted that is the timer overflow
12 × [256 − (TH1)]
frequency in timer mode 2, which is the auto-reload mode.
• If timer 1 is not run in mode 2, then the baud rate is,
SMOD
2
fbaud = × (timer - 1 overflow frequency)
32
fosc
• Timer-1 can be run using the internal clock, (timer mode) or from
12
any external source via pin T1 (P3.5) (Counter mode).
SOLVED EXAMPLE

Example 3.24 : If standard baud rate is desired, then 11.0592 MHz


crystal could be selected. To get a standard 9600 baud rate, calculate the
setting of TH1.
Solution : Assuming SMOD to be '0'
0 6
2 11.0592 × 10
9600 = ×
32 12 × (26 − TH1)
6
1 11.0592 × 10
Or 256 − TH1 = × =3
32 12 × 9600
Or TH1 = 256 − 3 = 253 = FDH
In mode-1, if SM2 is set to 1, no receive interrupt (RI) is generated
unless a valid stop bit is received.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 3-56 Timer/Counter, Interrupts

Think Over It

• Why is it not possible to protect a microprocessor system from


software piracy?
• Why microcontroller is designed with their bit-wise width
always a multiple of 8?

Points to Remember
• Timer 0 and timer 1 are 16-bit wide.
• The 16-bit register of timer 0 is accessed as low byte and high byte.
The low byte register is called TL0 (Timer 0 low byte) and high byte
register is referred as TH0 (Timer 0 high byte).
• Timer 1 register is also 16-bits and is split into two bytes referred as
TL1 (Timer 1 low byte) and TH1 (Timer 1 high byte).
• TMOD is an 8-bit register in which the lower 4-bits are set aside for
timer 0 and upper 4-bits for timer 1.
• GATE : Gating control when set. Timer/counter is enabled only when
the INTX pin is high and the TRx control pin is set. When cleared, the
timer is enabled whenever the TRx control bit is set.
• There are two ways to create a time delay in 8051 C :
1. Using a simple for loop.
2. Using the 8051 timer.
• A timer can be used as a counter if pulses are provided from outside
the chip instead of using the frequency of the crystal oscillator as the
clock source.
• By feeding pulse to the T0 (P3.4) and T1 (P3.5) pins, Timer 0 and
Timer 1 are turned into Counter 0 and Counter 1, respectively.
• Serial data communication uses two methods, asynchronous and
synchronous. The synchronous method transfers a block of data
(character) at a time while asynchronous communication transfers a
single byte at a time.
• Start and stop bits are widely used in asynchronous data transfer,
for character transmission, while the block transmission uses
synchronous method.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 3-57 Timer/Counter, Interrupts

• The rate of data transfer is stated in bps (bits per second) referred as
baud rate.
• The 8051 has one serial port.
• Port pins P3.0 (RXD) and 3.1 (TXD) used to receive and transmit the
data respectively.
• The data is transmitted through a register called as SBUF.
• SBUF is an 8-bit register used for serial communication in 8051.
• The SCON register is an 8-bit register used to control data
communication, means it can program the start bit, stop bit, and
data bits of data framing.
• External hardware interrupt INT0 has highest priority.
• Serial com interrupt (RI and TI) has lowest priority.
• On reset, all interrupts are disabled.
• The interrupt must be enabled by software. It is possible with the SFR
register called as IE (Interrupt enable).
• Reset is considered to be the ultimate interrupt. It is type of non-
maskable interrupt.
• In the interrupt method, whenever any device needs service, the
device notifies the microcontroller by sending an interrupt signal.
Upon receiving an interrupt signal, the microcontroller stops
whatever it is doing and serves the device.
• The program which is associated with the interrupt is called Interrupt
Service Subroutine (ISR).
• In polling, the microcontroller continuously monitor the status of a
given device, when the condition met, it gives the service to that
device. After that, it again starts monitoring the next device until
everyone is serviced.
• The advantage with the interrupt method is that microcontroller can
serve many devices and each device can get the attention of the
microcontroller based on the priority assigned to it.
• More importantly, in the interrupt method, the microcontroller can
ignore (mask) the device request for service.
• SBUF register is used solely for serial communication in the 8051.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 3-58 Timer/Counter, Interrupts

• When the SBUF register has a byte, RI is raised to indicate that the
received byte needs to be picked up before it is overrun (lost) by new
incoming serial data.
• Before RETI instruction, in the program, RI and TI flags must be
cleared.
• In serial communication, RI (or TI) must be cleared by the
programmer using software instructions such as “CLR TI” and “CLR
RI” in the ISR.

Exercises
[A] True or False :
1. TMOD register is a bit addressable register.
2. In 8-bit auto-reload mode, values from 00 to FFH are allowed to be
loaded in THX.
3. The counter and timer has the same source of clock pulses.
4. 8051 has an in-built UART.
5. External interrupt 1 (INT1) has lowest priority.
[B] Fill in the blanks :
1. Each timer has …… registers that are …… bits wide.
2. In mode 1, the counter rolls over when it goes from …… to …….
3. In mode 2, the counter rolls over when it goes from …… to …….
4. In edge triggering interrupt, the external source must be held ……
for at least …… machine cycle.
5. The 8051 has two external hardware interrupts …… and ……
[C] Multiple Choice Questions :
1. Timer 0 and 1 of 8051 are …… wide.
(a) 8-bit (b) 16-bit
(c) 12-bit (d) none of these
2. TMOD is an …… register.
(a) 8-bit (b) 16-bit
(c) 12-bit (d) none of these
3. Mode 2 of timer is ……
(a) 13-bit timer mode (b) 16-bit timer mode
(c) 8-bit auto-reload mode (d) split timer mode
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 3-59 Timer/Counter, Interrupts


4. If C/ T = 1, then it decides mode of ……
(a) Timer (b) Counter
(c) Timer/Counter (d) None of these
5. …… is automatically set when timer changes from FFFF to 0000.
(a) TR1 (b) TR2
(c) TF1 (d) none of these
6. …… interrupt has highest priority.
(a) Timer 0 interrupt (TF0)
(b) Timer 1 interrupt
(c) External hardware interrupt INT0
(d) Serial com interrupt (RI and TI)
7. On reset, all interrupts are ……
(a) disabled (b) enabled
(c) in tristate (d) none of these
8. When reset pin is activated, the 8051 jumps to address location ……
(a) FFFF (b) 0000
(c) IF current address (d) none of these
[D] Short Answer Questions :
1. What is the advantage of interrupt based data transfer ?
2. What is meant by the term ISR ?
3. Show the instruction to enable the EX1 and timer 1 interrupt.
4. Which pin of the 8051 is assigned to the external hardware interrupts
INT0 and INT1 ?
5. When an interrupt is activated, what is the first step taken by the
8051 ?
6. What address in the interrupt vector table is assigned to timer 1 ?
7. Can the 8051 generate two square waves simultaneously?
8. How many hardware interrupts has the 8051 ? How are they
activated ?
9. Which bit of TCON belong to EX0 ?
10. Explain the role of TCON.1 and TCON.3 in the execution of external
interrupt 1.
11. How many serial interrupt has the 8051 ?
12. Which bit of the IE register belong to the serial interrupt ?
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 3-60 Timer/Counter, Interrupts

13. Which is the highest priority interrupt of 8051 ?


14. Assume that IP register has all OS. Explain what happens if both TF0
and TF1 are activated at the same time.
[E] Long Answer Questions :
1. Explain in detail about the interrupts available in 8051.
2. Explain interrupt vector table for the 8051.
3. Which steps are followed to enable an interrupt ?
4. Explain the concept of interrupts versus polling.
5. Write a program that displays a value ‘Y’ at a port 0 and ‘N’ at port 2
and also generate a square waves of 10 kHz with timer 0 in mode 2
at port pin P1.2, XTAL = 22 MHz.
6. Write a program to generate two square waves one of 10 kHz
frequency at pin P1.3 and another of frequency 20 kHz at pin P2.3.
Assume XTAL = 22 MHz.
7. Write a program to toggle pin P1.3 every 2 seconds.
8. Two switches are connected to P1.2 and P1.3, when a switch is
pressed the corresponding line goes low, write a program :
(a) Light all LEDs connected to port 0, if the first switch is pressed.
(b) Light all LEDs connected to port 2, if the second switch is
pressed.
9. Generate from all pins of port 0, a square wave which is half the
frequency of the signal applied at INT0 (P3.2).
10. Write a program in which 8051 reads data from P1 and writes it to P2
continuously while giving a copy of it to the serial COM port to be
transferred serially. Assume XTAL = 11.0592 MHz.
11. Explain in detail the concept of interrupt priority in 8051.

Answers
[A] True or False :
(1) True (2) True (3) False
(4) True (5) False
[C] Fill in the blanks :
(1) 2, 8 bit, (2) 0000 to FFFF, (3) 00 to FFH, (4) high, one,
(5) INT0 and INT1
[C] Multiple Choice Questions :
1. (b) 2. (a) 3. (c) 4. (b) 5. (c) 6. (c) 7. (a)
8. (b)
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 3-61 Timer/Counter, Interrupts

University Questions
April 2015

1. If crystal frequency is 11.0592 MHz, find the time required to execute


one machine cycle. (1M)
2. Draw bit format of IP register and give function of each bit. (5M)

October 2015
1. To assign serial port interrupt at highest priority, what are the
contents of IP register ? (1M)
2. Write a ‘C’ program to generate a square wave form of 20 kHz on
port P1.5 with duty cycle 50% using mode 1 timer 1. [XTAL = 11.0592
MHz]. (5M)
3. Draw bit format of SCON register and give the function of each bit.
(5M)
4. If IP contains 07h. Give the sentence of execution of interrupts. (2M)

April 2016

1. Which is the lowest priority interrupt of 8051? (1M)


2. How are timer 0 and timer 1 started and stopped in 8051? (1M)
3. Write a ‘C’ program to generate a square waveform of 2KHz on port
P2.4 with duty cycle 50% using mode 1 timer 1 (XTAL=12MHz) (5M)
4. Draw the bit format of TMOD register. Explain each bit of it. (5M)
5. List various types of interrupts used in 8051. (2M)

April 2017

1. State the role of C/T bit in TOMD register. (1M)


2. What is the significance of timer flag (TF) flag in 8051. (1M)
3. Write a ‘C’ program to generate a 5KHz square wave of 50% duty
cycle on port P2.2 using timer 1 mode 1. Assume crystal frequency =
11.0592MHz. (5M)
4. Draw bit format of IE register and explain function of each bit. (5M)

April 2018

1. What is the use of bit TR1 in TCON register? (1M)


2. Write full form of IVT and ISR with respect to interrupts of 8051. (1M)
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 3-62 Timer/Counter, Interrupts

3. Write 8051 ‘C’ program to generate square wave on P1.5 having


1000Hz frequency and 50% duty cycle. Assume crystal frequency of
12 MHz and given a value to be loaded into timer register is
(FEOC)Hex . Use Timer 1 in mode 1. (5M)
4. Draw the bit format of TMOD register. Explain each bit of it. (5M)

April 2019
1. What is the significance of Gate bit of TMOD register. (1M)
2. Which special function register of 8051 is used to keep track of
priority of interrupts? (1M)
3. Write an 8051 ‘C’ program for generating a pulse on P3.2 using Timer
1 in mode 1. (5M)
4. Draw control word format of TCON register of 8051. Explain the
significance of each bit of the register. (5M)
5. Write the types of interrupts of 8051. Mention the vector addresses
of these interrupts. (5M)

✍✍✍
Unit 4…
Interfacing Serial
Communication
Introduction
• The applications of microcontrollers with real world device
interfacing are explored in this Chapter. These devices are Analog to
Digital Converters (DAC), Digital to Analog Converters (DAC), Liquid
Crystal Display (LCD) etc.
• The interfacing of these devices with 8051 is explained in detail.
• The 8-bit parallel ADC chips ADC0804, ADC0808/0809 and ADC0848
are studied. At the beginning of this Chapter, parallel and serial
interfacing concept of ADC are explained.
4.1 Parallel ADC
ADC Devices :
• Analog to digital converters are most widely used in instrumentation
for data acquisition.
• Physical parameters like Temperature, Pressure, Humidity and
Velocity are few examples that are used every day. These physical
quantities are converted into electrical (voltage, current) signal using
transducers.
• Sometime transducers are synonymously referred as sensors.
• The sensors produce an output in the form of voltage or current.
Therefore, it is essential to convert the analog voltage into digital
form. Once it is converted into digital fashion, microcontroller can
read it and processes it.
• ADC has n-bit resolution where n can be 8, 10, 12, 16 or 24-bits. The
higher resolution of ADC provides smaller step size. The step size is
defined as the smallest change that can be discerned by an ADC.
• In this Chapter, 8-bit ADC is discussed. In addition to resolution,
conversion time is another major factor which influences on the
performance of ADC.
(4.1)
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 4-2 Interfacing Serial Communication

• Conversion time is defined as ‘the time taken by the ADC to convert


the analog input to digital output.’
• The ADC chips are either parallel to serial.
• In the serial chip, only one pin for data output is used while as in
parallel, 8 or more pins are dedicated to bringing out the binary data.
4.1.1 ADC0804 Chip
• ADC0808/0848/0809/0804 provides an 8-bit parallel data path
between the ADC chip and the CPU.
• The ADC0804 IC is widely used in the family of the ADC800 series
from National Semi-conductor. It works with +5 volts and has a
resolution of 8-bits.
• In ADC0804, the conversion time varies depending on clocking
signals applied to the CLK IN pin. The pin description is shown in
Fig. 4.1.
+5V

20
6 VCC
10 kW Vin (+)
POT 7 18
Vin (-) D0
8 17
A GND D1
16
9 Vref/2 D2
15
D3 LEDs
19 14
CLK R D4
13
10 kW 4 D5
CLK in 12
D6
150 pF 11
1 D7
CS
2 3
RD WR
10 5
D GND INTR Normally
open
Start

Fig. 4.1 : ADC0804 chip


Description of pins is given below.
• D0 - D7 : As ADC0804 is a parallel ADC chip, it has 8 digital data
output (D0 - D7) from pin 11 to 18.
• These are tristate buffered and the converted data is accessed only
when CS = 0 and RD is forced low.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 4-3 Interfacing Serial Communication

• To calculate the output voltage the following formula is used:


Vin
Dout =
Step size
where, Dout = Digital data output (in decimal),
Vin = Analog input voltage,
2 × Vref
 2 
and step size (resolution) is the smallest change, which is  
 256 
for ADC0804.
Vref
: Pin 9 is an input voltage used for the reference voltage. If this
2
pin is open, the analog input voltage for ADC0804 is in the range of
0 to 5 V. However, there are many applications where the analog
Vref
input applied to Vin needs to be other than 0 to +5 V range. is
2
used to implement analog input voltages other than 0 to +5 V. For
Vref
example, if the analog input range needs to be 0 to 4 volts, then
2
is connected to 2 volts.
Vref
Following Table 4.1 shows the Vin range for various inputs.
2
Vref
Table 4.1 : relations to Vin range of (ADC0804)
2
Vref Vin (V) Step Size (mV)
2
Not connected 0 to 5 5
= 19.53
256
2.0 0 to 4 4
= 15.62
256
1.5 0 to 3 3
= 11.71
256
1.28 0 to 2.56 2.56
= 10
256
• VCC : This is the +5 V power supply. It is also used as a reference
Vref
voltage when the input (pin 9) is open.
2
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 4-4 Interfacing Serial Communication

• Vin (+) and Vin (− −) : Pin 6 and 7 are differential analog inputs where
Vin = Vin (+) − Vin (−). Often Vin (−) pin is connected to ground and Vin
(+) pin is used as the analog input to be converted to digital.
• CS : Chip Select is an active low input used to activate the ADC0804
chip. To access the ADC0804, this pin is connected to low.
• RD (read) : This is an input signal and is active low. The ADC
converts the analog input to its binary equivalent and holds it in an
internal register. RD is used to get the converted data out of the
ADC0804 chip. When CS = 0 and if the H − L pulse is applied to the
RD pin, the 8-bit digital output shows up at the D0 − D7 data pins.
The RD pin is also referred as Output Enable (OE).
• WR (write) : This pin is also referred as a “Start Conversion” and is
active low. It informs the ADC0804 to start the conversion process. If
CS = 0 i.e. pin 9 is low, and WR makes a low to high transition, the
ADC0804 starts converting the analog input value of Vin to an 8-bit
digital number. The amount of time it takes for conversion varies
depending on the CLK IN and CLK R values explained below. When
the data conversion is complete, the INTR pin is forced low by
ADC0804.
• CLK IN and CLK R : CLK IN is an input pin connected to an external
clock source when an external clock is used for timing. However, the
IC0804 has an internal clock generator. To use the internal clock
generator, the CLK IN and CLK R pins of ADC0804 are connected to a
capacitor and resistor as shown in Table 4.2.
Table 4.2 : Resolution Vs Step size for ADC
n-bit Number of Steps Step Size (mV)
8 256 5
= 19.53
256
10 1024 5
= 4.88
1024
12 4096 5
= 1.2
4096
16 65536 5
= 0.076
65536
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 4-5 Interfacing Serial Communication

The clock frequency is determined by the equation


1
f=
1.1 RC

Typically, R = 10 kΩ and C = 150 pF.

Substituting these values, f = 606 kHz.

• INTR : This pin is referred as “End of Conversion”, which is


active low. It is a normally high pin and when the conversion is
finished, it goes low to high. After INTR goes low, make CS = 0
and send a high to low pulse to the RD pin to get the data out of
the ADC0804 chip.
4.1.2 Steps for Data Conversion in ADC

• Before following the steps for conversion, connect pin 10 (Digital


ground) and pin 8 (analog ground) in such way that analog ground
(AGND) is connected to the ground of the analog Vin while DGND is
connected to the ground of VCC pin.

• The reason is that, there are two ground pins, which are used to
isolate the analog Vin signal from transient voltages caused by the
digital switching of the output D0 - D7. Such isolation contributes to
the accuracy of digital data output.

Steps for data conversion by the ADC0804 chip :


1. Make CS = 0 and send low to high pulse to pin WR to start the
conversion.

2. Keep monitoring the INTR pin. If INTR is low, the conversion is


finished, go to the next step. If INTR is high, keep polling until it
goes low.

3. After the INTR has become low, make CS = 0 and send a high to
low pulse to the RD pin to get the data out of ADC0804 chip. The
timing for this process is shown in Fig. 4.2.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 4-6 Interfacing Serial Communication

CS

WR
D 0 - D7 Data out

INTR

Start conversion
End conversion
RD

Read
Fig. 4.2 : Read and write timing for ADC0804
• The speed at which an analog input is converted to the digital output
depends on the speed of the CLK input. According to the ADC0804
data sheets, the typical operating frequency is approximately 640
kHz at 5 V.
• Fig. 4.3 and Fig. 4.4 shows the ways of providing clock to ADC0804.

+5V

8051 ADC0804
P2.5 RD VCC 150 pF
10 kW
P2.6 WR CLK R
P1.0 D0 CLK IN +5V

Vref/2 1.28 V

Vin (+)
Vin (-) 10 kW
POT
P1.7 A GND
D7
CS
P2.7 INTR GND

Fig. 4.3 : 8051 connection to ADC0804 with self clocking


S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 4-7 Interfacing Serial Communication

Program in ‘C’ for Interfacing ADC0804 to 8051 Microcontroller :


#include <reg51.h>
Sbit read = P2^5;
Sbit write = P2^6;
Sbit intr = P2^7;
sfr my_data = P0;
void (main)
{
Unsigned char num;
my_data = 0×FF; // set port 0 as an input
intr = 1; // set port 2.7 pin as an input
read = 1; // set read high
write = 1; // set write high
while(1)
{
write = 0; // make write pulse low
write = 1; // make write pulse high to start
conversion
while(intr==1) // wait for end of conversion
{
read = 0;
num=my_data;
convertanddisplay(value);// display data on screen
read = 1; // make read high to read next data
}
}
}
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 4-8 Interfacing Serial Communication

+5V

8051 ADC0804
P2.5 RD VCC
XTAL
11.0592 MHz P2.6 WR CLK R +5V
P1.0 D0 CLK IN
XTAL2
Vref/2

Vin (+) 10 k
POT
Vin (-)

P1.7 A GND
D Q D7
CS
Q INTR
P2.7 GND

D Q D Q D Q
Q Q Q

Fig. 4.4 : 8051 connection to ADC0804 with clock from


XTAL2 of the 8051
• In Fig. 4.4, clock is from the crystal of the microcontroller, since this
frequency is too high, we use D flip-flop (74LS74) to divide the
frequency.

• A single D flip-flop divides the frequency by 2 if Q is connected to D
input.
SOLVED EXAMPLE
Example 4.1 : Examine the ADC0804 connection to the 8051. Write a
program to monitor the INTR pin and bring an analog input to register
into A. Then call a Hex to ASCII conversion and data display subroutine.
Do this continuously.
Solution : Let ; P2.6 = WR
; P2.7 when low, end of conversion
; P2.5 = RD
; P1.0 to P1.7 = D0 to D7
MOV P1, #0FFH ; make P1 as input
BACK : CLR P2.6 ; WR = 0
SETB P2.6 ; WR = 1
HERE : JB P2.7, HERE ; wait for end
conversion
CLR P2.5 ; conversion finished
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 4-9 Interfacing Serial Communication

MOV A, P1 ; read the data


ACALL CONVERSION ; hex to ASCII
ACALL DATA_DISPLAY ; display the data
SETB P2.5 ; RD = 1
SJMP BACK
Example 4.2 : What is step size of 8-bit ADC if Vin is 2.56 volt ?
Solution : Vin = 2.56 V
Vin
Resolution = n
2
2.56 2.56
= 8 =
2 256
= 10 mV
4.1.3 ADC0808/0809 Chip with 8 Analog Channels
• Another useful chip is the ADC0808/0809 from National
Semiconductor as shown in Fig. 4.5.
• The ADC0804 has 8 analog input channels.
• The ADC0808/0809 chip allows us to monitor up to 8 different
analog inputs using only a single chip.
• Notice that the ADC0808/0809 has an 8-bit data output just like the
ADC0804.
• The 8 analog input channels are multiplexed and selected according
to Table 4.3 using three address pins, A, B and C.
• In the ADC0808/0809, Vref(+) and Vref(−) set the reference voltage. If
Vref(−) GND and Vref(+) = 5 V, the step size is 5 V/256 = 19.53 mV.
Therefore, to get a 10 mV step size we need to set Vref(+) = 2.56 V
and Vref(−) = GND.
• From Fig. 4.5, note the ALE pin. A, B and C addresses are used to
select IN0 - IN7 and activate ALE to latch in the address.
• SC is for start conversion. SC is same as the WR pin in the other ADC
chips.
• EOC is for end of conversion and OE is for output enable (READ).
• The EOC and OE are the same as the INTR and RD pins respectively.
• Table 4.4 shows the step size relation to the Vref voltage. There is no
Vref/2 in the ADC0808/0809 chip.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 4-10 Interfacing Serial Communication

GND Clock VCC


IN0 D0

ADC0808/0809

IN7 D7
Vref (+) EOC
Vref (-) OE

SC ALE C B A

(LSB)
Fig. 4.5 : ADC0808/0809
Table 4.3 : ADC0808/0809 analog channel selection
Selected analog channel C B A
IN0 0 0 0
IN1 0 0 1
IN2 0 1 0
IN3 0 1 1
IN4 1 0 0
IN5 1 0 1
IN6 1 1 0
IN7 1 1 1
Table 4.4 : Vref relation to Vin range for ADC0808/0809
Vout (V) Vin (V) Step Size (mV)
Not connected 0 to 5 5
= 19.53
256
4.0 0 to 4 4
= 15.62
256
3.0 0 to 3 3
= 11.71
256
2.56 0 to 2.56 2.56
= 10
256
2.0 0 to 2 2
= 7.81
256
1 0 to 1 1
= 3.90
256
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 4-11 Interfacing Serial Communication

ADC0808 C Program :
#include <reg51.h>
sbit ALE = P2^4;
sbit SOC = P2^6;
sbit OE = P2^7;
sbit ADD_A = P2^0;
sbit ADD_B = P2^1;
sbit ADD_C = P2^2;
sbit MY_DATA = P1;
void (main)
{
unsigned char num;
MY_DATA = OXOFF // Set port 1 as an input
EOC = 1; // Set P2.1 as an input to
read EOC signal
ALE = 0; // Clear ALE signal
SOC = 0; // Clear SOC signal
OE = 0; // Clear OE signal
while (1)
{
ADD_A = 0; // Select channel 0
ADD_B = 0;
ADD_C = 0;
MY_DELAY (1);
ALE = 1; // Latch address of channel
MY_DELAY (1);
SOC = 1; // Send start of conversion
signal
MY_DELAY (1);
ALE = 0; // Remove ALE signal
SOC = 0; // Remove SOC signal
while (EOC == 1);
while (EOC == 0); // Wait until data gets
converted
OE = 1; // Send output enable signal
MY_DELAY (1);
Num = MY_DATA // Read data from port 1
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 4-12 Interfacing Serial Communication

OE = 0; // Remove OE signal to read


next data
Convert and Display(Num);
// Display value on screen
}
}
4.2 Serial ADC Chips
• All ADC chips discussed above are parallel type ADC. The D0 - D7
data pins of the ADC0808/0809/0804 provide an 8-bit data path
between the ADC chip and CPU. In case of the 16-bit parallel ADC
chip, 16 pins are required for the data path.
• In recent years, for many applications where space is the critical issue,
using such large number of pins for data is not feasible. For this
reason, serial ADC chips are widely used.
• The MAX1112 serial ADC chip and its interfacing with microcontroller
8051 is discussed below.
4.2.1 MAX1112 ADC
• The MAX1112 is an 8-bit serial ADC chip with 8 channel analog input.
• It has a single Dout pin to bring out the digital data after it has been
converted.
• It is compatible with a popular SPI and microwire serial standard.
• The pin description of MAX1112 is shown in Fig. 4.6 and serial ADC
block diagram is shown in Fig. 4.7.

1 CH0 VDD 20
2 CH1 SCLK 19
3 CH2 CS 18
4 CH3 DIN 17
5 CH4 SSTRB 16
6 CH5 DOUT 15
7 CH6 DGND 14
8 CH7 AGND 13
9 COM REFOUT 12
10 SHDN REFIN 11

Fig. 4.6 : MAX1112 chip


S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 4-13 Interfacing Serial Communication

AGND DGND VDD


CH0

SCLK
MAX1112 CS
CH7 DIN
REFIN DOUT
REFOUT

SHDN SSTRB

Fig. 4.7 : MAX1112 serial ADC block diagram


• CH0 - CH7 : CH0 - CH7 are 8 channels of the analog inputs. In the
single-ended mode, each of the channels can be used for an analog
input where the COM pin is used as a ground reference for all the
channels. In single-ended mode, 8 channels of input allow us to read
8 different analog inputs. The input channel is selected by sending in
the control byte via the DIN pin. In differential mode, there are
four sets of 2-channel differentials. CH0 and CH1 go together, and
CH2 − CH3 and so on.
• COM : Ground reference for the analog input in single-ended mode.
• CS : Chip select is an active low input used to select the MAX1112
chip. To send the control byte via the DIN pin, CS must be low. When
CS is high, the DOUT is high impedance.
• SCLK : Serial clock SCLK is used to bring data out and send in the
control byte on bit at a time.
• DOUT : In serial data out, the digital data is clocked out one bit at a
time on the H to L edge of SCLK.
• DIN : In serial data in, the control byte is clocked in one bit at a time
on the L to H edge of SCLK.
• SSTRB : Serial strobe output. In internal clock mode, this indicates
end of conversion. It goes high when the conversion is complete.
• VDD : VDD is the +5 V power supply.
• AGND and DGND : Both are input pins providing ground for both
the analog and digital signals.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 4-14 Interfacing Serial Communication

• SHDN : Shut down is an input and is normally not connected. If low,


the ADC is shut down to save power. This is shut down by hardware.
The control byte causes shut down by software.
• REFIN : REFIN means reference voltage input, this voltage dictates
the step size.
• REFOUT : REFOUT means internal reference generator output. A 1 µF
bypass capacitor is placed between this pin and AGND.
• MAX1112 Control Byte : The MAX1112 chip has 8 channels of
analog inputs that are selected using a control byte. The control byte
is fed into the MAX1112 serial one bit at a time via the Din pin with
the help of SCLK. The control byte must be sent in with the MSB
(most significant bit) going in first. The MSB of the control byte is
high to indicate the start of the control byte, as shown in Fig. 4.8.
• REFIN Voltage and Step Size : The step size for the MAX1112
depends on the voltage connected to the REFIN pin. In unipolar
mode, with VDD = 5 V, we get 4.096 V for full-scale if the REFIN pin is
connected to the AGND with a 1 µF capacitor. That gives us a 16 mV
step size since 4.096 V/256 = 16 mV. To get a 10 mV step size, we
need to connect the REFIN pin to 2.56 V external voltage source
since 2.56V/256 = 10 mV according to the MAX1112 data sheet, the
external reference voltage must be between 1 V and VDD.
Start SEL2 SLE1 SEL0 UN/BIP SGL/DIF PD1 PD0
Fig. 4.8 : MAX1112 control byte
• Start : The MSB (D7) must be high to define the beginning of the
control byte. It must be sent in first.
SEL2 SEL1 SEL0 CHANNEL SELECTION (SINGLE-ENDED
MODE)
0 0 0 CHAN0
0 0 1 CHAN1
0 1 0 CHAN2
0 1 1 CHAN3
1 0 0 CHAN4
1 0 1 CHAN5
1 1 0 CHAN6
1 1 1 CHAN7
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 4-15 Interfacing Serial Communication

UNI/BIP 1 = Unipolar : Digital data output is binary 00 − FFH.


0 = Bipolar : Digital data output is in 2’s complement.
SGL/DIF 1 = Single-ended : 8 channels of single-ended with
COM as reference.
0 = Differential : Two channels (e.g., CH0 − CH1) are
differential.
PD1 1 = Fully operational.
0 = Power-down : Power down to save power using
software.
PD0 1 = External clock mode : The conversion speed is
dictated by SCLK.
0 = Internal clock mode : The conversion speed is
dictated internally, and the SSTRB pin goes high to
indicate end-of-conversion (EOC).
The 8051 microcontroller connections with MAX1112 is shown in
Fig. 4.9.
+5V

8051 MAX11112
+5V
P2.0 CS VDD
P2.1 SCLK SHDN
P2.2 DIN CH0
P2.3 DOUT CH1
CH2
CH3
CH4
CH5
CH6
2.56V REFIN CH7

REFOUT COM
1 mF
AGND DGND

Fig. 4.9 : Microcontroller 8051 interfaced with serial ADC


S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 4-16 Interfacing Serial Communication

SOLVED EXAMPLE
Example 4.3 : Find MAX1112 control byte for (a) CH0 and (b) CH3.
Assume single-ended, unipolar, internal clock and fully operational
modes.
Solution : From MAX1112 control byte Fig. 4.8, we may write.
(a) 10001110 (8EH)
(b) 10111110 (BEH).
Select the analog input channel using the control byte. From above
example we see that MSB (D7) of the control byte must be high. The
control byte is fed into the DIN pin one bit at a time using SCLK. The DIN
pin clocks in the control byte on the rising edge of the SCLK as shown in
Fig. 4.10.
; Assembly code for sending in control byte in
; MAX1112, See Fig. 4.9.
CS BIT P2.0
SCLK BIT P2.1
DIN BIT P2.2
DOUT BIT P2.3
MOV A, #9EH ; channel 1 selection
MOV R3, #8 ; load count
CLR CS ; CS = 0
CLR C
BACK : RLC A ; give bit to CY
MOV DIN, C ; send bit to DIN
CLR SCLK ; SCLK = 0 for L − H pulse
ACALL DELAY ; delay
SETB SCLK ; latch data
ACALL DELAY ; delay
DJNZ R3, BACK ; repeat for all 8-bits
SETB CS ; deselect ADC, conversion
CLR SCLK ; SCLK = 0 during conversion
CS

1 2 3 4 5 6 7 8
SCLK

START SEL2 SEL1 SEL0 UNI/BIP SGLI/ PD1 PD0


DIF
DIN

Fig. 4.10 : Sending control byte into MAX1112


S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 4-17 Interfacing Serial Communication

4.3 Interfacing DAC to the 8051


• This section will explain the way of interfacing DAC (Digital to Analog
Converter) to 8051.
• The Digital to Analog Converter (DAC) is a device widely used to
convert digital pulses to analog signal. Mostly binary weighed and
R − 2R ladder methods are used for conversion. MC1408 (DAC808)
are used for DAC.
• The first criterion for judging a DAC is its resolution, which is a
function of the number of binary inputs.
• The common DACs are 8, 10 and 12 bits. The number of data bit
inputs decides the resolution of the DAC since the number of analog
n
output level is equal to 2 , where n is the number of data bit inputs.
4.3.1 DAC0808
• In DAC0808, the digital inputs are converted to current (Iout) and by
connecting a resistor to the Iout pin, the result is converted to voltage.
The total current provided by the Iout pin is a function of the binary
number at D0 - D7 inputs of DAC808 and the reference current (Iref)
and is as follows.

Iout = Iref
D7 + D6 + D5 + D4 + D3 + D2 + D1 + D0 
2 4 8 16 32 64 128 256
Where, D0 is the LSB and D7 is MSB for the inputs and Iref is the input
current that must be applied to pin 14.
• The Iref current is generally set to 2.0 mA.
• Fig. 4.11 shows the generation of current reference (setting Iref = 2
mA) by using the standard 5 V power supply and 1 kΩ, 1.5 kΩ
standard resistors.
• The zener diode (LM336) can also be used, which overcomes any
fluctuation associated with the power supply voltage.
• Now assuming that Iref = 2 mA, if all the inputs to the DAC are high,
the maximum output current is 1.99 mA.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 4-18 Interfacing Serial Communication

+5V +5V

8051 DAC0808 5 kW 5 kW
RD VCC
WR Vref (+)
P1.0 D0 1 kW
D1 OUT -
To
D2 + Scope
D3 0.1 mF Vout = 0
D4 Vref (-)
to 10 V
D5
D6 5 kW
P1.7
D7
VEE COMP GND

0.1 mF

- 12 V
Fig. 4.11 : 8051 connection to DAC808
• For converting Iout to voltage in DAC808, connect Iout pin to a
resistor and observe it on the oscilloscope. However, practically,
this can cause inaccuracy since the input resistance of the load
where it is connected will also affect the output voltage.
• For this reason, the Iref current output is isolated by connecting it
to an OPAMP such as the 741 with feedback resistor Rf = 5 kΩ.
By changing the binary input, the output voltage changes as
shown in Fig. 4.11.
SOLVED EXAMPLES

Example 4.4 : Assuming that R = 5 kΩ, Iref = 2 mA, calculate Vout for
the following binary input 11001000 (C8H).

Solution : Iout = 2 mA
200 = 1.562 mA
256
Vout = 1.562 × 5 kΩ = 7.8125 V
Example 4.5 : If digital input is FCh, then find the output voltage if
Iref = 2 mA and R = 10 kΩ.
Solution : FC = 11111100

Iout = 2 mA
1 + 1 + 1 + 1 + 1 + 1 + 0 + 0 
2 4 8 16 32 64 128 256
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 4-19 Interfacing Serial Communication

= 2 mA
63 = 1.96 mA
64
Vout = 1.96 × 10 kΩ = 19.6 V
Example 4.6 : Generate stair step ramp, set up the circuit (Fig. 4.12)
and observe it on an oscilloscope. Then write a program to send data to
DAC to generate a stair step ramp.
Solution :
+5 V
+5 V
1 kW
Vref (+) 1.5 kW
P1.0 D0

Vref (-) 2.5 kW

8051
Iout
To scope
DAC 0808
Vout = 0 to 10 V
R = 5 kW POT

COMP
P1.7 D7 16
Range control
100 pF
VEE
3 -12 V

GND

Fig. 4.12 : 8051 connecting to DAC0808


ORG 0000H
CLR A
BACK : MOV P2, A
INC A
ACALL DELAY
SJMP BACK
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 4-20 Interfacing Serial Communication

Example 4.7 : Verify the values given for the following angles; (a) 30,
(b) 60.
Solution : (a) Vout = 5 V + (5 V × sin θ)
= 5 V + 5 × sin 30
= 5 V + 5 × 0.5
= 7.5 V
DAC input values = 7.5 V × 25.6
= 192D
(b) Vout = 5 V + (5 V × sin θ)
= 5 V + (5 × sin 60)
= 5 V + 5 × 0.866
= 9.33 V
DAC input values = 9.33 V × 25.6
= 238 D
Example 4.8 : For the circuit of Fig. 4.11. Find the maximum output
amplitude of the sawtooth waveform obtained with the following
program.
(a) MOV R0, #64H
REPEAT: MOV A, #00H
BACK: MOV P1, A
INC A
CJNE A, R0, BACK
SJMP REPEAT
Solution : The method to generate the sawtooth wave is to
increment the A register from 00 to FF to 00 continuously. In this case,
the maximum value of digital number is 64H = 01100100B.

Iout = Iref
D7 + D6 + D5 + D4 + D3 + D2 + D1 + D0 
2 4 8 16 32 64 128 256
= 0.78125 mA
∴ Vout = 0.78125 mA × 5 kΩ
= 3.906 V
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 4-21 Interfacing Serial Communication

(b) MOV A, #00H


MOV P1, A
BACK: INC A
SJMP BACK
Solution : Here maximum value of the digital number is
FFH = 11111111B

∴ Iout = Iref
D7 + D6 + D5 + D4 + D3 + D2 + D1 + D0 
2 4 8 16 32 64 128 256
255
= 2 mA ×
256
= 2 × 0.996
= 1.99 mA
Vout = 1.99 mA × 5 kΩ
= 9.96 V
4.3.2 Sine Wave Generation

• To generate a sine wave, a table is required whose values represent


the magnitude of the sine of angle between 0 and 360° angle.

• The values for sine function vary from −1.0 to +1.0 for 0 to 360°
angle. Therefore, the values in the table are integer number
representing the voltage magnitude for the sine of θ (theta).

• This method ensures that only integer numbers are output to the
DAC by the 8051 microcontroller.
• To generate Table 4.5, the full scale voltage 10 V for DAC output is
assumed.

• Full scale output of the DAC is achieved when all data inputs of the
DAC are high. Therefore, to achieve the full scale 10 V output, we use
the following equation,

Vout = 5 V + (5 × sin θ)

• Vout of DAC for various angles is calculated and shown in Table 4.5.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 4-22 Interfacing Serial Communication

Table 4.5 : Angle Vs Voltage magnitude for sine wave


Angle θ Sin θ Vout (Voltage Values sent to DAC
(degrees) magnitude) = 5 V (decimal) = (Voltage
+ (5 V × sin θ) magnitude × 25.6)
0 0 5 128
30 0.5 7.5 192
60 0.866 9.33 238
90 1.0 10 255
120 0.866 9.33 238
150 0.5 7.5 192
180 0 5 128
210 −0.5 2.5 64
240 −0.866 0.669 17
270 −1.0 0 0
300 −0.866 0.669 17
330 −0.5 2.5 64
360 0 5 128
• To find the values sent to the DAC for various angles, we simply
multiply the Vout voltage by 25.60 because there are 256 steps and
full scale Vout is 10 volts. Therefore, 256 steps/10 V = 25.6 steps per
volt.
• To further clarify this, look at the following code. This program sends
the value to the DAC continuously to produce sine wave as shown in
Fig. 4.13.
AGAIN : MOV DPTR, #TABLE
MOV R2, #COUNT
BACK : CLR A
MOVC A, @A+DPTR
MOV P1, A
INC DPTR
DJNZ R2, BACK
SJMP AGAIN
ORG 300
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 4-23 Interfacing Serial Communication

TABLE : DB 128, 192, 238, 255, 238, 192 ;


see Table 4.5
DB 128, 64, 17, 0, 17, 64, 128
;For better looking sine wave, regenerate
Table 4.5 for 2° angles

10
9
8
7
6
Volts 5
4
3
2
1
0
30 60 90 120 150 180 210 240 270 300 330 360
Degrees

Fig. 4.13 : Angle Vs Voltage magnitude for sine wave


Program in ‘C’ for Interfacing DAC to 8051 Microcontroller:
Example : Verify the values given for the angles (a) 30°.
Solution : Vout = 5V + (5V × sin θ) = 5V + 5 × sin 30° = 5V + 5 × 0.5
= 7.5V
DAC input values = 7.5V × 25.6 = 192 decimal
To find values sent to DAC, multiply Vout by 25.6 as there are 256
steps and full-scale Vout is 10V.
256 steps
So, = 25.6 steps per volt.
10V
Assembly Language Program :
AGAIN: MOV DPTR, #TABLE
MOV R2, #COUNT
BACK: CLR A
MOV C A, @A+DPTR
MOV P1 A
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 4-24 Interfacing Serial Communication

INC DPTR
DJNZ R2, BACK
SJMP AGAIN
ORG 300
TABLE: DB 128, 192, 238, 255, 238, 192
DB 128, 64, 17, 0, 17, 64, 128
Program of DAC in C
# include <reg51.h>
SFr DACDATA = P1;
void main ()
{
unsigned char x;
while (1)
{
for (x = 0; x < 12; x++)
{
DACDATA = WAVEVALUE [x];
}
}
}

4.4 LCD Interfacing


• This section describes the operation mode of LCD interfacing with
8051.
• In recent years the LCD is finding widespread use replacing LEDs. This
is because of some merits of LCD.
• The merits of LCD are given below :
1. The ability to display numbers, characters and graphics. This in
contrast to LED, which are limited to numbers and a few
characters.
2. Incorporation of a refreshing controller into the LCD, thereby
relieving the CPU of task of refreshing the LCD. In contrast, the
LED must be refreshed by the CPU to keep displaying the data.
3. Ease of programming for characters and graphics.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 4-25 Interfacing Serial Communication

4.4.1 Pin Descriptions of LCD


VSS VCC VEE

1 2 3

7 8 9 10 11 12 13 14 4 5 6

D0 D1 D2 D3 D4 D5 D6 D7 RS R/W EN
Fig. 4.14 : Pin description of LCD
• The LCD discussed in this section has 14 pins. There are various LCD
available having different pin positions are shown in Fig. 4.15.
12 14

14 13

2 1

14 21
DMC 1610A DMC16106B DMC20261
DMC1606C DMC16207 DMC24227
DMC16117 DMC 16230 DMC24138
DMC16128 DMC20215 DMC32132
DMC16129 DMC32216 DMC32239
DMC1616433 DMC40131
DMC20434 DMC40218

Fig. 4.15 : Pin positions for various LCDs from optrex


Table 4.6 : Pin positions for various LCDs from optex
Pin Symbol I/O Description

1. VSS − Ground

2. VCC − +5 V power supply

3. VEE − Power supply to control contrast

4. RS I RS = 0 to select command register,


RS = 1 to select data register
… (Contd.)
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 4-26 Interfacing Serial Communication

Pin Symbol I/O Description


5. R/W I R/W = 0 for write, R/W = 1 for read
6. E I/O Enable
7. DB0 I/O The 8-bit data bus
8. DB1 I/O The 8-bit data bus
9. DB2 I/O The 8-bit data bus
10. DB3 I/O The 8-bit data bus
11. DB4 I/O The 8-bit data bus
12. DB5 I/O The 8-bit data bus
13. DB6 I/O The 8-bit data bus
14. DB7 I/O The 8-bit data bus
• VCC, VSS and VEE : VCC and VSS provides +5 V and ground respectively,
VEE is used for controlling LCD display.
• RS Register Select : There are two very important registers inside
the LCD. The RS pin is used for their selection as follows. If RS = 0,
the instruction command code register is selected, allowing the user
to send a command such as clear display, cursor at home etc. If RS =
1, the data register is selected, allowing the user to send data to be
displayed on the LCD.
• R/W (read/write) : R/W input allows the user to write information to
the LCD or read information from it. R/W = 1 when reading, R/W = 0
when writing.
• E, Enable : The enable pin is used by the LCD to latch information
presented to its data pins. When data is supplied to data pins, a high
to low pulse must be applied to this pin in order for the LCD to latch
in the data present at the data pins.
• D0 - D7 : Pin 7 to 14, (8-bit data pins D0 - D7) are used to send
information to the LCD or read the contents of the LCD’s internal
registers. We can display letters A - Z or a - z and numbers 0 - 9, to
these pins while making RS = 1.
• There are also instruction command codes that can be sent to the
LCD to clear the display or force the cursor to the home position or
blink the cursor.
• Table 4.7 gives the list of instruction command codes.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 4-27 Interfacing Serial Communication

Table 4.7 : LCD command codes


Code Command to LCD instruction register
(Hex)
1 Clear display screen
2 Return home
4 Decrement cursor (shift cursor to left)
6 Increment cursor (shift cursor to right)
5 Shift display right
7 Shift display left
8 Display off, cursor off
A Display off, cursor on
C Display on, cursor off
E Display on, cursor blinking
F Display on, cursor blinking
10 Shift cursor position to left
14 Shift cursor position to right
18 Shift the entire display to the left
1C Shift the entire display to the right
st
80 Force cursor to beginning of 1 line
nd
C0 Force cursor to beginning of 2 line
38 2 lines and 5 × 7 matrix
• Use RS = 0 to check the busy flag bit to see if the LCD is ready to
receive information.
• The busy flag is D7 and can be read when R/W = 1, and RS = 0 as
follows.
• If R/W = 1, RS = 0. When D7 = 1 (busy flag = 1), the LCD is busy
taking care of internal operations and will not accept any new
information. When D7 = 0, the LCD is ready to receive new
information.
4.4.2 LCD Data Sheet
• In the LCD, one can put data at any location. The address locations
and the way of assessing it are shown below.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 4-28 Interfacing Serial Communication

RS R/W DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0


0 0 1 A A A A A A A
where, AAAAAAA = 0000000 to 0100111 for line 1 and AAAAAAA =
1000000 to 1100111 for line 2 shown in Table 4.8.
Table 4.8 : LCD addressing
DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0
Line 1 1 0 0 0 0 0 0 0
(min.)
Line 1 1 0 1 0 0 1 1 1
(max.)
Line 2 1 1 0 0 0 0 0 0
(min.)
Line 2 1 1 1 0 0 1 1 1
(max.)
• From the above discussion one can get the address of cursor
positions for various sizes of LCDs.
• Following Table 4.9 gives the cursor address for some LCDs and Fig.
4.16 and 4.17 gives diagram of LCD timing for read and write
operations respectively.
• Table 4.10 provides a detailed list of LCD command instructions.
Table 4.9 : Cursor Addresses for LCDs
16 × 2 LCD 80 81 82 83 84 85 86 through 8F
C0 C1 C2 C3 C4 C5 C6 through Cf
20 × 1 LCD 80 81 82 83 through 93
20 × 2 LCD 80 81 82 83 through 93
C0 C1 C2 C3 through D3
20 × 4 LCD 80 81 82 83 through 93
C0 C1 C2 C3 through D3
94 95 96 97 through A7
D4 D5 D6 D7 through E7
40 × 2 LCD 80 81 82 83 through A7
C0 C1 C2 C3 through E7
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 4-29 Interfacing Serial Communication

D0 - D7 Data
tD

R/ W tAS tAH

RS

tD = Data output delay time


tAS = Setup timer prior to E (going high) for both RS and
R/W = 140 ns (minimum)
tAH = Hold time after E has come down for both RS and
R/W = 10 ns (minimum)
Fig. 4.16 : LCD timing for read (L to H for E-line)

Data

E tDSW
tH
tPWH
R/ W tAS tAH

RS

tPWH = Enable pulse width = 450 ns (minimum)


tDSW = Data setup time = 195 ns (minimum)
tH = Data hold time = 10 ns (minimum)
tAS = Setup time prior to E (going high) for both RS and
R/W = 140 ns (minimum)
tAH = Hold time after E has come down for both RS and
R/W = 10 ns (minimum)
Fig. 4.17 : LCD timing for write (H to L for E-line)
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 4-30 Interfacing Serial Communication

Table 4.10 : List of LCD instructions


Instruction RS R/W DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0 Description
Clear Display 0 0 0 0 0 0 0 0 0 1 Clears entire display and
sets DD RAM address 0 in
address counter
Return Home 0 0 0 0 0 0 0 0 1 1 Sets DD RAM address 0 as
address counter. Also
returns display being
shifted to original position.
DD RAM contents remain
unchanged.
Entry Mode 0 0 0 0 0 0 0 1 1/D S Sets cursor move direction
Set and specifies shift of
display. These operations
are performed during data
write and read.
Display 0 0 0 0 0 0 1 D C B Sets On/Off of entire
ON/OFF display (D), cursor On/Off
Control (C) and blink of cursor
position character (B).
Cursor or 0 0 0 0 0 1 S/C R/L − − Moves cursor and shifts
Display Shift display without changing
DD RAM contents.
Function Set 0 0 0 0 1 DL N F − − Set interface data length
(DL), number of display
lines (L) and character font
(F).
Set CG RAM 0 0 0 1 − AGC − − − − Sets CG RAM address. DD
Address RAM data is sent and
received after this setting.
Set DD RAM 0 0 1 − − ADD − − − − Sets DD RAM address. DD
Address RAM data is sent and
received after this setting.
Read Busy 0 1 BF − − AC − − − − Reads Busy Flag (BF)
Flag & indicating internal operation
Address is being performed and
reads address counter
contents.
Write Data CG 1 0 − − − Write − − − − Writes data into DD or CG
or DD RAM Data RAM.
Read Data 1 1 − − − Read − − − − Read Data from DD or CG
CG or DD Data RAM.
RAM
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 4-31 Interfacing Serial Communication

SOLVED EXAMPLE
Example 4.9 : Write a program to send a command to LCD, make
pin RS = 0, for data RS = 1, then send a high to low pulse to E pin to
enable latch of LCD.
Solution :

8051
D0
P 1.0
LCD

ER/WRS
P 1.7
D7
+ 5 V VCC VEE VSS
P 2.0
P 2.1
P 2.2

Fig. 4.18 : LCD interfacing


P1.0 to P1.7 are connected to D0 - D7
P2.0 is connected to RS pin of LCD
P2.1 is connected to R/W pin of LCD
P2.2 is connected to E pin of LCD
ORG 0000
MOV A,#38H ; LCD 2 lines 5 × 7 matrix
ACALL COMSB ; CALL command subroutine
ACALL DELAY
MOV A,#0EH ; display and cursor ON
ACALL COMSB
ACALL DELAY
MOV A,#01H ; clear screen
ACALL COMSB
ACALL DELAY
MOV A,#06H ; shift cursor right
ACALL COMSB
ACALL DELAY
MOV A,#85H ; cursor at line 1 position 5
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 4-32 Interfacing Serial Communication

ACALL COMSB
ACALL DELAY
MOV A,#‘S’ ; displaying the message
“SCIENCE”
ACALL DISP
ACALL DELAY
MOV A,#‘C’
ACALL DISP
ACALL DELAY
MOV A,#‘I’
ACALL DISP
ACALL DELAY
MOV A,#‘E’
ACALL DISP
ACALL DELAY
MOV A,#‘N’
ACALL DISP
ACALL DELAY
MOV A,#‘C’
ACALL DISP
ACALL DELAY
MOV A,#‘E’
ACALL DISP
HERE : SJMP HERE
COMSB :
MOV P1,A ; copy A to port 1
CLR P2.0 ; RS = 0
CLR P2.1 ; R/W = 0
SETB P2.2 ; E = 1
CLR P2.2 ; E = 0 i.e. H to L pulse
DISP :
MOV P1, A
SETB P2.0
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 4-33 Interfacing Serial Communication

CLR P2.1
SETB P2.2
CLR P2.2
RET
DELAY : MOV R1, #4FH ; R1 = 4F
BACK 1 : MOV R4, #255 ; R4 = 255
BACK : DJNZ R4, BACK
DJNZ R1, BACK 1
RET
END
Program in ‘C’ for Interfacing LCD to 8051 Microcontroller:
# include C <reg51.h>
sbit lcd data = 0 × 80; // LCD data pins are
connected to port 0
sbit RS = P2^ 0;
sbit EN = P2^1;
void (main)
{
init_lcd(0×80);
MY_DELAY (250);
Write_lcd (‘S’);
MY_DELAY (250);
WRITE_lcd (‘C’);
MY_DELAY (250);
Write_lcd (‘I’);
MY_DELAY (250);
Write_lcd(‘E’);
MY_DELAY (250);
Write_lcd (‘N’);
MY_DELAY (250);
Write_lcd (‘C’);
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 4-34 Interfacing Serial Communication

MY_DELAY (250);
Write_lcd (‘E’);
MY_DELAY (250);
}
init_lcd (unsigned char num)
{
lcd data = num;
RS = 0;
EN = 1;
MY_DELAY (1);
EN = 0;
return ();
}
Write_lcd (unsigned char num)
{
lcd data = num;
RS = 1;
EN = 1;
MY_DELAY (1);
EN = 0;
return ();
}

4.5 Interfacing of Stepper motor


• A stepper motor is widely used to translate electrical pulses into
mechanical movement.
• Applications of stepper motor are disk drives, dot matrix printers,
robotics and position control.
• Steeper motor have a permanent magnet rotor (called as shaft)
surrounded by a starter as shown in Fig. 4.19.
• It has 4 stator windings that are paired with a center-tapped
common rotor as shown Fig. 4.19.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 4-35 Interfacing Serial Communication

A
S

Rotor
N

D B
N

S
S

S
Stator
N
C
(a) (b)
Fig. 4.19 Stepper motor
• The center tap allows a change of current direction.
• When a winding of each of two coils is grounded, it results in a
change in polarity of the stator. The stepper motor shaft moves in a
precise position.
Step Angle:
• Step angle is defined as ‘the minimum degree of rotation with a
single step’.
360°
Number of steps per revolution =
Step angle
(RPM × Steps per revolution)
Steps per second =
60
Example: Step angle = 2°
No. of steps per revolution = 180
4.5.1 Switching Sequence of Motor
• As discussed earlier the coils need to be energized for the rotation.
This can be done by sending a bits sequence to one end of the coil
while the other end is commonly connected.
• The bit sequence sent can make either one phase ON or two phase
ON for a full step sequence or it can be a combination of one and
two phase ON for half step sequence. Both are tabulated below.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 4-36 Interfacing Serial Communication

Full Step:
Two Phase ON
Clockwise Step # A B C D Counter-clockwise
1 1 0 0 1
2 1 1 0 0
3 0 1 1 0
4 0 0 1 1

One Phase ON
Clockwise Step # A B C D Counter-clockwise
1 1 0 0 0
2 0 1 0 0
3 0 0 1 0
4 0 0 0 1

The sequence is given in table below.


Clockwise Step # A B C D Anti-clockwise
1 1 0 0 1
2 1 0 0 0
3 1 1 0 0
4 0 1 0 0
5 0 1 1 0
6 0 0 1 0
7 0 0 1 1
8 0 0 0 1
4.5.2 Interfacing of Stepper Motor to 8051 Microcontroller
The interfacing of stepper motor to 8051 microcontroller is as shown
in below Fig. 4.20.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 4-37 Interfacing Serial Communication

5V 12V

C3
10mF
C1
40
22pF 19 39
XTAL1 VCC P0.0/AD0 38
24 MHz P0.1/AD1 37
crystal P0.2/AD2 36
C2 18 P0.3/AD3
XTAL2 35
22pF P0.4/AD4 34
P0.5/AD5 33
P0.6/AD6 32
9
RST P0.7/AD7
ULN2003
9
COM
P2.0/A8 21 1 1B 1C 16
P2.1/A9 22 2 15
2B 2C
P2.2/A10 23 3 14
R4 29 24 4 3B 3C
PSEN P2.3/A11 13
10k 30 ALE 25 5 4B 4C 12
P2.4/A12 5B 5C
31 P2.5/A13 26 6 11
EA 6B 6C
P2.6/A14 27 7 10
28 7B 7C
P2.7/A15 GND
10 8
1 P3.0/RXD
P1.0 11
2 P3.1/TXD 12
P1.1 P3.2/INT0
3 P1.2 13
4 P1.3 P3.2/INT1 14
5 P1.4 P3.4/T0
6 15
P1.5 P3.5/T1
7 16
8 P1.6 P3.6/WR
17
P1.7 GND P3.7/RD
AT89C51 20

Fig. 4.20 : Interfacing of stepper motor to 8051 microcontroller


• The above figure shows the interfacing of stepper motor which uses
ULN2003. 24MHz crystal is connected to provide the required clock
for the microcontroller. 10µF capacitor and 10kΩ is used to provide
Power On Reset (POR) for the 8051 microcontroller.
• When an input (1B) is high corresponding output (1C) will be
grounded. Thus, any windings of stepper motor can be energized.
The program for driving the stepper motor is given below:
C program For Wave Drive
#include<reg52.h>
#include<stdio.h>
void delay(int);
void main()
{
do
{
P2=0×01; //0001
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 4-38 Interfacing Serial Communication

delay(1000);
P2=0×02; //0010
delay(1000);
P2=0×04; //0100
delay(1000);
P2=0×08; //1000
delay(1000);
}
while(1);
}

void delay(int k)
{
int i,j;
for(i=0;i<k;i++)
{
for(j=0;j<100;j++)
{}
}
}
C Program for Full Drive
#include<reg52.h>
#include<stdio.h>

void delay(int);

void main()
{
do
{
P2 = 0×03; //0011
delay(1000);
P2 = 0×06; //0110
delay(1000);
P2 = 0×0C; //1100
delay(1000);
P2 = 0×09; //1001
delay(1000);
}
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 4-39 Interfacing Serial Communication

while(1);
}

void delay(int k)
{
int i,j;
for(i=0;i<k;i++)
{
for(j=0;j<100;j++)
{}
}
}
C program for Half Drive
#include<reg52.h>
#include<stdio.h>

void delay(int);

void main()
{
do
{
P2=0×01; //0001
delay(1000);
P2=0×03; //0011
delay(1000);
P2=0×02; //0010
delay(1000);
P2=0×06; //0110
delay(1000);
P2=0×04; //0100
delay(1000);
P2=0×0C; //1100
delay(1000);
P2=0×08; //1000
delay(1000);
P2=0×09; //1001
delay(1000);
} while(1);
}
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 4-40 Interfacing Serial Communication

void delay(int k)
{
int i,j;
for(i=0;i<k;i++)
{
for(j=0;j<100;j++)
{}
}
}
C program for Stepper Motor :
#include<reg52.h>
#include<stdio.h>
void delay(int);
void main()
{
do
{
P2=0×01; //0001
delay(1000);
P2=0×04; //0100
delay(1000);
P2=0×02; //0010
delay(1000);
P2=0×08; //1000
delay(1000);
}while(1);
}

void delay(int k)
{
int i,j;
for(i=0;i<k;i++)
{
for(j=0;j<100;j++)
{}
}
}
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 4-41 Interfacing Serial Communication

SOLVED EXAMPLES

Example 4.10: Write an ALP to rotate the stepper motor clockwise /


anticlockwise continuously with full step sequence.
Solution:
MOV A,#66H
BACK: MOV P1,A
RR A
ACALL DELAY
SJMP BACK
DELAY: MOV R1,#100
UP1: MOV R2,#50
UP: DJNZ R2,UP
DJNZ R1,UP1
RET
Note: Motor to rotate in anticlockwise use instruction RL A instead of RR
A
Example 4.11: Write an ALP to rotate a motor 90° clockwise. Step
angle of motor is 2°.
Solution:
Step angle = 2°
Steps per revolution = 180
Number of rotor teeth = 45
For 90° rotation the number of steps is 45.
Program:
ORG 0000H
MOV A,#66H
MOV R0,#45
BACK: RR A
MOV P1, A
ACALL DELAY
DJNZ R0, BACK
END
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 4-42 Interfacing Serial Communication

4.6 Study of Advance Microcontrollers


(ARM & PIC)
4.6.1 ARM Microcontrollers
• ARM is a family of instruction set architectures for computer
processors based on a Reduced Instruction Set Computing (RISC)
architecture developed by British company ARM Holdings.
• Being RISC architecture, ARM processors require significantly fewer
transistors than typical processors in average computers. This
approach reduces costs, heat and power use.
Features of ARM:
1. The relative simplicity of ARM machines is useful for low power
applications.
2. In-built DAC provides variable analog output.
3. In-built timers/external event counters, PWM unit and watchdog.
4. Low power real-time clock with independent power.
5. Multiple serial interfaces includes UARTs, I2C-bus, SPI and SSP
with buffering and variable data length capabilities.
6. Power saving modes include Idle and Power-down.
7. The technology used in AR is industry proven.
8. Programmers have the freedom to innovate on the right
processor feature set right from the start as well as a robust
roadmap for the future.
9. Lower costs enables programmers to deliver innovation at the
lowest possible price point.
10. ARM has long-term, scalable growth markets.
11. The low power consumption of ARM processors has made them
very popular.
Applications of ARM:
1. ARM is used in space and aerospace applications.
2. Terrestrial Radiation-sensitive (Nuclear Reactors, Particle
Accelerators and X-Ray Cargo Scanning).
3. Medical (CT, Ultrasound, MRI and implantable devices).
4. Automotive (ABS, EBS, Cruise and Airbag control devices).
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 4-43 Interfacing Serial Communication

5. It is used in battery-powered devices—including smartphones,


laptops, tablet and notepad computers, and other embedded
systems.
6. It is also used in digital televisions and set-top boxes and in
mobile computers.
Let us see the features of one of the controller from ARM family.
4.6.1.1 LPC2148 Controller
• LPC2148 is the widely used IC from ARM-7 family. It is manufactured
by Philips and it is pre-loaded with many inbuilt peripherals making it
more efficient and a reliable option for the beginners as well as high
end application developer.
Features of LPC214x Series Controllers:
1. 8 to 40 kB of on-chip static RAM and 32 to 512 kB of on-chip
flash program memory. 128-bit wide interface/accelerator
enables high speed 60 MHz operation.
2. In-System/In-Application Programming (ISP/IAP) via on-chip
boot-loader software. Single flash sector or full chip erase in 400
ms and programming of 256 bytes in 1ms.
3. Embedded ICE RT and Embedded Trace interfaces offer real-time
debugging with the on-chip Real Monitor software and high
speed tracing of instruction execution.
4. USB 2.0 Full Speed compliant Device Controller with 2 kB of
endpoint RAM. In addition, the LPC2146/8 provides 8 kB of on-
chip RAM accessible to USB by DMA.
5. One or two (LPC2141/2 vs. LPC2144/6/8) 10-bit A/D converters
provide a total of 6/14 analog inputs, with conversion times as
low as 2.44 µs per channel.
6. Single 10-bit D/A converter provides variable analog output.
7. Two 32-bit timers/external event counters (with four capture and
four compare channels each), PWM unit (six outputs) and
watchdog.
8. Low power real-time clock with independent power and
dedicated 32 kHz clock input.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 4-44 Interfacing Serial Communication

9. Multiple serial interfaces including two UARTs (16C550), two Fast


I2C-bus (400 kbit/s), SPI and SSP with buffering and variable data
length capabilities.
10. Vectored interrupt controller with configurable priorities and
vector addresses.
11. Up to 45 of 5 V tolerant fast general purpose I/O pins in a tiny
LQFP64 package.
12. Up to nine edge or level sensitive external interrupt pins
available.
13. On-chip integrated oscillator operates with an external crystal in
range from 1 MHz to 30 MHz and with an external oscillator up
to 50 MHz.
14. Power saving modes include Idle and Power-down.
15. Individual enable/disable of peripheral functions as well as
peripheral clock scaling for additional power optimization.
16. Processor wake-up from Power-down mode via external
interrupt, USB, Brown-Out Detect (BOD) or Real-Time Clock
(RTC).
17. Single power supply chip with Power-On Reset (POR) and BOD
circuits.
4.6.2 PIC Microcontrollers
• PIC microcontrollers have RISC architecture and it is manufactured by
the Microchip. The PIC microcontroller has many built-in peripherals.
• The powerful feature of PIC microcontroller is an internal RAM,
EPROM flash memory and peripherals.
• PIC has in-built ADC, memory, timers, high current I/O ports, a
comparator, USART, ADC, PWM and watchdog timer.
Features of PIC Microcontroller:
1. It provides customers a low-risk development environment and
offer seamless migration within the complete range of products.
2. It has In Circuit Serial Programming (ICSP), is the most important
benefit. Instead of transferring the chip from the programmer to
the development board it can be programmed in the board.
3. Contains high speed comparators.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 4-45 Interfacing Serial Communication

4. Microchip offers low power devices with peripherals like USB,


LCD and mTouch capacitive sensing.
5. A PIC Microcontroller can control outputs and react to inputs e.g.
can drive a relay or read input buttons.
6. With the larger devices it is possible to drive LCDs or seven
segment displays with very few control lines as all the work is
done inside the PIC Micro.
Applications of PIC Microcontrollers:
1. It is used in home alarm systems.
2. Smart doorbell systems.
3. In many embedded systems such as lighting control systems,
traffic lights etc.
4. It is also used in Smart phones, tablets etc.
Let us see the features of one of the PIC microcontroller.
4.6.2.1 PIC 16F877 Architecture
• PIC 16F877 is a 40-pin 8-Bit CMOS FLASH Microcontroller from
Microchip. The core architecture is high-performance RISC CPU with
only 35 single word instructions. It follows the RISC architecture. All
single cycle instructions take only one instruction cycle except for
program branches which take two cycles.
• 16F877 comes with 3 operating speeds with 4, 8, or 20 MHz clock
input. Since each instruction cycle takes four operating clock cycles,
each instruction takes 0.2 s when 20MHz oscillator is used.
• It has two types of internal memories: program memory and data
memory. Program memory is provided by 8 k bytes words (or 8 k
bytes, 14-bits) of FLASH Memory, and data memory has two sources.
• One type of data memory is a 368-byte RAM (random access
memory) and the other is 256-byte EEPROM (Electrically erasable
programmable ROM).
• The core feature includes interrupt capability upto 14 sources, power
saving SLEEP mode, and single 5V In-Circuit Serial Programming
(ICSP) capability. The sink/source current, which indicates a driving
power from I/O port, is high with 25mA. Power consumption is less
than 2mA in 5V operating condition.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 4-46 Interfacing Serial Communication

Features of Peripheral:
(a) 3 time blocks: Timer 0 for 8-bit timer/counter; Timer 1 for 16-bit
timer/counter; and Timer 2 : 8-bit timer/counter with 8-bit period
register, prescaler and postscaler.
(b) Two Capture, Compare, PWM modules for capturing, comparing
16-bit, and PWM generation with 10-bit resolution.
(c) 10-bit multi-channel (max 8) Analog-to-Digital converter
module.
(d) Synchronous Serial Port (SSP) with SPI ⋅ (Master Mode) and I2C2 ⋅
(Master/Slave).
(e) Universal Synchronous Asynchronous Receiver Transmitter
(USART/SCI) with 9-bit address detection.
(f) Parallel Slave Port (PSP) 8-bits wide, with external RD, WR and CS
controls.
(g) I/O ports.
The Key Feature of 16F877 :
o FLASH Program Memory (14-bit word) : 8 k Words
o Data Memory (RAM) : 368 Bytes
o Data Memory (EEPROM): 256 Bytes
o Interrupts : 14
o I/O Ports : A, B, C, D, E
o Timers : 3
o Capture/Compare/PWM Modules : 2
o Serial Communications MSSP, USART

Think Over It

• Can both RFID and GSM be interfaced together in a single chip


of 8051?
• What is the significance of ‘Power Management’ in case of
8051?
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 4-47 Interfacing Serial Communication

Points to Remember
• Analog to digital converters are most widely used in instrumentation
for data acquisition.
• ADC0808/0848/0809/0804 provides an 8-bit parallel data path
between the ADC chip and the CPU.
• The following steps must be followed for data conversion by the
ADC0804 chip : (1) Make CS = 0 and send low to high pulse to pin
WR to start the conversion (2) Keep monitoring the INTR pin. If INTR
is low, the conversion is finished and we can go to the next step. If
INTR is high, keep polling until it goes low. (3) After the INTR has
become low, we make CS = 0 and send a high to low pulse to the RD
pin to get the data out of ADC0804 chip.
• The speed at which an analog input is converted to the digital output
depends on the speed of the CLK input.
• The MAX1112 is an 8-bit serial ADC chip with 8 channel analog input.
• The Digital to Analog Converter (DAC) is a device widely used to
convert digital pulses to analog signal.
• The number of data bit inputs decides the resolution of the DAC
since the number of analog output level is equal to 2n, where n is the
number of data bit inputs.
• A stepper motor is widely used to translate electrical pulses into
mechanical movement. Applications of stepper motor are disk drives,
dot matrix printers, robotics and position control.
• Step angle is defined as ‘the minimum degree of rotation with a
single step’.
• ARM is a family of instruction set architectures for computer
processors based on a reduced instruction set computing (RISC)
architecture.
• The relative simplicity of ARM machines is useful for low power
applications.
• PIC microcontrollers have RISC architecture and it is manufactured by
the Microchip.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 4-48 Interfacing Serial Communication

Exercises
[A] True or False :
1. A stepper motor is widely used to translate electrical pulses into
mechanical movement.
2. The step angle is maximum degree of rotation associated with a
single step.
3. For the LCD to recognize information at the data pins as data, RS
must be set to high.
4. While ADC0804 has only one analog input, ADC0808 has 8 of them.
5. The output of DAC 0808 is not suitable to drive a motor.
[B] Fill in the Blanks :
1. In ADC0804, the INTR signal is an …… (Input, Output).
2. Both ADC0804, ADC0808 are …… (digital, analog) bit converters.
3. In DAC, input is …… and output is …… (digital, analog).
4. DAC0808 is a (n) …… bit D to A converter.
5. The RS pin is an …… (input, output) pin for LCD.
[C] Multiple Choice Questions :
1. The ADC0804 is an …… bit parallel ADC.
(a) 8 (b) 16
(c) 32 (d) none of these

2. To start conversion by ADC0804, we have to make CS = ……
(a) 1 (b) 0
(c) high to low state (d) none of these
3. The ADC0808 has …… number of analog channel.
(a) 8 (b) 16
(c) 32 (d) none of these
4. The MAX1112ADC is …… ADC.
(a) 8-bit parallel (b) 16-bit parallel
(c) 8-bit serial (d) 16-bit serial
5. Chip select (CS) is an …… input used to activate the ADC chip.
(a) active low (b) active high
(c) tristate (d) none of these
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 4-49 Interfacing Serial Communication

6. The step angle of a stepper motor is the …… degree of rotation


associated with a single step.
(a) maximum (b) minimum
(c) 360° (d) none of these
7. The contrast of LCD is controlled by pin ……
(a) VCC (b) VEE
(c) RS (d) none of these
8. …… pin is used to control Back Plane Light.
(a) BPL (b) LPB
(c) VCC (d) none of these
[D] Short Answer Questions :
1. Explain the concept of data converter.
2. What do you mean by ADC and DAC ? Explain with proper example.
3. Explain parallel and serial ADC.
4. Draw pin diagram of ADC0804 chip.
5. What is analog and digital ground ?
6. Explain the CLK IN and CLK R in ADC0808.
7. If R = 10 k and C = 150 pF calculate the CK frequency for ADC0808.
8. Write the steps followed for ADC0808 data converter.
9. Write the different ICs used for parallel DAC.
10. Draw pin configuration for ADC0808/0809.
11. Why it is essential to use serial ADC chips ?
12. List the various pins at MAX1112.
13. What is DAC ?
14. Write the equation for Iout in MC1408.
15. State the merits of LCDs over LEDs.
[E] Long Answer Questions :
1. Explain in brief the concept of parallel and serial ADC.
2. Explain the functions of pins of ADC0804.
3. Explain with proper example, the steps used for data conversion in
ADC.
4. Draw and explain the connection to ADC0804 with microcontroller
8051.
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 4-50 Interfacing Serial Communication

5. Draw and explain pin diagram of ADC0808.


6. Draw and explain the 8051 interfaced with serial ADC.
7. Find MAX1112 control byte for CH2.
8. How DAC is interfaced with microcontroller 8051 ?
9. Assuming that R = 5 kΩ, Iref = 2 mA. Calculate Vout for CBH
(11001010B).
10. Write a program for generation at crude square wave for the
microcontroller.
11. Draw and explain the diagram for LCD interface.
12. What are the pin position of LCD, elaborate with proper example.
13. List the command code of LCD. Explain how cursor return the home.
14. Write a program to display the message “WELCOME” on LCD with
8051 microcontroller.
15. Explain the features and applications of ARM microcontroller.
16. Explain the features and applications of PIC microcontroller.

Answers
[A] True or False :
(1) True (2) False (3) True
(4) True (5) False
[B] Fill in the blanks :
(1) input, (2) 8, (3) digital, analog, (4) 8, (5) input
[C] Multiple Choice Questions :
1. (a) 2. (b) 3. (a) 4. (c) 5. (a) 6. (b) 7. (b)
8. (a)

University Questions
April 2015
1. If digital input is FCh, then find the output voltage if Iref = 2 mA and
R = 10 kΩ. (2M)
2. Which pin of LCD is used for controlling contrast ? (2M)
3. Draw an interfacing diagram to show how stepper motor is
connected to 8051. Write a ‘C’ program to make the interface work.
(5M)
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 4-51 Interfacing Serial Communication

4. Mention important features of PIC microcontroller. (2M)


5. Draw pin configuration of ADC0804 and explain the function of each
pin. (5M)

October 2015
1. What is step size of 8-bit ADC if Vin is 2.56 volt ? (2M)
2. Define step angle of stepper motor. (2M)
3. Draw an interfacing diagram to show how LCD display is interface to
8051. Write a ‘C’ program to make the interface work. (5M)
4. Explain in brief working of stepper motor. (5M)
5. Write a short note on applications of ARM and PIC microcontroller.
(2M)

April 2016

1. Write full form of ARM and PIC. (1M)


2. What is the step size of 8 bit ADC if Vref is 2.5 volts? (1M)
3. Draw a neat diagram of interfacing stepper motor to 8051. Write a ‘C’
program to make this interface work. (5M)
4. Write any three features of ARM controller. (3M)
5. Draw a block diagram of interfacing DAC with 8051 and write a ‘C’
program to generate triangular waveform. (5M)

April 2017

1. What is the step size of 8-bit ADC if Vref = 2.56 volts. (1M)
2. Which pin of LCD is used for controlling the contrast? (1M)
3. Draw a schematic to show how a DAC can be interfaced to 8051.
Write a ‘C’ program to generate sawtooth waveform. (5M)
4. List any three features of PIC microcontroller. (3M)
5. Draw a schematic to show how stepper motor can be interfaced to
8051 microcontroller. (5M)

April 2018
1. What is the step size of 8 bit ADC if Vref = 4.5 volts? (1M)
2. Which pin of LCD is used to control the contrast? (1M)
3. Write any two features of ARM controller. (1M)
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 4-52 Interfacing Serial Communication

4. Draw a neat diagram of interfacing 8-bit ADC (Analog to Digital


Converter) to 8051 microcontroller and write a ‘C’ program to run the
interface. (1M)
5. Draw a neat diagram of interfacing stepper motor to 8051
microcontroller and write a ‘C’ program to run the motor. (5M)

April 2019
1. Write two applications of ARM processor. (1M)
2. What is the maximum input voltage which can be applied to ADC
0804 if Vref / 2= 2V? (1M)
3. Which pin of LCD is used for controlling the contrast? (1M)
4. Write 8051 ‘C’ program to rotate a stepper motor connected to port
1 in clockwise direction continuously. (5M)
5. Draw a block diagram of interfacing DAC to port 1 of 8051
microcontroller. Write a ‘C’ program to generate triangular
waveform. (5M)

✍✍✍

You might also like