0% found this document useful (0 votes)
25 views

8051 Microcontroller (Final)

Uploaded by

Sakthi sundharam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

8051 Microcontroller (Final)

Uploaded by

Sakthi sundharam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 65

SRI VASAVI COLLEGE, ERODE

(SELF FINANCE WING)

DEPARTMENT OF ELECTRONICS

Prepared By
Dr. S. KUMAR M.Sc., M.Phil., PGDCA., Ph.D.,
Assistant Professor of Electronics,
Sri Vasavi College (SF Wing),
Erode - 638 316.
SYLLABUS
8051 MICROCONTROLLER AND ITS APPLICATIONS
8051 MICROCONTROLLER AND ITS
Course code 53A L T P C
APPLICATIONS
Core/Elective/Supportive Core Paper IX 6 0 0 4
Syllabus
Pre-requisite Digital Electronics 8085 Microprocessor 2020-21
Version
Course Objectives:
The main objectives of this course are to:
 To Study the architecture and addressing modes of8051
 To Impart knowledge about assembly language programs of8051
 To understand the importance of different peripheral devices and their interfacing to 8051
 To Impart knowledge of different types of external interfaces including LCD, Keypad
Matrix, Stepper motor and sensors

Expected Course Outcomes:


On the successful completion of the course, student will be able to:
1 Describe architecture and operation of Microcontroller 8051 K1
2 Foster ability to understand the design concept of interfacing Microcontroller with K2
various peripherals
3 Analyse the data transfer and interfacing techniques K3
4 Foster ability to understand the role of embedded systems in industry K4
5 Analyze the data transfer through serial and parallel ports. K5

K1 - Remember; K2 - Understand; K3 - Apply; K4 - Analyze; K5 - Evaluate; K6 - Create

Unit:1 Microcontroller Overview 16 hours


Introduction to Microcontroller – Comparison of μP & μC - 8051 Microcontroller Block Diagram
- 8051 Oscillator and Clock – A, B & Register Banks – Stack - Program Counter & Data Pointer
-Flag & PSW – Special Function Registers – Internal Memory – Input / Output Pins & Ports.

Unit:2 Addressing Modes, Arithmetic And Logical Instructions 18 hours


Introduction – Addressing Modes: Direct – Indirect – Register - Indexed – Arithmetic
Instructions: Addition, Subtraction, Multiplication, Division, Increment And Decrement – Logic
& Compare Instructions: AND, OR, XOR, CPL & Compare – Rotate & Swap Instruction: RR,
RL, RRC, RLC - Simple Programs.

Unit:3 Data Transfer And Branch Operations 18 hours


Introduction – Internal Data Move - External Data Move – Code Memory Read Only Data
Move - Loop and Jump Instructions – Conditional Jump – Unconditional Jump – Call
Instructions: LCALL and ACALL – Push and Pop Instructions – Simple Programs.
Unit:4 Peripherals 18 hours
Timer and Counter: Timer Registers – TMOD – TCON - Mode 1 and 2 Programming – Counter
Programming – Serial: MAX232 – Baud Rate – SBUF Register – SCON Register – Program to
Transfer and Receive Data Serially - Interrupts: Enabling & Disabling Interrupt.

Unit:5 Interfacing 18 hours


Interfacing of LCD – LCD Operation – Pin Descriptions – Command and Data to the LCD –
ADC 0804 – LM34 Temperature Sensor – Stepper Motor – Step Angle – Motor Speed –
Interfacing Key Board – Scanning and Identifying the keys.

Unit:6 Contemporary Issues 2 hours


Addressing Modes, Interrupts

Total Lecture Hours 90 hours


Text Book(s)
1 Mohamed Ali Maszidi & Janice Gillispie Maszidi, “The 8051 Microcontroller and
Embedded System”, Pearson Publishers
2 Kenneth J. Ayala, “The 8051 Microcontroller Architecture, Programming and
Application” 2 nd Edition, Penram Int ernational Publications

Reference Books
1 Myke predko, “Programming and Customizing the 8051 Microcontroller”, Tata
McGraw Hill, 1st Edition
2 Ayala J.K., - The 8051 Microcontroller: Architecture, programming and applications”,
Penram International (2005) 3rd edition.

Related Online Contents [MOOC, SWAYAM, NPTEL, Websites etc.]


1 https://www.youtube.com/watch?v=84YUQu8tE4w
2 https://www.youtube.com/watch?v=GPz_mR7Flas
3 https://www.youtube.com/watch?v=uFhDGagZzjs

Course Designed By: M.Baskaran, Head & Assistant Professor, KSG College of Arts and
Science,Coimbatore.& Dr.N Om Muruga , Assistant Professor, Government Arts College ,Ooty.

Mapping with Programme Outcomes


COs PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10
CO1 S S S M M M S L L S
CO2 S S S M M M S L L L
CO3 S S S M M L L S S M
CO4 M M M S S S L L M M
CO5 M M S S S L M M S S
*S-Strong; M-Medium; L-Low
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR

UNIT - I
MICROCONTROLLER OVERVIEW

INTRODUCTION TO MICROCONTROLLER:
What is a Microcontroller?
A microcontroller is an electronic device belonging to the microcomputer family. These are
fabricated using the VLSI technology on a single chip. There are microcontrollers available in
the present market with different word length starting from 4 bit, 8 bit, 64 bit to 128 bit. This
chapter is about microcontrollers, their architecture, and various features.

[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 1
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR

INTRODUCTION TO µP & µC :

Microprocessor µP:

 It has only the CPU inside; i.e the processing powers such as Intel‟s Pentium 1,2,3,4
core 2 duos, i3, i5 etc.
 Don‟t have RAM, ROM and other peripheral on the chip. The system designer has to
add them externally to make them functional.
 Application includes desktop PCs, laptops, notepads etc.
 Applications are where tasks are unspecific like developing software, games, websites,
photo editing, creating documents etc.
 Since microprocessors cannot be used stand alone as it needs RAM, ROM and other
peripherals the system that uses microprocessors is costlier than a microcontroller.
 The clock speed of the microprocessor is quite high as compared to the microcontroller.
This can operate above 1 GHz as they perform complex tasks.

[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 2
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR

Microcontroller µC:

 In a microcontroller CPU, RAM, ROM, and other peripherals are embedded on a single
chip.
 At times it is termed a mini computer or a computer on a single chip.
 Some giants in the manufacturing business of microcontrollers are ATMEL, microchip,
TI, Freescale, Philips, Motorola etc.
 Designed to perform specific tasks. ie, the relationship between the input and output is
defined.
 Since the applications are very specific, they need small resources like RAM, ROM, I/O
ports and hence can be embedded on a single chip.
 The clock speed of a microcontroller varies from a few MHz to 30-50 MHz.

Difference Between μP and μC

MICROPROCESSORS MICROCONTROLLER

 Contains ALU,GP registers, SP,  In addition, it contains inbuilt


PC, clock timing circuit and ROM, RAM I/O devices,
interrupts Timers/Counters, etc..,

 Many instructions to move data  One or two instructions to move


between Memory and CPU data between memory and CPU

 One or two bit handling  Powerful Boolean processor


instructions are available instruction set is available

 Access time for memory and I/O  Less access time for inbuilt
devices are more memory and I/O devices

 Requires more hardware,  Requires less hardware, reduced


increase in PCB size PCB size and increased reliability

 More flexible from design point  Less Flexible


of view
 Separate memory Map for data and
 Single memory map for data and code
code
 More pins are multifunctional
 Few pins are multifunctional
 Microcontroller based systems are
 Microprocessor based computers normally operated with RTOS like
are normally operated with tiny RTOS, VxWorks, PSOS, RT
general purpose operating Linux, etc..,
systems like Windows, Unix etc.,

[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 3
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR

Applications

 Peripheral controller of a PC
 Robotics
 In bio-medical equipment
 In communication system
 In automobiles
 In fire detection devices
 In light and temperature sensing and controlling devices
 Process control and industrial automation devices
 In measuring devices such as volt and current meters

8051 MICROCONTROLLER BLOCK DIAGRAM:

[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 4
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR

8051 Microcontroller PIN Diagram:

8051 Oscillator and Clock:


What is Oscillator?

An oscillator is a circuit which produces a continuous, repeated, alternating waveform without


any input. Oscillators basically convert unidirectional current flow from a DC source into an
alternating waveform which is of the desired frequency, as decided by its circuit components.

The 8051 Has On Chip Oscillator Pin XTAL1 And XTAL2 Are Provided For Connecting A
Resonant Network To From An Oscillator Crystal Frequency Ranges From 1 MHZ To 24
MHZ. The oscillator formed by the crystal, capacitors, and on chip inverter generates pulse
train at the frequency of the crystal as shown in fig.

[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 5
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR

[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 6
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR

MEMORY:

The 8051 has generally two types of memory:

1. Program memory (ROM)

2. Data memory (RAM)

Program Memory (ROM) is used to permanently save the program being executed, while
Data Memory (RAM) is used for temporarily storing data and intermediate results created and
used during the operation of the microcontroller.

Program Memory: Internal Memory:


Program/Code memory is the memory that holds the actual 8051 program that is to be run.
This memory is limited to 64 KB. The first models of the 8051 microcontroller family (for
example 8031 or 8032) did not have internal program memory, so it was added as an external
separate chip. All later models have a few Kbyte of on-chip ROM. For example 8051 has 4KB
and 8052 has 8KB of on-chip ROM.

Program that is to be run can be stored in internal as well as in external ROM but from where
microcontroller will read program depends on the logic of EA pin(microcontroller 31 no. pin).

If /EA=0: MC will Read Program from External ROM

If /EA=1: MC will Read Program from Internal ROM

[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 7
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR

Data Memory:
 Data Memory (RAM) is used for temporarily storing data and intermediate results
created and used during the operation of the microcontroller. It is often associated with
volatile types of memory where its stored information is lost if the power is removed.
 Internal RAM is limited to 128 bytes (256 bytes for 8052), the 8051 supports External
RAM up to 64K.
 RAM of 8051 is divided in basic three blocks which are presented in the following
memory map:

[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 8
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR

[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 9
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR

A or Accumulator (ACC) Register:


The Accumulator or Register A is the most important and most used 8051 Microcontroller
SFRs. The Register A is located at the address E0H in the SFR memory space. The
Accumulator is used to hold the data for almost all the ALU Operations.

Some of the operations where the Accumulator is used are:

 Arithmetic Operations like Addition, Subtraction, Multiplication etc.


 Logical Operations like AND, OR, NOT etc.
 Data Transfer Operations (between 8051 and External Memory)

The name “Accumulator” came from the fact this register is used to accumulate (or store) the
result of all Arithmetic and most of the Logical Operations.

B Register:
The B Register is used along with the ACC in Multiplication and Division operations. These
two operations are performed on data that are stored only in Registers A and B. During
Multiplication Operation, one of the operand (multiplier or multiplicand) is stores in B
Register and also the higher byte of the result.

In case of Division Operation, the B Register holds the divisor and also the remainder of the
result. It can also be used as a General Purpose Register for normal operations and is often
used as an Auxiliary Register by Programmers to store temporary results.

Register B is located at the address F0H of the SFR Address Space.

Register Banks:
 The first 32 bytes of RAM (from location 00 to 1F hex) are set aside for the register
banks. These 32 bytes are divided into 4 banks each including 8 registers denoted by
R0-R7.

[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 10
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR

 RAM locations from 00 to 07 hex are set aside for bank 0 in which address location 00
hex is denoted by R0, address location 01 hex is denoted by R1 and so on to address
location 07 hex which is denoted by R7.

 Each register from R0 to R7 is of 1 byte.

 RAM locations from 08 to 0F hex are set aside for bank 1 in which address location 08
hex is denoted by R0, address location 09 hex is denoted by R1 and so on to address
location 0F hex which is denoted by R7.

 Similarly RAM locations from 10 to 17 hex are set aside for register bank 2 and RAM
locations from 18 to 1F hex are set aside for register bank 3.

We can switch to other register banks by use of D4 and D3 bits of PSW (program status word)
register. Any register bank can be selected by changing bits in RS1 and RS0 which are D4 and
D3 bits of PSW.

Stack Pointer (SP):


SP or Stack Pointer points out to the top of the Stack and it indicates the next data to be
accessed. Stack Pointer can be accesses using PUSH, POP, CALL and RET Instructions. The
Stack Pointer is an 8-bit register and upon reset, the Stack Pointer is initialized with 07H.

When writing a new data byte into the stack, the SP (Stack Pointer) is automatically
incremented by 1 and the new data is written at an address SP+1. When reading data from
stack, the data is retrieved from the Address in SP and after that the SP is decremented by 1
(SP-1).

[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 11
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR

Program Counter & Data Pointer:


Program Counter:
Another important register in the 8051 is the PC (program counter). The program counter
points to the address of the next instruction to be executed. As the CPU fetches the opcode
from the program ROM, the program counter is incremented to point to the next instruction.
The program counter in the 8051 is 16 bits wide. This means that the 8051 can access program
addresses 0000 to FFFFH, a total of 64K bytes of code.

Data Pointer Registers:


Data Pointer (DPTR – DPL and DPH)

The Data Pointer is a 16-bit Register and is physically the combination of DPL (Data Pointer
Low) and DPH (Data Pointer High) SFRs. The Data Pointer can be used as a single 16- bit
register (as DPTR) or two 8-bit registers (as DPL and DPH).

DPTR doesn‟t have a physical Memory Address but the DPL (Lower Byte of DPTR) and DPH
(Higher Byte of DPTR) have separate addresses in the SFR Memory Space. DPL = 82H and
DPH = 83H. The DPTR Register is used by the programmer addressing external memory
(Program – ROM or Data – RAM).

[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 12
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR

Flag & PSW:


Program Status Word (PSW):
The PSW or Program Status Word Register is also called as Flag Register and is one of the
important SFRs. The PSW Register consists of Flag Bits, which help the programmer in
checking the condition of the result and also make decisions.

Flags are 1-bit storage elements that store and indicate the nature of the result that is generated
by execution of certain instructions. The following image shows the contents of the PSW
Register.

The following table describes the function of each flag.

[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 13
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR

Special Function Registers:

[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 14
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR

What Are SFRs?


 The 8051 is a flexible microcontroller with a relatively large number of modes of
operations. Your program may inspect and/or change the operating mode of the 8051 by
manipulating the values of the 8051‟s Special Function Registers (SFRs).

 SFRs are accessed as if they were normal Internal RAM. The only difference is that
Internal RAM is from address 00h through 7Fh whereas SFR registers exist in the
address range of 80h through FFh.

 Each SFR has an address (80h through FFh) and a name. The following chart provides a
graphical presentation of the 8051‟s SFRs, their names, and their address.

Peripheral Control Registers


PCON (Power Control)
The PCON or Power Control register, as the name suggests is used to control the 8051
Microcontroller‟s Power Modes and is located at 87H of the SFR Memory Space. Using two
bits in the PCON Register, the microcontroller can be set to Idle Mode and Power Down
Mode.

SCON (Serial Control)


The Serial Control or SCON SFR is used to control the 8051 Microcontroller‟s Serial Port. It is
located as an address of 98H. Using SCON, you can control the Operation Modes of the Serial
Port, Baud Rate of the Serial Port and Send or Receive Data using Serial Port. SCON Register
also consists of bits that are automatically SET when a byte of data is transmitted or received.

TCON (Timer Control)


Timer Control or TCON Register is used to start or stop the Timers of 8051 Microcontroller. It
also contains bits to indicate if the Timers has overflowed. The TCON SFR also consists of
Interrupt related bits.

[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 15
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR

TMOD (Timer Mode)

The TMOD or Timer Mode register or SFR is used to set the Operating Modes of the Timers
T0 and T1. The lower four bits are used to configure Timer0 and the higher four bits are used
to configure Timer1.

IE (Interrupt Enable)

The IE or Interrupt Enable Register is used to enable or disable individual interrupts. If a bit is
SET, the corresponding interrupt is enabled and if the bit is cleared, the interrupt is disabled.
The Bit7 of the IE register i.e. EA bit is used to enable or disable all the interrupts.

IP (Interrupt Priority)

The IP or Interrupt Priority Register is used to set the priority of the interrupt as High or Low.
If a bit is CLEARED, the corresponding interrupt is assigned low priority and if the bit is SET,
the interrupt is assigned high priority.

Peripheral Data Registers


SBUF (Serial Data Buffer)

The Serial Buffer or SBUF register is used to hold the serial data while transmission or
reception.

[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 16
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR

TL0/TH0 (Timer 0 Low/High)

The Timer 0 consists of two SFRs: TL0 and TH0. The TL0 is the lower byte and the TH0 is
the higher byte and together they form a 16-bit Timer0 Register.

TL1/TH1 (Timer 1 Low/High)

The TL1 and TH1 are the lower and higher bytes of the Timer 0.

[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 17
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR

Input / Output Pins & Ports:


I/O Port Registers (P0, P1, P2 and P3)

The 8051 Microcontroller four Ports which can be used as Input and/or Output. These four
ports are P0, P1, P2 and P3. Each Port has a corresponding register with same names (the Port
Registers are also P0, P1, P2 and P3). The addresses of the Port Registers are as follows: P0 –
80H, P1 – 90H, P2 – A0H and P2 – B0H.

Each bit in these SFRs corresponds to one physical Pin in the 8051 Microcontroller. All these
Port Registers are both Bit Addressable and Byte Addressable. Writing 1 or 0 on a Port
Register Bit will reflect as an appropriate voltage (5V and 0V) on the corresponding Pin.

If a Port Bit is SET (declared as 1), the corresponding Port Pin will be configured as Input and
similarly if a Port Bit is CLEARED (declared as 0), the corresponding Port Pin is configured
as Output. Upon reset, all the Port Bits are SET (1) and hence, all the Port Pins are configured
as Inputs.

[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 18
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR

[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 19
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR

UNIT - II
ADDRESSING MODES, ARITHMETIC AND LOGICAL
INSTRUCTIONS

Introduction:
Writing a Program for any Microcontroller consists of giving commands to the
Microcontroller in a particular order in which they must be executed in order to perform a
specific task. The commands to the Microcontroller are known as a Microcontroller‟s
Instruction Set.

Just as our sentences are made of words, a Microcontroller‟s (for that matter, any computer)
program is made of Instructions. Instructions written in a program tell the Microcontroller
which operation to carry out. An Instruction Set is unique to a family of computers. This
tutorial introduces the 8051 Microcontroller Instruction Set also called as the MCS-51
Instruction Set. As the 8051 family of Microcontrollers are 8-bit processors, the 8051
Microcontroller Instruction Set is optimized for 8-bit control applications. As a typical 8-bit
processor, the 8051 Microcontroller instructions have 8-bit Opcodes. As a result, the 8051
Microcontroller instruction set can have up to 28 = 256 Instructions.

A Brief Look at 8051 Microcontroller Instructions and Groups

The following table shows the 8051 Instruction Groups and Instructions in each group. There
are 49 Instruction Mnemonics in the 8051 Microcontroller Instruction Set and these 49
Mnemonics are divided into five groups.
DATA ARITHMETIC LOGICAL BOOLEAN PROGRAM
TRANSFER BRANCHING
MOV ADD ANL CLR LJMP
MOVC ADDC ORL SETB AJMP
MOVX SUBB XRL MOV SJMP
PUSH INC CLR JC JZ
POP DEC CPL JNC JNZ
XCH MUL RL JB CJNE
XCHD DIV RLC JNB DJNZ
DA A RR JBC NOP
RRC ANL LCALL
SWAP ORL ACALL
CPL RET
RETI
JMP

[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 20
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR

Addressing Modes:
What is an Addressing Mode?
An Addressing Mode is a way to locate a target Data, which is also called as Operand. The
8051 Family of Microcontrollers allows five types of Addressing Modes for addressing the
Operands. They are:

 Immediate Addressing
 Register Addressing
 Direct Addressing
 Register – Indirect Addressing
 Indexed Addressing

Immediate Addressing
 In Immediate Addressing mode, the operand, which follows the Opcode, is a constant
data of either 8 or 16 bits. The name Immediate Addressing came from the fact that the
constant data to be stored in the memory immediately follows the Opcode.

 The constant value to be stored is specified in the instruction itself rather than taking
from a register. The destination register to which the constant data must be copied
should be the same size as the operand mentioned in the instruction.

Example: MOV A, #030H


 Here, the Accumulator is loaded with 30 (hexadecimal). The # in the operand indicates
that it is a data and not the address of a Register. Immediate Addressing is very fast as
the data to be loaded is given in the instruction itself.

Register Addressing
 In the 8051 Microcontroller Memory Organization Tutorial, we have seen the
organization of RAM and four banks of Working Registers with eight Registers in each
bank. In Register Addressing mode, one of the eight registers (R0 – R7) is specified as
Operand in the Instruction.

 It is important to select the appropriate Bank with the help of PSW Register. Let us see
a example of Register Addressing assuming that Bank0 is selected.

Example: MOV A, R5

 Here, the 8-bit content of the Register R5 of Bank0 is moved to the Accumulator.

[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 21
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR

Direct Addressing
 In Direct Addressing Mode, the address of the data is specified as the Operand in the
instruction. Using Direct Addressing Mode, we can access any register or on-chip
variable. This includes general purpose RAM, SFRs, I/O Ports, Control registers.

Example: MOV A, 47H

 Here, the data in the RAM location 47H is moved to the Accumulator.

Register – Indirect Addressing


 In the Indirect Addressing Mode or Register Indirect Addressing Mode, the address of
the Operand is specified as the content of a Register. This will be clearer with an
example.

Example: MOV A, @R1

 The @ symbol indicates that the addressing mode is indirect. If the contents of R1 is
56H, for example, then the operand is in the internal RAM location 56H. If the contents
of the RAM location 56H is 24H, then 24H is moved into accumulator.

 Only R0 and R1 are allowed in Indirect Addressing Mode. These register in the indirect
addressing mode are called as Pointer registers.

Indexed Addressing
 With Indexed Addressing Mode, the effective address of the Operand is the sum of a
base register and an offset register. The Base Register can be either Data Pointer
(DPTR) or Program Counter (PC) while the Offset register is the Accumulator (A).

 In Indexed Addressing Mode, only MOVC and JMP instructions can be used. Indexed
Addressing Mode is useful when retrieving data from look-up tables.

Example: MOVC A, @A+DPTR


 Here, the address for the operand is the sum of contents of DPTR and Accumulator.

[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 22
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR

Classifications of Instruction sets:


Based on the operation they perform, all the instructions in the 8051 Microcontroller
Instruction Set are divided into five groups. They are:

 Data Transfer Instructions


 Arithmetic Instructions
 Logical Instructions
 Boolean or Bit Manipulation Instructions
 Program Branching Instructions

Arithmetic Instructions:
Addition, Subtraction, Multiplication, Division, Increment And
Decrement:
Using Arithmetic Instructions, you can perform addition, subtraction, multiplication and
division. The arithmetic instructions also include increment by one, decrement by one and a
special instruction called Decimal Adjust Accumulator. The Mnemonics associated with the
Arithmetic Instructions of the 8051 Microcontroller Instruction Set are:

Mnemonic Description

ADD Addition without Carry

ADDC Addition with Carry

SUBB Subtract with Carry

INC Increment by 1

DEC Decrement by 1

MUL Multiply

DIV Divide

DA A Decimal Adjust the Accumulator (A Register)

The arithmetic instructions have no knowledge about the data format i.e., signed, unsigned,
ASCII, BCD, etc. Also, the operations performed by the arithmetic instructions affect flags
like carry, overflow, zero, etc. in the PSW Register. All the possible Mnemonics associated
with Arithmetic Instructions are mentioned in the following table.

[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 23
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR

Logic & Compare Instructions:


Logic Instructions: (AND, OR, XOR, CPL)

Rotate & Swap Instruction:


RR, RL, RRC, RLC:
The next group of instructions are the Logical Instructions, which perform logical operations
like AND, OR, XOR, NOT, Rotate, Clear and Swap. Logical Instruction are performed on
Bytes of data on a bit-by-bit basis. Mnemonics associated with Logical Instructions are as
follows:

[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 24
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR

Mnemonic Description
ANL Logical AND
ORL Logical OR
XRL Ex-OR
CLR Clear Register
CPL Complement the Register
RL Rotate a Byte to Left
RLC Rotate a Byte and Carry Bit to Left
RR Rotate a Byte to Right
RRC Rotate a Byte and Carry Bit to Right
SWAP Exchange lower and higher nibbles in a Byte

[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 25
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR

Program Branching Instructions Compare Instructions: (Compare):


The last group of instructions in the 8051 Microcontroller Instruction Set are the Program
Branching Instructions. These instructions control the flow of program logic. The mnemonics
of the Program Branching Instructions are as follows.
Mnemonic Description
LJMP Long Jump (Unconditional)
AJMP Absolute Jump (Unconditional)
SJMP Short Jump (Unconditional)
JZ Jump if A is equal to 0
JNZ Jump if A is not equal to 0
CJNE Compare and Jump if Not Equal
DJNZ Decrement and Jump if Not Zero
NOP No Operation
LCALL Long Call to Subroutine
ACALL Absolute Call to Subroutine (Unconditional)
RET Return from Subroutine
RETI Return from Interrupt
JMP Jump to an Address (Unconditional)

[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 26
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR

Simple Programs:
Refer Record Note

[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 27
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR

UNIT - III
DATA TRANSFER AND BRANCH OPERATIONS

DATA TRANSFER:
The Data Transfer Instructions are associated with transfer of data between registers or
external program memory or external data memory. The Mnemonics associated with Data
Transfer are given below.
Mnemonic Description
MOV Move Data
MOVC Move Code
MOCX Move External Data
PUSH Move Data to Stack
POP Copy Data from Stack
XCH Exchange Data between two Registers
XCHD Exchange Lower Order Data between two Registers

[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 28
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR

Internal Data Move, External Data Move:

Code Memory Read Only Data Move:


READ ABOVE

Loop and Jump Instructions:


There are two types of jump instructions in 8051 microcontroller.
 Conditional Jump
 Unconditional Jump

Conditional Jump:

Instruction Action

JZ Jump if A = 0

JNZ Jump if A ≠ 0

DJNZ Decrement and Jump if register ≠ 0

CJNE A, data Jump if A ≠ data

CJNE reg, #data Jump if byte ≠ data

JC Jump if CY = 1

JNC Jump if CY ≠ 1

JB Jump if bit = 1

JNB Jump if bit = 0

JBC Jump if bit = 1 and clear bit

 JZ (jump if A = 0) − In this instruction, the content of the accumulator is checked. If it


is zero, then the 8051 jumps to the target address. JZ instruction can be used only for
the accumulator, it does not apply to any other register.

 JNZ (jump if A is not equal to 0) − In this instruction, the content of the accumulator is
checked to be non-zero. If it is not zero, then the 8051 jumps to the target address.

 JNC (Jump if no carry, jumps if CY = 0) − The Carry flag bit in the flag (or PSW)
register is used to make the decision whether to jump or not "JNC label". The CPU
looks at the carry flag to see if it is raised (CY = 1). If it is not raised, then the CPU
starts to fetch and execute instructions from the address of the label. If CY = 1, it will
not jump but will execute the next instruction below JNC.

[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 29
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR

 JC (Jump if carry, jumps if CY = 1) − If CY = 1, it jumps to the target address.

 JB (jump if bit is high)

 JNB (jump if bit is low)

Unconditional Jump:
There are two unconditional jumps in 8051.
 LJMP (long jump)
 SJMP (short jump)
LJMP (long jump) − LJMP is 3-byte instruction in which the first byte represents opcode,
and the second and third bytes represent the 16-bit address of the target location. The 2-byte
target address is to allow a jump to any memory location from 0000 to FFFFH.

SJMP (short jump) − It is a 2-byte instruction where the first byte is the opcode and the
second byte is the relative address of the target location. The relative address ranges from 00H
to FFH which is divided into forward and backward jumps; that is, within –128 to +127 bytes
of memory relative to the address of the current PC (program counter). In case of forward
jump, the target address can be within a space of 127 bytes from the current PC. In case of
backward jump, the target address can be within –128 bytes from the current PC.

Call Instructions:
CALL is used to call a subroutine or method. Subroutines are used to perform operations or
tasks that need to be performed frequently. This makes a program more structured and saves
memory space. There are two instructions
 LCALL
 ACALL.

LCALL and ACALL:


LCALL (Long Call)
LCALL is a 3-byte instruction where the first byte represents the opcode and the second and
third bytes are used to provide the address of the target subroutine. LCALL can be used to call
subroutines which are available within the 64K-byte address space of the 8051.

To make a successful return to the point after execution of the called subroutine, the CPU
saves the address of the instruction immediately below the LCALL on the stack. Thus, when a
subroutine is called, the control is transferred to that subroutine, and the processor saves the
PC (program counter) on the stack and begins to fetch instructions from the new location. The
instruction RET (return) transfers the control back to the caller after finishing execution of the
subroutine. Every subroutine uses RET as the last instruction.

[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 30
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR

ACALL (Absolute Call)


ACALL is a 2-byte instruction, in contrast to LCALL which is 3 bytes. The target address of
the subroutine must be within 2K bytes because only 11 bits of the 2 bytes are used for
address. The difference between the ACALL and LCALL is that the target address for LCALL
can be anywhere within the 64K-bytes address space of the 8051, while the target address of
CALL is within a 2K-byte range.

Push and Pop Instructions:


Stack operations
The RAM of the 8051 microcontroller is home to a set of 32 general-purpose registers (00H-
1FH). These registers are 8 bit wide and are bundled in groups of 8 forming four register
banks. Stack operations can be used to place data into these registers in an efficient manner.
These stack operations use special commands (PUSH, POP) to place and extract data from
these general purposes registers.

The PUSH operation


The PUSH operation is used to place data into the stack. When this command is given the
value of address stored in the stack pointer is increased by one. After incrementing the address
in the stack pointer, data is placed at that memory location. For example, when the 8051 is
powered up, it holds the address 07H. When it receives the first PUSH instruction, the address
is updated to 08H, and data is stored in that location.

Example (R6 contains 80H and stack pointer points at 07H)

PUSH 6; This instruction moves data stored in register R6 to 08H

The POP operation


The POP operation is used to extract data that is stored in the stack. This operation is the
complete opposite of the PUSH operation. It extracts the data from the location which the stack
pointer points to and then decreases the value of the SP by 1.

Example (Stack pointer points at memory location 08H which contains 50H)

POP 6; register R6 now contains the data 50H and the stack pointer points to 07H

Execution Flags
Opcode Operand Description Size
Time affected
PUSH Rn Places value at the top of the stack 24 clock cycles 2 bytes None

Extracts the data from the top of the


POP Rn 24 clock cycles 2 bytes None
stack

[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 31
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR

Simple Programs:
Refer Record Note

[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 32
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR

UNIT - IV
PERIPHERALS

Timer and Counter:


Clock:
Every Timer needs a clock to work, and 8051 provides it from an external crystal which is the
main clock source for Timer. The internal circuitry in the 8051 microcontrollers provides a
clock source to the timers which is 1/12th of the frequency of crystal attached to the
microcontroller, also called Machine cycle frequency.

For example, suppose we have a crystal frequency of 11.0592 MHz then the microcontroller
will provide 1/12th i.e.

Timer clock frequency= (Xtal Osc.frequency)/12 = (11.0592 MHz)/12 = 921.6 KHz

period T= 1/(921.6 kHz)=1.085 μS

Timer:
A timer is a specialized type of clock which is used to measure time intervals. A timer that
counts from zero upwards for measuring time elapsed is often called a stopwatch. It is a device
that counts down from a specified time interval and used to generate a time delay, for
example, an hourglass is a timer.

[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 33
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR

Counter:
A counter is a device that stores (and sometimes displays) the number of times a particular
event or process occurred, with respect to a clock signal. It is used to count the events
happening outside the microcontroller. In electronics, counters can be implemented quite
easily using register-type circuits such as a flip-flop.

Difference between a Timer and a Counter


Timer Counter

The register is incremented considering 1 to 0


The register incremented for every machine
transition at its corresponding to an external
cycle.
input pin (T0, T1).

Maximum count rate is 1/12 of the oscillator Maximum count rate is 1/24 of the oscillator
frequency. frequency.

A timer uses the frequency of the internal A counter uses an external signal to count
clock, and generates delay. pulses.

Timer Registers:
There are two 16-bit timers and counters in 8051 microcontroller:
 Timer 0
 Timer 1.
Both timers consist of 16-bit register in which the lower byte is stored in TL and the higher
byte is stored in TH. Timer can be used as a counter as well as for timing operation that
depends on the source of clock pulses to counters.

[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 34
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR

Counters and Timers in 8051 microcontroller contain two special function registers:
 TMOD (Timer Mode Register)
 TCON (Timer Control Register)
which are used for activating and configuring timers and counters.

TMOD: (Timer Mode Register)


TMOD is an 8-bit register used to set timer mode of timer0 and timer1.

Its lower 4 bits are used for Timer0 and the upper 4 bits are used for Timer1
Bit 7,3 – GATE:
1 = Enable Timer/Counter only when the INT0/INT1 pin is high and TR0/TR1 is set.
0 = Enable Timer/Counter when TR0/TR1 is set.
Bit 6,2 - C/ (Counter/Timer): Timer or Counter select bit
1 = Use as Counter
0 = Use as Timer
Bit 5:4 & 1:0 - M1:M0: Timer/Counter mode select bit
These are Timer/Counter mode select bit as per the below table

M1 M0 Mode Operation

0 0 0 (13-bit timer mode) 13-bit timer/counter, 8-bit of THx & 5-bit of TLx

0 1 1 (16-bit timer mode) 16-bit timer/counter, THx cascaded with TLx

8-bit timer/counter (auto-reload mode), TLx reload


1 0 2 (8-bit auto-reload mode)
with the value held by THx each time TLx overflow

Split the 16-bit timer into two 8-bit timers i.e. THx
1 1 3 (split timer mode)
and TLx like two 8-bit timer

[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 35
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR

TCON: (Timer Control Register)

TCON is an 8-bit control register and contains a timer and interrupt flags.
Bit 7 - TF1: Timer1 Overflow Flag
1 = Timer1 overflow occurred (i.e. Timer1 goes to its max and roll over back to zero).
0 = Timer1 overflow not occurred.
It is cleared through software. In the Timer1 overflow interrupt service routine, this bit will get
cleared automatically while exiting from ISR.
Bit 6 - TR1: Timer1 Run Control Bit
1 = Timer1 start.
0 = Timer1 stop.
It is set and cleared by software.
Bit 5 – TF0: Timer0 Overflow Flag
1 = Timer0 overflow occurred (i.e. Timer0 goes to its max and roll over back to zero).
0 = Timer0 overflow not occurred.
It is cleared through software. In the Timer0 overflow interrupt service routine, this bit will get
cleared automatically while exiting from ISR.
Bit 4 – TR0: Timer0 Run Control Bit
1 = Timer0 start.
0 = Timer0 stop.
It is set and cleared by software.
Bit 3 - IE1: External Interrupt1 Edge Flag
1 = External interrupt1 occurred.
0 = External interrupt1 Processed.
It is set and cleared by hardware.
Bit 2 - IT1: External Interrupt1 Trigger Type Select Bit
1 = Interrupt occurs on falling edge at INT1 pin.
0 = Interrupt occur on a low level at the INT1 pin.

[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 36
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR

Bit 1 – IE0: External Interrupt0 Edge Flag


1 = External interrupt0 occurred.
0 = External interrupt0 Processed.
It is set and cleared by hardware.
Bit 0 – IT0: External Interrupt0 Trigger Type Select Bit
1 = Interrupt occurs on falling edge at INT0 pin.
0 = Interrupt occur on a low level at INT0 pin.

Mode 1 and 2 Programming:


Mode 0 (13-Bit Timer Mode)
Both Timer 1 and Timer 0 in Mode 0 operate as 8-bit counters (with a divide-by-32 prescaler).
Timer register is configured as a 13-bit register consisting of all the 8 bits of TH1 and the
lower 5 bits of TL1. The upper 3 bits of TL1 are indeterminate and should be ignored. Setting
the run flag (TR1) does not clear the register. The timer interrupt flag TF1 is set when the
count rolls over from all 1s to all 0s. Mode 0 operation is the same for Timer 0 as it is for
Timer 1.

Mode 1 (16-Bit Timer Mode)


Timer mode "1" is a 16-bit timer and is a commonly used mode. It functions in the same way
as 13-bit mode except that all 16 bits are used. TLx is incremented starting from 0 to a
maximum 255. Once the value 255 is reached, TLx resets to 0 and then THx is incremented by
1. As being a full 16-bit timer, the timer may contain up to 65536 distinct values and it will
overflow back to 0 after 65,536 machine cycles.

[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 37
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR

Mode 2 (8 Bit Auto Reload)


Both the timer registers are configured as 8-bit counters (TL1 and TL0) with automatic reload.
Overflow from TL1 (TL0) sets TF1 (TF0) and also reloads TL1 (TL0) with the contents of
Th1 (TH0), which is preset by software. The reload leaves TH1 (TH0) unchanged.
Mode 2 is an 8-bit auto-reload timer mode. In this mode, we have to load the THx-8 bit value
only. when the Timer gets started, the THx value gets automatically loaded into the TLx and
TLx starts counting from that value. After the value of TLx overflows from the 0xFF to 0x0,
the TFx flag gets set and again value from the THx gets automatically loaded into the TLx
register. That‟s why this is called the auto-reload mode.

Mode 3 (Split Timer Mode)


Timer mode "3" is known as split-timer mode. When Timer 0 is placed in mode 3, it becomes
two separate 8-bit timers. Timer 0 is TL0 and Timer 1 is TH0. Both the timers count from 0 to
255 and in case of overflow, reset back to 0. All the bits that are of Timer 1 will now be tied to
TH0.
When Timer 0 is in split mode, the real Timer 1 (i.e. TH1 and TL1) can be set in modes 0, 1 or
2, but it cannot be started/stopped as the bits that do that are now linked to TH0. The real timer
1 will be incremented with every machine cycle.

Counter Programming:
Example
In the following program, we create a square wave of 50% duty cycle (with equal portions
high and low) on the P1.5 bit. Timer 0 is used to generate the time delay. Analyze the program.
Also calculate the delay generated. Assume XTAL=11.0592MHz.
Program:
MOV TMOD,#01 ;Timer 0, mode 1(16-bit mode)
HERE: MOV TL0,#0F2H ;TL0=F2H, the low byte
MOV TH0,#0FFH ;TH0=FFH, the high byte
CPL P1.5 ;toggle P1.5
ACALL DELAY
SJMP HERE
DELAY:
SETB TR0 ;start the timer 0
AGAIN: JNB TF0,AGAI ;monitor timer flag 0 until it rolls over
CLR TR0 ;stop timer 0
CLR TF0 ;clear timer 0 flag
RET

[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 38
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR

In the above program notice the following step.


1. TMOD is loaded.
2. FFF2H is loaded into TH0-TL0.
3. P1.5 is toggled for the high and low portions of the pulse.
4. The DELAY subroutine using the timer is called.
5. In the DELAY subroutine, timer 0 is started by the SETB TR0 instruction.
6. Timer 0 counts up with the passing of each clock, which is provided by the crystal

Serial: MAX232:
Serial communication on the other hand, uses only one or two data lines to transfer data and is
generally used for long distance communication. In serial communication the data is sent as
one bit at a time. This article describes the interfacing of 8051 microcontroller with a computer
via serial port, RS232. Serial communication is commonly used in applications such as
industrial automation systems, scientific analysis and certain consumer products.

Serial Port
The microcontroller AT89C51 has an inbuilt UART for carrying out serial communication.
The serial communication is done in the asynchronous mode. A serial port, like other PC ports,
is a physical interface to establish data transfer between computer and an external hardware or
device. This transfer, through serial port, takes place bit by bit.

IBM introduced the DB-9 RS-232 version of serial I/O standard, which is most widely used in
PCs and several devices. In RS232, high and low bits are represented by flowing voltage
ranges:
Bit Voltage Range (in V)
0 +3 +25
1 -25 -3

Voltage Range for High and Low bit in RS232

The range -3V to +3V is undefined. The TTL standards came a long time after the RS232
standard was set. Due to this reason RS232 voltage levels are not compatible with TTL logic.
Therefore, while connecting an RS232 to microcontroller system, a voltage converter is
required. This converter converts the microcontroller output level to the RS232 voltage levels,
and vice versa. IC MAX232, also known as line driver, is very commonly used for this
purpose. The simplest connection between a PC and microcontroller requires a minimum of
three pins, RxD (receiver, pin2), TxD (transmitter, pin3) and ground (pin5) of the serial port of
computer.

[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 39
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR

Serial port of PC

TxD pin of serial port connects to RxD pin of controller via MAX232. And similarly, RxD pin
of serial port connects to the TxD pin of controller through MAX232.

MAX232 has two sets of line drivers for transferring and receiving data. The line drivers used
for transmission are called T1 and T2, where as the line drivers for receiver are designated as
R1 and R2. The connection of MAX232 with computer and the controller is shown in the
circuit diagram.

Block Diagram of ICMAX232 Connection With Computer And Controller

An important parameter considered while interfacing serial port is the Baud rate which is the
speed at which data is transmitted serially. It is defined as number of bits transmitted or
received per second. It is generally expressed in bps (bits per second). 8051 microcontroller
can be set to transfer and receive serial data at different baud rates using software instructions.
Timer1 is used to set the baud rate of serial communication for the microcontroller. For this
purpose, Timer1 is used in mode2 which is an 8-bit auto reload mode. To get baud rates
compatible with the PC, TH1 should be loaded with the values as shown:

Baud Rate (bps) TH1 (Hex value)


9600 FD
4800 FA
2400 F4
1200 E8

MAX232
Max 232 acts as a buffer driver for the processor. It accepts the standard digital logic values of
0 & 5 volts and converts them to the RS232 standard of +10 & -10 volts. Few Microcontrollers
have built-in serial ports that allow for direct connection with the RS232 serial port of the PC.
However many microcontrollers give a 0 to 5V output and require an intermediate buffer
circuit to convert the 0 to 5 volts to +10 and -10V required by the RS232 port.

[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 40
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR

The Max 232 IC consists of two transmitter-receiver arrangement such that two serial ports can
be connected using the same chip. 5 capacitors of each 1microFarad are used to generate the
required RS232 standard voltage from the TTL/CMOS voltage. The transmitters convert the
TTL/CMOS level to RS232 level whereas the receiver receives the RS232 input and converts
them to TTL level voltage.

It is a 16 pin IC with the transmitter pins connected to the microcontroller and the port such
that the input transmitter pin get TTL input from the Microcontroller and the output transmitter
pin supply output to the RS232 port. The receiver pins are connected to the RS232 port such
that the input receiver pin receives RS232 standard input from the PC port and the output
receiver pin supplies the TTL input to the Microcontroller. Thus the transmitter takes input
from the Microcontroller and gives output to the RS232 port whereas the receiver takes input
from the RS232 port and gives output to the Microcontroller. The other pins are connected to
5 electrolyte capacitors such that one of the capacitors is used as a voltage doubler to get +10V
from 5Vs and another capacitor is used as a voltage inverter to get -10V and the other three
capacitors are used as bypass capacitors for Vcc, V+, and V- pins respectively. Thus the
capacitors act as voltage generators.

One of the basic advantages of Max 232 is that it operates with a 5V supply, which allows for
a single 5V supply to be used for both the IC and the Microcontroller.

Max 232 Pin Diagram and Circuit Diagram

Features of Max232 IC
 Input supply voltage of 5V.
 Input voltage levels compatible with the TTL standard.
 Output voltage levels compatible with RS 232 standard.
 The low input current of 0.1microAmpere and output current of 24mA.
 It operates in a temperature range of -40 degree Celsius to +85 degree Celsius

[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 41
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR

Application of Max 232

Typical applications of Max232 involve modems, computers, RS232 systems, and terminals.
For a typical working application involving Max 232 as an intermediate between the
Microcontroller and the RS 232 connected to the Computer, one of the transmitter input pins
receive TTL input from the Microcontroller and the capacitor arrangement obtains the +/-10V
signal which is applied to the corresponding transmitter output pin, given to the RS232 port.

The receiver input pin receives 232 standard input from the RS232 port and accordingly the
corresponding receiver output pin gives the TTL standard output to the Microcontroller. Thus
the Max 232 IC can be used as an intermediate connection between Microcontroller and a
Computer.

Baud Rate:
The 8051 transfers and receives data serially at many different baud rates. The baud rate in the
8051 is programmable. This is done with the help of Timer 1.

It is defined as number of bits transmitted or received per second and is usually expressed in
Bits per second bps. After setting the operation mode of serial port, the program must
configure the serial ports baud rate which is only applicable to Serial Port modes 1 and 3. For
mode 0 and 2 oscillator frequency determines the Baud Rate.

Basis For Bit Rate Baud Rate


Comparison

Basic The number of bits per second is The number of signal units per second is
known as the bit rate. known as the baud rate.

Meaning Bit rate determines the number of Baud Rate determines the number of
bits travelled per second. times a signal's status changes.

Formula Bit rate= baud rate x the count of Baud rate= bit rate/ the number of bits
bits per signal unit. per signal unit.

Term Usually Used The focus is on computer On the other hand, data transmission over
efficiency. the channel is more concerned.

Bandwidth Bit Rate cannot determine the It can determine the amount of
Determination bandwidth. bandwidth necessary to send the signal.

For mode 0:

Baud rate is always the oscillator frequency divided by 12. For crystal of frequency
11.059MHz, Baud rate will be 921,583 baud.
Baud rate = (11.0592MHz/12) = 921,583 baud

[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 42
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR

For mode 2:
Baud rate is always the oscillator frequency divided by 64. For crystal of frequency
11.059MHz, Baud rate will be 172,797 baud.
Baud rate = (11.0592MHz/64) = 172,797 baud

SBUF Register:
SBUF is an 8-bit register used solely for serial communication in the 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 is received by the 8051 „s RxD line. SBUF can be accessed like
any other register in the 8051. Look at the following examples of how this register is accessed:

The moment a byte is written into SBUF, it is framed with the start and stop bits and
transferred serially via the TxD pin. Similarly, when the bits are received serially via RxD, the
8051 deframes it by eliminating the stop and start bits, making a byte out of the data received,
and then placing it in the SBUF.

SCON Register:
The SCON register is an 8-bit register used to program the start bit, stop bit, and data bits of
data framing, among other things.
The following describes various bits of the SCON register.

SMO and SMI are D7 and D6 of the SCON register, respectively. These two bits determine the
framing of data by specifying the number of bits per character, and the start and stop bits. They
take the following combinations.

[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 43
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR

Program to Transfer and Receive Data Serially:


Programming the 8051 to Send data serially
In programming the 8051 to transfer character bytes serially, the following steps must be
taken.
 The TMOD register is loaded with the value 20H, indicating the use of Timer 1 in mode
2 to set the baud rate.
 The TH1 is loaded with one of the values in Table (2) to set the baud rate for serial data
 transfer.
 The SCON register is loaded wit the value 50H, indicating serial mode 1, where an 8-bit
 data is framed with start and stop bits.
 TR1 is set to 1 to start Timer 1.
 TI is cleared by the “CLR TI” instruction .
 The character byte to be transferred serially is written into the SBUF register.
 The TI flag bit is monitored with the use of the instruction “JNB TI, xx” to see if the
 character has been transferred completely.
 To transfer the next character, go to Step 5.

Programming the 8051 to receive data serially

In programming the 8051 to transfer character bytes serially, the following steps must be
taken.
 The TMOD register is loaded with the value 20H, indicating the use of Timer 1 in mode
2 to set the baud rate.
 TH1 is loaded with one of the values in Table(2) to set the baud rate.
 The SCON register is loaded with the value 50H, indicating serial mode 1, where 8-bit
 data is framed with start and stop bits and receive enable is turned on.
 TR1 is set to 1 to start Timer 1.
 RI is cleared with the “CLR RI” instruction.
 The RI flag bit is monitored with the use of the instruction “JNB RI, xx” to see if an
 entire character has been received yet.
 When RI is raised, SBUF has the byte. Its contents are moved into safe place.
 To receive the next character, go to Step 5.

[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 44
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR

Interrupts:
The interrupts are basically those instructions from the external devices which stops the current
program execution and passes on the control to the external devices. The 8051 interrupts are
 Reset
 External hardware interrupt - INT0
 Timer 0 overflow interrupt - TF0
 External hardware interrupt - INT1
 Timer 1 overflow interrupt - TF1
 Serial communication interrupt - RI/TI
All these interrupts are controlled by IE (interrupt enable) register.

The timer and serial interrupts are internally produced by the microcontroller, whereas the
external interrupts are produced by additional interfacing devices or switches that are
externally connected with the microcontroller. These external interrupts can be level triggered
or edge triggered.

Reset
Reset is the highest priority interrupt, upon reset 8051 microcontroller start executing code
from 0x0000 address.

Internal interrupt (Timer Interrupt)


8051 has two internal interrupts namely timer0 and timer1. Whenever timer overflows, timer
overflow flags (TF0/TF1) are set. Then the microcontroller jumps to their vector address to
serve the interrupt. For this, global and timer interrupt should be enabled.

Serial interrupt
8051 has a serial communication port and have related serial interrupt flags (TI/RI). When the
last bit (stop bit) of a byte is transmitted, the TI serial interrupt flag is set, and when the last bit
(stop bit) of the receiving data byte is received, the RI flag gets set.

[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 45
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR

When interrupt occur then the microcontroller executes the interrupt service routine. Therefore
the memory location corresponds to interrupt enables it. Consider the interrupt corresponding
to the memory location is shown in the interrupt vector table below.

Enabling & Disabling Interrupt:


IE (Interrupt Enable) Register
This register is responsible for enabling and disabling the interrupt. EA register is set to 1 for
enabling interrupts and set to 0 for disabling the interrupts. The individual bits in this register
enables the particular interrupt like timer, serial and external inputs. Consider in the below IE
register, bit corresponds to 1 activate the interrupt and 0 disable the interrupt.

It disables all interrupts. When EA = 0 no interrupt will be


EA IE.7
acknowledged and EA = 1 enables the interrupt individually.

- IE.6 Reserved for future use.

- IE.5 Reserved for future use.

ES IE.4 Enables/disables serial port interrupt.

ET1 IE.3 Enables/disables timer1 overflow interrupt.

EX1 IE.2 Enables/disables external interrupt1.

ET0 IE.1 Enables/disables timer0 overflow interrupt.

EX0 IE.0 Enables/disables external interrupt0

[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 46
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR

Interrupt Priority Register (IP)


Using IP register it is possible to change the priority levels of an interrupts by clearing or
setting the individual bit in the Interrupt priority (IP) register as shown in figure. It allows the
low priority interrupt can interrupt the high-priority interrupt, but it prohibits the interruption
by using another low-priority interrupt. If the priorities of interrupt are not programmed, then
microcontroller executes the instruction in a predefined manner and its order are INT0, TF0,
INT1, TF1, and SI.

[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 47
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR

UNIT - V
INTERFACING

Interfacing of LCD:
Liquid Crystal Display (LCD) is an electronic device, which is frequently used in many
applications for displaying the information in a text or image format. The LCD is used for
displaying the alphanumeric character on its screen. The LCD display is consists of 8-data
lines and 3-control lines which are used for interfacing the LCD display with 8051
microcontroller.

16×2 LCD module is a very common type of LCD module to be used in 8051 based embedded
projects. It has 16 rows and 2 columns [5×7] or [5×8] LCD dot matrices. The module we will
use is number JHD162A. Available in 16 pin packages with features like contrast adjustment
function, backlight, and each dot matrix has 5×8 dot resolution. This LCD has two registers.

 Command/Instruction Register – stores the command instructions given to the LCD.


A command is an instruction given to LCD to do a predefined task like initializing,
clearing the screen, setting the cursor position, controlling display etc.

 Data Register – stores the data to be displayed on the LCD. The data is the ASCII
value of the character to be displayed on the LCD.

[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 48
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR

LCD Operation:
 VEE pin is meant for adjusting the contrast of the LCD display and the contrast can be
adjusted by varying the voltage at this pin. This is done by connecting one end of a POT
to the Vcc (5V), other end to the Ground and connecting the center terminal (wiper) of
of the POT to the VEE pin. See the circuit diagram for better understanding.

 The JHD162A has two built in registers namely data register and command register.
Data register is for placing the data to be displayed , and the command register is to
place the commands. The 16×2 LCD module has a set of commands each meant for
doing a particular job with the display. We will discuss in detail about the commands
later. High logic at the RS pin will select the data register and Low logic at the RS pin
will select the command register. If we make the RS pin high and the put a data in the 8
bit data line (DB0 to DB7) , the LCD module will recognize it as a data to be displayed
. If we make RS pin low and put a data on the data line, the module will recognize it as
a command.

 R/W pin is meant for selecting between read and write modes. High level at this pin
enables read mode and low level at this pin enables write mode.

 E pin is for enabling the module. A high to low transition at this pin will enable the
module.

 DB0 to DB7 are the data pins. The data to be displayed and the command instructions
are placed on these pins.

 LED+ is the anode of the back light LED and this pin must be connected to Vcc through
a suitable series current limiting resistor. LED- is the cathode of the back light LED and
this pin must be connected to ground.

Pin Descriptions:

[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 49
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR

Category Pin NO. Pin Name Function

1 VSS Ground Pin, connected to Ground


Power Pins
2 VDD or Vcc Voltage Pin +5V

Contrast Setting, connected to Vcc thorough a


Contrast Pin 3 V0 or VEE
variable resistor.

Register Select Pin, RS=0 Command mode,


4 RS
RS=1 Data mode

Control Pins Read/ Write pin, RW=0 Write mode,


5 RW
RW=1 Read mode
Enable, a high to low pulse need to enable the
6 E
LCD
Data Pins, Stores the Data to be displayed on
Data Pins 7-14 D0-D7
LCD or the command instructions
15 LED+ or A To power the Backlight +5V
Backlight Pins
16 LED- or K Backlight Ground

Command and Data to the LCD:


There are some preset commands instructions in LCD, which we need to send to LCD through
8051 microcontroller. Some important command instructions are given below:

Hex Code Command to LCD Instruction Register


0F LCD ON, cursor ON
01 Clear display screen
02 Return home
04 Decrement cursor (shift cursor to left)
06 Increment cursor (shift cursor to right)
05 Shift display right
07 Shift display left
0E Display ON, cursor blinking
80 Force cursor to beginning of first line
C0 Force cursor to beginning of second line
38 2 lines and 5×7 matrix
83 Cursor line 1 position 3

[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 50
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR

3C Activate second line


08 Display OFF, cursor OFF
OC Display ON, cursor OFF
C1 Jump to second line, position 1
C2 Jump to second line, position 2

ADC 0804:
The ADC0804 is a converter from analog to digital 8 bits. This ADC0804 has only one analog
input channel with digital output of eight bits that can be 256 values of different measures. The
step size is adjusted by setting the reference voltage in pin9 the reference input voltage can be
adjusted to allow encoding any smaller range for the entire 8-bit resolution analog voltage.
When the voltage reference pin is not connected to the ADC0804, the reference voltage
defaults to the operating voltage, ie, Vcc. The step size is 19.53mV 5V (5V / 255), ie, for every
increase of 19.53mV at the analog input, the output varies by 1 unit. To establish a determined
voltage level as the reference value, this pin is connected to half the voltage. For example, to
establish a reference 2V (Vref) is connected to 1V pin9 (Vref / 2), thereby reducing the step
size to 7.84mV (2V / 255).

ADC0804 also needs a clock to operate. The conversion time of the analog value to a digital
value depends on the clock source. An external clock can be given at the Clock IN pin. ADC
0804 also has an inbuilt clock that can be used in absence of an external clock. A suitable RC
circuit is connected between the Clock IN and Clock R pins to use the internal clock.

[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 51
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR

Features
 0V to 5V analog input voltage range with single 5V supply
 Compatible with microcontrollers, access time is 135 ns
 Easy interface to all microprocessors
 Logic inputs and outputs meet both MOS and TTL voltage level specifications
 Works with 2.5V (LM336) voltage reference
 On-chip clock generator
 No zero adjust required
 0.3[Prime] standard width 20-pin DIP package
 Operates ratio metrically or with 5 VDC, 2.5 VDC, or analog span adjusted voltage
reference
 Differential analog voltage inputs

Pin Diagram

Pin Description
Resolution:
The resolution of an ADC is how accurately it will sample (convert) the analog signals into
digital values (i.e. 0s and 1s). Higher is the resolution then greater will be the accuracy.

Step size:
It is the minimum voltage change that measures by the ADC. In other words, you can say that
the step size is the voltage difference between one digital level (i.e. 0001) and the next digital
level (i.e. 0010 or 0000). For example, If a 4bit ADC has the step size 1 volt then if we will
give the 1 volt as input the output will be 0001.

[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 52
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR

LM34 / LM35 Temperature Sensor:


The difference between them is that they provide proportional output calibrated to different
units of measure: LM34 provides output in degrees Farenheit, between −50°F and 300°F (-
45.5°C to 149°C). LM35 provides output in degrees Centigrade, between −55°C and 150°C.

The LM35 series are precision integrated-circuit temperature sensors, whose output voltage is
linearly proportional to the Celsius (Centigrade) temperature. The LM35 thus has an advantage
over linear temperature sensors calibrated in ˚ Kelvin, as the user is not required to subtract a
large constant voltage from its output to obtain convenient Centigrade scaling. The LM35 does
not require any external calibration or trimming to provide typical accuracies of ±1 ⁄4˚C at
room temperature and ±3 ⁄4˚C over a full −55 to +150˚C temperature range. Low cost is
assured by trimming and calibration at the wafer level. The LM35‟s low output impedance, a
linear output, and precise inherent calibration make interfacing to readout or control circuitry
especially easy. It can be used with single power supplies, or with plus and minus supplies. As
it draws only 60 µA from its supply, it has very low self-heating, less than 0.1˚C in still air.

[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 53
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR

The LM35 is rated to operate over a −55˚ to +150˚C temperature range, while the LM35C is
rated for a −40˚ to +110˚C range (−10˚ with improved accuracy). The LM35 series is available
packaged in hermetic TO-46 transistor packages, while the LM35C, LM35CA, and LM35D
are also available in the plastic TO-92 transistor package. The LM35D is also available in an
8-lead surface to mount small outline package and a plastic TO-220 package.

Block Diagram of LM34 / LM35 Temperature Sensor Interfacing with 8051 Microcontroller

Circuit Diagram of LM34 / LM35 Temperature Sensor Interfacing with 8051 Microcontroller

[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 54
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR

Features

 Calibrated directly in ˚ Celsius (Centigrade)


 Linear + 10.0 mV/˚C scale factor
 0.5˚C accuracy guarantee able (at +25˚C)
 Rated for full −55˚ to +150˚C range
 Suitable for remote applications
 Low cost due to wafer-level trimming
 Operates from 4 to 30 volts
 Less than 60 µA current drain
 Low self-heating, 0.08˚C in still air
 Nonlinearity only ±1 ⁄4˚C typical
 Low impedance output, 0.1 Ω for 1 mA load

Stepper Motor:
What is a stepper motor?
 A motor, in general, is a device that converts electrical energy into mechanical energy.
 As the name suggests, a stepper motor is a device that does the same task as above but,
in steps.In an isolated.
 Stepper motors generally have a permanent magnet shaft (rotor), and it is surrounded by
a stator.
 In some disk drives, dot matrix printers, and some other different places the stepper
motors are used. The main advantage of using the stepper motor is the position control.

[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 55
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR

Stepper motors are basically two types:


 Unipolar
 Bipolar.

Unipolar stepper motor


The unipolar stepper motor has five or six wires out of which four wires are joined to one
of the ends of each of the four stator coils. The connections at the center of the coils are joined
together and are connected to the 12V supply. They are called unipolar steppers because power
always comes in on this one pole.

Bipolar stepper motor

The bipolar stepper motor usually has four wires coming out of it. Unlike unipolar steppers,
bipolar steppers have no common center connection. They have two independent sets of coils
instead.

What are the components of a stepper motor


 Stator: The stator is made up of four coils, that are energized by the pulses from a
microcontroller or a stepper controller.
 Rotor: The number of steps of the rotor and its alignment with the stator determines the
step angle and steps per revolution.
 Permanent magnets: The rotor is mounted on a permanent magnet that attracts or
repels the stator coils and hence the propulsion occurs.

[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 56
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR

Working of a stepper motor


A stepper motor works on the principle of magnetic attraction and repulsion. The coils of
stators are placed on electromagnets which are energized and de-energized by the pulses of the
microcontroller which creates magnetic north and south on the stator poles. The rotor is
mounted on a permanent magnet having a permanent N and S marked. The sequence in which
the coils are excited to form the poles causes the rotor to attract one pair of stator pole and
repel the other causing motion in the shaft and the load connected to it.

Some parameters of stepper motors:


 Step Angle − The step angle is the angle in which the rotor moves when one pulse is
applied as an input of the stator. This parameter is used to determine the positioning of a
stepper motor.
 Steps per Revolution − This is the number of step angles required for a complete
revolution. So the formula is 360° /Step Angle.
 Steps per Second − This parameter is used to measure a number of steps covered in
each second.
 RPM − The RPM is the Revolution Per Minute. It measures the frequency of rotation.
By this parameter, we can measure the number of rotations in one minute.

Interfacing of Stepper Motor with 8051

[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 57
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR

Step Angle:
 A stepper motor rotates at small angles to complete 360 degrees rotation, these small
angles are called steps, hence the name Stepper Motor. Typically, a stepper motor
consists of 200 steps.
200 Steps = 360 degrees
1 Step = ‘x’ degrees
x = 360 / 200 = 1.8 degree
 Therefore, every step is 1.8 degrees.
 The high and low pulses are provided to the stator coil of the stepper motor.
 Here we‟re using a 4-coil stator. The angle of each step is decided by the steps in the
rotor and its alignment with the stator.
 In the case of a 200-steps motor, the step angle is 360 / 200 = 1.8 degrees, and in the
case of an 8-steps rotor, the step angle is 360 / 8 = 45 degrees. Here‟s a simulation.

200 Steps Rotor: Step angle = 1.8 degrees


8 Steps Rotor: Step angle = 45 degrees
Motor Speed:
The relation between RPM, steps per revolution, and steps per second is like below:

Steps per Second = rpm x steps per revolution / 60

The speed of the stepper motor is proportional to the pulse rate according to the equation
below:

Where:
Stepper motor speed is measured in revolutions per minute (rpm)
Pulse rate is measured in Hz
Step angle is measured in (º/step)

[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 58
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR

Interfacing Key Board:


Keyboards and LCDs are the most widely used input/output devices of the 8051, and a basic
understanding of them is essential. In this section, we discuss keyboard fundamentals, along
with key press and key detection mechanisms.

Matrix Keypad Interfacing

The advantage of a matrix keypad is that the use of it will allow the programmer to reduce the
number of pins to be used. In a 4×4 matrix keypad, there are four rows and four columns
connected to 16 push button switches. It may look like one needs 16 pins for the
microcontroller to be connected to the matrix keypad but practically 16 inputs of keypad
interface are possible with the 8 pins of a microcontroller port. All 8 lines can be connected to
the same port or different ports based on the application requirements. In fact, 8 port pins of a
microcontroller are sufficient for a 4×4 keypad interface using row & column matrix
connection technique by saving other 8 bits of the port.

Figure shows how to interface the 4 X 4 matrix keypad to two ports in microcontroller. The
rows are connected to an output port and the columns are connected to an input port.

To detect a pressed key, the microcontroller grounds all rows by providing 0 to the output
latch, and then it reads the columns. If the data read from the columns is C3-C0=1111, no key
has been pressed and the process continues until a key press is detected. However, if one of the
column bits has a zero, this means that a key press has occurred.

After a key press is detected, the microcontroller will go through the process of identifying the
key. Starting with the top row, the microcontroller grounds it by providing a low to row only;
then it reads the columns.

If the data read is all 1s, no key in that row is activated and the process is moved to the next
row. It grounds the next row, reads the columns, and checks for any zero. This process
continues until the row is identified. After identification of the row in which the key has been
pressed, the next task is to find out which column the pressed key belongs to.

[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 59
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR

Interfacing 4×3 keypad and 16×2 lcd with 8051 microcontroller

Interfacing 4×4 keypad and 16×2 lcd with 8051 microcontroller

[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 60
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR

Scanning and Identifying the keys.


Matrix keypad interfacing and key press identification can be explained in a step by step
manner which involves a software. First, we give a HIGH on row pins. When a key is pressed,
the corresponding row and column get shorted. In the second step, a software scans the pins
connected to the columns. If it detects a HIGH on any particular column, then it is found that
the key press has been made of a key in that column. The third step is to figure out which key
is pressed exactly. For this, the software writes logic high on row pins sequentially. The pin of
the column on which the pressed key is situated will become high.

READING DATA:

Scanning is done in a different way. Columns pins are used as input pins, and rows pins are
used as output. If a low logic is given to all the Rows and high logic is given to each Column.

For finding Column number:


 When a switch/key is pressed, the corresponding row and column will get short.
 Output of the corresponding column goes to go low.
 Since we have made all the rows zero so this gives the column number of the pressed
key.

For Finding Row number:

 After the detection of column number, the controller set‟s all the rows to high.
 Each row is one by one set to zero by the microcontroller and the earlier detected
column is checked and obviously it becomes zero.
 The row due to which the column gets zero is the row number of the pressed key.

[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 61
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR

[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 62

You might also like