ESD UNIT-1 Part1
ESD UNIT-1 Part1
INTRODUCTION TO MICROCONTROLLERS
https://dokumen.tips/documents/mc-8051-mazidi.ht
ml?page=39
https://www.techulator.com/resources/11036-Micro
controller-block-diagram-components.aspx
http://download.mikroe.com/documents/
compilers/mikroc/8051/help/
8051_memory_organization.htm
8051
Microcontroller
8051
2
CONTENTS
• Overview of 8051 Microcontroller
• 8051 Architecture
• Pin Diagram
• Memory Organization
• Addressing Modes
• Instruction set of 8051
Introduction to a Microcontroller
• A microcontroller is a highly integrated single chip, which consists
of on chip CPU (Central Processing Unit), RAM (Random Access
Memory), EPROM/PROM/ROM (Erasable Programmable Read
Only Memory), I/O (input/output) – serial and parallel, timers,
interrupt controller. For example, Intel 8051 is 8-bit microcontroller
and Intel 8096 is 16-bit microcontroller. The block diagram of
Microcontroller is shown below:
Overview of 8051 Microcontroller
Overview of 8051 Microcontroller
TXD
P0 P1 P2 RXD
Pin diagram of 8051
Pin Description Summary
PIN TYPE NAME AND FUNCTION
15
8051 Microcontroller Pin Description
P3.0 (RXD) : 10th pin is RXD (serial data receive pin) which is for
serial input. Through this input signal microcontroller receives
data for serial communication.
P3.1 (TXD) : 11th pin is TXD (serial data transmit pin) which is
serial output pin. Through this output signal microcontroller
transmits data for serial communication.
P3.2 and P3.3 (INT0′, INT1′ ) : 12th and 13th pins are for
External Hardware Interrupt 0 and Interrupt 1 respectively.
When this interrupt is activated(i.e. when it is low), 8051 gets
interrupted in whatever it is doing and jumps to the vector
value of the interrupt (0003H for INT0 and 0013H for INT1) and
starts performing Interrupt Service Routine (ISR) from that
vector location.
8051 Microcontroller Pin Description
P3.4 and P3.5 (T0 and T1) : 14th and 15th pin are for Timer 0 and
Timer 1 external input. They can be connected with 16 bit
timer/counter.
P3.6 (WR’) : 16th pin is for external memory write i.e. writing data
to the external memory.
P3.7 (RD’) : 17th pin is for external memory read i.e. reading data
from external memory.
8051 Microcontroller Pin Description
Pin 18 and Pin 19 (XTAL2 And XTAL1) – These pins are
connected to an external oscillator which is generally a
quartz crystal oscillator. They are used to provide an external
clock frequency of 4MHz to 30MHz.
Pins from 32-39: Pin 32 to pin 39 are port 0 pins also referred
to as P0.0 to P0.7. They are bidirectional input/output pin. Port
0 is also designated as AD0-AD7 because 8051 multiplexes
address and data through port 0 to save pins.
Pin 40
Named as Vcc. This pin provides power supply voltage i.e. +5
Volts to the circuit.
8051 Microcontroller Pin Description
I/O Pins
The 8051 microcontrollers are mostly 8-bit ports, thus giving a
total of 32 pins which you can use to read input and control
output. All of them are bidirectional in nature so they can perform
as both input and output.
Some of the ports perform alternate functions as discussed
above such as to support access to external memory. This is done
to reduce the size of the microcontroller device. When these
ports are busy in performing their alternate functions, they can
not be made to act as input-output ports.
ARCHITECTURE OF 8051 MICROCONTROLLER
• The architecture of the 8051 microcontroller can be understood
from the block diagram. It has Harvard architecture with RISC
(Reduced Instruction Set Computer) concept.
• 8051 Microcontroller consists of
an 8-bit ALU
Oscillator
Interrupt Control
Bus Control
one 8-bit PSW(Program Status Word Register),
A and B registers ,
one 16-bit Program counter ,
one 16-bit Data pointer register(DPTR),
128 bytes of RAM and 4kB of ROM and
four parallel I/O ports each of 8-bit width
Intel 8051 Micro Architecture
A and B CPU 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.
• The other 32 are in four banks,B0 – B3 of eight registers each.
• The A register is also called the Accumulator and as it’s name
suggests, it is used 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.
• Register B holds high order byte of the result in the multiplication
and remainder in the division operation.
24
Program Counter
• 16 bit register to hold the address of a byte in memory
• PC contains 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. Since the PC is 16-bit width ,8051 can access program
addresses from 0000H to FFFFH ,a total of 64KB of code.
Data Pointer
• DPTR is made up of two 8 bit register DPH and DPL;
• DPTR points to data. When the 8051 accesses external memory it
will access data at the address indicated by DPTR.
• DPTR contains the address of internal & external code and data that
has to be accessed.
• DPTR Register is usually used for storing address of
data and intermediate results.
25
Stack Pointer
• It is an 8-bit register which stores the address of the stack top.
• Stack Pointer is used to indicate where the next value to be
removed from the stack should be taken from. When 8051
microcontroller is powered up it contains 07H value. 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.
• 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. 26
Program Status Word Register (PSW)/ Flag Register
--
CY AC F0 RS1 RS0 OV P
Carry flag PSW.7 CY
Auxiliary carry flag PSW.6 AC
Available to the user for general purpose PSW.5 --
Register Bank selector bit 1 PSW.4 RS1
Register Bank selector bit 0 PSW.3 RS0
Overflow flag PSW.2 OV
User define bit PSW.1 --
Parity flag Set/Reset odd/even parity PSW.0 P
0 1 1 08H-0FH
1 0 2 10H-17H
1 1 3 18H-1FH
27
P, the parity flag
The parity flag reflects the number of 1 s in the A (accumulator) register only.
If the A register contains an odd number of Is, then P = 1. Therefore, P = 0 if A has an
even number of 1s.
OV, the overflow flag
This flag is set whenever the result of a signed number operation is too large, causing
the high-order bit to overflow into the sign bit.
In general, the carry flag is used to detect errors in unsigned arithmetic operations.
The overflow flag is only used to detect errors in signed arithmetic operations.
Carry Flag
This flag is set whenever there is a carry out from the D7 bit.
This flag bit is affected after an 8-bit addition or subtraction.
It can also be set to 1 or 0 directly by an instruction such as “SETB C” and “CLR
C” where “SETB C” stands for “set bit carry” and “CLR C” for “clear carry”.
29
Clock:
Synchronization among internal operations can be achieved with
the help of clock circuits which are responsible for generating clock
pulses. During each clock pulse a particular operation will be carried
out, thereby, assuring synchronization among operations.
There are two pins XTAL1 and XTAL2 which form an oscillator circuit .
Internal RAM and ROM
ROM
• A code of 4K memory is incorporated as on-chip ROM in 8051. The 8051
ROM is a non-volatile memory meaning that its contents cannot be
altered and hence has a similar range of data and program memory, i.e,
they can address program memory as well as a 64K separate block of
data memory.
RAM
• The 8051 microcontroller is composed of 128 bytes of internal RAM. This
is a volatile memory since its contents will be lost if power is switched off.
These 128 bytes of internal RAM contains 32 working registers which in
turn constitutes 4 register banks (Bank 0-Bank 3) with each bank
consisting of 8 registers (R0 - R7). There are 128 addressable bits in the
internal RAM.
INTERNAL RAM
ARCHITECTURE & BLOCK DIAGRAM OF 8051
MICROCONTROLLER
Four General Purpose Parallel Input/Output Ports:
• The 8051 microcontroller has four 8-bit input/output ports. These are:
PORT P0: When there is no external memory present, this port acts as a
general purpose input/output port. In the presence of external memory, it
functions as a multiplexed address and data bus. It performs a dual role.
PORT P1: This port is used for various interfacing activities. This 8-bit port is
a normal I/O port i.e. it does not perform dual functions.
PORT P2: Similar to PORT P0, this port can be used as a general purpose
port when there is no external memory but when external memory is
present it works in conjunction with PORT PO as an address bus. This is an 8-
bit port and performs dual functions.
The SBUF register has 2 parts – one for storing the data to be
transmitted and another for receiving data from outer sources. The
first function is done using TXD pin and the second function is done
8051 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. Since the memory is off-chip it is not as flexible in
terms of accessing, and is also slower.
8051 Memory Organization
60K
64K
64K
4K
36
8051 Memory Organization
• The upper addresses and SFRs occupy the same block of
address space, 80H through FFH, although they are
physically separate entities.
• The upper address space is accessible by indirect
addressing only and SFRs are accessible by direct
addressing only.
• The lower address space can be accessed either by direct
addressing or by indirect addressing
• The 8051 can address up to 64 Kbytes of external data
memory.
• The “MOVX” instruction is used to access the external
data memory.
8051 Memory Organization
• Internal RAM OF 8051: The 128 bytes of
internal RAM is organized as below.
(i) Four register banks (Bank0, Bank1, Bank2
and Bank3) each of 8-bytes (total 32 bytes).
The default bank register is Bank0. The
remaining Banks are selected with the help
of RS0 and RS1 bits of PSW Register.
(ii) 16 bytes of bit addressable area and
(iii) 80 bytes of general purpose area (Scratch
pad memory). This area is also utilized by the
microcontroller as a storage area for the
operating stack.
•The 32 bytes of RAM from address 00 H to 1FH
are used as working registers organized as four
banks of eight registers each. The registers are
named as R0-R7 .Each register can be addressed
by its name or by its RAM address.
Data memory organization in 8051:Upper 128 Bytes of RAM
• The upper 128B of the RAM i.e. memory addresses from 80H to
FFH is allocated for Special Function Registers (SFRs). SFRs
control specific functions of the 8051 Microcontroller. Some of
the SFRs are I/O Port Registers (P0, P1, P2 and P3), PSW
(Program Status Word), A (Accumulator), IE (Interrupt Enable),
PCON (Power Control), etc.
• SFRs Memory addresses are only direct addressable. Even
though some of the addresses between 80H and FFH are not
assigned to any SFR, they cannot be used as additional RAM
area.
• In some microcontrollers, there is an additional 128B of RAM,
which share the memory address with SFRs i.e. 80H to FFH.
But, this additional RAM block is only accessed by indirect
addressing.
Special Function Registers (SFRs)
Examples:
• MOV A,R0 : Move the contents of the register R0 to the
accumulator
• ADD A,R6 :Add the contents of R6 register to the accumulator
• MOV P1, R2 : Move the contents of the R2 register into port 1
• MOV R5, R2 : This is invalid .The data transfer between the
registers is not allowed.
46
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 .
• This mode allows you to specify the operand by giving its
actual memory address
Examples:
MOV R1, 42H : Move the contents of RAM location 42 into R1
register
MOV 49H,A : Move the contents of the accumulator into the
RAM location 49.
ADD A, 56H : Add the contents of the RAM location 56 to the
accumulator 47
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.
• In this mode, register is used as a pointer to the data.
• The operands are specified using the 8-bit address
• The address register for 16-bit addresses can only be Data pointer
(DPTR)
• Only register R0 and R1 are used for this purpose.
• R2-R7 cannot be used to hold the address of an operand located in
RAM.
• When R0 and R1 hold the addresses of RAM locations, they must be
preceded by the “@” sign.
Examples:
• MOV A,@ R0 :Move the contents of RAM location whose address is in
R0 into A
• MOV @ R1 , B : Move the contents of B into RAM location whose
address is held by R1
48
• MOVX A, DPTR Moves contents of DPTR to A
Indexed Addressing Mode for 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
• The address of the operand is formed by adding the contents
of A and DPTR.
MOVC A,@A+DPTR
A= content of address A +DPTR from ROM
.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.
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.
49
8051 Instruction Set
•An "instruction" refers to a command or operation that the
microcontroller executes.
The format of instruction is as follows:
MNEMONIC OPERANDS
•The first part of each instruction, called MNEMONIC refers to the
operation an instruction performs Mnemonics are abbreviations of
the name of operation being executed.
•The other part of instruction, called OPERAND is separated from
mnemonic by at least one whitespace and defines data being
processed by instructions.
If there is more than one operand in an instruction, they are separated
by a comma.
Eg: INC R1 - Increment register R1 (increment register R1);
RET - return from a subroutine; no operand
JZ TEMP - if the number in the accumulator is 0, jump to the
address marked as TEMP;
ADD A,R3 - add R3 and accumulator;
8051 Instruction Set
The following nomenclatures for register, data, address and
variables are used while write instructions.
• A: Accumulator
• B: "B" register
• C: Carry bit
• Rn: Register R0 - R7 of the currently selected register bank
• Direct: 8-bit internal direct address for data. The data could be in
lower 128bytes of RAM (00 - 7FH) or it could be in the special
function register (80 - FFH).
• @Ri: 8-bit external or internal RAM address available in register
R0 or R1. This is used for indirect addressing mode.
• #data8: Immediate 8-bit data available in the instruction.
• #data16: Immediate 16-bit data available in the instruction.
• bit: Directly addressed bit in internal RAM or SFR
51
Instruction Groups
54
• 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.
• Eg: ADD A, R1
• 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.
• Eg: ADDC A, R1
55
• 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.
– The result is always saved in the accumulator.
– The CY flag is set/reset appropriately.
Eg: SUBB A, R1
• MUL AB
– Multiply A by B and place result in A & B.
– lower byte of result stored in A, higher byte in B
•DIV AB
– Divide A by B and place result quotient in A and remainder in B.
• INC
– Increment the operand by one.
• The operand can be a register, a direct address, an indirect
address, the data pointer.
• Eg: INC A
• DEC
– Decrement the operand by one.
• The operand can be a register, a direct address, an indirect
address.
• Eg: DEC A
Arithmetic Instructions
Arithmetic Instructions
59
Data Transfer Instructions
• Data transfer instructions move the content of one register to
another.
• These instructions are associated with transfer of data between
registers or external program memory or external data memory.
• The register the content of which is moved remains unchanged.
Mnemonic Description
MOV Move Data
MOVC Move Code
MOVX Move External Data
PUSH Move Data to Stack
POP Copy Data from Stack
XCH Exchange Data between two Registers
Data Transfer Instructions
• MOV
– 8-bit data transfer for internal RAM and the SFR.
• Eg: MOV A, Rn
• MOV
– 1-bit data transfer involving the CY flag
• Eg: MOV C, bit
MOV bit, C
• MOV
– 16-bit data transfer involving the DPTR
• Eg: MOV DPTR, #data
• MOVC
– Move Code Byte
• Load the accumulator with a code byte from external
program memory.
• Must use indexed addressing
• Eg: MOVC A, @A+DPTR
MOVC A, @A+PC
• MOVX
– Data transfer between the accumulator and a byte from external
data memory.
• Eg: MOVX A, @Ri
MOVX A, @DPTR
MOVX @Ri, A
MOVX @DPTR, A
• 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.
– Eg: PUSH DPL
POP 40H
• XCH
– Exchange accumulator and a byte variable
• XCH A, Rn
• 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.
Logical Operations
• Logic instructions perform logic operations like AND, OR, XOR,
NOT, Rotate, Clear and Swap upon corresponding bits of two
registers. After execution, the result is stored in the first
operand. Mnemonic Description
ANL Logical AND
ORL Logical OR
XRL Ex-OR
CLR Clear Register
CPL Complement the Register
RL Rotate a Byte to Left
RLC Rotate a Byte and Carry Bit to Left
RR Rotate a Byte to Right
RRC Rotate a Byte and Carry Bit to Right
SWAP Exchange lower and higher nibbles in
a Byte
• ANL (Logical AND) / ORL (Logical OR)
– Work on byte sized operands or the CY flag.
• Eg: ANL A, Rn
ANL A, direct
ORL A, Rn
• XRL (Exclusive-OR)
– Works on bytes only.
– Eg: XRL A, Rn
• CPL / CLR
– Complement / Clear.
– Work on the accumulator or a bit.
Eg: CPL P1.2, CLR A
• RL / RLC / RR / RRC
– Rotate the accumulator.
• RL and RR without the carry
• RLC and RRC rotate through the carry.
• Eg: RL A, RRC A
• SWAP A
– Swap the upper and lower nibbles of the accumulator.
Boolean Variable Instructions
• 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.
– TheMnemonic
P, OV, and AC flags Description
cannot be directly altered.
CLR Clear a Bit (Reset to 0)
SETB Set a Bit (Set to 1)
MOV Move a Bit
• No Operation
– NOP
Simple Programs
1.Write an ALP to add two 8-bit numbers 20H and 30H ,store the
result in the internal RAM Location Address 10H and display same
result at Port0 (P0) .
ORG 0000H ; Originate at 0000H
MOV A,#20H ;copy immediate 8-bit data 20H into A (Accumulator)
register
ADD A,#30H ;add the immediate 8-bit data 30H to the contents of A
and store the result in A
MOV 10H,A ;Move the result from A into Internal RAM(IRAM) address
10H
MOV P0,A ; Display the result at port0 (P0)
END ; stop
Simple Programs
2.Write an ALP to add two 8-bit numbers stored in the internal RAM
address at 30H and 40H and store the result in the internal RAM
Location Address 50H
Program:
ORG 0000H ; Originate at 0000H
MOV R0,#30H ; InitiAlize R0 with internal RAM address of the first 8-
bit number
MOV A,40H ; Copy the data from the address 40H in to A
ADD A,@R0 ; Add the contents of memory location, address(30H)
stored in R0 to A
MOV 50H,A ; store the result from A in to memory location address
50H
END
Simple Programs
3.Write an ALP to add two 8-bit numbers stored in the internal RAM
address at 30H and 40H and store the result in the External RAM
Location Address 2050H