Electronics-5 Microcontrollers & Interfacing
Electronics-5 Microcontrollers & Interfacing
IV – Semester
Electronics Paper-5
Microcontrollers
&
Interfacing
(Study Material)
Group : __________________________________
Prepared by:
K. Venugopala Rao M.Sc.(Tech), M. Tech
Lecturer in Electronics
SRI GCSR COLLEGE, GMR Nagar, Rajam
[email protected]
+91-9603803168, +91-9182061933
Department of ELECTRONICS
2nd - B.Sc. 4th – Semester Microcontrollers & Interfacing
UNIT I
Microcontroller Architecture
Introduction
A microcontroller is a computer present in a single integrated circuit which is
dedicated to perform one task and execute one specific application.
It contains memory, programmable input/output peripherals as well a
processor. Microcontrollers are mostly designed for embedded applications and are
heavily used in automatically controlled electronic devices such as cell phones,
cameras, microwave ovens, washing machines, etc.
Microcontrollers and Microprocessors
A microprocessor is an integrated circuit (IC) and is a computer’s central
processing unit (CPU on a chip). It is a programmable multipurpose silicon chip,
clock driven, register based, accepts binary data as input and provides output after
processing it as per the instructions stored in the memory. It consists of Arithmetical
and Logical Unit (ALU), Control Unit and Register Array.
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
2
Evolution of Microcontroller
The last decade has seen an exciting evolution with capabilities of
microprocessors. The development of 16 and 32 bit microprocessors contributed to
the growth of powerful personal computers. In the evolution of microprocessor
capability, instead of focusing upon larger word widths and address spaces. The
present emphasis is upon exceedingly fast real time control. The development of
microcontrollers has focused upon the integration of the facilities needed to support
fast control into a single chip.
Intel has introduced standard 8-bit microcontroller 8048 in 1976. In the year
1980, Intel has introduced the 8051 microcontroller, with higher performance than
8048. With the advantages of 8051, the microcontroller applications took a peak
level.
Because of the advanced semiconductor technology, it has become possible
to integrate more than 1,00,000 transistors onto a single silicon chip. With this Intel
developed a new generation of single chip 16 bit microcontrollers called the MCS-
96 (8096 family).
The Motorola Microcontroller family was first introduced to the market in
1978 and is built in the same pattern of the microprocessor 6800.
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
3
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
4
and Motorola MC68HC12 and MC68332 families. The performance and computing
capability of 16 bit microcontrollers are enhanced as compared to the 8-bit
microcontrollers.
32-bit microcontroller
When the ALU performs arithmetic and logical operations on a double word
(32- bits), the microcontroller is an 32-bit microcontroller. The internal bus width of
32-bit microcontroller is of 32-bit. Examples of 32-bit microcontrollers are Intel
80960 family and Motorola M683xx and Intel/Atmel 251 family. The performance
and computing capability of 32 bit microcontrollers are enhanced as compared to
the 16-bit microcontrollers.
Various features of 8051 microcontroller are given as follows.
8-bit CPU
16-bit Program Counter
8-bit Processor Status Word (PSW)
8-bit Stack Pointer
Internal RAM of 128bytes
Special Function Registers (SFRs) of 128 bytes
32 I/O pins arranged as four 8-bit ports (P0 - P3)
Two 16-bit timer/counters : T0 and T1
Two external and three internal vectored interrupts
One full duplex serial I/O
Some of the microcontrollers of 8051 family are given as follows:
ON-CHIP ON-CHIP
DATA PROGRAM NO. OF
16-BIT
DEVICE MEMORY MEMORY VECTORED Serial I/O
TIMER/COUNTER
(RAM) (ROM) INTERUPTS
(bytes) (bytes)
8031 128 None 2 5 1
8032 256 none 2 6 1
8051 128 4k ROM 2 5 1
8052 256 8k ROM 3 6 1
8751 128 4k EPROM 2 5 1
8752 256 8k EPROM 3 6 1
4k Flash
AT89C51 128 2 5 1
Memory
8k Flash
AT89C52 256 3 6 1
memory
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
5
It is 8-bit microcontroller, means MC 8051 can Read, Write and Process 8 bit
data. This is mostly used microcontroller in the robotics, home appliances like mp3
player, washing machines, electronic iron and industries. Mostly used blocks in the
architecture of 8051 are as follows:
128 Byte RAM for Data Storage
MC 8051 has 128 byte Random Access memory for data storage. During
execution for storing the data the RAM is used. RAM consists of the register banks,
stack for temporary data storage. It also consists of some special function register
(SFR) which are used for some specific purpose like timer, input output ports etc.
Normally microcontroller has 256 byte RAM in which 128 byte (address range 00H
to 7FH) is used for user space which is normally Register banks and stack. But
other 128 byte (address range 80H to FFH) RAM which consists of SFRs.
4KB ROM
In 8051, 4KB read only memory (ROM) is available for program storage,
addressed from 0000H to 0FFFH
Timers and Counters
Timer is which can give the delay of particular time between some events. In
MC8051, two 16 bit timers are available, named as T0 (TH0, TL0) and T1 (TH1,
TL1),by these timers we can give the delay of particular time. TMOD, TCON
registers are used for controlling timer operation.
Serial Port
There are two pins available for serial communication TXD and RXD.
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
6
Normally TXD is used for transmitting serial data which is in SBUF register, RXD is
used for receiving the serial data.
SCON register is used for controlling the operation.
Input Output Ports
There are four input output ports available P0, P1, P2, P3. Each port is 8 bit
wide.
The port 0 can perform dual works. It is also used as Lower order address bus (A0
to A7) multiplexed with 8 bit data bus P0.0 to P0.7 is AD0 to AD7 respectively the
address bus and data bus is demultiplex by the ALE signal.
Port 2 can be used as I/O port as well as higher order address bus A8 to A15.
Port 3 also have dual functions it can be worked as I/O as well as each pin of P3
has specific function.
P3.0 – RXD – Serial data IO
P3.1 – TXD – Serial data transmit
P3.2 – INT0 – External Interrupt 0
P3.3 – INT1 – External Interrupt 1
P3.4 – T0 – Clock input for counter 0
P3.5 – T1 – Clock input for counter 1
P3.6 – WR – Signal for writing to external memory
P3.7 – RD – Signal for reading from external memory
When external memory is interfaced with 8051 then P0 and P2 can’t be worked as
I/O port they works as address bus and data bus, otherwise they can be accessed
as I/O ports.
Oscillator
It is used for providing the clock to MC8051 which decides the speed or baud rate
of MC.
We use crystal which frequency vary from 4MHz to 30 MHz, normally we use
11.0592 MHz frequency.
Interrupts
Interrupts are defined as requests because they can be refused (masked) if they
are not used, that is when an interrupt is acknowledged. A special set of events or
routines are followed to handle the interrupts. These special routines are known as
or interrupt service routines (ISR).
INT0 and INT1 are the pins for external interrupts.
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
7
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
8
PSW (Program Status Word) - This is an 8 bit register which contains the
arithmetic status of ALU and the bank select bits of register banks.
CY - carry flag
AC - auxiliary carry flag
F0 - available to the user for general purpose
RS1, RS0 - register bank select bits
OV - overflow
P - parity
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
9
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
10
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
11
Pin 21-28 : Port 2. If there is no intention to use external memory then these
port pins are configured as general inputs/outputs. In case external
memory is used, the higher address byte, i.e. addresses A8-A15 will
appear on this port. Even though memory with capacity of 64Kb is
not used, which means that not all eight port bits are used for its
addressing, the rest of them are not available as inputs/outputs.
Pin 29 : PSEN. If external ROM is used for storing program then a logic zero
(0) appears on it every time the microcontroller reads a byte from
memory.
Pin 30 : ALE. Prior to reading from external memory, the microcontroller puts
the lower address byte (A0-A7) on P0 and activates the ALE output.
After receiving signal from the ALE pin, the external latch latches the
state of P0 and uses it as a memory chip address. Immediately after
that, the ALE pin is returned its previous logic state and P0 is now
used as a Data Bus.
Pin 31 : EA. By applying logic zero to this pin, P2 and P3 are used for data
and address transmission with no regard to whether there is internal
memory or not. It means that even there is a program written to the
microcontroller, it will not be executed. Instead, the program written
to external ROM will be executed. By applying logic one to the EA
pin, the microcontroller will use both memories, first internal then
external (if exists).
Pin 32-39 : PORT 0. Similar to P2, if external memory is not used, these pins
can be used as general inputs/outputs. Otherwise, P0 is configured
as address output (A0-A7) when the ALE pin is driven high (1) or as
data output (Data Bus) when the ALE pin is driven low (0).
Pin 40 : VCC. +5V power supply.
SFR Registers
Accumulator
This is an 8 bit register and is used to the hold result of various arithmetic
and logic operations.
The accumulator, referred to as ACC or A.
MOV A, #52h - Move immediate the value 52h to the accumulator
MOV 70h, #52h - Move immediate the value 52h to Internal RAM location 70h.
B Register
The B register is an SFR register at addresses F0h which is bit-addressable.
The B register is used in two instructions only: i.e. MUL (multiply) and DIV (divide).
The B register can also be used as a general-purpose register.
Program Counter
The PC (Program Counter) is a 2 byte (16 bit) register which always contains
the memory address of the next instruction to be executed. When the 8051 is reset
the PC is always initialised to 0000h.
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
12
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
13
Data Pointer
The Data Pointer, DPTR, is a special 16-bit register used to address the
external code or external data memory. Since the SFR registers are just 8-bits wide
the DPTR is stored in two SFR registers, where DPL (82h) holds the low byte of the
DPTR and DPH (83h) holds the high byte of the DPTR.
For example, for write the value 46h to external data memory location 2500h.
The following instructions are needed.
MOV A, #46h ; Move immediate 8 bit data 46h to A (accumulator)
MOV DPTR, #2500h ; Move immediate 16 bit address value 2500h to DPTR.
Now DPL holds 04h and DPH holds25h.
MOVX @DPTR, A ; Move the value in A to external RAM location 2500h.
Note: The MOVX (Move X) instruction is used to access external data memory.
The Stack and the Stack Pointer
The stack refers to an area of internal RAM that is used in conjunction with
certain opcode to store and retrieve data quickly. The 8-bit Stack Pointer (SP)
register is used by the 8051 to hold an internal RAM address that is called the top
of the stack. The address held in the SP register is the location in internal RAM
where the last byte of data was stored by a stack operation.
When data is to be placed on the stack, the SP is increments before storing
data on the stack so that the stack grows up as data is stored. As data is retrieved
from the stack, the byte is read from the stack, and then the SP decrements to point
to the next available byte of stored data.
Operation of the stack and the SP is shown in Figure. The SP is set to 07h
when the 8051 is reset and can be changed to any internal RAM address by the
programmer. The stack is limited in height to the size of the internal RAM. The
stack has the potential to over write valuable data in the register banks, bit-
addressable RAM, and scratch pad RAM areas. The programmer is responsible for
making sure the stack does not grow beyond predefined bounds!
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
14
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
15
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
16
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
17
Internal memory
The first 128 bytes of internal memory is organised as shown in figure, and is
referred to as Internal RAM.
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
18
The 8051 supports a special feature which allows access to bit variables.
That means the individual memory bits in Internal RAM can be set or cleared. The
Bit Addressable area of the RAM is just 16 bytes of Internal RAM located between
20h and 2Fh. In all there are
128 bits numbered 00h to 7Fh. A bit variable can be set with a command such as
SETB and cleared with a command such as CLR.
SETB 25h ; sets the bit 25h (becomes 1)
CLR 25h ; clears bit 25h (becomes 0)
Note, bit 25h is actually bit b5 of Internal RAM location 24h.
General Purpose RAM: 30h to 7Fh
These 80 bytes of Internal RAM memory are available for general-purpose data
storage.
External Memory interfacing
Figure 3.8 shows the connections between an 8051 and an external memory
configuration consisting of 16K of EPROM and 8K of RAM. The 8051 accesses
external RAM whenever certain program instructions are executed. External ROM
___
is accessed whenever the EA (external access) pin is connected to ground or
when the PC contains an address higher than the last address in the internal 4K
ROM (0FFFh). 8051 designs can thus use internal and external ROM automatically.
The lower address byte from port 0 must be latched into an external register
to save the byte with help of ALE clock pulse, that provides the correct timing for
the 373 data latch.
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
19
________
If the PSEN (program store enable) pin is low, the code byte in the ROM is placed
on the data bus. If the access is for a RAM byte, the WR (write)or RD (read) pins
will go low, enabling data to flow between the RAM and the data bus.
The ROM may be expanded to 64K by connecting the remaining port 2
upper address lines A14-A15 to the chip.
Fig shows external memory timing diagram.
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
20
ORG (origin)
It defines the starting address for the program in program (code) memory
EQU (equate)
This is used to define a constant without occupying a memory location. This
equates a numerical value to symbol.
PR EQU 25h
END directive
The END directive is indicates to the assembler the end of the source (asm)
file. The END directive is the last line of an 8051 program, meaning that in the
source code anything after the END directive is ignored by the assembler. Some
assemblers use “. END” instead of “END”.
Counters / Timers
Many microcontroller applications require the counting of external events or
internal time delays between actions. There are two I6-bit up counters, named T0
and TI, are provided for the general use of the programmer. Each counter may be
programmed to count internal clock pulses, acting as a timer, or programmed to
count external pulses as a counter.
The counters are divided into two 8-bit registers called the timer low (TLO,
TL1) and high (THO, TH1) bytes. All counter action is controlled by bit states in the
timer mode control register (TMOD), the timer/counter control register (TCON), and
certain program instructions.
TMOD is dedicated solely to the two timers and considered as a two 4-bit
registers for 2 timers. TCON has control bits and flags for the timers in the upper
nibble, and control bits and flags for the external interrupts in the lower nibble.
Timer control (TCON) special function register
This is an 8bit SFR register shown in figure, upper 4 bits for timer 0 and 1
and lower 4bits are used to control interrupts. It is a bit addressable SFR as
TCON.0 to TCON.7
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
21
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
22
Timer/counter logic
The resultant timer clock is gated to the timer by means of the circuit shown
__
in Figure. The C/ T bit in the TMOD register must be set to 0. Bit TRX in the TCON
register must be set to 1 (timer run), and the gate bit in the TMOD register must be
0, or external pin INTX must be a 1.
Timer Modes of operation
The timers may operate in any one of four modes that are determined by the
mode bits, M1 and MO, in the TMOD register.
TIMER MODE 0 – 13 bit Timer/Counter
In the mode 0, the timer register is configured as a 13 bit register. The 13 bit
register consists of all 8 bits of THX and the lower 5 bits of TLX. It holds value
between 0000h to 1FFFh. The upper 3 bits of TLX are indetermined and should be
ignored. When timer reaches its maximum value 1FFFh, it comes to initial value
0000h.
TIMER MODE 1 – 16 bit Timer/Counter
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
23
It is an 8 bit timer, therefore it allows only values between 00h to FFh. After TH is
loaded with 8bit value, the timer must be started. It counts up by incrementing the
TL register. When it moves from FFh to 00h, it sets the timer flag. TL is reloaded
automatically with the original value, which is in the TH register.
TIMER MODE 3 – two 8 bit timers using Timer 0
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
24
For every interrupt, there must be an interrupt service routine (ISR). When an
interrupt is invoked, the microcontroller runs the interrupt service routine. For every
interrupt, there is a fixed location in memory that holds the address of its ISR, is
called the interrupt vector table, shown below.
Interrupt Vector Table for the 8051
Interrupt ROM Location (Hex) Pin
Reset 0000 9
External interrupt 0 (INT 0) 0003 P3.2 (12)
Timer 0 interrupt (TF0) 000B --
External interrupt 1 (INT 1) 0013 P3.3 (13)
Timer 1 interrupt (TF1) 0013 --
Serial COM interrupt (RI and TI) 0023 --
Enabling and disabling an Interrupt
Upon reset, all interrupts disabled. that is none will be responded to by the
microcontroller even if they are in active. The interrupt must beenabled by the
software, a SFR called IE ( Interrupt Enable) is responsible for enabling
(unmasking) and disabling (masking) the interrupts.
The Interrupt Enable (IE) Special Function Register
It is Bit addressable as IE.0 to IE.7
Set to 1 by program to enable interrupt; cleared to 0 to disable interrupt.
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
25
If two interrupts with same priority occur at same time, then the following level can
be used.
Interrupt Priority
External interrupt 0 (IE0) Highest
Timer/counter 0 overflow interrupt (TF0) -
External interrupt 1 (IE1) -
Timer/counter 1 overflow interrupt (TF1) -
Serial port interrupt Lowest
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
26
UNIT II
ADDRESSING MODES – INSTRUCTION SET
Addressing Modes
The way of data source or destination addresses (operands) are specified in
the mnemonic, that means the way of data determines within the instruction is
called addressing mode. There are a number of addressing modes available to the
8051 instruction set, as follows:
Immediate Addressing
Register Addressing
Direct Addressing
Indirect Addressing
Relative Addressing
Long Addressing
Indexed Addressing
Immediate Addressing
Immediate addressing means that the operand is the data value to be used.
Ex: MOV A, #99d - Moves the value 99 into the accumulator.
The # symbol tells the assembler that the immediate addressing mode is to be
used.
Register Addressing
One of the eight general-registers, R0 to R7, can be specified as the instruction
operand.
Ex: ADD A, R5 - the contents of R5 is added to the accumulator.
Direct Addressing
Direct addressing means that the operand specifies with RAM address
Ex: MOV A, 47h
The content of RAM address 47h is store in the accumulator.
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
27
Indirect Addressing
Indirect addressing provides a powerful addressing capability, An example
instruction, which uses indirect addressing, is as follows:
MOV A, @R0
Note the @ symbol indicated that the indirect addressing mode is used.
The content of address location which is stored in R0 is moved to the accumulator.
Relative Addressing
This is a special addressing mode used with certain jump instructions. The
relative
address, is an 8-bit signed number, which is automatically added to the PC to make
the address of the next instruction. The 8-bit signed offset value gives an address
range of + 127 to –128 locations.
Ex: SJMP LABEL_X
Long Addressing
The long addressing mode within the 8051 is used with the instructions LJMP and
LCALL. The address specifies a full 16 bit destination address so that a jump or a
call
can be made to a location within a 64KByte code memory space.
Ex: LJMP 5000h ; full 16 bit address is specified in operand
Indexed Addressing
With indexed addressing a separate register, either the program counter, PC, or the
data pointer DTPR, is used as a base address and the accumulator is used as an
offset
address. The effective address is formed by adding the value from the base
address to
the value from the offset address.
Ex: MOVC A, @A+DPTR
MOVC is a move instruction, which moves data from the external code memory
space. The address operand in this example is formed by adding the content of the
DPTR register to the accumulator value.
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
28
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
29
MOV
The MOV instruction is concerned with moving data internally, i.e. between Internal
RAM, SFR registers, general registers etc. MOVX and MOVC are used in
accessing external memory data.
Syntax - MOV destination source
The instruction copies data from a defined source location to a destination location.
MOVX
The MOVX instruction is used to access the external data memory (X indicates
eXternal memory access). All external moves must work through the A register
(accumulator).
MOVC
The MOVC instruction is used to read data from the external code memory (ROM).
Like the MOVX instruction the DPTR register is used as the indirect address
register.
MOV DPTR, # 2000h - Copy the data value 2000h to the DPTR register
MOV A, #80h - Copy the data value 80h to register A
MOVC A, @A+DPTR - Copy the contents of the address 2080h (2000h + 80h) to
register A
Note: For the MOVC the program counter, PC, can also be used to form the
address.
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
30
XCH
A special XCH (eXCHange) instruction will swap the data between source and
destination. XCH instructions must use register A. XCHD is a special case of the
exchange instruction where just the lower nibbles are exchanged.
Arithmetic instructions
Some flags within the PSW, i.e. C, AC, OV, P, are utilised in many of the arithmetic
instructions.
The arithmetic instructions are
Addition
Subtraction
Increment/decrement
Multiply/divide
Decimal adjust
Addition
Register A (the accumulator) is used to hold the result of any addition operation.
The ADDC instruction is used to include the carry bit in the addition process.
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
31
Subtraction
Computer subtraction can be achieved using 2’s complement arithmetic. Most
computers also provide instructions to directly subtract signed or unsigned
numbers. The accumulator A will contain the result (difference) of the subtraction
operation. The C (carry) flag is treated as a borrow flag, which is always subtracted
from the minuend during a subtraction operation.
SUBB A, #55d - Subtract the number 55 (decimal) and the C flag from A; and put
the result in A.
SUBB A, R6 - Subtract R6 the C flag from A; and put the result in A.
SUBB A, 58h - Subtract the number in RAM location 58h and the C flag From A;
and put the result in A.
Increment/Decrement
The increment (INC), simply adds a binary 1 to a number while a decrement (DEC)
instruction subtracts a binary 1 from a number.
Multiply / Divide
The 8051 supports 8-bit multiplication and division. The arithmetic is relatively fast
since multiplication and division are implemented as single instructions. For the
MUL or DIV instructions the A and B registers must be used and only unsigned
numbers are supported.
Multiplication
The MUL instruction is used as follows,
MUL AB - Multiply A by B.
The resulting product resides in registers A and B, the low-order byte is in A and
the high order byte is in B.
Division
The DIV instruction is used as follows,
DIV AB - A is divided by B.
The remainder is put in register B and the integer part of the quotient is put in
register A.
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
32
Decimal Adjust
The 8051 performs all arithmetic in binary numbers (i.e. it does not support BCD
arithmetic). If two BCD numbers are added then the result can be adjusted by using
the DA, decimal adjust, instruction.
DA A - Decimal adjust A following the addition of two BCD numbers.
Logical instructions
Boolean Operations
Most control applications implement control logic using Boolean operators to act on
the data. Most microcomputers provide a set of Boolean instructions that act on
byte level data. However, the 8051 additionally provides Boolean instruction which
can operate on bit level data.
The following Boolean operations can operate on byte level or bit level data:
ANL - Logical AND
ORL - Logical OR
CPL - Complement (logical NOT)
XRL - Logical XOR (exclusive OR)
Logical operations at the BYTE level
ANL A, #55h - AND each bit in A with corresponding bit in number 55h, leaving the
result in A.
ANL 42h, R4 - AND each bit in RAM location 42h with corresponding bit in R4,
leaving the result in RAM location 42h.
ORL A, @R1 - OR each bit in A with corresponding bit in the number whose
address is contained in R1 leaving the result in A.
XRL R4, 80h - XOR each bit in R4 with corresponding bit in RAM location 80h (port
0), leaving result in A.
CPL R0 - Complement each bit in R0
Logical operations at the BIT level
SETB 2Fh - Bit 7 of Internal RAM location 25h is set
CLR C - Clear the carry flag (flag =0)
CPL 20h - Complement bit 0 of Internal RAM location 24h
MOV C, 87h - Move to carry flag the bit 7of Port 0 (SFR at 80h)
ANL C,90h - AND C with the bit 0 of Port 1 (SFR at 90)
ORL C, 91h - OR C with the bit 1 of Port 1 (SFR at 90)
Rotate Instructions
The ability to rotate the A register (accumulator) data is useful to allow examination
of individual bits. The options for such rotation are as follows:
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
33
RL A ; Rotate A one bit to the left. Bit 7 rotates to the bit 0 position
RLC A ; The Carry flag is used as a ninth bit in the rotation loop
RRC A ; Rotates to the right and includes the carry bit as the 9th bit.
Swap
The Swap instruction swaps the accumulator’s high order nibble with the low-order
nibble using the instruction SWAP A
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
34
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
35
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
36
Arithmetic Instructions
ADD A, Rn : A A + Rn
ADD A, direct : A A + (direct)
ADD A, @Ri : A A + @Ri
ADD A, #data : A A + data
ADDC A, Rn : A A + Rn + C
ADDC A, direct : A A + (direct) + C
ADDC A, @Ri : A A + @Ri + C
ADDC A, #data : A A + data + C
DA A : Decimal adjust accumulator
DIV AB : Divide A by B
A quotient
B remainder
DEC A : A A -1
DEC Rn : Rn Rn - 1
DEC direct : (direct) (direct) - 1
DEC @Ri : @Ri @Ri - 1
INC A : A A+1
INC Rn : Rn Rn + 1
INC direct : (direct) (direct) + 1
INC @Ri : @Ri @Ri +1
INC DPTR : DPTR DPTR +1
MUL AB : Multiply A by B
A low byte (A*B)
B high byte (A* B)
SUBB A, Rn : A A - Rn - C
SUBB A, direct : A A - (direct) - C
SUBB A, @Ri : A A - @Ri - C
SUBB A, #data : A A - data - C
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
37
Logical Instructions
ANL A, Rn : A A AND Rn
ANL A, direct : A A AND (direct)
ANL A, @Ri : A A AND @Ri
ANL A, #data : A A AND data
ANL direct, A : (direct) (direct) AND A
ANL direct, #data : (direct) (direct) AND data
CLR A : A 00H
CPL A : A A
ORL A, Rn : A A OR Rn
ORL A, direct : A A OR (direct)
ORL A, @Ri : A A OR @Ri
ORL A, #data : A A OR data
ORL direct, A : (direct) (direct) OR A
ORL direct, #data : (direct) (direct) OR data
RL A : Rotate accumulator left
RLC A : Rotate accumulator left through
carry
RR A : Rotate accumulator right
RRC A : Rotate accumulator right through
carry
SWAP A : Swap nibbles within accumulator
XRL A, Rn : A A EXOR Rn
XRL A, direct : A A EXOR (direct)
XRL A, @Ri : A A EXOR @Ri
XRL A, #data : A A EXOR data
XRL direct, A : (direct) (direct) EXOR A
XRL direct, #data : (direct) (direct) EXOR data
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
38
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
39
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
40
For calculate delay in the program, let us consider following program with crystal
frequency 11.0592 MHz.
MOV A,#55H
AGAIN: MOV P1,A
ACALL DELAY
CPL A
SJMP AGAIN
Port organization
One major feature of microcontroller is in built IO ports that connect the 8051
to the outside world.
There are four 8bit ports in 8051.
Port 0
Port 0 pins may serve as inputs, outputs, or, when used together, as a bidirectional
lower order address and data bus for external memory.
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
41
Port 1
Port 1 pins have no dual function; simply it acts as input or output port.
Port 2
Port 2 may be used as an input/output port similar in operation to port l. The
alternate use of port 2 is to supply a high-order address byte in conjunction with the
port 0 low-order byte to address external memory.
Port 3
Port 3 is an input /output port similar to port 1. The every pin of port 3 has alternate
function.
P3.0 - RXD -Serial data input
P3.1-TXD - Serial data output
P3.2 – INT0 - External interrupt 0
P3.3 - lNT1 - External interrupt 1
P3.4 –T0 - External timer 0 input
P3.5 - T1 - External timer 1 input
P3.6 – WR - External memory write pulse
P3.7 – RD - External memory read pulse
IO programming
The 4 ports P0, Pl, P2, and P3 are 8bit ports. All the ports upon RESFT are
configured as inputs and ready to be used as input ports. When the first 0 is written
to a port, it becomes an output port. To reconfigure as an input, a 1 must be sent to
the port. To use any of these ports as an input port, it must be programmed.
Port 0
Port 0 occupies a total of 8 pins (pins
32 - 39) and used for input or output. To use
the pins of port 0 as both input and output
ports, each pin must be connected externally
to a 10K-ohm pull-up resistor. This is due to
the fact that P0 is an open drain.
Following code will continuously send
out to port 0 the alternating values of 55H and
AAH.
BACK: MOV A,#55H
MOV P0,A
ACALL DELAY
MOV A,#0AAH
MOV PO,A
ACALL DELAY
SJMP BACK
The above example continuously complements the port0 bits.
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
42
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
43
Port 2
Port 2 occupies a total of 8 pins (pins 21 -28). It can be used as input or
output just like P1. Port 2 does not need any pull-up resistors since it already has
pull-up resistors internally. Upon reset, port 2 is configured as an input
The following code will send out continuously to port 2 the alternating values 55H
and AAH.
MOV A, #55H
BACK: MOV P2, A
ACALL DELAY
CPL A
SJMP BACK
Port 2 as input
To make port 2 an input, it must programmed as such by writing 1 to all its bits.
In the following code, port 2 is configured first as an input port by writing 1s to it.
Then data is received from that port and is sent to P1 continuously.
MOV A, #0FFH
MOV P2, A ; make P2 an input port
BACK: MOV A, P2
MOV P1, A
SJMP BACK
Dual role of port 2
P2 is used as simple I / O. port 2 acts as a higher order address bus, along
with P0; provide the 16-bit address for external memory.
Port 3
Port 3 occupies a total of 8 pins (10-17). It can be used as input or output.
P3 does not need any pull up resistors.
The table shows alternating functions of port3.
P3 Bit Function Pin Description
P3.0 RXD 10 Serial data input
P3.1 TXD 11 Serial data
P3.2 INT 0 12 External interrupt 0
P3.3 INT 1 13 External interrupt 1
P3.4 T0 14 Timer 0 input
P3.5 T1 15 Timer 1 input
P3.6 WR 16 External memory write pulse
P3.7 RD 17 External read write pulse
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
44
IO bit-manipulation
Sometimes we need to access only 1 or 2 bits of the port instead of the
entire 8 bits. A powerful feature of 8051 I / O ports is their capability to access
individual bits of the port without altering the rest of the bits in that port.
When accessing a port in single-bit manner, we use the syntax ”SETB X .Y" where
X is the port number 0, 1, 2, or 3, and Y is the desired bit number from 0 to 7.
For example SETB P1.5 instruction sets high bit 5 of port 1.
Single bit addressability of ports
P0 P1 P2 P3 Port Bit
P0.0 P1 .0 P2.0 P30 D0
P0.1 P1.1 P2.1 P3.1 D1
P0.2 P1.2 P2.2 P3.2 D2
P0.3 P1.3 P2.3 P3.3 D3
P0.4 P1.4 P2.4 P3.4 D4
P0.5 P1.5 P2.5 P3.5 D5
P0.6 P1.6 P2.6 P3.6 D6
P0.7 P1.7 P2.7 P3.7 D7
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
45
UNT IV
INTERFACING OF PERIPHERALS TO MICROCONTROLLER
Serial communication
When a microprocessor communicates with the outside world, it provides the
data in byte-sized chunks. The 8-bit data bus is required to transmit; an 8-bit data
path is not safe and expensive, For these reasons, serial communication is used for
transferring data between two systems located at distances of hundreds of feet to
millions of miles apart. Figure shows serial versus parallel data transfers.
The fact that serial communication uses a single data line instead of the 8-bit
data line of parallel communication not only makes it much cheaper but also
enables two computers located in two different cities to communicate over the
telephone.
For serial data communication, the byte of data must be converted to serial
bits using a parallel-in serial-out shift register; then it can be transmitted over a
single data line. This also means that at the receiving end there must be a serial-in-
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
46
parallel-out shift register to receive the serial data and pack them into a byte. This
conversion is performed by a peripheral device called a modem, which stands for
“modulator/demodulator.”
Serial data communication uses two methods, asynchronous and
synchronous. The synchronous method transfers a block of data (characters) at a
time, while the asynchronous method transfers a single byte at a time. The 8051
has a built-in UART (universal asynchronous receiver-transmitter). The 8051 uses
serial data communication circuit that uses register SBUF to hold data. Register
SCON controls data communication, register PCON controls data rates, and pins
RXD and TXD connect to the serial data network.
SBUF is physically two registers. One for transmission and second for
reception, but both the registers use address 99h.
There are four programmable modes for serial data communication that are
chosen by setting the SM0 and SM1 bits in SCON. Baud rates are determined by
the mode selection.
SCON REGISTER
It is bit addressable as SCON.0 to SCON.7.
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
47
Bit 7 - SMOD - Serial baud rate modify bit. Set to 1 by program to double baud rate
using timer 1 for modes 1, 2 and 3 Cleared to 0 by program to use timer 1 baud
rate.
Bits 6, 5 and 4 - Not implemented.
Bit3 - GF 1 - General purpose user flag bit 1. Set/cleared by program.
Bit 2 GF0 - General purpose user flag bit 0. Set/cleared by program.
Bit1 - PD - Power down bit. Set to 1 by program to enter power down configuration
for CHMOS processors.
Bit 0 - IDL - Idle mode bit. Set to 1 by program to enter idle mode configuration for
CHMOS processors.
Serial Data Interrupts
Serial data communication is a relatively slow process, occupying many
milliseconds per data byte to accomplish. The Serial Data flags in SCON TI and RI
are set whenever data byte is transmitted (TI) or received (RI).
Data Transmission
Transmission of serial data bits begins at any time data is written to SBUF. TI is set
to a 1 when the data has been transmitted.
Data Reception
Reception of serial data will begin if the receive enable bit (REN) in SCON is set to
1 for all modes. In addition, for mode 0 only RI must be cleared to 0. Receiver
Interrupt flag RI is set after data has been received in all modes.
Start and stop bits
Asynchronous serial data communication is widely used for character-oriented
transmissions, while block-oriented data transfers use the synchronous method. In
the asynchronous method, each character is placed between start and stop bits.
This is called framing. The start bit is always one bit is always a 0 (low), but the
stop bit can be one or two bits and the stop bit(s) is 1 (high).
Figure shows framing of 8 bits.
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
48
To get baud rates compatible with the PC, we must load TH1 with the values shown
in Table.
Baud Rate TH1 (Decimal) TH1 (Hex)
9600 -3 FD
4800 -6 FA
2400 -12 F4
1200 -24 E8
With XTAL = 11.0592 MHz.
To get 9600 baud rate, we have to load TH1 with -3 or FDh. That is explained as
follows.
Note: XTAL = 1140592 MHz,
2SMOD Crystal frequency
baud rate = X
32 12d (256 - TH1)
20 11.0592 1 11.0592
baud rate = X X 9600Hz
32 12 (256 - FD) 32 12 (3)
Serial data transmission modes and port programming
Serial Data Transmission Modes
The 8051 have included four modes of serial data transmission in a variety
of ways and a multitude of baud rates. Modes are selected by the programmer by
setting the mode bits SM0 and SM1 in SCON. Baud rates are fixed for mode 0 and
variable, using timer 1 and the serial baud rate modify bit (SMOD) in PCON, for
modes 1, 2, and 3.
Serial data mode 0 – Shift register mode
Setting bits SM0 and SM1 in SCON to 00, configures ‘SBUF to receive or
transmit eight data bits using pin RXD for both functions. Pin TXD is connected to
the internal shift frequency pulse source to supply shift pulses to external circuits.
The shift frequency, or baud rate, is fixed at 1/12 of the oscillator frequency.
Received data comes in on pin RXD and should be synchronized with the
shift clock produced at TXD. The baud rate used in mode 0 will be much higher
than standard for any reasonable oscillator frequency.
Serial data mode 1 – Standard UART (8 bit)
When SM0 and SM1 are set to 01, SBUF becomes a 10-bit full-duplex
receiver/transmitter that may receive and transmit data at the same time. Pin RXD
receives all data, and pin TXD transmits all data. Figure shows the format of a data
word.
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
49
Transmitted data is sent as a start bit, eight data bits (LSB first), and a stop
bit. Interrupt flag TI is set once all ten bits have been sent. Each bit interval is the
inverse of the baud rate frequency.
Received data is obtained in the same order; reception is triggered by the
falling edge of the start bit. Data bits are shifted into the receiver at the programmed
baud rate, and the data word will be loaded to SBUF if the following conditions are
true: RI must be 0, and mode bit SM2 is 0 or the stop bit is 1.
Of the original ten bits, the start bit is discarded, the eight data bits go to
SBUF, and the stop bit is saved in bit RB8 of SCON. RI is set to 1, indicating a new
data byte has been received.
Mode 1 baud rate:
Timer 1 is used to generate the baud rate for mode 1 by using the Overflow flag of
the timer. Typically timer 1 is used in timer mode 2 as an autoload-8-bit timer that
generates the baud frequency:
2SMOD Crystal frequency
fbaud 1 = X
32 12d (256 - TH1)
Serial data mode 2 (Multiprocessor mode) : 9bit UART
Mode 2 is similar to mode 1 except 11 bits are transmitted: a start bit, nine data
bits, and a stop bit, as shown in Figure.
The ninth data bit is copied from bit TB8 in SCON during transmit and stored in bit
RB8 of SCON when data is received. Both the start and stop bits are discarded.
The baud rate is programmed as follows:
2SMOD
fbaud 2 = X Crystal frequency
32
Here, as in the case for mode 0, the baud rate is much higher than standard
communication fates. This high data rate is needed in many multiprocessor
applications.
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
50
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
51
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
52
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
53
D0-D7
D0 - D7 are the digital data output pins of a parallel ADC0804. The converted data
is accessed only when CS = O and RD is forced low.
Analog ground and digital ground
These are the input pins providing the ground for both the analog signal and the
digital signal.
The following steps must be followed for data conversion by the ADC0804.
Make CS = 0 and send a low-to-high pulse to pin WR to start the conversion.
Keep monitoring the INTR pin.
If INTR is low, the conversion is finished.
After the INTR has become low, we make CS = 0 and send high -to-low to
RD to get the data out of ADC0804.
The timing for this process is shown in figure.
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
54
Program:
RD BIT P2.5
WR BIT P2.6
INTR BIT P2.7
MYDATA EQU P1
MOV P1, #0FFH
BACK: SETB INTR
CLR WR
SETB WR
HERE: JB INTR, HERE
CLR RD
MOV A, MYDATA
ACALL CONVERSION
ACALL DISPLAY
SETB RD
SJMP BACK
Another useful chip is the
ADC0808/0809 has 8 analog inputs.
This chip allows 8 different analog inputs
by using selection ckt and has 8 output
data lines just like ADC0804.
Fig. shows pin configuration of ADC
0808/09 and table shows selection of
analog input channel.
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
55
Selected
C B A
i/p channel
IN0 0 0 0
IN1 0 0 1
IN2 0 1 0
IN3 0 1 1
IN4 1 0 0
IN5 1 0 1
IN6 1 1 0
IN7 1 1 1
In many applications, space is critical issue, using large no. of pins for data
is not feasible. For this reason serial ADCs are becoming widely used than parallel
ADC. MAX 1112 is a serial ADC chip from MAXIM Corporation.
DAC interfacing
Digital-to-analog (DAC) converter
The digital-to-analog converter (DAC) is a device widely used to convert digital
pulses to analog signals. Basically from digital electronics, we have 2 methods of
designing a DAC.
1. Binary weighted 2. R/2R ladder network
The MC1408 (DAC0808) uses the R/2R method. It is an 8bit DAC converts 8bit
digital data into analog signal. The no. of data inputs decides the resolution of the
DAC. So, the number of analog output levels is equal to 2n, where n is the number
of data bit inputs. Therefore, an 8-input DAC (0808) provides 256 discrete voltage
or current levels of output. In the DAC0808, the digital inputs are converted to
current (Iout), and by connecting a resistor to the Iout pin, we convert the result to
voltage.
The total current provided by the Iout pin is a function of the binary numbers
D0 - D7 inputs of the DAC0808 and the reference current (Iref), and is given by:
D D D D D D D D
Iout = Iref 7 6 5 4 3 2 1 0
2 4 8 16 32 64 128 256
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
56
Where D0 is the LSB, D7 is the MSB for the inputs, and Iref is the input current that
must be applied. If Iref = 2 mA, that all inputs to the DAC are high. The maximum
current is 1.99 mA, which is as follows.
Digital i/p = FFh = 11111111b
1 1 1 1 1 1 1 1 255
Iout = 2 2 2 0.996 1.99 mA
2 4 8 16 32 64 128 256 256
Ex: Program for generation of saw tooth wave form by using above interfacing ckt:
MOV A, #00H
HERE: MOVE P1, A
INC A
SJMP HERE
Ex: The following program is to set maximum required peak value in saw tooth
wave.
MOV R0, #7FH
BACK: MOV A, #00H
HERE: MOVE P1, A
INC A
CJNE A, R0 HERE
SJMP BACK
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
57
CS A1 A0 Port/CWR
0 0 0 A
0 0 1 B
0 1 0 C
0 1 0 CWR
1 x x 8051 not selected
Ex: Obtain the control word for the following configuration of the ports of 8255A.
Port A – as input port,
Mode for Port A – mode 1
Port B – input port,
Mode for Port B – mode 0,
Port CLower – output port
The remaining pins PC6 and PC7 of port C are to be used input pins.
Sol: The control word for this case is given as
Ex: Obtain the control word for the following configuration of the ports of 8255A.
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
58
Port A – as bidirectional
Mode for Port A – mode 2
Port B – input port
Mode for Port B – mode 1
Sol: The control word for this case is given as
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
59
MOV A, 99H
MOV DPTR, #4003H
MOVX A, @DPTR
BACK: MOV DPTR, #4000H
MOVX A, @DPTR
SWAP A
MOV DPTR, #4001H
MOV @DPTR, A
SJMP BACK
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
60
UNIT - V
APPLICATIONS
Temperature measurement
Temperature sensor interfacing
Transducers convert physical data such as temperature, light intensity, flow,
and speed to electrical signals. Depending on the transducer, the output produced
is in the form of voltage, current, resistance, or capacitance. Temperature is
converted to electrical signals using a transducer called a thermistor, it responds to
temperature change by changing resistance, but its response is not linear.
The complexity associated with writing software for such nonlinear devices.
That’s why manufacturers introduced linear temperature sensor. Simple and widely
used linear temperature sensors are the LM34 and LM35.
LM34 and LM35 temperature sensors
The sensor LM34 is a temperature sensor whose output voltage is linearly
proportional to the Fahrenheit temperature. The LM34 requires no external
calibration since it is internally calibrated. It outputs 10 mV for each degree of
Fahrenheit temperature.
The LM35 sensor is a temperature sensor whose output voltage is linearly
proportional to the Celsius (centigrade) temperature. The LM35 requires no
external calibration since it is internally calibrated. It outputs 10 mV for each degree
of centigrade temperature.
Interfacing LM 35 to 8051
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
61
Signal conditioning
The most common transducers produce an output in the
form of voltage, current, charge, capacitance, and
resistance. However, we need to convert these signals to
voltage in order to send input to an A-to-D converter. This
conversion (modification) is called signal conditioning.
The thermistor changes resistance with temperature. The
change of resistance must be translated into voltages by
using signal conditioning process to give ADC.
The interfacing ckt for temperature sensor to the microcontroller via ADC0804 as
shown in fig.
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
62
VCC VSS :
VCC and VSS provide +5V and ground respectively.
VE : it is used for controlling LCD contrast.
RS - register select
There are two very important registers inside the LCD. The RS pin is used for their
selection as follows.
If RS = 0, the instruction command code register is selected, allowing the user to
send a command such as clear display, cursor at home, etc. If RS = 1 the data
register is selected, allowing the user to send data to be displayed on the LCD.
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
63
R/W - read/write
R/W input allows the user to write information to the LCD or read information from
it.
R/W =1 when reading;
R/W = 0 when writing.
E - Enable
The enable pin is used by the LCD to latch information presented to its data pins.
When data is supplied to data pins, a high to low pulse must be applied to this pin.
D0 - D7
The 8-bit data pins, D0 - D7, are used to send information to The LCD or read the
contents of the LCD’s internal registers.
To display letters and numbers, we send ASCII codes for while making RS=1.
There are also instruction commands to the LCD by making RS=0.
We also use RS=0 to check the busy flag bit (D7) to see the LCD is ready to
receive information or not. The busy flag D7 can be read when R/W = 1 and RS =
0. When D7 is 0 the LCD is ready to receive new information.
Code Command to LCD Instruction Register
01 Clear display screen
02 Return home
08 Display off, cursor off
0E Display on, cursor blinking
38 2 lines and 5x7 matrix
Figure shows the interfacing connections of LCD to 8051
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
64
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
65
Data write:
DATA WRT subroutine is used to display on the LCD. RS=1 by SETB RS,
write operation selected (R/W) by CLR R/W, the data displays on LCD which is on
data bus when high to low signal presents at enable pin E.
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
66
A stepper motor is a widely used device that translates electrical pulses into
mechanical movement. In applications such as disk drives, dot matrix printers, and
robotics, the stepper motor is used for position control. Stepper motors commonly
have a permanent magnet rotor (shaft) surrounded by a stator sown in figure.
There are also other steppers called variable reluctance stepper motors that do not
have permanent rotor.
The most common stepper motors have four stator windings that are paired
with a center tapped common as shown in figure.
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
67
Keyboard Interface
Once you add a keyboard for your system, you should allow the user to input
information to the microcontroller in real time.
In general keyboards are organized in a matrix of rows and columns. Figure
shows a 4 x 4 matrix connected to two ports, two sides of matrix are connected to
VCC through resisters while 3rd side is connected to 8051 port 2 which is
configured as input port, and last side connected to port 1 which is configured as an
output port.
Microcontroller keeps scanning the keyboard, if all inputs are high, that
means no key is pressed. If one bit is low that means there is a pressed key.
System designer setup a look up table contained ASCII code for each key. In this
keyboard we have 16 keys to represent the hexa number 0 to F arranged as shown
in fig.
To detect a pressed key
The microcontroller grounds all rows by providing 0 to the P1, and then it reads the
columns. If data is 1111, no key has been pressed and the process continues until
a key press is detected. If one of the column bits has a zero, this means that a key
press has occurred.
To identify exact key pressed
Microcontroller Start with the top row by grounding it, 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 until reach the row that has a pressed key. At this stage,
microcontroller identifies the row that has a pressed key and can setup the starting
address in the look-up table for that row. The last step is finding the column that
has a pressed key by rotating the column bits by using carry flag and RRC
instruction, when 0 bit found in carry, microcontroller pulls the corresponding code
from the look-up table.
The following code is for scanning the 4 X 4 keyboard.
MOV P2, #0FFH - P2 as a i/p port
BACK: MOV P1, #00H - P1 as a o/p port
GO: MOV A, P2
ANL A, #000D1111B
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
68
CJNE A, #00001111B GO
GO1: ACALL DELAY
MOV A, P2
ANL A, #0000111lB
CJNE A, #00001111B, PRS
SJMP GO1
PRS: ACALL DELAY
MOV A, P2
ANL A, #000011l1B
CJNE A, #00001111B, OVER
SJMP GO1
OVER: MOV P1, #11111110B
MOV A, P2
ANL A, #00001111B
CJNE A, #O00O1111B, ROW0
MOV P1, #11111101B
MOV A,P2
ANL A,#000O1111B
CJNE A,#D00Oll1lB,ROW_1
MOV Pl,#11111011B
MOV A,P2
ANL A,#00001111B
CJNE A,#00001111B,ROW_2
MOV P1,#11110111B
MOV A,P2
ANL A,#00001111B
CJNE A,#00001111B,ROW_3
LJMP K2
MOV DPTR,#KCODEO
SJMP FIND
MOV DPTR,#KCODE1
SJMP FIND
MOV DPTR,#KCODE2
SJMP FIND
MOV DPTR,#KCODE3
RRC A
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
69
JNC MATCH
INC DPTR
SJMP FIND
CLR A
MOVC A,OA+DPTR
MOV P0/A
LJMP K1
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam
70
B.Sc. Electronics R L V Prasad, Sr. Lect., Dept of Electronics, SGCSR College, Rajam