Microcontroller Architecture and Programming Electronics Paper I
Microcontroller Architecture and Programming Electronics Paper I
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
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.
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
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 ….
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 ….
Internal
Internal ROM Interrupt
RAM circuits
Program counter
Port 0
Latch
and PSW function
registers A0-A7
logic unit
RAM D0-D7
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)
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
• 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
• 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 = 07 Address 07 SP = 07
Storing data Internal RAM Getting data
on the stack from the
(increment stack (get then
then store) decrement)
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
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 ….
10 mf
30 pF EA/VPP
X1
11.052 MHz
X2
30 pF
RST
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
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
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 ….
D7
D0
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 ….
D7
D0
• 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 2 A8-A15
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
C1 = 30 PF
18 XTAL2
Crystal
or 8051
Ceramic resonator
19 XTAL1
C2 = 30 PF
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
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 ….
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
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
284d 1 00011100b
S.Y.B.Sc. (Comp. Sci.) : Elect. - P-I - S-III 2-7 Programming Model of 8051
− 080d 1 1 0 1 1 0 0 0 0 b = − 80 d
− 085d 1 10101011b
− 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
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
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
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
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
7 6 5 4 3 2 1 0
RR A
7 6 5 4 3 2 1 0 C
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
FFFF
LADD limit
Next page
SADD limit
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
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
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
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
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
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. 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
SOLVED EXAMPLES
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
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
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
• 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
g f com a b
e d com c dot
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
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
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
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
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
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
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
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
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
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
• 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
GATE − M1 M0 GATE − M1 M0
C/T C/T
Timer 1 Timer 0
XTAL ..
oscillator 12
To timer
stages
Counter
T1/0 input pin C/T = 1
(TMOD counter operation)
TR1/0 Bit in TCON
Reload TLX
THX 8-bits
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
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
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
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
Parallel transfer
D0
Serial
transfer
Sender Receiver
Sender Receiver
D7
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
• 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
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
• 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
(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.
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
1 000BH
Jump to
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
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
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
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
P1.3
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
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
Think Over It
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
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
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
April 2017
April 2018
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
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
• 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 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.
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
+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
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
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
SCLK
MAX1112 CS
CH7 DIN
REFIN DOUT
REFOUT
SHDN SSTRB
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
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
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
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
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
∴ 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
• 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
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
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];
}
}
}
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
1. VSS − Ground
D0 - D7 Data
tD
R/ W tAS tAH
RS
Data
E tDSW
tH
tPWH
R/ W tAS tAH
RS
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
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 ();
}
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
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
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
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
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
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
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
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
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)
✍✍✍