8051 Microcontroller (Final)
8051 Microcontroller (Final)
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
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.
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.
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.
MICROPROCESSORS MICROCONTROLLER
Access time for memory and I/O Less access time for inbuilt
devices are more memory and I/O devices
[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
[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 4
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR
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:
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 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).
[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
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 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.
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.
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
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
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.
[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 13
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR
[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 14
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR
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.
[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 15
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR
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.
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
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.
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
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.
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.
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.
Here, the data in the RAM location 47H is moved to the Accumulator.
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.
[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 22
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR
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
INC Increment by 1
DEC Decrement by 1
MUL Multiply
DIV Divide
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
[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
[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
Conditional Jump:
Instruction Action
JZ Jump if A = 0
JNZ Jump if A ≠ 0
JC Jump if CY = 1
JNC Jump if CY ≠ 1
JB Jump if bit = 1
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
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.
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
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
[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
For example, suppose we have a crystal frequency of 11.0592 MHz then the microcontroller
will provide 1/12th i.e.
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.
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.
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
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 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
[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 37
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR
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
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
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.
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:
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.
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
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.
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
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.
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.
[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 46
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR
[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.
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
[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 50
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR
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
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
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
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.
[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 56
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR
[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.
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
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
[Dr.S.KUMAR, Assistant Professor of Electronics, Sri Vasavi College (SF Wing), Erode] Page 60
8051 MICROCONTROLLER AND ITS APPLICATIONS Dr. S. KUMAR
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.
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