CAM - M2 - Ktunotes - in
CAM - M2 - Ktunotes - in
MODULE II
8051 ARCHITECTURE
2.1 INTRODUCTION
Salient features of 8051 microcontroller are given below.
Eight bit CPU
8051 CPU consists of 8 bit ALU with associated registers like accumulator ‘A’ , B
register, PSW, SP, 16 bit program counter, stack pointer.
ALU can perform arithmetic and logic functions on 8 bit variables.
8051 has 4 K Bytes of internal ROM. The address space is from 0000 to 0FFFh. If the
program size is more than 4 K Bytes 8051 will fetch the code automatically from
external memory.
Accumulator is an 8 bit register widely used for all arithmetic and logical
operations. Accumulator is also used to transfer data between external memory. B
register is used along with Accumulator for multiplication and division. A and B
CY AC F0 RS1 RS0 OV - P
CY - carry flag
AC - auxiliary carry flag
F0 - Available for user for general purpose
RS1,RS0 - register bank select bits
OV - overflow
P - parity
Stack Pointer (SP) – it contains the address of the data item on the top of the stack.
Stack may reside anywhere on the internal RAM. On reset, SP is initialized to 07 so
that the default stack will start from address 08 onwards.
Data Pointer (DPTR) – DPH (Data pointer higher byte), DPL (Data pointer lower
byte). This is a 16 bit register which is used to furnish address information for
internal and external program memory and for external data memory.
Program Counter (PC) – 16 bit PC contains the address of next instruction to be
executed. On reset PC will set to 0000. After fetching every instruction PC will
increment by one.
PINOUT DESCRIPTION
Pins 1-8 PORT 1. Each of these pins can be configured as an input or an output.
Pin 9 RESET. A logic one on this pin disables the microcontroller and clears the contents of
most registers. In other words, the positive voltage on this pin resets the
microcontroller. By applying logic zero to this pin, the program starts execution from
the beginning.
Pins10-17 PORT 3. Similar to port 1, each of these pins can serve as general input or output.
Besides, all of them have alternative functions
Pin 10 RXD. Serial asynchronous communication input or Serial synchronous
communication output.
Pin 11 TXD. Serial asynchronous communication output or Serial
synchronous communication clock output.
Pin 12 INT0.External Interrupt 0 input
Pin 18, 19 XTAL2, XTAL1. Internal oscillator input and output. A quartz crystal which
specifies operating frequency is usually connected to these pins.
Pin 20 GND. Ground.
Pin 21-28 Port 2. If there is no intention to use external memory then these port pins are
configured as general inputs/outputs. In case external memory is used, the
higher address byte, i.e. addresses A8-A15 will appear on this port. Even though
memory with capacity of 64Kb is not used, which means that not all eight port
bits are used for its addressing, the rest of them are not available as
inputs/outputs.
Pin 29 PSEN. If external ROM is used for storing program then a logic zero (0) appears
on it every time the microcontroller reads a byte from memory.
Pin 30 ALE. Prior to reading from external memory, the microcontroller puts the lower
address byte (A0-A7) on P0 and activates the ALE output. After receiving signal
from the ALE pin, the external latch latches the state of P0 and uses it as a
memory chip address. Immediately after that, the ALE pin is returned its
previous logic state and P0 is now used as a Data Bus.
Pin 31 EA. By applying logic zero to this pin, P2 and P3 are used for data and address
transmission with no regard to whether there is internal memory or not. It
means that even there is a program written to the microcontroller, it will not be
executed. Instead, the program written to external ROM will be executed. By
applying logic one to the EA pin, the microcontroller will use both memories, first
internal then external (if exists).
Pin 32-39 PORT 0. Similar to P2, if external memory is not used, these pins can be used as
general inputs/outputs. Otherwise, P0 is configured as address output (A0-A7)
when the ALE pin is driven high (1) or as data output (Data Bus) when the ALE
pin is driven low (0).
Pin 40 VCC. +5V power supply.
Working Registers
Register Banks: 00h to 1Fh. The 8051 uses 8 general-purpose registers R0 through R7 (R0, R1,
R2, R3, R4, R5, R6, and R7). There are four such register banks. Selection of register bank can be
done through RS1,RS0 bits of PSW. On reset, the default Register Bank 0 will be selected.
Bit Addressable RAM: 20h to 2Fh . The 8051 supports a special feature which allows access to bit
variables. This is where individual memory bits in Internal RAM can be set or cleared. In all there
are 128 bits numbered 00h to 7Fh. Being bit variables any one variable can have a value 0 or 1. A bit
variable can be set with a command such as SETB and cleared with a command such as CLR.
Example instructions are:
SETB 25h ; sets the bit 25h (becomes 1)
CLR 25h ; clears bit 25h (becomes 0)
The Bit Addressable area of the RAM is just 16 bytes of Internal RAM located between 20h and 2Fh.
General Purpose RAM: 30h to 7Fh. Even if 80 bytes of Internal RAM memory are available for
general-purpose data storage, user should take care while using the memory location from 00 -2Fh
since these locations are also the default register space, stack space, and bit addressable space. It is
a good practice to use general purpose memory from 30 – 7Fh. The general purpose RAM can be
accessed using direct or indirect addressing modes.
Number of address lines required for 16 Kbyte memory is 14 lines and that of 32Kbytes of
memory is 15 lines.
The connections of external memory is shown in figure. The lower order address and data bus are
multiplexed. De-multiplexing is done by the latch. Initially the address will appear in the bus and
this latched at the output of latch using ALE signal. The output of the latch is directly connected to
the lower byte address lines of the memory. Later data will be available in this bus. Still the latch
output is address it self. The higher byte of address bus is directly connected to the memory. The
number of lines connected depends on the memory size.
The RD and WR (both active low) signals are connected to RAM for reading and writing the data.
PSEN of microcontroller is connected to the output enable of the ROM to read the data from the
memory.
EA (active low) pin is always grounded if we use only external memory. Otherwise, once the
program size exceeds internal memory the microcontroller will automatically switch to external
memory.
STACK
A stack is a last in first out memory. In 8051 internal RAM space can be used as stack. The
address of the stack is contained in a register called stack pointer. Instructions PUSH and
POP are used for stack operations. When a data is to be placed on the stack, the stack
pointer increments before storing the data on the stack so that the stack grows up as data is
stored (pre-increment). As the data is retrieved from the stack the byte is read from the
stack, and then SP decrements to point the next available byte of stored data (post
decrement). The stack pointer is set to 07 when the 8051 resets. So that default stack
memory starts from address location 08 onwards (to avoid overwriting the default register
bank ie., bank 0).
ISR will always ends with RETI instruction. The execution of RETI instruction results in the
following.
Classification of interrupts.
6. IE Register
This is an 8 bit register used for enabling or disabling the interrupts. The structure of IE
register is shown below.
7. IP Register.
This is an 8 bit register used for setting the priority of the interrupts.
Time reference
Creating delay
Wave form properties measurement
Periodic interrupt generation
Waveform generation
8051 has two timers, Timer 0 and Timer 1.
Timer in 8051 is used as timer, counter and baud rate generator. Timer always counts up
irrespective of whether it is used as timer, counter, or baud rate generator: Timer is always
incremented by the microcontroller. The time taken to count one digit up is based on master clock
frequency.
If Master CLK=12 MHz,
This indicates that one increment in count will take 1 micro second.
The two timers in 8051 share two SFRs (TMOD and TCON) which control the timers, and each timer
also has two SFRs dedicated solely to itself (TH0/TL0 and TH1/TL1).
TMOD Register
8051 timers have both software and hardware controls. The start and stop of a timer is controlled
by software using the instruction SETB TR1 and CLR TR1 for timer 1, and SETB TR0 and CLR
TR0 for timer 0.
The SETB instruction is used to start it and it is stopped by the CLR instruction. These instructions
start and stop the timers as long as GATE = 0 in the TMOD register. Timers can be started and
stopped by an external source by making GATE = 1 in the TMOD register.
TCON Register
TIMER MODES
The lower 5 bits of TLX and 8 bits of THX are used for the 13 bit count.Upper 3 bits of TLX are
ignored. When the counter rolls over from all 0's to all 1's, TFX flag is set and an interrupt is
generated. The input pulse is obtained from the previous stage. If TR1/0 bit is 1 and Gate bit is 0,
the counter continues counting up. If TR1/0 bit is 1 and Gate bit is 1, then the operation of the
counter is controlled by input. This mode is useful to measure the width of a given pulse fed to
input.
Timer Mode-1: This mode is similar to mode-0 except for the fact that the Timer operates in 16-bit
mode.
Timer Mode-3: Timer 1 in mode-3 simply holds its count. The effect is same as setting TR1=0.
Timer0 in mode-3 establishes TL0 and TH0 as two separate counters.
Control bits TR1 and TF1 are used by Timer-0 (higher 8 bits) (TH0) in Mode-3 while TR0 and TF0
are available to Timer-0 lower 8 bits(TL0).
Programming in the sense of Microcontrollers (or any computer) means writing a sequence of
instructions that are executed by the processor in a particular order to perform a predefined task.
Programming also involves debugging and troubleshooting of instructions and instruction
sequence to make sure that the desired task is performed.
Like any language, Programming Languages have certain words, grammar and rules. There are
three types or levels of Programming Languages for 8051 Microcontroller. These levels are based
on how closely the statements in the language resemble the operations or tasks performed by the
Microcontroller.
Machine Language
Assembly Language
High-level Language
MACHINE LANGUAGE
In Machine language or Machine Code, the instructions are written in binary bit patterns i.e.
combination of binary digits 1 and 0, which are stored as HIGH and LOW Voltage Levels. This is the
lowest level of programming languages and is the language that a Microcontroller or
Microprocessor actually understands.
ASSEMBLY LANGUAGE
The next level of Programming Language is the Assembly Language. Since Machine Language or
Code involves all the instructions in 1’s and 0’s, it is very difficult for humans to program using it.
Assembly Language is a pseudo-English representation of the Machine Language. The 8051
Microcontroller Assembly Language is a combination of English like words called Mnemonics and
Hexadecimal codes.
It is also a low level language and requires extensive understanding of the architecture of the
Microcontroller.
HIGH-LEVEL LANGUAGE
The name High-level language means that you need not worry about the architecture or other
internal details of a microcontroller and they use words and statements that are easily understood
by humans.
Few examples of High-level Languages are BASIC, C Pascal, C++ and Java. A program called
Compiler will convert the Programs written in High-level languages to Machine Code.
The Programs written in Assembly gets executed faster and they occupy less memory.
With the help of Assembly Language, you can directly exploit all the features of a
Microcontroller.
Using Assembly Language, you can have direct and accurate control of all the
Microcontroller’s resources like I/O Ports, RAM, SFRs, etc.
Compared to High-level Languages, Assembly Language has less rules and restrictions.
The Structure or Syntax of the 8051 Microcontroller Assembly Language is discussed here. Each
line or statement of the assembly language program of 8051 Microcontroller consists of three
fields: Label, Instruction and Comments.
The arrangement of these fields or the order in which they appear is shown below.
The brackets for Label and Comments mean that these fields are optional and may not be used in
all statements in a program.
Before seeing about these three fields, let us first see an example of how a typical statement or line
in an 8051 Microcontroller Assembly Language looks like.
In the above statement, the “TESTLABEL” is the name of the Label, “MOV A, 24H” is the Instruction
and the “THIS IS A SAMPLE COMMENT” is a Comment.
LABEL
The Label is programmer chosen name for a Memory Location or a statement in a program. The
Label part of the statement is optional and if present, the Label must be terminated with a Colon (:).
An important point to remember while selecting a name for the Label is that they should reduce
the need for documentation.
INSTRUCTION
The Instruction is the main part of the 8051 Microcontroller Assembly Language Programming as
it is responsible for the task performed by the Microcontroller. Any Instruction in the Assembly
Language consists of two parts: Op-code and Operand(s).
The first part of the Instruction is the Op-code, which is short for Operation Code, specifies the
operation to be performed by the Microcontroller. Op-codes in Assembly Language are called as
Mnemonics. Op-codes are in binary format (used in Machine Language) while the Mnemonic
(which are equivalent to Op-codes) are English like statements.
The second part of the instruction is called the Operand(s) and it represents the Data on which the
operation is performed. There are two types of Operands: the Source Operand and the Destination
Operand. The Source Operand is the Input of the operation and the Destination Operand is where
the result is stored.
COMMENTS
The last part of the Structure of 8051 Assembly Language is the Comments. Comments are
statements included by the developer for easier understanding of the code and is used for proper
documentation of the Program.
Comments are optional and if used, they must begin with a semicolon (;) or double slash (//)
depending on the Assembler.
classified as follows.
1. Immediate addressing.
2. Register addressing.
3. Direct addressing.
4. Indirect addressing.
5. Relative addressing.
6. Absolute addressing.
7. Long addressing.
8. Indexed addressing.
9. Bit inherent addressing.
10. Bit direct addressing.
I. IMMEDIATE ADDRESSING.
In this addressing mode the data is provided as a part of instruction itself. In other
words data immediately follows the instruction.
Eg. MOV A,#30H
R0 – R7 will be selected from the current selection of register bank. The default
register bank will be bank 0.
V. INDEXED ADDRESSING.
In indexed addressing, either the program counter (PC), or the data pointer (DTPR)—
is used to hold the base address, and the A is used to hold the offset address. Adding the value
of the base address to the value of the offset address forms the effective address. Indexed
addressing is used with JMP or MOVC instructions. Look up tables are easily implemented
with the help of index addressing.
Eg. MOVC A, @A+DPTR // copies the contents of memory location pointed by the sum of the
accumulator A and the DPTR into accumulator A.
MOVC A, @A+PC // copies the contents of memory location pointed by the sum of the
accumulator A and the program counter into accumulator A.
RAM space 20H to 2FH and most of the special function registers are bit addressable. Bit
address values are between 00H to 7FH.
Eg. CLR 07h ; Clears the bit 7 of 20h RAM space
1. INSTRUCTION TIMINGS
The 8051 internal operations and external read/write operations are controlled by the oscillator
clock.
T-state, Machine cycle and Instruction cycle are terms used in instruction timings.
T-state is defined as one subdivision of the operation performed in one clock period. The terms 'T-
state' and 'clock period' are often used synonymously.
Machine cycle is defined as 12 oscillator periods. A machine cycle consists of six states and each
state lasts for two oscillator periods. An instruction takes one to four machine cycles to execute an
instruction. Instruction cycle is defined as the time required for completing the execution of an
instruction. The 8051 instruction cycle consists of one to four machine cycles.
Eg. If 8051 microcontroller is operated with 12 MHz oscillator, find the execution time for the
following four instructions.
1. ADD A, 45H
2. SUBB A, #55H
3. MOV DPTR, #2000H
4. MUL AB
Since the oscillator frequency is 12 MHz, the clock period is, Clock period = 1/12 MHz = 0.08333 µS.
Time for 1 machine cycle = 0.08333 µS x 12 =1 µS.
2. 8051 INSTRUCTIONS
The instructions of 8051 can be broadly classified under the following headings.
Data transfer instructions
Arithmetic instructions
Logical instructions
Branch instructions
Subroutine instructions
Bit manipulation instructions
In this group, the instructions perform data transfer operations of the following types.
a. Move the contents of a register Rn to A
i. MOV A,R2
ii. MOV A,R7
b. Move the contents of a register A to Rn
i. MOV R4,A
ii. MOV R1,A
c. Move an immediate 8 bit data to register A or to Rn or to a memory
location(direct orindirect)
i. MOV A, #45H
ii. MOV R6, #51H
iii. MOV 30H, #44H
Arithmetic instructions.
The 8051 can perform addition, subtraction. Multiplication and division operations on
8 bitnumbers.
Addition
In this group, we have instructions to
i. Add the contents of A with immediate data with or without carry.
i. ADD A, #45H
ii. ADDC A, #OB4H
ii. Add the contents of A with register Rn with or without carry.
i. ADD A, R5
ii. ADDC A, R2
iii. Add the contents of A with contents of memory with or without carry
using direct andindirect addressing
i. ADD A, 51H
ii. ADDC A, 75H
iii. ADD A, @R1
iv. ADDC A, @R0
Subtraction
In this group, we have instructions to
i. Subtract the contents of A with immediate data with or without carry.
i. SUBB A, #45H
ii. SUBB A, #OB4H
ii. Subtract the contents of A with register Rn with or without borrow.
i. SUBB A, R5
ii. SUBB A, R2
iii. Subtract the contents of A with contents of memory with or without carry
using direct andindirect addressing
i. SUBB A, 51H
operation.Multiplication
MUL AB. This instruction multiplies two 8 bit unsigned numbers which are stored
in A and B register. After multiplication the lower byte of the result will be stored
in accumulator and higher byte of result will be stored in B register.
MOV A,#45H ;[A]=45H
MOV B,#0F5H ;[B]=F5H
MUL AB ;[A] x [B] = 45 x F5 = 4209
;[A]=09H, [B]=42H
Division
DIV AB. This instruction divides the 8 bit unsigned number which is stored in A
by the 8 bit unsigned number which is stored in B register. After division the
result will be stored in accumulator and remainder will be stored in B register.
Eg. MOV A,#45H ;[A]=0E8H
MOV B,#0F5H ;[B]=1BH
DIV AB ;[A] / [B] = E8 /1B = 08 H with remainder 10H
;[A] = 08H, [B]=10H
When two BCD numbers are added, the answer is a non-BCD number. To get the
result in BCD, we use DA A instruction after the addition. DA A works as follows.
If lower nibble is greater than 9 or auxiliary carry is 1, 6 is added to lower nibble.
If upper nibble is greater than 9 or carry is 1, 6 is added to upper nibble.
Eg 1: MOV A,#23H
MOV R1,#55H
ADD A,R1 // [A]=78
DA A // [A]=78 no changes in the accumulator after DA A
Eg 2: MOV A,#53H
MOV R1,#58H
ADD A,R1 // [A]=ABh
DA A // [A]=11, C=1 . ANSWER IS 111. Accumulator data is
changed after DA A
In the case of "INC DPTR", the value two-byte unsigned integer value of DPTR is
incremented. If the initial value of DPTR is FFFFh, incrementing the value will cause it
to reset to 0.
DEC decrements the value of source by 1. If the initial value of is 0, decrementing the
value will cause it to reset to FFh. The Carry Flag is not set when the value "rolls over"
from 0 to FFh.
Logical Instructions
Logical AND
ANL destination, source: ANL does a bitwise "AND" operation between source and
destination, leaving the resulting value in destination. The value in source is not
affected. "AND" instruction logically AND the bits of source and destination.
ANL
A,#DATA
ANL A, Rn
ANL
A,DIRECT
ANL A,@Ri
Logical OR
ORL destination, source:ORL does a bitwise "OR" operation between source and
destination,
leaving the resulting value in destination. The value in source is not affected. " OR "
instruction logically OR the bits of source and destination.
ORL
A,#DATA
ORL A, Rn
ORL
Logical Ex-OR
XRL destination, source: XRL does a bitwise "EX-OR" operation between source
and destination, leaving the resulting value in destination. The value in source is
not affected. " XRL " instruction logically EX-OR the bits of source and destination.
XRL
A,#DATA XRL A,Rn XRL A,DIRECT XRL A,@Ri
Logical NOT
CPL complements operand, leaving the result in operand. If operand is a single bit
then the state of the bit will be reversed. If operand is the Accumulator then all the
bits in the Accumulator will be reversed.
Rotate Instructions
RR A
This instruction is rotate right the accumulator. Its operation is illustrated below.
Each bit is shifted one location to the right, with bit 0 going to bit 7.
RL A
Rotate left the accumulator. Each bit is shifted one location to the left, with bit 7
going to bit 0
Rotate right through the carry. Each bit is shifted one location to the right, with bit
going into the carry bit in the PSW, while the carry was at goes into bit 7
RLC A
Rotate left through the carry. Each bit is shifted one location to the left, with bit
7 going into the carry bit in the PSW, while the carry goes into bit 0.
Relative Jump
Jump that replaces the PC (program counter) content with a new address that is
greater than (the address following the jump instruction by 127 or less) or less
than (the address following the jump by -128 or less) is called a relative jump.
Schematically, the relative jump can be shown as follows: -
00 0000 - 07FF
01 0800 - 0FFF
02 1000 - 17FF
03 1800 - 1FFF
.
.
1E F000 - F7FF
1F F800 - FFFF
It can be seen that the upper 5bits of the program counter (PC) hold the page
number and the lower 11bits of the PC hold the address within that page. Thus, an
absolute address is formed by taking page numbers of the instruction (from the
program counter) following the jump and attaching the specified 11bits to it to
form the 16-bit address.
Applications that need to access the entire program memory from 0000H to
FFFFH use long absolute jump. Since the absolute address has to be specified in
the op-code, the instruction length is 3 bytes (except for JMP @ A+DPTR). This
jump is not re-locatable.
Example: -
Bit level JUMP instructions will check the conditions of the bit and if condition is true, it
jumps to the address specified in the instruction. All the bit jumps are relative jumps.
JB bit, rel ; jump if the direct bit is set to the relative address specified.
JNB bit, rel ; jump if the direct bit is clear to the relative address specified.
JBC bit, rel ; jump if the direct bit is set to the relative address specified and then clear the bit.
a. During execution of LCALL, [PC] = [PC]+3; (if address where LCALL resides is
say, 0x3254;during execution of this instruction [PC] = 3254h + 3h = 3257h
b. [SP]=[SP]+1; (if SP contains default value 07, then SP increments and [SP]=08
a. During execution of SCALL, [PC] = [PC]+2; (if address where LCALL resides is say,
0x8549;during execution of this instruction [PC] = 8549h + 2h = 854Bh
b. [SP]=[SP]+1; (if SP contains default value 07, then SP increments and [SP]=08
c. [[SP]] = [PC7-0]; (lower byte of PC content ie., 4B will be stored in memory location
08.
d. [SP]=[SP]+1; (SP increments again and [SP]=09)
e. [[SP]] = [PC15-8]; (higher byte of PC content ie., 85 will be stored in memory location
09.
f. [PC10-0]= address (11 bit); the new address of subroutine is loaded to PC.
No flags are affected.
RET instruction
RET instruction pops top two contents from the stack and load it to PC.
g. [PC15-8] = [[SP]] ;content of current top of the stack will be moved to higher byte of
PC.
h. [SP]=[SP]-1; (SP decrements)
i. [PC7-0] = [[SP]] ;content of bottom of the stack will be moved to lower byte of PC.
j. [SP]=[SP]-1; (SP decrements again)
8051 has 128 bit addressable memory. Bit addressable SFRs and bit addressable
PORT pins. It is possible to perform following bit wise operations for these bit
addressable locations.
1. LOGICAL AND
a. ANL C,BIT(BIT ADDRESS) ; ‘Logically and’ carry and content of bit address, store result in
carry
b. ANL C, /BIT; ; ‘Logically and’ carry and complement of content of bit address, store result
in carry
2. LOGICAL OR
a. ORL C,BIT(BIT ADDRESS) ; ‘Logically or’ carry and content of bit address, store result in