Unit-Iv: Intel 8051 Microcontroller
Unit-Iv: Intel 8051 Microcontroller
Unit-Iv: Intel 8051 Microcontroller
Types of microcontrollers:
Applications of microcontrollers
Microcontrollers are mostly used in following electronic equipments:
1. Mobile Phones
2. Auto Mobiles
3. CD/DVD Players
4. Washing Machines
5. Cameras
6. In Computers-> Modems and Keyboard Controllers
7. Security Alarms
8. Electronic Measurement Instruments.
9. Microwave Oven.
2. Intel 8051 microcontroller:
● The 8051 microcontroller is a very popular 8-bit microcontroller introduced by Intel
in the year 1981 and it has become almost the academic standard now a days.
● The 8051 is based on an 8-bit CISC core with Harvard architecture. Its 8-bit
architecture is optimized for control applications with extensive Boolean
processing.
● It is available as a 40-pin DIP chip and works at +5 Volts DC. The salient features
of 8051 controller are given below.
SALIENT FEATURES: The salient features of 8051 Microcontroller are
i. 4 KB on chip program memory (ROM or EPROM)).
ii. 128 bytes on chip data memory (RAM).
iii. 8-bit data bus
iv. 16-bit address bus
v. 32 general purpose registers each of 8 bits
vi. Two -16 bit timers T0 and T1
vii. Five Interrupts (3 internal and 2 external).
ix. Four Parallel ports each of 8-bits (PORT0, PORT1, PORT2, PORT3) with a
total of 32 I/O lines.
x. One 16-bit program counter and One 16-bit DPTR (data pointer)
xi. One 8-bit stack pointer
xii. One Microsecond instruction cycle with 12 MHz Crystal.
xiii. One full duplex serial communication port.
3. PIN Diagram of 8051 Microcontroller:
● The 8051 microcontroller is available as a 40 pin DIP chip and it works at +5 volts
DC. Among the 40 pins, a total of 32 pins are allotted for the four parallel ports P0,
P1, P2 and P3 i.e each port occupies 8-pins .
● The remaining pins are VCC, GND, XTAL1, XTAL2, RST, EA, PSEN.
XTAL1, XTAL2: These two pins are connected to Quartz crystal oscillator which
runs the on-chip oscillator. The quartz crystal oscillator is connected to the two
pins along with a capacitor of 30pF as shown in the circuit. If we use a source
other than the crystal oscillator, it will be connected to XTAL1 and XTAL2 is left
unconnected.
RST: The RESET pin is an input pin and it is an active high pin. When a high pulse
is applied to this pin the microcontroller will reset and terminate all activities. Upon
reset all the registers except PC will reset to 0000 Value and PC register will reset
to 0007 value.
(External Access): This pin is an active low pin. This pin is connected to
ground when microcontroller is accessing the program code stored in the external
memory and connected to Vcc when it is accessing the program code in the on
chip memory. This pin should not be left unconnected.
(Program Store Enable): This is an output pin which is active low. When
the microcontroller is accessing the program code stored in the external ROM, this
pin is connected to the OE (Output Enable) pin of the ROM.
ALE (Address latch enable): This is an output pin, which is active high. When
connected to external memory , port 0 provides both address and data i.e address
and data are multiplexed through port 0 .This ALE pin will de-multiplex the address
and data bus .When the pin is High , the AD bus will act as address bus otherwise
the AD bus will act as Data bus.
P0.0- P0.7(AD0-AD7) : The port 0 pins multiplexed with Address/data pins .If the
microcontroller is accessing external memory these pins will act as address/data
pins otherwise they are used for Port 0 pins.
P2.0- P2.7(A8-A15) : The port2 pins are multiplexed with the higher order address
pins .When the microcontroller is accessing external memory these pins provide
the higher order address byte otherwise they act as Port 2 pins.
P1.0- P1.7: These 8-pins are dedicated for Port1 to perform input or output port
operations.
P3.0- P3.7: These 8-pins are meant for Port3 operations and also for some
control operations like Read, Write, Timer0, Timer1, INT0, INT1, RxD and TxD
● A and B Registers : The A and B registers are special function registers which
hold the results of many arithmetic and logical operations of 8051.
● The A register is also called the Accumulator and as its name suggests, is used as
a general register to accumulate the results of a large number of instructions.
● By default it is used for all mathematical operations and also data transfer
operations between CPU and any external memory.
● The B register is mainly used for multiplication and division operations along with A
register.
MUL AB : DIV AB.
● It has no other function other than as a location where data may be stored.
The R registers:
● The "R" registers are a set of eight registers that are named R0, R1, etc. up to R7.
● These registers are used as auxiliary registers in many operations. The "R"
registers are also used to temporarily store values.
Program Counter (PC): 8051 has a 16-bit program counter .The program
counter always points to the address of the next instruction to be executed. After
execution of one instruction the program counter is incremented to point to the
address of the next instruction to be executed. It is the contents of the PC that
are placed on the address bus to find and fetch the desired instruction. Since the
PC is 16-bit width, 8051 can access program addresses from 0000H to FFFFH, a
total of 6kB of code.
Stack Pointer Register (SP): It is an 8-bit register which stores the address of
the stack top. i.e the Stack Pointer is used to indicate where the next value to be
removed from the stack should be taken from. When a value is pushed onto the
stack, the 8051 first increments the value of SP and then stores the value at the
resulting memory location. Similarly when a value is popped off the stack, the
8051 returns the value from the memory location indicated by SP, and then
decrements the value of SP. Since the SP is only 8-bit wide it is incremented or
decremented by two. SP is modified directly by the 8051 by six instructions:
PUSH, POP, ACALL, LCALL, RET, and RETI. It is also used intrinsically
whenever an interrupt is triggered.
STACK in 8051 Microcontroller: The stack is a part of RAM used by the CPU to
store information
temporarily. This information may be either data or an address .The CPU needs
this storage area as there are only limited number of registers. The register used
to access the stack is called the Stack pointer which is an 8-bit register. So, it can
take values of 00 to FF H. When the 8051 is powered up, the SP register
contains the value 07.i.e the RAM location value 08 is the first location being
used for the stack by the 8051 controller
There are two important instructions to handle this stack. One is the PUSH
and the other is the POP. The loading of data from CPU registers to the stack is
done by PUSH and the loading of the contents of the stack back into a CPU
register is done by POP.
EX: MOV
R6,
#35 H
MOV
R1,
#21 H
PUSH
6
PUSH 1
In the above instructions the contents of the Registers R6 and R1 are moved to
stack and they occupy the 08 and 09 locations of the stack. Now the contents of
the SP are incremented by two and it is 0A Similarly POP 3 instruction pops the
contents of stack into R3 register. Now the contents of the SP is decremented by
1 In 8051 the RAM locations 08 to 1F (24 bytes) can be used for the Stack. In any
program if we need more than 24 bytes of stack, we can change the SP point to
RAM locations 30-7F H. This can be done with the instruction MOV SP, # XX.
Data Pointer Register (DPTR): It is a 16-bit register which is the only user-
accessible. DPTR, as the name suggests, is used to point to data. It is used by a
number of commands which allow the 8051 to access external memory. When
the 8051 accesses external memory it will access external memory at the
address indicated by DPTR. This DPTR can also be used as two 8-registers
DPH and DPL.
Program Status Register (PSW): The 8051 has a 8-bit PSW register which is
also known as Flag register. In the 8-bit register only 6-bits are used by 8051.The
two unused bits are user definable bits. In the 6-bits four of them are conditional
flags .They are Carry –CY, Auxiliary Carry-AC, Parity-P, and Overflow-OV .These
flag bits indicate some conditions that resulted after an instruction was executed.
CY PSW.7 Carry Flag
0 0 0 00H-07H
0 1 1 08H-0FH
1 0 2 10H-17H
1 1 3 18H-1FH
Memory Organization:
● The 8051 microcontroller has 128 bytes of Internal RAM and 4kB of on chip
ROM.
● The RAM is also known as Data memory and the ROM is known as program
memory. The program memory is also known as Code memory .
● This Code memory holds the actual 8051 program that is to be executed.
● In 8051 this memory is limited to 64K .Code memory may be found on-chip, as
ROM or EPROM. It may also be stored completely off-chip in an external ROM
or, more commonly, an external EPROM.
● The 8051 has only 128 bytes of Internal RAM but it supports 64kB of external
RAM. As the name suggests, external RAM is any random access memory
which is off-chip.
● Since the memory is off-chip it is not as flexible in terms of accessing, and is
also slower.
● For example, to increment an Internal RAM location by 1, it requires only 1
instruction and 1 instruction cycle but to increment a 1-byte value stored in
External RAM requires 4 instructions and 7 instruction cycles. So, here the
external memory is 7 times slower.
The 8051 microcontroller has 4kB of on chip ROM but it can be extended up
to 64kB. This ROM is also called program memory or code memory. The
CODE segment is accessed using the program counter (PC) for opcode
fetches and by DPTR for data. The external ROM is accessed when the
EA(active low) pin is connected to ground or the contents of program counter
exceeds 0FFFH.When the Internal ROM address is exceeded the 8051
automatically fetches the code bytes from the external program memory.
●
●
●
■
6.PARALLEL I /O PORTS :
The 8051 microcontroller has four parallel I/O ports, each of 8-bits .So; it provides the
user 32 I/O lines for connecting the microcontroller to the peripherals. The four ports
are P0 (Port 0), P1 (Port1), P2 (Port 2) and P3 (Port3). Upon reset all the ports are
output ports. In order to make them input, all the ports must be set i.e a high bit must be
sent to all the port pins. This is normally done by the instruction “SETB”.
Ex: MOV A, #0FFH ; A = FF
PORT 0:Port 0 is an 8-bit I/O port with dual purpose. If external memory is used, these port
pins are used for the lower address byte address/data (AD0-AD7), otherwise all bits of the
port are either input or output. Unlike other ports, Port 0 is not provided with pull- up resistors
internally, so for PORT0 pull- up resistors of nearly 10k are to be connected externally as
shown in the fig.2.
Dual role of port 0: Port 0 can also be used as address/data bus (AD0-AD7), allowing
it to be used for both address and data. When connecting the 8051 to an external
memory, port 0 provides both address and data. The 8051 multiplexes address and
data through port 0 to save the pins. ALE indicates whether P0 has address or data.
When ALE = 0, it provides data D0-D7, and when ALE =1 it provides address and data
with the help of a 74LS373 latch.
Port 1: Port 1 occupies a total of 8 pins (pins 1 through 8). It has no dual
application and acts only as input or output port. In contrast to port 0, this port
does not need any pull- up resistors since pull- up resistors connected internally.
Upon reset, Port 1 is configured as an output port. To configure it as an input
port, port bits must be set i.e a high bit must be sent to all the port pins. This is
normally done by the instruction “SETB”. For
Ex: MOV A, #0FFH ; A=FF HEX
MOV P1, A ; make P1 an input port by writing 1’s to all of its pins
Port 2: Port 2 is also an eight bit parallel port. (Pins 21- 28). It can be used as input or
output port. As this port is provided with internal pull- up resistors it does not need any
external pull- up resistors. Upon reset, Port 2 is configured as an output port. If the port
is to be used as input port, all the port bits must be made high by sending FF to the
port. For ex,
MOV A, #0FFH ; A=FF hex
MOV P2, A ; make P2 an input port by writing all 1’s to it
Dual role of port 2: Port2 lines are also associated with the higher order address lines
A8-A15. In systems based on the 8751, 8951, and DS5000, Port2 is used as simple I/O
port. But, in 8031-based systems, port 2 is used along with P0 to provide the 16-bit
address for the external memory. Since an 8031 is capable of accessing 64K bytes of
external memory, it needs a path for the 16 bits of the address. While P0 provides the
lower 8 bits via A0-A7, it is the job of P2 to provide bits A8-A15 of the address. In other
words, when 8031 is connected to external memory, Port 2 is used for the upper 8 bits
of the 16 bit address, and it cannot be used for I/O operations.
PORT 3: Port3 is also an 8-bit parallel port with dual function. (Pins 10 to 17). The port
pins can be used for I/O operations as well as for control operations. The details of
these additional operations are given below in the table. Port 3 also do not need any
external pull- up resistors as they are provided internally similar to the case of Port2 &
Port 1. Upon reset port 3 is configured as an output port. If the port is to be used as
input port, all the port bits must be made high by sending FF to the port. For ex,
1 P3.0 10 RxD
2 P3.1 11 TxD
3 P3.2 12
4 P3.3 13
5 P3.4 14 T0
6 P3.5 15 T1
7 P3.6 16
8 P3.7 17
The 8051 microcontroller has FIVE interrupts in addition to Reset. They are
Each interrupt has a specific place in code memory where program execution (interrupt
service routine) begins.
Upon reset all Interrupts are disabled & do not respond to the Microcontroller. These
interrupts must be enabled by software in order for the Microcontroller to respond to
them. This is done by an 8-bit register called Interrupt Enable Register (IE).
EA : Global enable/disable. To enable the interrupts this bit must be set high.
IP.7:
reserved
IP.6:
reserved
bit
The 8051 microcontroller has two 16-bit timers Timer 0 (T0) and Timer 1(T1) which can be used
either to generate accurate time delays or as event counters. These timers are accessed as two 8-bit
registers TLO, THO & TL1, TH1 because the 8051 microcontroller has 8-bit architecture.
TIMER 0 : The Timer 0 is a 16-bit register and can be treated as two 8-bit registers (TL0 & TH0) and
these registers can be accessed similar to any other registers like A,B or R1,R2,R3 etc…
Ex: The instruction MOV TL0,#07 moves the value 07 into lower byte of Timer0.
Similarly MOV R5, TH0 saves the contents of TH0 in the R5 register.
TIMER 1 : The Timer 1 is also a 16-bit register and can be treated as two 8-bit registers (TL1 & TH1)
and these registers can be accessed similar to any other registers like A,B or R1,R2,R3 etc…
Ex: The instruction MOV TL1,#05 moves the value 05 into lower byte of
Timer1. Similarly MOV R0, TH1 saves the contents of TH1 in the R0 register
TMOD Register:
The various operating modes of both the timers T0 and T1 are set by an 8-bit register called
TMOD register. In this TMOD register the lower 4-bits are meant for Timer 0 and the higher 4-bits are
meant for Timer1.
GATE: This bit is used to start or stop the timers by hardware .When GATE= 1, the timers can be started
/ stopped by the external sources. When GATE= 0, the timers can be started or stopped by software
instructions like SETB TR0 or SETB TR1
C/T (clock/Timer): This bit decides whether the timer is used as delay generator or event counter.
When C/T = 0, the Timer is used as delay generator and if C/T=1 the timer is used as an event counter.
The clock source for the time delay is the crystal frequency of 8051.
M1, M0 (Mode): These two bits are the timer mode bits. The timers of the 8051 can be configured in
three modes. Mode0, Mode1 and Mode2. The selection and operation of the modes is shown below.
S. No M0 M1 Mode Operation
13-bit Timer mode
1 0 0 0 8-bit Timer/counter. THx with TLx as 5-bit
prescalar
16-bit Timer mode.16-bit timer /counter
2 0 1 1
without pre-scalar
8-bit auto reload. THx contains a value that
3 1 0 2 is to be loaded into TLx each time
it overflows
4 1 1 3 Split timer mode
Timer Control (TCON) Register:
TCON register is also one of the registers whose bits are directly in control of timer
operation.
Only 4 bits of this register are used for this purpose, while rest of them is used for
interrupt control to be discussed later.
9. 8051-SERIAL COMMUNICATION:
Basics of Serial communication
Data transfer between two electronic devices (Ex Between a computer and
microcontroller or a peripheral device) is generally done in two ways
(i).Serialdata
Transferand
(ii).Paralleldata
Transfer
Serial communication 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 in a timed sequence on a single wire. Serial Communication takes place in
two methods, Asynchronous data Transfer and Synchronous data Transfer.
Asynchronous data transfer allows data to be transmitted without the sender having to send a clock
signal to the receiver. Instead, special bits will be added to each word in order to synchronize the
sending and receiving of the data. When a word is given to the UART for Asynchronous
transmissions, a bit called the "Start Bit" is added to the beginning of each word that is to be
transmitted. The Start Bit is used to alert the receiver that a word of data is about to be sent, and to
force the clock in the receiver into synchronization with the clock in the transmitter.
After the Start Bit, the individual bits of the word of data are sent .Here each bit in
the word is transmitted for exactly the same amount of time as all of the other bits. When
the entire data word has been sent, the transmitter may add a Parity Bit that the
transmitter generates. The Parity bit may be used by the receiver to perform simple error
checking. Then at least one Stop Bit is sent by the transmitter. If the Stop Bit does not
appear when it is supposed to, the UART considers the entire word to be corrupted and
will report a Framing Error.
In the Synchronous data transfer method the receiver knows when to “read” the next
bit coming from the sender. This is achieved by sharing a clock between sender and
receiver. In most forms of serial Synchronous communication, if there is no data
available at a given time to transmit, a fill character will be sent instead so that data is
always being transmitted. Synchronous communication is usually more efficient because
only data bits are transmitted between sender and receiver, however it will be more costly
because extra wiring and control circuits are required to share a clock signal between
the sender and receiver.
Devices that use serial cables for their communication are split into two categories.
1. DTE (Data Terminal Equipment). Examples of DTE are computers, printers & terminals.
2. DCE (Data Communication Equipment). Example of DCE is modems.
Parallel communication uses multiple wires (bus)running parallel to each other, and
can transmit data on all the wires simultaneously. i.e all the bits of the byte are
transmitted at a time.
So, speed of the parallel data transfer is extremely high compared to serial data
transfer. An 8-bit parallel data transfer is 8-times faster than serial data transfer.
Hence with in the computer all data transfer is mainly based on Parallel data transfer.
But only limitation is due to the high cost ,this method is limited to only short distance
communications.
7 No effect of inter symbol interference and Parallel ports suffer extremely from
noise inter-symbol interference (ISI) and
noise, and therefore the data can be
corrupted over long distances.
8 The bandwidth of serial wires is much The bandwidth of parallel wires is much
higher. lower.
10 Serial communication work effectively Parallel buses are hard to run at high
even at high frequencies. frequencies.
The 8051 has two pins for transferring and receiving data by serial communication. These two pins are
part of the Port3(P3.0 &P3.1) .These pins are TTL compatible and hence they require a line driver to
make them RS232 compatible .Max232 chip is one such line driver in use. Serial communication is
controlled by an 8-bit register called SCON register,it is a bit addressable register.
SM0, SM1: These two bits of SCON register determine the framing of data by specifying the
number of bits per character and start bit and stop bits. There are 4 serial modes.
SM0 SM1
0 0 : Serial Mode 0
1 0 : Serial Mode 2
1 1 : Serial Mode 3
REN (Receive Enable) also referred as SCON.4. When it is high,it allows the 8051 to receive
data on the RxD pin. So to receive and transfer data REN must be set to 1.When REN=0,the
receiver is disabled. This is achieved as below
SETB SCON.4
& CLR
SCON.4
TI (Transmit interrupt) is the D1 bit of SCON register. When 8051 finishes the transfer of 8-bit
character, it raises the TI flag to indicate that it is ready to transfer another byte. The TI bit is raised at
the beginning of the stop bit.RI (Receive interrupt) is the D0 bit of the SCON register. When the
8051 receives data serially ,via RxD, it gets rid of the start and stop bits and places the byte in
the SBUF register. Then it raises the RI flag bit to indicate that a byte has been received and
should be picked up before it is lost. RI is raised halfway through the stop bit.
Communication through RS232
A personal computer has a serial port known as communication port or COM Port used to connect a
modem for example or any other device, there could be more then one COM Port in a PC. Serial ports
are controlled by a special chip called UART (Universal Asynchronous Receiver Transmitter).
RS 232 standard describes a communication method where information is sent bit by bit on a physical
channel. The RS stands for Recommended Standard. The information must be broken up in data
words. The length of a data word is variable.
It is one of the popularly known interface standard for serial communication between DTE &
DCE. This RS-232-C is the commonly used standard when data are transmitted as voltage .This standard
was first developed by Electronic industries association(EIA). For the RS-232C, a 25 pin D type
connector is used . DB-25P male and DB-25S female. RS-232 standard was first introduced in 1960’s by
Telecommunications Industry Association(TIA).
The TxD and Rx D pins are connected to the TI in and RI in pins of the MAX 232 IC and the TI
out and RI in pins of the MAX IC are connected to the RxD and TxD pins of the DB9 connector as
shown in the interface diagram.
MOV DPTR ,# 8245H :Move the data 8245 into the data pointer
Direct addressing mode: The addressing mode in which the data operand is in the RAM
location (00 - 7FH) and the address of the data operand is given in the instruction is known as
Direct addressing mode. The direct addressing mode uses the lower 128 bytes of Internal RAM
and the SFRs
register MOV 49H,A : Move the contents of the accumulator into the
RAM location 49. ADD A, 56H : Add the contents of the RAM
Register addressing mode :The addressing mode in which the data operand to be manipulated
lies in one of the registers is known as register addressing mode.
MOV R5, R2 : This is invalid .The data transfer between the registers is not allowed.
Register Indirect addressing mode :The addressing mode in which a register is used
as a pointer to the data memory block is known as Register indirect addressing mode.
MOV A,@ R0 :Move the contents of RAM location whose address is in R0 into A
(accumulator) MOV @ R1 , B : Move the contents of B into RAM location whose address is
held by R1
One of the advantages of register indirect addressing mode is that it makes accessing the data
Indexed addressing mode : This addressing mode is used in accessing the data elements of
Ex : MOVC A, @ A+DPTR
The 16-bit register DPTR and register A are used to form the address of the data element
stored in on- chip ROM. Here C denotes code .In this instruction the contents of A are added
to the 16-bit DPTR register to form the 16-bit address of the data operand.
• Some instructions have one or two additional bytes for operand (data or addresses).
Arithmetic
Logic
Data Transfer
Boolean
Branching
Arithmetic Instructions:
• ADD
• ADDC
– 8-bit addition between the accumulator, a second operand and the previous
value of the CY flag.
•DAA
– Decimal adjust the accumulator.
• Format the accumulator into a proper 2 digit packed BCD number.
• Operates only on the accumulator.
• Works only after the ADD instruction.
• SUBB
– Subtract with Borrow.
• Subtract an operand and the previous value of the borrow (carry) flag from
the accumulator.
– A A - <operand> - CY.
ADD 34 to 49 BCD
in A
DAA ; A = 83H
•INC
– Increment the operand by one.
• The operand can be a register, a direct address, an indirect address, the data pointer.
• DEC
– Decrement the operand by one.
• The operand can be a register, a direct address, an indirect address.
• MUL AB / DIV AB
– Multiply A by B and place result in A:B.
– Divide A by B and place result in A:B.
– Logical Operations:
• ANL / ORL
– Work on byte sized operands or the CY flag.
• ANL A, Rn
• ANL A, direct
• ANL A, @Ri
• ANL A, #data
• ANL direct, A
• ANL direct, #data
• ANL C, bit
• ANL C, /bit
• XRL
– Works on bytes only.
– CPL / CLR
– Complement / Clear.
– Work on the accumulator or a bit.
• CLR P1.2
• RL / RLC / RR / RRC
– Rotate the accumulator.
• RL and RR without the carry
• RLC and RRC rotate through the carry.
• SWAP A
– Swap the upper and lower nibbles of the accumulator.
• No compare instruction.
– Built into conditional branching instructions.
Boolean Operations:
• This group of instructions is associated with the single-bit operations of the 8051.
• This group allows manipulating the individual bits of bit addressable registers and
memory locations as well as the CY flag.
– The P, OV, and AC flags cannot be directly altered.
• This group includes:
– Set, clear, and, or complement, move.
– Conditional jumps.
• CLR
– Clear a bit or the CY flag.
• CLR P1.1
• CLR C
• SETB
– Set a bit or the CY flag.
• SETB A.2
• SETB C
• CPL
– Complement a bit or the CY flag.
• CPL 40H ; Complement bit 40 of the bit addressable memory
• ORL / ANL
– OR / AND a bit with the CY flag.
• ORL C, 20H ; OR bit 20 of bit addressable memory with the CY
flag
• ANL C, /34H ; AND complement of bit 34 of bit addressable
memory with the CY flag.
• MOV
– Data transfer between a bit and the CY flag.
MOV C, 3FH ; Copy the CY flag to bit 3F of the bit
addressablememory.
• MOV P1.2, C ; Copy the CY flag to bit 2 of P1.
• JC / JNC
• Jump to a relative address if CY is set / cleared.
• JB / JNB
• Jump to a relative address if a bit is set / cleared.
• JB ACC.2, <label>
• JBC
• Jump to a relative address if a bit is set and clear the bit.
Branching Instructions:
• The 8051 provides four different types of unconditional jump instructions:
– Short Jump – SJMP
• Uses an 8-bit signed offset relative to the 1st byte of the next instruction.
– Long Jump – LJMP
• Uses a 16-bit address.
• 3 byte instruction capable of referencing any location in the entire 64K of program
memory.
– Absolute Jump – AJMP
• Uses an 11-bit address.
• 2 byte instruction
• The upper 3-bits of the address combine with the 5-bit opcode to
form the 1st byte and the lower 8-bits of the address form the 2nd
byte.
• The 11-bit address is substituted for the lower 11- bits of the PC to
calculate the 16-bit address of the target.
• The location referenced must be within the 2K Byte memory page containing the AJMP
instruction.
– Indirect Jump – JMP
JMP @A + DPTR
• The 8051 provides 2 forms for the CALL instruction:
– Absolute Call – ACALL
• Uses an 11-bit address similar to AJMP
• The subroutine must be within the same 2K page.
– Long Call – LCALL
• Uses a 16-bit address similar to LJMP
• The subroutine can be anywhere.
– Both forms push the 16-bit address of the next instruction on the stack and update the
stack pointer.
• The 8051 provides 2 forms for the return instruction:
– Return from subroutine – RET
• Pop the return address from the stack and continue execution there.
– Return from ISV – RETI
• Pop the return address from the stack.
• Restore the interrupt logic to accept additional interrupts at the same priority level as the
one just processed.
• Continue execution at the address retrieved from the stack.
• The PSW is not automatically restored.
• The 8051 supports 5 different conditional jump instructions.
– ALL conditional jump instructions use an 8-bit signed offset.
– Jump on Zero – JZ / JNZ
• Jump if the A == 0 / A != 0
• The check is done at the time of the instruction execution.
– Jump on Carry – JC / JNC
• Jump if the C flag is set / cleared.
– Jump on Bit – JB / JNB
• Jump if the specified bit is set / cleared.
• Any addressable bit can be specified.
– Jump if the Bit is set then Clear the bit – JBC
• Jump if the specified bit is set.
• Then clear the bit.
• Compare and Jump if Not Equal – CJNE
– Compare the magnitude of the two operands and jump if they are not equal.
• The values are considered to be unsigned.
• The Carry flag is set / cleared appropriately.
• CJNE A, direct, rel
• CJNE A, #data, rel
• CJNE Rn, #data, rel
• CJNE @Ri, #data, rel
• Decrement and Jump if Not Zero – DJNZ
– Decrement the first operand by 1 and jump to the location identified by the second
operand if the resulting value is not zero.
• DJNZ Rn, rel
• DJNZ direct, rel
• No Operation
– NOP
o Whenever a key is pressed, a row and a column gets shorted through that pressed key and all
the other keys are left open.
o When a key is pressed only a bit in the port goes high. Which indicates microcontroller that
the key is pressed. By this high on the bit key in the corresponding column is identified.
o Once we are sure that one of key in the key board is pressed next our aim is to identify that
key. To do this we firstly check for particular row and then we check the corresponding
column the key board.
o To check the row of the pressed key in the keyboard, one of the row is made high by making
one of bit in the output port of 8051 high . This is done until the row is found out. Once we
get the row next out job is to find out the column of the pressed key. The column is detected
by contents in the input ports with the help of a counter. The content of the input port is
rotated with carry until the carry bit is set.
o The contents of the counter is then compared and displayed in the display. This display is
designed using a seven segment display and a BCD to seven segment decoder IC 7447.
o The BCD equivalent number of counter is sent through output part of 8051 displays the
number of pressed key.
1. The 8051 has 4 I/O ports P0 to P3 each with 8 I/O pins, P0.0 to P0.7, P1.0 to P1.7, P2.0
to P2.7, and P3.0 to P3.7. The one of the port P1 (it understood that P1 means P1.0 to
P1.7) as an I/P port for microcontroller 8051, port P0 as an O/P port of microcontroller
8051 and port P2 is used for displaying the number of pressed key.
2. Make all rows of port P0 high so that it gives high signal when key is pressed.
3. See if any key is pressed by scanning the port P1 by checking all columns for non zero
condition.
4. If any key is pressed, to identify which key is pressed make one row high at a time.
5. Initiate a counter to hold the count so that each key is counted.
6. Check port P1 for nonzero condition. If any nonzero number is there in
[accumulator], start column scanning by following step 9.
7. Otherwise make next row high in port P1.
8. Add a count of 08h to the counter to move to the next row by repeating steps from step 6.
9. If any key pressed is found, the [accumulator] content is rotated right through the
carry until carry bit sets, while doing this increment the count in the counter till carry is
found.
10. Move the content in the counter to display in data field or to memory location
11. To repeat the procedures go to step 2.
o 7 segment LED display is very popular and it can display digits from 0 to 9 and quite a few
characters like A, b, C, ., H, E, e, F, n, o,t,u,y, etc.
o Knowledge about how to interface a seven segment display to a micro controller is very
essential in designing embedded systems.
o A seven segment display consists of seven LEDs arranged in the form of a squarish „8‟
slightly inclined to the right and a single LED as the dot character.
o Different characters can be displayed by selectively glowing the required LED segments.
Seven segment displays are of two types, common cathode and common anode.
In common cathode type , the cathode of all LEDs are tied together to a single terminal
which is usually labeled as„com„ and the anode of all LEDs are left alone as individual pins
labeled as a, b, c, d, e, f, g & h (or dot) .
In common anode type, the anode of all LEDs are tied together as a single terminal and
cathodes are left alone as individual pins.
The pin out scheme and picture of a typical 7 segment LED display is shown in the image
below.