SlideShare a Scribd company logo
1 of 112
RAMCO INSTITUTE OF
TECHNOLOGY
DEPARTMENT OF ECE
Dr. A. Azhagu Jaisudhan Pazhani
Contents:
Introduction
Block Diagram and Pin Description of the 8051
Registers
Memory mapping in 8051
I/O Port Programming
Timer
Interrupt
Why do we need to learn
Microprocessors/controllers?
• The microprocessor is the core of computer systems.
• Nowadays many communication, digital
entertainment, portable devices, are controlled by
them.
• A designer should know what types of components
he needs, ways to reduce production costs and
product reliable.
Different aspects of a
microprocessor/controller
• Hardware :Interface to the real world
• Software :order how to deal with inputs
The necessary tools for a
microprocessor/controller
• CPU: Central Processing Unit
• I/O: Input /Output
• Bus: Address bus & Data bus
• Memory: RAM & ROM
• Timer
• Interrupt
• Serial Port
• Parallel Port
CPU
General-
Purpose
Micro-
processor
RAM ROM I/O
Port
Timer
Serial
COM
Port
Data Bus
Address Bus
General-Purpose Microprocessor System
Microprocessors:
• CPU for Computers
• No RAM, ROM, I/O on CPU chip itself
• Example:Intel’s x86, Motorola’s 680x0
Many chips on mother’s board
General-purpose microprocessor
RAM ROM
I/O
Port
Timer
Serial
COM
Port
Microcontroller
CPU
• A smaller computer
• On-chip RAM, ROM, I/O ports...
• Example:Motorola’s 6811, Intel’s 8051, Zilog’s Z8 and PIC 16X
A single chip
Microcontroller :
Microprocessor
• CPU is stand-alone, RAM,
ROM, I/O, timer are
separate
• designer can decide on the
amount of ROM, RAM and
I/O ports.
• expansive
• general-purpose
Microcontroller
• CPU, RAM, ROM, I/O and
timer are all on a single chip
• fix amount of on-chip ROM,
RAM, I/O ports
• Not expansive
• single-purpose
Microprocessor vs. Microcontroller
Block Diagram
CPU
On-chip
RAM
On-chip
ROM for
program
code
4 I/O Ports
Timer 0
Serial
Port
OSC
Interrupt
Control
External interrupts
Timer 1
Timer/Counter
Bus
Control
TxD RxD
P0 P1 P2 P3
Address/Data
Counter
Inputs
• The Intel 8051 is a very popular general purpose microcontroller
widely used for small scale embedded systems.
• Many vendors such as Atmel, Philips, and Texas Instruments
produce MCS-51 family microcontroller chips.
• The 8051 is an 8-bit microcontroller with 8 bit data bus and 16-
bit address bus.
• The 16 bit address bus can address a 64K( 216) byte code memory
space and a separate 64K byte of data memory space.
• The 8051 has 4K on-chip read only code memory and 128 bytes
of internal Random Access Memory (RAM)
Overview
• Besides internal RAM, the 8051 has various Special Function Registers
(SFR) such as the Accumulator, the B register, and many other control
registers.
• 34 8-bit general purpose registers in total.
The ALU performs one 8-bit operation at a time.
• Two 16 bit /Counter timers
• 3 internal interrupts (one serial), 2 external interrupts.
• 4 8-bit I/O ports (3 of them are dual purposed). One of them used for
serial port
• Some 8051 chips come with UART for serial communication and ADC for
analog to digital conversion.
Overview
8051 Chip Pins
40 pins on the 8051 chip.
Most of these pins are used to connect to I/O devices or external data
and code memory.
• 4 I/O port take 32 pins(4 x 8 bits) plus a pair of XTALS pins for
crystal clock
• A pair of Vcc and GND pins for power supply (the 8051 chip
needs +5V 500mA to function properly)
• A pair of timer pins for timing controls, a group of pins (EA, ALE,
PSEN, WR, RD) for internal and external data and code memory
access controls
• One Reset pin for reboot purpose
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
40
39
38
37
36
35
34
33
32
30
29
28
27
26
25
24
23
22
21
31
P1.0
P1.1
P1.2
P1.3
P1.4
P1.5
P1.6
RST
(RXD) P3.0
(TXD) P3.1
(INT0) P3.2
(INT1) P3.3
(T0) P3.4
(T1) P3.5
(WR) P3.6
XTAL 2
XTAL 1
GND
Vcc
P0.0(AD0)
P2.0(A8)
P2.1(A9)
P2.2(A10)
P2.3(A11)
P2.4(A12)
P2.5(A13)
P2.6(A14)
P2.7(A15)
PSEN
ALE/PROG
EA/VPP
P0.6(AD6)
P0.7(AD7)
P0.5(AD5)
P0.4(AD4)
P0.3(AD3)
P0.2(AD2)
P0.1(AD1)
(RD) P3.7
P1.7
(Serial)
interrupt
Timer
Ex M W/R
clock
Ext
Memory
Address
Ext Memory
Access Control
Ext Memory
Address
8051
Pin out Diagram of the 8051 Microcontroller
Port 0
(enable Ex. Memory)
Port 2
ALE Address Latch Enable
EA’
PSEN’
RD’
WR’ Ex.Data RAM write
OE’
A8 - A15
A0 – A7
D0 – D7
Ex. Data RAM Reade
Extensl Code Memory
ROM or EPROM
8051
adress
multiplex
latch
The Pin Connection for External Code and Data Memory
• The EA' (External Access) pin is used to control the internal or
external memory access.
The signal 0 is for external memory access and signal 1
for internal memory access.
• The PSEN' (Program Store Enable) is for reading external
code memory when it is low (0) and EA is also 0.
• The ALE (Address Latch Enable) activates the port 0 joined with port 2 to
provide 16 bit external address bus to access the external memory. The
ALE multiplexes the P0:
1 for latching address on P0 as A0-A7 in the 16 bit address buss, 0
for latching P0 as data I/O.
• P0.x is named ADx because P0 is multiplexed for Address bus and Data
bus at different clock time.
WR' only provides the signal to write external data memory
RD' provides the signal to read external data and code memory.
System Clock and Oscillator Circuits
• The 8051 requires an external oscillator circuit. The
oscillator circuit usually runs around 12MHz. the
crystal generates 12M pulses in one second. The pulse
is used to synchronize the system operation in a
controlled pace..
• A machine cycle is minimum amount time a simplest
machine instruction must take
• An 8051 machine cycle consists of 12 crystal pulses
(clock cycle).
• instruction with a memory operand so that it needs
multiple memory accesses.
The first 6 crystal pulses (clock cycle) is used to fetch
the opcode and the second 6 pulses are used to
perform the operation on the operands in the ALU.
This gives an effective machine cycle rate at 1MIPS
(Million Instructions Per Second).
XTAL!
XTAL2
GND
Crystal
Crystal to 8051 XTAL 1/2
Features of the 8051 Microcontroller
• The 8031 requires external instruction memory.
– It can be as large as 64K Bytes.
– You lose 2 ports for interfacing to the external memory.
• You can replace these by interfacing the chip to an I/O port
controller like the 8255.
• The 8051 is the original member of the Intel MCS-51 family of
Microcontrollers.
– There are several varieties that differ slightly in the available features.
8051 Architecture
• Programmer’s View
– Memory Organization
– Register Set
– Instruction Set
• Hardware Designer’s View
– Pin-out
– Timing characteristics
– Current / Voltage requirements
Monday, May 31, 2021
Block Diagram
• Internal ROM and RAM
• I/O Ports with programmable Pins
• ALU
• Working Registers
• Clock Circuits
• Timers and Counters
• Serial Data Communication.
Monday, May 31, 2021
8051 Internal Architecture
• The CPU has many important registers. The Program Count
(PC) always holds the code memory location of next
instruction.
• The CPU is the heart of any computer which is in charge of
computer operations.
• It fetches instructions from the code memory into the
instruction Register (IR), analyzes the opcode of the
instruction.
• updates the PC to the location of next instruction, fetches the
operand from the data memory if necessary, and finally
performs the operation in the Arithmetic-Logic Unit (ALU)
within the CPU.
• The B register is a register just for multiplication and
division operation which requires more register
spaces for the product of multiplication and the
quotient and the remainder for the division.
• The immediate result is stored in the accumulator
register (Acc) for next operation and the Program
Status Word (PSW) is updated depending on the
status of the operation result
The B Register
• Commonly used as a temporary registe
• Used by two op-codes
– MUL AB, div AB
• B register holds the second operand and will hold part of the
result
– Upper 8 bits of the multiplication result
– Remainder in case of division.
• Bit addressable.
The DPL and DPH Registers
• Two 8-bit registers that can be combined into a 16-bit DPTR – Data
Pointer.
• Used by commands that access external memory
• Also used for storing 16bit values
mov DPTR, #data16
; setup DPTR with 16bit ext address
movx A, @DPTR
; copy mem[DPTR] to A
• Can be accessed as 2 separate 8-bit registers if needed.
• DPTR is useful for string operations and Look-Up-Table (LUT)
operations.
The SP Register
• SP is the stack pointer.
• SP points to the last used location of the stack.
– Push operation will first increment SP and then copy data.
– Pop operation will first copy data and then decrement SP.
• In 8051, stack grows upwards (from low memory to high memory) and can
be in the internal RAM only.
• On power-up, SP points to 07H.
– Register banks 2,3,4 (08H to 1FH) form the default stack area.
• Stack can be relocated by setting SP to the upper memory area in 30H to
7FH.
– mov SP, #32H
A and B registers
• Totally 34 general purpose registers or working registers.
• Two of these A and B hold results of many instructions,
particularly math and logical operations of 8051 cpu.
• The other 32 are in four banks,B0 – B3 of eight registers each.
• A(accumulator) is used for addition,subtraction,mul,div,boolean
bit manipulation and for data transfers.
• But B register can only be used for mul and div operations.
Monday, May 31, 2021
The SBUF Register
• Serial Port Data Buffer.
• 2 registers at the same location
– One is read-only used for reading serial input data.
• Serial Data Receive Buffer.
– The other is write-only used for storing serial
output data.
• Serial Data Transmit Buffer.
Link: SFR address
Timer Registers – TH0 and TL0
• The high and low bytes of the 16-bit counting register for
timer/counter T0.
• There is also a TH1 / TL1 pair for the T1 timer.
• In the 8052, one more pair exists (TH2) / (TL2) for the T2
timer.
• (RCAP2H) and (RCAP2L) exist only in the 8052 and they are
copies of the TH2 and TL2 registers.
Link: SFR address
Control Registers
• IP – Interrupt Priority.
• IE – Interrupt Enable.
• TMOD – Timer Mode.
• TCON – Timer Control.
• T2CON – Timer 2 Control (8052)
• SCON – Serial Port Control.
• PCON – Power Control (80C51).
Link: SFR address
The PSW Register
• Program Status Word is a “bit addressable” 8-bit register that
has all the flags.
MSB LSB
CY AC F0 RS1 RS2 OV - P
Symbol Position Function
CY PSW.7 Carry Flag
AC PSW.6 Auxiliary Carry Flag. For BCD
Operations
F0 PSW.5 Flag 0. Available to the user for general
purposes.
RS1 PSW.4 Register bank select bits. Set by
software to determine which register
bank is being used.
RS2 PSW.3
OV PSW.2 Overflow Flag
- PSW.1 Not used
P PSW.0 Parity Flag. Even Parity.
Monday, May 31, 2021
8051 Flag bits and the PSW register
• PSW Register
CY AC F0 RS1 OV
RS0 P
--
CY
PSW.7
Carry flag
AC
PSW.6
Auxiliary carry flag
--
PSW.5
Available to the user for general purpose
RS1
PSW.4
Register Bank selector bit 1
RS0
PSW.3
Register Bank selector bit 0
OV
PSW.2
Overflow flag
--
PSW.1
User define bit
P
PSW.0
Parity flag Set/Reset odd/even parity
RS1 RS0 Register Bank Address
0 0 0 00H-07H
0 1 1 08H-0FH
1 0 2 10H-17H
1 1 3 18H-1FH
Ports
Port Reading and Writing
There are 4 8-bit ports: P0, P1, P2 and P3. All of them are
dual purpose ports except P1 which is only used for I/O. The
following diagram shows a single bit in an 8051 I/O port.
Internal data bus(one bit)
D Q
CL Q’
Latch
Read latch Read pin
Vcc(5v)
Port pin
10k
FET
Single Bit In I/O Port
The Port Alternate Functions
• PORT P1 (Pins 1 to 8): The port P1 is a port dedicated for general
I/O purpose. The other ports P0, P2 and P3 have dual roles in
addition to their basic I/O function.
• PORT P0 (pins 32 to 39): When the external memory access is
required then Port P0 is multiplexed for address bus and data
bus that can be used to access external memory in conjunction
with port P2. P0 acts as A0-A7 in address bus and D0-D7 for port
data. It can be used for general purpose I/O if no external
memory presents.
• PORT P2 (pins 21 to 28): Similar to P0, the port P2 can also play a
role (A8-A15) in the address bus in conjunction with PORT P0 to
access external memory.
Contd.
• PORT P3 (Pins 10 to 17):
In addition to acting as a normal I/O port,
• P3.0 can be used for serial receive input pin(RXD)
• P3.1 can be used for serial transmit output pin(TXD)
in a serial port,
• P3.2 and P3.3 can be used as external interrupt
pins(INT0’ and INT1’),
• P3.4 and P3.5 are used for external counter input
pins(T0 and T1),
• P3.6 and P3.7 can be used as external data memory
write and read control signal pins(WR’ and RD’)read
and write pins for memory access.
Memory Organization
The 8051 has separate address spaces for
program storage and data storage.
– Depending on the type of instruction, the same
address can refer to two logically and physically
different memory locations.
Program Storage
• After reset, the MCS-51 starts fetching instructions from
0000H.
– This can be either on-chip or external depending on the
value of the EA input pin.
• If EA* is low, then the program memory is external.
• If EA* is high, then addresses from 0000 to 0FFF will
refer to on-chip memory and addresses 1000 up to FFFF
refer to external memory.
– Note that the 8031 must have its EA connected low as all of
its memory is external.
Link: EA connection
Access to External Memory
• Port 0 acts as a multiplexed address/data bus. Sending the
low byte of the program counter (PCL) as an address.
• Port 2 sends the program counter high byte (PCH) directly to
the external memory.
• The signal ALE operates as in the 8051 to allow an external
latch to store the PCL byte while the multiplexed bus is made
ready to receive the code byte from the external memory.
• Port 0 then switches function and becomes the data bus
receiving the byte from memory.
Link: H/w Interfacing
Data Storage
• The 8051 has 256 bytes of RAM on-chip.
– The lower 128 bytes are intended for internal data storage.
– The upper 128 bytes are the Special Function Registers
(SFR).
• The lower 128 bytes are not to be used as standard RAM.
– Internally 8051’s registers default to stack area, and other
features. [00-7FH]
Link: Memory Organization
Data Storage [Cont...]
• The lowest 32 bytes of the on-chip RAM form 4 banks of 8
registers each.
• Only one of these banks can be active at any time.
• Bank is chosen by setting 2 bits in PSW
– Default bank (at power up) is bank 0 (locations 00 – 07).
• The 8 registers in any active bank are referred to as R0 through
R7
• Given that each register has a specific address, it can be
accessed directly using that address even if its bank is not the
active one.
Data Storage [Cont...]
• The next 16 bytes – locations 20H to 2FH – form a block that
can be addressed as either bytes or individual bits.
– The bytes have addresses 20H to 2FH.
– The bits have addresses 00H to 7FH.
– Specific instructions are used for accessing
the bits.
• Locations 30H to 7FH are general purpose RAM.
Link: Memory Organization
The SFR (Special Function Register)
• The upper 128 bytes of the on-chip RAM are used to house special
function registers.
• In reality, only about 25 of these bytes are actually used. The
others are reserved for future versions of the 8051.
– These are registers associated with important functions in the
operation of the MCS-51.
– Some of these registers are bit-addressable as well as byte-
addressable.
• The address of bit 0 of the register will be the same as the address
of the register.
The Elements of SFR
• ACC and B registers – 8 bit each
• DPTR : [DPH:DPL] – 16 bit combined
• PC (Program Counter) – 16 bits
• SP (Stack Pointer) – 8 bit
• PSW (Program Status Word)
• Port Latches
• Serial Data Buffer
• Timer Registers
• Control Registers
Link: SFR Elements
Monday, May 31, 2021
Port 0(pins 32-39)
• When connecting an 8051 to an external memory, the 8051
uses ports to send addresses and read instructions.
– 16-bit address:P0 provides both address A0-A7, P2
provides address A8-A15.
– Also, P0 provides data lines D0-D7.
• When P0 is used for address/data multiplexing, it is
connected to the 74LS373 to latch the address.
I/O Port Programming
Monday, May 31, 2021

Port 1(pins 1-8)
• Port 1 is denoted by P1.
– P1.0 ~ P1.7
– P1 as an output port (i.e., write CPU data to the external pin)
– P1 as an input port (i.e., read pin data into CPU bus)
Monday, May 31, 2021
ALE Pin
• The ALE pin is used for de-multiplexing the
address and data by connecting to the G pin of
the 74LS373 latch.
– When ALE=0, P0 provides data D0-D7.
– When ALE=1, P0 provides address A0-A7.
– The reason is to allow P0 to multiplex address and
data.
Monday, May 31, 2021
Port 3(pins 10-17)
• Although port 3 is configured as an output port upon reset,
this is not the way it is most commonly used.
• Port 3 has the additional function of providing signals.
– Serial communications signal:RxD, TxD
– External interrupt:/INT0, /INT1
– Timer/counter:T0, T1
– External memory accesses :/WR, /RD
Monday, May 31, 2021
Port 3 Alternate Functions
17
RD
P3.7
16
WR
P3.6
15
T1
P3.5
14
T0
P3.4
13
INT1
P3.3
12
INT0
P3.2
11
TxD
P3.1
10
RxD
P3.0
Pin
Function
P3 Bit

Monday, May 31, 2021
Addressing Modes
• Immediate
• Register
• Direct
• Register Indirect
• Indexed
The way in which the instruction is specified.
Monday, May 31, 2021
Immediate Addressing Mode
• Immediate Data is specified in the instruction itself
• Egs:
MOV A,#65H
MOV A,#’A’
MOV R6,#65H
MOV DPTR,#2343H
MOV P1,#65H
Monday, May 31, 2021
Register Addressing Mode
MOV Rn, A ;n=0,..,7
ADD A, Rn
MOV DPL, R6
MOV DPTR, A
MOV Rm, Rn
Monday, May 31, 2021
Direct Addressing Mode
Although the entire of 128 bytes of RAM can
be accessed using direct addressing mode, it is
most often used to access RAM loc. 30 – 7FH.
MOV R0, 40H
MOV 56H, A
MOV A, 4 ; ≡ MOV A, R4
MOV 6, 2 ; copy R2 to R6
; MOV R6,R2 is invalid !
Monday, May 31, 2021
Register Indirect Addressing Mode
• In this mode, register is used as a pointer to the data.
MOV A,@Ri
; move content of RAM loc. Where address is held by Ri into
A
( i=0 or 1 )
MOV @R1,B
In other word, the content of register R0 or R1 is sources or
target in MOV, ADD and SUBB insructions.
 jump
Monday, May 31, 2021
Indexed Addressing Mode And On-Chip
ROM Access
• This mode is widely used in accessing data elements
of look-up table entries located in the program (code)
space ROM at the 8051
MOVC A,@A+DPTR
A= content of address A +DPTR from ROM
Note:
Because the data elements are stored in the program
(code ) space ROM of the 8051, it uses the instruction
MOVC instead of MOV. The “C” means code.
Microcontroller Intel 8051
[Instruction Set]
Structure of Assembly Language
[ label: ] mnemonic [operands] [ ;comment ]
Example:
MOV R1, #25H ; load data 25H into R1
63
8051 Assembly Language
• Registers
64
MOV Instruction:
MOV destination, source
Example:
1. MOV A, $55H
2. MOV R0, A
3. MOV A, R3
Instruction Groups
• The 8051 has 255 instructions
– Every 8-bit opcode from 00 to FF is used except for A5.
• The instructions are grouped into 5 groups
– Arithmetic
– Logic
– Data Transfer
– Boolean
– Branching
Arithmetic Instructions
• ADD
– 8-bit addition between the accumulator (A) and a second
operand.
• The result is always in the accumulator.
• The CY flag is set/reset appropriately.
• ADDC
– 8-bit addition between the accumulator, a second operand
and the previous value of the CY flag.
• Useful for 16-bit addition in two steps.
• The CY flag is set/reset appropriately.
Arithmetic Instructions
• DA
– 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.
– The result is always saved in the accumulator.
– The CY flag is set/reset appropriately.
Arithmetic Instructions
• 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
Logical Operations
• XRL
– Works on bytes only.
• CPL / CLR
– Complement / Clear.
– Work on the accumulator or a bit.
• CLR P1.2
Logical Operations
• 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.
Data Transfer Instructions
• MOV
– 8-bit data transfer for internal RAM and the SFR.
• MOV A, Rn MOV A, direct
• MOV A, @Ri MOV A, #data
• MOV Rn, A MOV Rn, direct
• MOV Rn, #data MOV direct, A
• MOV direct, Rn MOV direct, direct
• MOV direct, @Ri MOV direct, #data
• MOV @Ri, A MOV @Ri, direct
• MOV @Ri, #data
Data Transfer Operations
• MOV
– 1-bit data transfer involving the CY flag
• MOV C, bit
• MOV bit, C
• MOV
– 16-bit data transfer involving the DPTR
• MOV DPTR, #data
Data Transfer Instructions
• MOVC
– Move Code Byte
• Load the accumulator with a byte from program
memory.
• Must use indexed addressing
 MOVC A, @A+DPTR
 MOVC A, @A+PC
Data Transfer Instructions
• MOVX
– Data transfer between the accumulator and a byte
from external data memory.
• MOVX A, @Ri
• MOVX A, @DPTR
• MOVX @Ri, A
• MOVX @DPTR, A
Data Transfer Instructions
• PUSH / POP
– Push and Pop a data byte onto the stack.
– The data byte is identified by a direct address
from the internal RAM locations.
• PUSHDPL
• POP 40H
Data Transfer Instructions
• XCH
– Exchange accumulator and a byte variable
• XCH A, Rn
• XCH A, direct
• XCH A, @Ri
• XCHD
– Exchange lower digit of accumulator with the lower digit of the
memory location specified.
• XCHD A, @Ri
• The lower 4-bits of the accumulator are exchanged with the lower
4-bits of the internal memory location identified indirectly by the
index register.
• The upper 4-bits of each are not modified.
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.
Boolean Operations
• 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
Boolean Operations
• 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 addressable memory.
• MOV P1.2, C ; Copy the CY flag to bit 2 of P1.
Boolean Operations
• 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.
Branching Instructions
– 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
Branching Instructions
• 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.
Branching Instructions
• 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.
Branching Instructions
• 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.
Branching Instructions
– 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.
Branching Instructions
• 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
Branching Instructions
• 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
Some Simple Instructions
MOV dest,source ; dest = source
MOV A,#72H ;A=72H
MOV R4,#62H;R4=62H
MOV B,0F9H ;B=the content of F9’th byte of RAM
MOV DPTR,#7634H
MOV DPL,#34H
MOV DPH,#76H
MOV P1,A ;mov A to port 1
Note 1:
MOV A,#72H ≠ MOV A,72H
After instruction “MOV A,72H ” the content of 72’th byte of RAM will
replace in Accumulator.
Note 2:
MOV A,R3 ≡ MOV A,3
ADDA, Source ;A=A+SOURCE
ADDA,#6 ;A=A+6
ADDA,R6 ;A=A+R6
ADD A,6 ;A=A+[6] or A=A+R6
ADD A,0F3H ;A=A+[0F3H]
SUBB A, Source ;A=A-SOURCE-C
SUBB A,#6 ;A=A-6
SUBB A,R6 ;A=A+R6
MUL & DIV
• MUL AB ;B|A = A*B
MOV A,#25H
MOV B,#65H
MUL AB ;25H*65H=0E99
;B=0EH, A=99H
• DIV AB ;A = A/B, B = A mod B
MOV A,#25
MOV B,#10
DIV AB ;A=2, B=5
SETB bit ; bit=1
CLR bit ; bit=0
SETB C ; CY=1
SETB P0.0 ;bit 0 from port 0 =1
SETB P3.7 ;bit 7 from port 3 =1
SETB ACC.2 ;bit 2 from ACCUMULATOR =1
SETB 05 ;set high D5 of RAM loc. 20h
Note:
CLR instruction is as same as SETB
i.e.:
CLR C ;CY=0
But following instruction is only for CLR:
CLR A ;A=0
DEC byte ;byte=byte-1
INC byte ;byte=byte+1
INC R7
DEC A
DEC 40H ; [40]=[40]-1
RR – RL – RRC – RLC A
EXAMPLE:
RR A
RR:
RRC:
RL:
RLC:
C
C
DJNZ:
Write a program to clear ACC, then
add 3 to the accumulator ten time
Solution:
MOV A,#0
MOV R2,#10
AGAIN: ADD A,#03
DJNZ R2,AGAIN ;repeat until R2=0 (10 times)
MOV R5,A
Example:
Write a program to copy a block of 10 bytes from RAM location
starting at 37h to RAM location starting at 59h.
Solution:
MOV R0,#37h ; source pointer
MOV R1,#59h ; dest pointer
MOV R2,#10 ; counter
L1: MOV A,@R0
MOV @R1,A
INC R0
INC R1
DJNZ R2,L1
write a program to add two 8 bit number and store
the result at external memory location 2050H.
• ORG 0000H
• MOV A, #05H
• MOV R, #09H
• ADD A, R1
• MOV DPTR, #2050H
• MOV @ DPTR, A
• AGAIN: SJMP AGAIN
Example: Read the content of external RAM locations
10F4H and 10F5H and place values in R6 and R7,
respectively.
MOV DPTR,#10F4H
MOVX, A,@DPTR
MOV R6,A
INC DPTR
MOVX A,@DPTR
MOV R7,A
BCD ADDITION
Multiplication
Square of a number
• mov dptr,#4200h
• movx a,@dptr
• mov b,a
• mul ab
• inc dptr
• movx @dptr,a
• inc dptr
• mov a,b
• movx @dptr,a
• l:sjmp l
Two’s complement of a number
• mov dptr,#4200h
• movx a,@dptr
• cpl a
• add a,#01h
• inc dptr
• movx @dptr,a
• l:sjmp l
Unpacked bcd to ascii conversion program for 8051
• mov dptr,#4200h
• movx a,@dptr
• mov b,a
• anl a,#0fh
• add a,#30h
• mov r0,a
• mov a,b
• swap a
• anl a,#0fh
• add a,#30h
• mov r1,a
• inc dptr
• mov a,r0
• movx @dptr,a
• inc dptr
• mov a,r1
• movx @dptr,a
• l:sjmp l
105
ADDITION OF TWO 8 bit Numbers
ADDRESS LABEL MNEMONICS
9100 START CLR C
MOV R0, #00
MOV A,#05
MOV B,#03
ADD A,B
MOV DPTR,#9200
JNC AHEAD
INC R0
AHEAD MOV X @DPTR,A
INC DPTR
MOV A,R0
MOV X @DPTR,A
HERE SJMP HERE
ADD TWO 8 BIT NUMBERS
MOV DPTR, #2400
MOVX A,@DPTR INPUTS:
MOV R1,A [2400]: 08
INC DPTR [2401]:07
MOVX A,@DPTR OUTPUTS: [2402]:0F
MOV R0,#00 [2403]:00
ADD A,R1 AA+R1
JNC L1
INC RO
L1: INC DPTR
MOVX @DPTR,A
INC DPTR
MOV A,RO
MOVX @DPTR,A
L2:SJMP:L2
Multiplication of two numbers
MOV DPTR,#2400
MOVX A,@DPTR
MOV B,A
INC DPTR [2401]
MOVX A,@DPTR
MUL AB
INC DPTR [2402]
MOVX @DPTR,A
INC DPTR [2403]
MOV A,B
MOVX @DPTR,A
L1:SJMP:L1
108
SUBTRACTION OF TWO 8 bit Numbers
ADDRESS LABEL MNEMONICS
9100 START CLR C
MOV R0, #00
MOV A,#05
MOV B,#03
SUBBA,B
MOV DPTR,#9200
JNC AHEAD
INC R0
AHEAD MOV X @DPTR,A
INC DPTR
MOV A,R0
MOV X @DPTR,A
HERE SJMP HERE
Multiplication Concept
MUL AB ; A  B, place 16-bit result in B and A
MOV A,#25H ;load 25H to reg. A
MOV B,#65H ;load 65H in reg. B
MUL AB ;25H * 65H = E99 where B = 0EH and A = 99H
Table 6-1:Unsigned Multiplication Summary (MULAB)
Multiplication Operand 1 Operand 2 Result
109
byte  byte A B A=low byte,
B=high byte
Division Concept
• MOV A,#95H
• MOV B,#10H
• DIV AB
DIV AB ; divide A by B
;load 95 into A
;load 10 into B
;now A = 09 (quotient) and B = 05 (remainder)
Table 6-2:Unsigned Division Summary (DIVAB)
110
Division
byte / byte
Numerator
A
Denominator
B
Quotient
A
Remainder
B
MULTIPLICATION OF TWO
8 bit Numbers
DIVISION OF TWO 8 bit
Numbers
Address Label Mnemonics
9000 START MOV A,#05
MOV F0,#03
MUL AB
MOV DPTR,#9200
MOVX @ DPTR,A
INC DPTR
MOV A,F0
MOVX @DPTR,A
HERE SJMP HERE
Address Label Mnemonics
9000 START MOV A,#05
MOV F0,#03
DIVAB
MOV DPTR,#9200
MOVX @ DPTR,A
INC DPTR
MOV A,F0
MOVX @DPTR,A
HERE SJMP HERE
530
store I st number in location 40H
Average of Five(or N) 8 bit Numbers
MOV 40H, #05H
MOV 41H, #04H
MOV 42H, #03H
MOV 43H, #02H
MOV 44H, #01H
MOV R0, #40H
MOV R5, #05H
store I st number address 40H in R0
store the number 05H in R5
store the number 05H in B
Clear Acc
LOOP:
Save the quotient in location 55H
MOV B,R5
CLR A
ADD A,@R0
INC R0
DJNZ R5,LOOP
DIV AB
MOV 55H,A
END
531

More Related Content

What's hot

What's hot (20)

8051 block diagram
8051 block diagram8051 block diagram
8051 block diagram
 
8085 Microprocessor
8085 Microprocessor8085 Microprocessor
8085 Microprocessor
 
Microcontroller presentation
Microcontroller presentationMicrocontroller presentation
Microcontroller presentation
 
Arithmetic and logical instructions
Arithmetic and logical instructionsArithmetic and logical instructions
Arithmetic and logical instructions
 
8086 MICROPROCESSOR
8086 MICROPROCESSOR8086 MICROPROCESSOR
8086 MICROPROCESSOR
 
INTRODUCTION TO MICROCONTROLLER
INTRODUCTION TO MICROCONTROLLERINTRODUCTION TO MICROCONTROLLER
INTRODUCTION TO MICROCONTROLLER
 
MICROCONTROLLER 8051
MICROCONTROLLER 8051MICROCONTROLLER 8051
MICROCONTROLLER 8051
 
Interfacing external memory in 8051
Interfacing external memory in 8051Interfacing external memory in 8051
Interfacing external memory in 8051
 
Introduction to ARM LPC2148
Introduction to ARM LPC2148Introduction to ARM LPC2148
Introduction to ARM LPC2148
 
Programmable peripheral interface 8255
Programmable peripheral interface 8255Programmable peripheral interface 8255
Programmable peripheral interface 8255
 
Pin diagram 8085
Pin diagram 8085 Pin diagram 8085
Pin diagram 8085
 
8086 pin details
8086 pin details8086 pin details
8086 pin details
 
Memory organization of 8051
Memory organization of 8051Memory organization of 8051
Memory organization of 8051
 
5.programmable interval timer 8253
5.programmable interval timer 82535.programmable interval timer 8253
5.programmable interval timer 8253
 
8051 assembly programming
8051 assembly programming8051 assembly programming
8051 assembly programming
 
Assembly Language Programming Of 8085
Assembly Language Programming Of 8085Assembly Language Programming Of 8085
Assembly Language Programming Of 8085
 
8051 architecture
8051 architecture8051 architecture
8051 architecture
 
Interrupts of microprocessor 8085
Interrupts of microprocessor  8085Interrupts of microprocessor  8085
Interrupts of microprocessor 8085
 
Introduction to Avr Microcontrollers
Introduction to Avr MicrocontrollersIntroduction to Avr Microcontrollers
Introduction to Avr Microcontrollers
 
8051 Assembly Language Programming
8051 Assembly Language Programming8051 Assembly Language Programming
8051 Assembly Language Programming
 

Similar to 8051 Microcontroller

microcontroller 8051 17.07.2023.pdf
microcontroller 8051 17.07.2023.pdfmicrocontroller 8051 17.07.2023.pdf
microcontroller 8051 17.07.2023.pdf818Farida
 
8051 microcontroller
8051 microcontroller 8051 microcontroller
8051 microcontroller nitugatkal
 
Architecture of the Intel 8051 Microcontroller
Architecture of the Intel 8051 MicrocontrollerArchitecture of the Intel 8051 Microcontroller
Architecture of the Intel 8051 MicrocontrollerSudhanshu Janwadkar
 
Live B tech Projects & Industrial Training @Technogroovy
Live B tech Projects & Industrial Training @Technogroovy Live B tech Projects & Industrial Training @Technogroovy
Live B tech Projects & Industrial Training @Technogroovy Technogroovy India
 
8051 Architecture and PIN Configuration.pdf
8051 Architecture and PIN Configuration.pdf8051 Architecture and PIN Configuration.pdf
8051 Architecture and PIN Configuration.pdfSrikrishna Thota
 
EMBEDDED SYSTEMS AND IOT lab manual for enginnering students
EMBEDDED SYSTEMS AND IOT lab manual for enginnering studentsEMBEDDED SYSTEMS AND IOT lab manual for enginnering students
EMBEDDED SYSTEMS AND IOT lab manual for enginnering studentseceprinter6
 
UNIT 4 8051Microcontroller.pptx
UNIT 4 8051Microcontroller.pptxUNIT 4 8051Microcontroller.pptx
UNIT 4 8051Microcontroller.pptxGowrishankar C
 
Study of 8051 microcontroller
Study of 8051 microcontrollerStudy of 8051 microcontroller
Study of 8051 microcontroller918007165995
 

Similar to 8051 Microcontroller (20)

microcontroller 8051 17.07.2023.pdf
microcontroller 8051 17.07.2023.pdfmicrocontroller 8051 17.07.2023.pdf
microcontroller 8051 17.07.2023.pdf
 
8051 microcontroller
8051 microcontroller 8051 microcontroller
8051 microcontroller
 
8051 microcontroller
8051 microcontroller8051 microcontroller
8051 microcontroller
 
Ch3 ppt
Ch3 pptCh3 ppt
Ch3 ppt
 
Architecture of the Intel 8051 Microcontroller
Architecture of the Intel 8051 MicrocontrollerArchitecture of the Intel 8051 Microcontroller
Architecture of the Intel 8051 Microcontroller
 
8051
80518051
8051
 
8051.pptx
8051.pptx8051.pptx
8051.pptx
 
8051.pptx
8051.pptx8051.pptx
8051.pptx
 
Live B tech Projects & Industrial Training @Technogroovy
Live B tech Projects & Industrial Training @Technogroovy Live B tech Projects & Industrial Training @Technogroovy
Live B tech Projects & Industrial Training @Technogroovy
 
89s52 2
89s52 289s52 2
89s52 2
 
Unit 4.pptx
Unit 4.pptxUnit 4.pptx
Unit 4.pptx
 
8051 Architecture and PIN Configuration.pdf
8051 Architecture and PIN Configuration.pdf8051 Architecture and PIN Configuration.pdf
8051 Architecture and PIN Configuration.pdf
 
UNIT 4.pptx
UNIT 4.pptxUNIT 4.pptx
UNIT 4.pptx
 
8051.ppt
8051.ppt8051.ppt
8051.ppt
 
8051 Presentation
8051 Presentation8051 Presentation
8051 Presentation
 
EMBEDDED SYSTEMS AND IOT lab manual for enginnering students
EMBEDDED SYSTEMS AND IOT lab manual for enginnering studentsEMBEDDED SYSTEMS AND IOT lab manual for enginnering students
EMBEDDED SYSTEMS AND IOT lab manual for enginnering students
 
UNIT 4 8051Microcontroller.pptx
UNIT 4 8051Microcontroller.pptxUNIT 4 8051Microcontroller.pptx
UNIT 4 8051Microcontroller.pptx
 
Study of 8051 microcontroller
Study of 8051 microcontrollerStudy of 8051 microcontroller
Study of 8051 microcontroller
 
Microprocessor history1
Microprocessor history1Microprocessor history1
Microprocessor history1
 
Microprocessor history1
Microprocessor history1Microprocessor history1
Microprocessor history1
 

Recently uploaded

the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxhumanexperienceaaa
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 

Recently uploaded (20)

Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 

8051 Microcontroller

  • 1. RAMCO INSTITUTE OF TECHNOLOGY DEPARTMENT OF ECE Dr. A. Azhagu Jaisudhan Pazhani
  • 2.
  • 3. Contents: Introduction Block Diagram and Pin Description of the 8051 Registers Memory mapping in 8051 I/O Port Programming Timer Interrupt
  • 4. Why do we need to learn Microprocessors/controllers? • The microprocessor is the core of computer systems. • Nowadays many communication, digital entertainment, portable devices, are controlled by them. • A designer should know what types of components he needs, ways to reduce production costs and product reliable.
  • 5. Different aspects of a microprocessor/controller • Hardware :Interface to the real world • Software :order how to deal with inputs
  • 6. The necessary tools for a microprocessor/controller • CPU: Central Processing Unit • I/O: Input /Output • Bus: Address bus & Data bus • Memory: RAM & ROM • Timer • Interrupt • Serial Port • Parallel Port
  • 7. CPU General- Purpose Micro- processor RAM ROM I/O Port Timer Serial COM Port Data Bus Address Bus General-Purpose Microprocessor System Microprocessors: • CPU for Computers • No RAM, ROM, I/O on CPU chip itself • Example:Intel’s x86, Motorola’s 680x0 Many chips on mother’s board General-purpose microprocessor
  • 8. RAM ROM I/O Port Timer Serial COM Port Microcontroller CPU • A smaller computer • On-chip RAM, ROM, I/O ports... • Example:Motorola’s 6811, Intel’s 8051, Zilog’s Z8 and PIC 16X A single chip Microcontroller :
  • 9. Microprocessor • CPU is stand-alone, RAM, ROM, I/O, timer are separate • designer can decide on the amount of ROM, RAM and I/O ports. • expansive • general-purpose Microcontroller • CPU, RAM, ROM, I/O and timer are all on a single chip • fix amount of on-chip ROM, RAM, I/O ports • Not expansive • single-purpose Microprocessor vs. Microcontroller
  • 10. Block Diagram CPU On-chip RAM On-chip ROM for program code 4 I/O Ports Timer 0 Serial Port OSC Interrupt Control External interrupts Timer 1 Timer/Counter Bus Control TxD RxD P0 P1 P2 P3 Address/Data Counter Inputs
  • 11. • The Intel 8051 is a very popular general purpose microcontroller widely used for small scale embedded systems. • Many vendors such as Atmel, Philips, and Texas Instruments produce MCS-51 family microcontroller chips. • The 8051 is an 8-bit microcontroller with 8 bit data bus and 16- bit address bus. • The 16 bit address bus can address a 64K( 216) byte code memory space and a separate 64K byte of data memory space. • The 8051 has 4K on-chip read only code memory and 128 bytes of internal Random Access Memory (RAM) Overview
  • 12. • Besides internal RAM, the 8051 has various Special Function Registers (SFR) such as the Accumulator, the B register, and many other control registers. • 34 8-bit general purpose registers in total. The ALU performs one 8-bit operation at a time. • Two 16 bit /Counter timers • 3 internal interrupts (one serial), 2 external interrupts. • 4 8-bit I/O ports (3 of them are dual purposed). One of them used for serial port • Some 8051 chips come with UART for serial communication and ADC for analog to digital conversion. Overview
  • 13. 8051 Chip Pins 40 pins on the 8051 chip. Most of these pins are used to connect to I/O devices or external data and code memory. • 4 I/O port take 32 pins(4 x 8 bits) plus a pair of XTALS pins for crystal clock • A pair of Vcc and GND pins for power supply (the 8051 chip needs +5V 500mA to function properly) • A pair of timer pins for timing controls, a group of pins (EA, ALE, PSEN, WR, RD) for internal and external data and code memory access controls • One Reset pin for reboot purpose
  • 14. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 40 39 38 37 36 35 34 33 32 30 29 28 27 26 25 24 23 22 21 31 P1.0 P1.1 P1.2 P1.3 P1.4 P1.5 P1.6 RST (RXD) P3.0 (TXD) P3.1 (INT0) P3.2 (INT1) P3.3 (T0) P3.4 (T1) P3.5 (WR) P3.6 XTAL 2 XTAL 1 GND Vcc P0.0(AD0) P2.0(A8) P2.1(A9) P2.2(A10) P2.3(A11) P2.4(A12) P2.5(A13) P2.6(A14) P2.7(A15) PSEN ALE/PROG EA/VPP P0.6(AD6) P0.7(AD7) P0.5(AD5) P0.4(AD4) P0.3(AD3) P0.2(AD2) P0.1(AD1) (RD) P3.7 P1.7 (Serial) interrupt Timer Ex M W/R clock Ext Memory Address Ext Memory Access Control Ext Memory Address 8051 Pin out Diagram of the 8051 Microcontroller
  • 15. Port 0 (enable Ex. Memory) Port 2 ALE Address Latch Enable EA’ PSEN’ RD’ WR’ Ex.Data RAM write OE’ A8 - A15 A0 – A7 D0 – D7 Ex. Data RAM Reade Extensl Code Memory ROM or EPROM 8051 adress multiplex latch The Pin Connection for External Code and Data Memory
  • 16. • The EA' (External Access) pin is used to control the internal or external memory access. The signal 0 is for external memory access and signal 1 for internal memory access. • The PSEN' (Program Store Enable) is for reading external code memory when it is low (0) and EA is also 0.
  • 17. • The ALE (Address Latch Enable) activates the port 0 joined with port 2 to provide 16 bit external address bus to access the external memory. The ALE multiplexes the P0: 1 for latching address on P0 as A0-A7 in the 16 bit address buss, 0 for latching P0 as data I/O. • P0.x is named ADx because P0 is multiplexed for Address bus and Data bus at different clock time. WR' only provides the signal to write external data memory RD' provides the signal to read external data and code memory.
  • 18. System Clock and Oscillator Circuits • The 8051 requires an external oscillator circuit. The oscillator circuit usually runs around 12MHz. the crystal generates 12M pulses in one second. The pulse is used to synchronize the system operation in a controlled pace.. • A machine cycle is minimum amount time a simplest machine instruction must take • An 8051 machine cycle consists of 12 crystal pulses (clock cycle). • instruction with a memory operand so that it needs multiple memory accesses.
  • 19. The first 6 crystal pulses (clock cycle) is used to fetch the opcode and the second 6 pulses are used to perform the operation on the operands in the ALU. This gives an effective machine cycle rate at 1MIPS (Million Instructions Per Second). XTAL! XTAL2 GND Crystal Crystal to 8051 XTAL 1/2
  • 20. Features of the 8051 Microcontroller • The 8031 requires external instruction memory. – It can be as large as 64K Bytes. – You lose 2 ports for interfacing to the external memory. • You can replace these by interfacing the chip to an I/O port controller like the 8255. • The 8051 is the original member of the Intel MCS-51 family of Microcontrollers. – There are several varieties that differ slightly in the available features.
  • 21. 8051 Architecture • Programmer’s View – Memory Organization – Register Set – Instruction Set • Hardware Designer’s View – Pin-out – Timing characteristics – Current / Voltage requirements
  • 22. Monday, May 31, 2021 Block Diagram
  • 23.
  • 24. • Internal ROM and RAM • I/O Ports with programmable Pins • ALU • Working Registers • Clock Circuits • Timers and Counters • Serial Data Communication. Monday, May 31, 2021
  • 25. 8051 Internal Architecture • The CPU has many important registers. The Program Count (PC) always holds the code memory location of next instruction. • The CPU is the heart of any computer which is in charge of computer operations. • It fetches instructions from the code memory into the instruction Register (IR), analyzes the opcode of the instruction. • updates the PC to the location of next instruction, fetches the operand from the data memory if necessary, and finally performs the operation in the Arithmetic-Logic Unit (ALU) within the CPU.
  • 26. • The B register is a register just for multiplication and division operation which requires more register spaces for the product of multiplication and the quotient and the remainder for the division. • The immediate result is stored in the accumulator register (Acc) for next operation and the Program Status Word (PSW) is updated depending on the status of the operation result
  • 27. The B Register • Commonly used as a temporary registe • Used by two op-codes – MUL AB, div AB • B register holds the second operand and will hold part of the result – Upper 8 bits of the multiplication result – Remainder in case of division. • Bit addressable.
  • 28. The DPL and DPH Registers • Two 8-bit registers that can be combined into a 16-bit DPTR – Data Pointer. • Used by commands that access external memory • Also used for storing 16bit values mov DPTR, #data16 ; setup DPTR with 16bit ext address movx A, @DPTR ; copy mem[DPTR] to A • Can be accessed as 2 separate 8-bit registers if needed. • DPTR is useful for string operations and Look-Up-Table (LUT) operations.
  • 29. The SP Register • SP is the stack pointer. • SP points to the last used location of the stack. – Push operation will first increment SP and then copy data. – Pop operation will first copy data and then decrement SP. • In 8051, stack grows upwards (from low memory to high memory) and can be in the internal RAM only. • On power-up, SP points to 07H. – Register banks 2,3,4 (08H to 1FH) form the default stack area. • Stack can be relocated by setting SP to the upper memory area in 30H to 7FH. – mov SP, #32H
  • 30. A and B registers • Totally 34 general purpose registers or working registers. • Two of these A and B hold results of many instructions, particularly math and logical operations of 8051 cpu. • The other 32 are in four banks,B0 – B3 of eight registers each. • A(accumulator) is used for addition,subtraction,mul,div,boolean bit manipulation and for data transfers. • But B register can only be used for mul and div operations. Monday, May 31, 2021
  • 31. The SBUF Register • Serial Port Data Buffer. • 2 registers at the same location – One is read-only used for reading serial input data. • Serial Data Receive Buffer. – The other is write-only used for storing serial output data. • Serial Data Transmit Buffer. Link: SFR address
  • 32. Timer Registers – TH0 and TL0 • The high and low bytes of the 16-bit counting register for timer/counter T0. • There is also a TH1 / TL1 pair for the T1 timer. • In the 8052, one more pair exists (TH2) / (TL2) for the T2 timer. • (RCAP2H) and (RCAP2L) exist only in the 8052 and they are copies of the TH2 and TL2 registers. Link: SFR address
  • 33. Control Registers • IP – Interrupt Priority. • IE – Interrupt Enable. • TMOD – Timer Mode. • TCON – Timer Control. • T2CON – Timer 2 Control (8052) • SCON – Serial Port Control. • PCON – Power Control (80C51). Link: SFR address
  • 34. The PSW Register • Program Status Word is a “bit addressable” 8-bit register that has all the flags. MSB LSB CY AC F0 RS1 RS2 OV - P Symbol Position Function CY PSW.7 Carry Flag AC PSW.6 Auxiliary Carry Flag. For BCD Operations F0 PSW.5 Flag 0. Available to the user for general purposes. RS1 PSW.4 Register bank select bits. Set by software to determine which register bank is being used. RS2 PSW.3 OV PSW.2 Overflow Flag - PSW.1 Not used P PSW.0 Parity Flag. Even Parity.
  • 35. Monday, May 31, 2021 8051 Flag bits and the PSW register • PSW Register CY AC F0 RS1 OV RS0 P -- CY PSW.7 Carry flag AC PSW.6 Auxiliary carry flag -- PSW.5 Available to the user for general purpose RS1 PSW.4 Register Bank selector bit 1 RS0 PSW.3 Register Bank selector bit 0 OV PSW.2 Overflow flag -- PSW.1 User define bit P PSW.0 Parity flag Set/Reset odd/even parity RS1 RS0 Register Bank Address 0 0 0 00H-07H 0 1 1 08H-0FH 1 0 2 10H-17H 1 1 3 18H-1FH
  • 36. Ports Port Reading and Writing There are 4 8-bit ports: P0, P1, P2 and P3. All of them are dual purpose ports except P1 which is only used for I/O. The following diagram shows a single bit in an 8051 I/O port. Internal data bus(one bit) D Q CL Q’ Latch Read latch Read pin Vcc(5v) Port pin 10k FET Single Bit In I/O Port
  • 37. The Port Alternate Functions • PORT P1 (Pins 1 to 8): The port P1 is a port dedicated for general I/O purpose. The other ports P0, P2 and P3 have dual roles in addition to their basic I/O function. • PORT P0 (pins 32 to 39): When the external memory access is required then Port P0 is multiplexed for address bus and data bus that can be used to access external memory in conjunction with port P2. P0 acts as A0-A7 in address bus and D0-D7 for port data. It can be used for general purpose I/O if no external memory presents. • PORT P2 (pins 21 to 28): Similar to P0, the port P2 can also play a role (A8-A15) in the address bus in conjunction with PORT P0 to access external memory.
  • 38. Contd. • PORT P3 (Pins 10 to 17): In addition to acting as a normal I/O port, • P3.0 can be used for serial receive input pin(RXD) • P3.1 can be used for serial transmit output pin(TXD) in a serial port, • P3.2 and P3.3 can be used as external interrupt pins(INT0’ and INT1’), • P3.4 and P3.5 are used for external counter input pins(T0 and T1), • P3.6 and P3.7 can be used as external data memory write and read control signal pins(WR’ and RD’)read and write pins for memory access.
  • 39. Memory Organization The 8051 has separate address spaces for program storage and data storage. – Depending on the type of instruction, the same address can refer to two logically and physically different memory locations.
  • 40. Program Storage • After reset, the MCS-51 starts fetching instructions from 0000H. – This can be either on-chip or external depending on the value of the EA input pin. • If EA* is low, then the program memory is external. • If EA* is high, then addresses from 0000 to 0FFF will refer to on-chip memory and addresses 1000 up to FFFF refer to external memory. – Note that the 8031 must have its EA connected low as all of its memory is external. Link: EA connection
  • 41. Access to External Memory • Port 0 acts as a multiplexed address/data bus. Sending the low byte of the program counter (PCL) as an address. • Port 2 sends the program counter high byte (PCH) directly to the external memory. • The signal ALE operates as in the 8051 to allow an external latch to store the PCL byte while the multiplexed bus is made ready to receive the code byte from the external memory. • Port 0 then switches function and becomes the data bus receiving the byte from memory. Link: H/w Interfacing
  • 42. Data Storage • The 8051 has 256 bytes of RAM on-chip. – The lower 128 bytes are intended for internal data storage. – The upper 128 bytes are the Special Function Registers (SFR). • The lower 128 bytes are not to be used as standard RAM. – Internally 8051’s registers default to stack area, and other features. [00-7FH] Link: Memory Organization
  • 43. Data Storage [Cont...] • The lowest 32 bytes of the on-chip RAM form 4 banks of 8 registers each. • Only one of these banks can be active at any time. • Bank is chosen by setting 2 bits in PSW – Default bank (at power up) is bank 0 (locations 00 – 07). • The 8 registers in any active bank are referred to as R0 through R7 • Given that each register has a specific address, it can be accessed directly using that address even if its bank is not the active one.
  • 44. Data Storage [Cont...] • The next 16 bytes – locations 20H to 2FH – form a block that can be addressed as either bytes or individual bits. – The bytes have addresses 20H to 2FH. – The bits have addresses 00H to 7FH. – Specific instructions are used for accessing the bits. • Locations 30H to 7FH are general purpose RAM. Link: Memory Organization
  • 45. The SFR (Special Function Register) • The upper 128 bytes of the on-chip RAM are used to house special function registers. • In reality, only about 25 of these bytes are actually used. The others are reserved for future versions of the 8051. – These are registers associated with important functions in the operation of the MCS-51. – Some of these registers are bit-addressable as well as byte- addressable. • The address of bit 0 of the register will be the same as the address of the register.
  • 46. The Elements of SFR • ACC and B registers – 8 bit each • DPTR : [DPH:DPL] – 16 bit combined • PC (Program Counter) – 16 bits • SP (Stack Pointer) – 8 bit • PSW (Program Status Word) • Port Latches • Serial Data Buffer • Timer Registers • Control Registers Link: SFR Elements
  • 47. Monday, May 31, 2021 Port 0(pins 32-39) • When connecting an 8051 to an external memory, the 8051 uses ports to send addresses and read instructions. – 16-bit address:P0 provides both address A0-A7, P2 provides address A8-A15. – Also, P0 provides data lines D0-D7. • When P0 is used for address/data multiplexing, it is connected to the 74LS373 to latch the address. I/O Port Programming
  • 48. Monday, May 31, 2021  Port 1(pins 1-8) • Port 1 is denoted by P1. – P1.0 ~ P1.7 – P1 as an output port (i.e., write CPU data to the external pin) – P1 as an input port (i.e., read pin data into CPU bus)
  • 49. Monday, May 31, 2021 ALE Pin • The ALE pin is used for de-multiplexing the address and data by connecting to the G pin of the 74LS373 latch. – When ALE=0, P0 provides data D0-D7. – When ALE=1, P0 provides address A0-A7. – The reason is to allow P0 to multiplex address and data.
  • 50. Monday, May 31, 2021 Port 3(pins 10-17) • Although port 3 is configured as an output port upon reset, this is not the way it is most commonly used. • Port 3 has the additional function of providing signals. – Serial communications signal:RxD, TxD – External interrupt:/INT0, /INT1 – Timer/counter:T0, T1 – External memory accesses :/WR, /RD
  • 51. Monday, May 31, 2021 Port 3 Alternate Functions 17 RD P3.7 16 WR P3.6 15 T1 P3.5 14 T0 P3.4 13 INT1 P3.3 12 INT0 P3.2 11 TxD P3.1 10 RxD P3.0 Pin Function P3 Bit 
  • 52. Monday, May 31, 2021 Addressing Modes • Immediate • Register • Direct • Register Indirect • Indexed The way in which the instruction is specified.
  • 53. Monday, May 31, 2021 Immediate Addressing Mode • Immediate Data is specified in the instruction itself • Egs: MOV A,#65H MOV A,#’A’ MOV R6,#65H MOV DPTR,#2343H MOV P1,#65H
  • 54. Monday, May 31, 2021 Register Addressing Mode MOV Rn, A ;n=0,..,7 ADD A, Rn MOV DPL, R6 MOV DPTR, A MOV Rm, Rn
  • 55. Monday, May 31, 2021 Direct Addressing Mode Although the entire of 128 bytes of RAM can be accessed using direct addressing mode, it is most often used to access RAM loc. 30 – 7FH. MOV R0, 40H MOV 56H, A MOV A, 4 ; ≡ MOV A, R4 MOV 6, 2 ; copy R2 to R6 ; MOV R6,R2 is invalid !
  • 56. Monday, May 31, 2021 Register Indirect Addressing Mode • In this mode, register is used as a pointer to the data. MOV A,@Ri ; move content of RAM loc. Where address is held by Ri into A ( i=0 or 1 ) MOV @R1,B In other word, the content of register R0 or R1 is sources or target in MOV, ADD and SUBB insructions.  jump
  • 57. Monday, May 31, 2021 Indexed Addressing Mode And On-Chip ROM Access • This mode is widely used in accessing data elements of look-up table entries located in the program (code) space ROM at the 8051 MOVC A,@A+DPTR A= content of address A +DPTR from ROM Note: Because the data elements are stored in the program (code ) space ROM of the 8051, it uses the instruction MOVC instead of MOV. The “C” means code.
  • 58.
  • 59.
  • 60.
  • 61.
  • 63. Structure of Assembly Language [ label: ] mnemonic [operands] [ ;comment ] Example: MOV R1, #25H ; load data 25H into R1 63
  • 64. 8051 Assembly Language • Registers 64 MOV Instruction: MOV destination, source Example: 1. MOV A, $55H 2. MOV R0, A 3. MOV A, R3
  • 65. Instruction Groups • The 8051 has 255 instructions – Every 8-bit opcode from 00 to FF is used except for A5. • The instructions are grouped into 5 groups – Arithmetic – Logic – Data Transfer – Boolean – Branching
  • 66. Arithmetic Instructions • ADD – 8-bit addition between the accumulator (A) and a second operand. • The result is always in the accumulator. • The CY flag is set/reset appropriately. • ADDC – 8-bit addition between the accumulator, a second operand and the previous value of the CY flag. • Useful for 16-bit addition in two steps. • The CY flag is set/reset appropriately.
  • 67. Arithmetic Instructions • DA – 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. – The result is always saved in the accumulator. – The CY flag is set/reset appropriately.
  • 68. Arithmetic Instructions • 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.
  • 69. 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
  • 70. Logical Operations • XRL – Works on bytes only. • CPL / CLR – Complement / Clear. – Work on the accumulator or a bit. • CLR P1.2
  • 71. Logical Operations • 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.
  • 72. Data Transfer Instructions • MOV – 8-bit data transfer for internal RAM and the SFR. • MOV A, Rn MOV A, direct • MOV A, @Ri MOV A, #data • MOV Rn, A MOV Rn, direct • MOV Rn, #data MOV direct, A • MOV direct, Rn MOV direct, direct • MOV direct, @Ri MOV direct, #data • MOV @Ri, A MOV @Ri, direct • MOV @Ri, #data
  • 73. Data Transfer Operations • MOV – 1-bit data transfer involving the CY flag • MOV C, bit • MOV bit, C • MOV – 16-bit data transfer involving the DPTR • MOV DPTR, #data
  • 74. Data Transfer Instructions • MOVC – Move Code Byte • Load the accumulator with a byte from program memory. • Must use indexed addressing  MOVC A, @A+DPTR  MOVC A, @A+PC
  • 75. Data Transfer Instructions • MOVX – Data transfer between the accumulator and a byte from external data memory. • MOVX A, @Ri • MOVX A, @DPTR • MOVX @Ri, A • MOVX @DPTR, A
  • 76. Data Transfer Instructions • PUSH / POP – Push and Pop a data byte onto the stack. – The data byte is identified by a direct address from the internal RAM locations. • PUSHDPL • POP 40H
  • 77. Data Transfer Instructions • XCH – Exchange accumulator and a byte variable • XCH A, Rn • XCH A, direct • XCH A, @Ri • XCHD – Exchange lower digit of accumulator with the lower digit of the memory location specified. • XCHD A, @Ri • The lower 4-bits of the accumulator are exchanged with the lower 4-bits of the internal memory location identified indirectly by the index register. • The upper 4-bits of each are not modified.
  • 78. 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.
  • 79. Boolean Operations • 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
  • 80. Boolean Operations • 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 addressable memory. • MOV P1.2, C ; Copy the CY flag to bit 2 of P1.
  • 81. Boolean Operations • 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.
  • 82. 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.
  • 83. Branching Instructions – 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
  • 84. Branching Instructions • 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.
  • 85. Branching Instructions • 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.
  • 86. Branching Instructions • 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.
  • 87. Branching Instructions – 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.
  • 88. Branching Instructions • 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
  • 89. Branching Instructions • 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
  • 90. Some Simple Instructions MOV dest,source ; dest = source MOV A,#72H ;A=72H MOV R4,#62H;R4=62H MOV B,0F9H ;B=the content of F9’th byte of RAM MOV DPTR,#7634H MOV DPL,#34H MOV DPH,#76H MOV P1,A ;mov A to port 1 Note 1: MOV A,#72H ≠ MOV A,72H After instruction “MOV A,72H ” the content of 72’th byte of RAM will replace in Accumulator. Note 2: MOV A,R3 ≡ MOV A,3
  • 91. ADDA, Source ;A=A+SOURCE ADDA,#6 ;A=A+6 ADDA,R6 ;A=A+R6 ADD A,6 ;A=A+[6] or A=A+R6 ADD A,0F3H ;A=A+[0F3H] SUBB A, Source ;A=A-SOURCE-C SUBB A,#6 ;A=A-6 SUBB A,R6 ;A=A+R6
  • 92. MUL & DIV • MUL AB ;B|A = A*B MOV A,#25H MOV B,#65H MUL AB ;25H*65H=0E99 ;B=0EH, A=99H • DIV AB ;A = A/B, B = A mod B MOV A,#25 MOV B,#10 DIV AB ;A=2, B=5
  • 93. SETB bit ; bit=1 CLR bit ; bit=0 SETB C ; CY=1 SETB P0.0 ;bit 0 from port 0 =1 SETB P3.7 ;bit 7 from port 3 =1 SETB ACC.2 ;bit 2 from ACCUMULATOR =1 SETB 05 ;set high D5 of RAM loc. 20h Note: CLR instruction is as same as SETB i.e.: CLR C ;CY=0 But following instruction is only for CLR: CLR A ;A=0
  • 94. DEC byte ;byte=byte-1 INC byte ;byte=byte+1 INC R7 DEC A DEC 40H ; [40]=[40]-1
  • 95. RR – RL – RRC – RLC A EXAMPLE: RR A RR: RRC: RL: RLC: C C
  • 96. DJNZ: Write a program to clear ACC, then add 3 to the accumulator ten time Solution: MOV A,#0 MOV R2,#10 AGAIN: ADD A,#03 DJNZ R2,AGAIN ;repeat until R2=0 (10 times) MOV R5,A
  • 97. Example: Write a program to copy a block of 10 bytes from RAM location starting at 37h to RAM location starting at 59h. Solution: MOV R0,#37h ; source pointer MOV R1,#59h ; dest pointer MOV R2,#10 ; counter L1: MOV A,@R0 MOV @R1,A INC R0 INC R1 DJNZ R2,L1
  • 98. write a program to add two 8 bit number and store the result at external memory location 2050H. • ORG 0000H • MOV A, #05H • MOV R, #09H • ADD A, R1 • MOV DPTR, #2050H • MOV @ DPTR, A • AGAIN: SJMP AGAIN
  • 99. Example: Read the content of external RAM locations 10F4H and 10F5H and place values in R6 and R7, respectively. MOV DPTR,#10F4H MOVX, A,@DPTR MOV R6,A INC DPTR MOVX A,@DPTR MOV R7,A
  • 102. Square of a number • mov dptr,#4200h • movx a,@dptr • mov b,a • mul ab • inc dptr • movx @dptr,a • inc dptr • mov a,b • movx @dptr,a • l:sjmp l
  • 103. Two’s complement of a number • mov dptr,#4200h • movx a,@dptr • cpl a • add a,#01h • inc dptr • movx @dptr,a • l:sjmp l
  • 104. Unpacked bcd to ascii conversion program for 8051 • mov dptr,#4200h • movx a,@dptr • mov b,a • anl a,#0fh • add a,#30h • mov r0,a • mov a,b • swap a • anl a,#0fh • add a,#30h • mov r1,a • inc dptr • mov a,r0 • movx @dptr,a • inc dptr • mov a,r1 • movx @dptr,a • l:sjmp l
  • 105. 105 ADDITION OF TWO 8 bit Numbers ADDRESS LABEL MNEMONICS 9100 START CLR C MOV R0, #00 MOV A,#05 MOV B,#03 ADD A,B MOV DPTR,#9200 JNC AHEAD INC R0 AHEAD MOV X @DPTR,A INC DPTR MOV A,R0 MOV X @DPTR,A HERE SJMP HERE
  • 106. ADD TWO 8 BIT NUMBERS MOV DPTR, #2400 MOVX A,@DPTR INPUTS: MOV R1,A [2400]: 08 INC DPTR [2401]:07 MOVX A,@DPTR OUTPUTS: [2402]:0F MOV R0,#00 [2403]:00 ADD A,R1 AA+R1 JNC L1 INC RO L1: INC DPTR MOVX @DPTR,A INC DPTR MOV A,RO MOVX @DPTR,A L2:SJMP:L2
  • 107. Multiplication of two numbers MOV DPTR,#2400 MOVX A,@DPTR MOV B,A INC DPTR [2401] MOVX A,@DPTR MUL AB INC DPTR [2402] MOVX @DPTR,A INC DPTR [2403] MOV A,B MOVX @DPTR,A L1:SJMP:L1
  • 108. 108 SUBTRACTION OF TWO 8 bit Numbers ADDRESS LABEL MNEMONICS 9100 START CLR C MOV R0, #00 MOV A,#05 MOV B,#03 SUBBA,B MOV DPTR,#9200 JNC AHEAD INC R0 AHEAD MOV X @DPTR,A INC DPTR MOV A,R0 MOV X @DPTR,A HERE SJMP HERE
  • 109. Multiplication Concept MUL AB ; A  B, place 16-bit result in B and A MOV A,#25H ;load 25H to reg. A MOV B,#65H ;load 65H in reg. B MUL AB ;25H * 65H = E99 where B = 0EH and A = 99H Table 6-1:Unsigned Multiplication Summary (MULAB) Multiplication Operand 1 Operand 2 Result 109 byte  byte A B A=low byte, B=high byte
  • 110. Division Concept • MOV A,#95H • MOV B,#10H • DIV AB DIV AB ; divide A by B ;load 95 into A ;load 10 into B ;now A = 09 (quotient) and B = 05 (remainder) Table 6-2:Unsigned Division Summary (DIVAB) 110 Division byte / byte Numerator A Denominator B Quotient A Remainder B
  • 111. MULTIPLICATION OF TWO 8 bit Numbers DIVISION OF TWO 8 bit Numbers Address Label Mnemonics 9000 START MOV A,#05 MOV F0,#03 MUL AB MOV DPTR,#9200 MOVX @ DPTR,A INC DPTR MOV A,F0 MOVX @DPTR,A HERE SJMP HERE Address Label Mnemonics 9000 START MOV A,#05 MOV F0,#03 DIVAB MOV DPTR,#9200 MOVX @ DPTR,A INC DPTR MOV A,F0 MOVX @DPTR,A HERE SJMP HERE 530
  • 112. store I st number in location 40H Average of Five(or N) 8 bit Numbers MOV 40H, #05H MOV 41H, #04H MOV 42H, #03H MOV 43H, #02H MOV 44H, #01H MOV R0, #40H MOV R5, #05H store I st number address 40H in R0 store the number 05H in R5 store the number 05H in B Clear Acc LOOP: Save the quotient in location 55H MOV B,R5 CLR A ADD A,@R0 INC R0 DJNZ R5,LOOP DIV AB MOV 55H,A END 531