Unit 2-Paper 6
Unit 2-Paper 6
The 8051 supports number of arithmetic and logical operations. ADDITION, SUBTRACTION,
MULTIPLICATION, DIVISION, INCREMENTING AND DECREMENTING and DECIMAL ADJUST can be
used to perform arithmetic operations. The arithmetic opcodes are -
MNEMONIC OPERATION
INC destination Increment destination by 1
DEC destination Decrement destination by 1
ADD destination, source Add source to destination
ADDC destination , source Add source to destination with carry.
SUBB destination, source Subtract with barrow, source from destination
MUL AB Multiply the contents of register A and B
DIV AB Divide the contents of register A by the contents of register B
DA A Decimal Adjust the A register
MNEMONIC OPERATION
INC A Add 1 to accumulator
INC Rp Add 1 to register Rr
INC @Rp Add 1 to contents of the direct memory address
INC add Add 1 to contents of memory address in Rp
INC DPTR Add 1 to 16 bit contents of DPTR
DEC A Subtract 1 from accumulator
DEC Rr Subtract 1 from register Rr
DEC add Subtract 1from contents of the direct memory address
DEC @Rp Subtract 1 from contents of memory address in Rp
Note:
There is no DEC DPTR instruction.
Addition ( Without Carry):
All addition is done with the A register as the destination of the result. All addressing modes may be
used for the source. The following table lists the addition mnemonics.
MNEMONIC OPERATION
ADD A, #n
Add A and the immediate number n ; put the sum in A
Eg: ADD A, #3C
ADD A,#Rr
Add A and register Rr; put the sum in A
Eg: ADD A, R4
ADD A,add
Add A and address contents; put the sum in A
Eg: ADD A, 3C
ADD A,@Rp
Add A and the contents of the address in Rp; put the sum in A
Eg: ADD A,@R1
Addition (With Carry):
1 of 17 P a g e
If carry is to be added to the higher order bytes of the addition operation, ADDC instruction is used.
The following table lists the ADD instruction with carry mnemonics.
MNEMONIC OPERATION
ADDC A, #n Add A and the immediate number n and the C flag ; put the sum in A
Eg: ADDC A, #45
ADD C A, Rr Add A and register Rr and C flag; put the sum in A
Eg: ADDC A, R3
ADDC A,add Add A and address contents and C flag ; put the sum in A
Eg: ADDC A, 45
ADDC A,@Rp Add A and the contents of the address in Rp and C flag ; put the sum in A
Eg: ADDC A,@R0
In all the above instructions the result is stored in A
Subtraction:
Subtraction can be done by taking the two`s complement of the number to be subtracted (subtrahend), and
adding it to another number (minuend). Register A is the destination address for the subtraction. All
addressing modes may be used for source address. The commands treat the Carry flag as a borrow flag
and always subtract the carry flag as part of the operation.
Multiplication:
Multiplication operation uses registers A and B as both source and destination addresses for the operation.
The number in register A is multiplied by the number in register B as indicated in the following
instruction.
MNEMONIC OPERATION
Multiply A by B; put the lower order byte of the product in A, and higher
MUL AB
order byte of the product in B
Division:
Division operation uses registers A and B as both source and destination addresses for the operation. The
number in register A is divided by the number in register B as indicated in the following instruction.
2 of 17 P a g e
MNEMONIC OPERATION
Divide A by B; put the integer part of the quotient in register A and integer
DIV AB part of the reminder in B
Decimal Arithmetic:
Sometimes it becomes necessary to used decimal numbers 0-9 than hex or binary numbers. These are
represented in BCD numbers.
Unpacked BCD:
Unpacked BCD is the term used to represent decimal numbers with eight bits. The upper nibble is zero
and the lower nibble represents the BCD number.
Eg; 0000 0001 represents unpacked BCD 1 and 0000 1001 represents the unpacked BCD 9. This
is used to represent the numbers from 0-9.
Packed BCD:
In packed BCD, a single byte has a two BCD numbers in it. One is the lower 4-bits and another is the
upper 4-bits.
Eg: 0100 1001 represents packed BCD of 49 and 1000 0001 represents the packed BCD of 81.
The 8051 supports an instruction “DA” (Decimal Adjust for addition) which is used to add two packed
BCD numbers. The instruction is as follows
MNEMONIC OPERATION
Adjust the sum of two packed numbers for decimal addition. The adjusted
DA A number is in A
LOGICAL OPERATIONS
The Boolean operators are – AND, OR, NOT and XOR. The two data levels –byte or bit at which the
Boolean instructions operate are as shown in the following table
Byte level logical operations involve each individual bit of a source byte operating on the same bit
positions in the destination byte. The result is put in the destination byte. The source byte is not changed.
3 of 17 P a g e
The byte level logical operations use all four addressing modes for the source of data byte. A register or
a direct address in internal RAM is the destination of the logical operation result . These operations are
called byte level Boolean operation because the entire byte is affected.
The following are the byte level Boolean Operations.
MNEMONIC OPERATION
ANL A,#n AND each bit of A with the same bit of immediate number n; put the results in A
Eg: ANL A,#8C
ANL A ,add AND each bit of A with the same bit of direct RAM address; put the results in A
Eg: ANL A, 7C
ANL A,Rr AND each bit of A with the same bit of register Rr; put the results in A
Eg: ANL A, R3
ANL A, @Rp AND each bit of A with the same bit of the contents of the RAM address
Eg: ANL A, @R1 contained in Rp; put the results in A
ANL add , A AND each bit of A with the direct RAM address; put the results in direct RAM
Eg:ANL 27, A address.
ANL add,#n AND each bit of RAM address with the same bit in the number n ; put the results
Eg: ANL 4E,#5 in the direct RAM address.
ORL A,#n OR each bit of A with the same bit of immediate number n; put the results in A
Eg: ORL A,#8C
ORL A ,add OR each bit of A with the same bit of direct RAM address; put the results in A
Eg: ORL A, 7C
ORL A,Rr OR each bit of A with the same bit of register Rr; put the results in A
Eg: ORL A, R3
ORL A, @Rp OR each bit of A with the same bit of the contents of the RAM address contained
Eg: ORL A, @R1 in Rp; put the results in A
ORL add , A OR each bit of A with the direct RAM address; put the results in direct RAM
address.
Eg: ORL 27, A
ORL add,#n OR each bit of RAM address with the same bit in the number n ; put the results in
Eg: ORL 6F,#n the direct RAM address.
XRL A,#n XOR each bit of A with the same bit of immediate number n; put the results in A
Eg: XRL A,#58
XRL A ,add XOR each bit of A with the same bit of direct RAM address; put the results in A
Eg: XRL A ,68
XRL A,Rr XOR each bit of A with the same bit of register Rr; put the results in A
Eg: XRL A,R2
XRL A, @Rp XOR each bit of A with the same bit of the contents of the RAM address
Eg: XRL A, @R1 contained in Rp; put the results in A
XRL add , A XOR each bit of A with the direct RAM address; put the results in direct RAM
Eg: XRL 80, A address.
XRL add,#n XOR each bit of RAM address with the same bit in the number n; put the results in
Eg: XRL 8C,#55 the direct RAM address.
CLR A Clear each bit of register A to 0
CPL A Complement each bit of A; every 1 becomes 0, and each 0 becomes 1.
Note:
4 of 17 P a g e
The destination of the result of the logical operation is either the register A or the direct address in
the internal RAM.
Bit level operations involve individual bits found in the internal RAM and certain SFRs that may be
addressed either by direct address or by individual bit addresses. The following are the bit address.
Byte addresses Bit addresses Byte addresses Bit addresses
Internal RAM 00 – 07 2C 60 – 67
20
21 08 – 0F 2D 68 – 6F
22 10 – 17 2E 70 – 77
23 18 – 1F 2F 78 – 7F
24 20 – 27 SFRs A - 0E0 0E0 – 0E7
25 28 – 2F B - 0F0 0F0 – 0F7
26 30 – 37 IE - 0A8 0A8 – 0AF
27 38 – 3F IP - 0B8 0B8 – 0BF
28 40 – 47 P0 - 80 80 – 87
29 48 – 4F P1 - 90 90 – 97
2A 50 – 57 P2 - 0A0 0A0 – 0A7
2B 58 – 5F P3 - 0B0 0B0 – 0B7
MNEMONIC OPERATION
ANL C,b AND C and the addressed bit b; put the result in C
ANL C, /b AND C and complement of the addressed bit b; put the result in C
ORL C, b OR C and the addressed bit b; put the result in C
ORL C, /b OR C and complement of the addressed bit b; put the result in C
CPL C Complement the C flag
CPL b Complement the addressed bit
CLR C Clear the C flag ( C = 0)
CLR b Clear the addressed bit
MOV C, b Copy the addressed bit to C flag
MOV b, C Copy the C flag to the addressed bit
SETB C Set the carry flag ( C = 1)
SETB b Set the addressed bit to 1
Note:
5 of 17 P a g e
In the entire above, no flag except carry flag is affected.
If the destination bit is a port bit, the SFR latch bit is affected, not the pin.
Only SFRs which are bit addressable can be used in bit level operations.
ANL C, /b and ORL C, /b do not alter the addressed bit b.
The 8-bits of the accumulator are rotated right one bit. A7 is placed to A0 and every other bit moved left
by 1 bit.
6 of 17 P a g e
Eg: If A = 0011 0110 then RLA = 0110 1100
RRC A: rotate Accumulator right through carry :
In RRC A, accumulator bits are rotated right, the LSB A0 moves into carry and carry moves to A7. It is
considered as 9 bit rotation including carry.
In RLC A, accumulator bits are rotated left, the MSB A7 moves into carry and carry moves to A0. It is
considered as 9 bit rotation including carry.
SWAP operation:
The SWAP operation interchanges the upper and lower nibbles of the Accumulator.
7 of 17 P a g e
8051 PROGRAMMING
ADDRESSING MODES
The CPU can access data in various ways. The data could be in a register, or in memory, or be
provided as an immediate value. These various ways of accessing data are called addressing modes. .
In 8051 there are five distinct addressing modes. They are -
1. Immediate
2. Register
3. Direct
4. Register Indirect
5. Indexed
This addressing mode can be used to load information into any of the registers, including the
DPTR register and Ports. Although the DPTR register is 16-bit, it can also be accessed as two 8-bit
registers, DPH and DPL, where DPH is the high byte and DPL is the low byte.
Mnemonic Operation
MOV Rr.#n Copy the 8 bit number n into register Rr
Eg: MOV R3,#2A
MOV B, #22
MOV A.#n Copy the 8 bit number n into Accumulator register
Eg: MOV A, #20H
MOV DPTR,#nn Copy the 16 bit number nn into DPTR register
Eg: MOV DPTR, #32FAH
MOV DPL, #FF
This involves the use of registers to hold the data to be manipulated. Both the source and destination are registers
and their sizes should match.
Mnemonic Operation
MOV A, Rr Copy data from register Rr into A
Eg: MOV A, R0
ADD A, R7
MOV Rr, A Copy data from register A to register Rr
Eg: MOV R5, A
MOVR3, A
NOTE:
1|6
MOV R1,R2 IS INVALID INSTRUCTION
In this mode the direct address of memory location is provided in instruction to fetch the operand. Only
internal RAM and SFR's address can be used. All 128 bytes of internal RAM and the SFR may be
addressed directly using single byte address assigned to each RAM location and each special function
register. We know that
RAM locations 00 – 1FH are assigned to the register banks and stack.
RAM locations 20 – 2FH are bit addressable
RAM locations 30 – 7FH are available for general purpose RAM.
Some addresses between 80 – FFH are uniquely assigned to SFR.
2|6
MNEMONIC OPERATION
MOV A, add Copy data from direct add to register A
Eg: MOV A, 30H; Copy data from direct address 30 to register A;
MOV add, A Copy data from A to direct address add
Eg: MOV 23H, A Copy data from A to direct address address 23;
MOV Rr, add Copy data from direct add to register Rr
Eg: MOV R1, 75H Copy data from direct address 75 to register R1;
MOV add, Rr Copy data from register Rr direct address add
Eg: MOV 75H , R1 Copy data from R1 to direct address 75 ;
MOV add, #n Copy immediate data byte n to direct address add
Eg: MOV 52H ,# 0FH Copy immediate data byte 0F to direct address 52;
MOV add1, add2 Copy data from direct address add2 to direct address add1
Eg: MOV 45H, 36H Copy data from direct address 36 to direct address 45;
The stack is a section of RAM used by CPU for storage of information temporarily. This is needed since
we have limited number of registers. The register used to access the stack is the stack pointer SP. By
default, when power is switched on, the SP is initialized at 07H, which is the address of R7 of Bank 0.
There are two data functions PUSH & POP associated with Stack.
PUSH OPCODE:
A PUSH opcode copies data from the source address to the stack. SP is incremented by 1 before
the data is copied to the internal RAM location contained in SP. The data is stored from low address to
high address in the internal RAM. The stack grows up in memory as it is PUSHed. When the SP reaches
FFH it “rolls over” to 00H and the data is lost.
The Mnemonic is
PUSH add ; Increment SP. Copy the contents of add to the internal RAM address in SP
For eg:
3|6
MOV R2, #22H
PUSH 03H
PUSH 02H
In this example the SP is initialized at 07H. The first two instructions load the data 33 and 22
into registers R3 and R2. . Stack is changed as shown below.
0B 0B
0A 0B
09 SP = 070A SP = 08 0A
SP = 09
09 22
08 09
08 33
08 33
POP OPCODE:
A POP opcode copies from the stack to the destination address. SP is decremented by 1 after
data is copied from stack RAM address to direct destination. The data is retrieved in the same manner
as it was stored.
The Mnemonic is
POP add; Copy the contents of the internal RAM address in SP to add. ; Decrement SP
E.g.: POP 02
POP 01
POP 00
0B 5B 0B 0B 5B 0B 5B
0A 5A 0A 5A 0A 5A 0A 5A
09 59 09 59 09 59 09 59
08 58 08 58 08 58 08 58
Original After POP 02 After POP 01 After POP 00
SP = 0B R2 = 5B R1 = 5A R0 = 59
SP = 0A SP = 09 SP = 08
Program
Show the code to push R5, R6, and A onto the stack and then pop them back them into R2, R3, and B,
where register B = register A, R2 = R6, and R3 = R5.
4|6
In the register indirect addressing mode, a register is used as a pointer to the data. If the data is
inside the CPU, only registers R0 and Rl are used for this purpose. In other words, R2 – R7 cannot be
used to hold the address of an operand located in RAM when using this addressing mode.
Notice that R0 (as well as Rl) is preceded by the “@” sign. In the absence of the “@” sign, MOV will be
interpreted as an instruction moving the contents of register R0 to A, instead of the contents of the
memory location pointed to by R0.
MNEMONIC OPERATION
MOV @Rp , #n Copy the immediate byte ‘n’ to the address in Rp.
Eg: MOV @R1, #35H Copy the number 35H to the address in R1;
MOV @Rp, add Copy the contents of add to the address in Rp
Eg: MOV @R1 , #35H; Copy the number 35 to the address in R1;
MOV @Rp, A Copy the data in A to the address in Rp
Eg: MOV @R1,A ; Copy the data in A to the address in R1;
MOV add @Rp Copy the contents of address in Rp to add
Eg: MOV 40H , @R0 ; Copy the contents of address in R0 to address 40;
MOV A, @Rp Copy the contents of the address in Rp to A
Eg: MOV A,@R0 ; Copy the contents of the address in R0 to A;
Sometimes it becomes necessary to access the pre-programmed data. This data is permanent and is
stored in ROM locations. This data can be accessed using indirect addressing mode between the register A and
either with DPTR or PC. The contents of A are added to the pointer to get the address of the data. The data is
placed in A. hence the old data in A is lost. The syntax is as follows.
The syntax is as follows.
MNEMONIC OPERATION
MOVC A,@A +DPTR Copy the code byte , found in ROM address formed by adding A and DPTR to A
MOVC A,@A +PC Copy the code byte , found in ROM address formed by adding A and PC to A
5|6
Eg: MOVX @R1,A
MOVX @DPTR , A Copy data from A to the external address in DPTR
One of the important features of the 8051 is the ability to access the registers, RAM, and I/O
ports in bits instead of bytes.
Bit-addressable RAM
The bit-addressable RAM locations are 20H to 2FH. These 16 bytes provide 128 bits of RAM
bit-addressability. They are addressed as 0 to 127 (in decimal) or 00 to 7FH. In orderto access these 128
bitsof RAM locations andother bit-addressablespace of 8051 individually, single-bit instructions such as
SETB is used
Single-Bit Instructions
DATA EXCHANGE:
MOV, PUSH and POP instructions involve copying the data found in source address to the destination
address; the original data in the source is not changed. Exchange instructions move data from source to
destination and from destination to source. In effect the data is swapped between two locations. All
addressing modes except immediate addressing mode may be used. The mnemonic is XCH
e.g.;
MNEMONIC OPERATION
XCH A, Rr Exchange data bytes between Rr and A
Eg: XCH A, R2
XCH A, add Exchange data bytes between add and A
Eg: XCH A, 4A
XCH A, @Rp Exchange data bytes between A and address in Rp
Eg: XCH A,
@R1
XCHD A, @Rp Exchange lower nibble between A and address in Rp
Eg: XCHD A,
@R0
NOTE:
All exchanges are internal to 8051
All exchanges use register A
When using XCHD, the upper nibble of A and upper nibble of address location in Rp do not change .
6|6
7|6
1
MICRONTROLLERS
Sl.
Microprocessor Microcontroller
No
1 CPU is stand alone, RAM,ROM ,I/O devices
CPU, RAM, ROM,I/O devices and TIMERS
and timers are separate and interfaced with
are all located on a single chip.
CPU
2 Designer can decide on the amount of RAM,
Fixed amount of RAM,ROM and I/O ports
ROM and I/O ports.
3 Applications in which cost, space and power
Expensive applications
are critical
4 Versatile and general purpose Not very versatile
5 Uses different program memory and data
Program memory and data memory are same
memory.
6 Large number of instructions with flexible Limited number of instructions with few
addressing modes addressing modes
7 Very few instructions which have bit handling Many instructions with bit handling
capability capability
8 System cost is more System cost is less
9 Clock frequency > 1 G Hz Clock frequency 10-20 M Hz
4Kb ROM
128 bytes RAM
Four 8 bits I/O ports
Two 16 bit timers
Serial interface
64K external code memory space
64 K data memory space.
The architectural block diagram of the microcontroller is as shown below. The architecture can be
explained under the following headings.
1. Registers of microcontroller
2. Memory
3. Timers/counters
4. Interrupts.
REGISTERS IN 8051:
The 8051 has general purpose or working registers, Stack Pointer, Program Counter in addition to
the CPU registers. There are special function registers also.
Accumulator (A):
This is one of the CPU register. It is 8 bit register used by all arithmetic and logical instructions.
This stores one of the operands of the instruction before execution. It also stores the result after the
execution of an instruction. Access to accumulator is faster than the main memory.
B-register:
This is another register of CPU. It is an 8 bit register. It is being used for arithmetic operations of
multiplication and division. It is available as general purpose register when it is not being used by
multiplication and division operations.
Registers R0 through R7:
These 8 registers are used as scratch pad registers. There are 4 register banks each containing R0
through R7 registers. Each of these registers is 8 bit wide. Any register bank can be selected by
appropriate setting of bits in PSW. These register banks are located in the on-chip RAM.
8051 has 4 math flags that respond to the output of math operations and 3 general purpose flags.
The math flags are – Carry (C), Auxiliary Carry (AC), Overflow (OV) and Parity (P).
3
The general purpose flags are – F0, GF0 and GF1 that may be used by the programmer to record
Page
Overflow flag:
Overflow is set as a result of an arithmetic operation provided there is a carry out of bit 6, but not
out of bit 7 OR a carry out of bit 7 but not out of bit 6.
E.g.: 11000010 + 10101010 = 01101100. Here carry is generated out of bit 7 but not out of bit 6.
HenceOV flag is set.
Parity flag:
Parity flag indicates the number of 1`s in accumulator. If there are odd number of 1`s in
accumulator, then the odd parity sets the parity flag to 1. The parity flag is 0, for even parity
Problems:
4
Page
5
MEMORY ORGANIZATION
Internal ROM
The 8051 has 64 K external data memory, 256 bytes of internal data memory and 64 K program memory.
The program memory map of 8051 is as shown below.
Program addresses higher than 0FFFh which exceed the internal ROM capacity, will cause the 8051 to
automatically fetch code bytes from the external memory portion i.e., 1000H through 0FFFFH
Internal RAM
The internal data memory of 8051 is 256 bytes, which is divided into two parts. The lower 128 bytes
(00H through 7FH) called as the internal data RAM and the upper 128 bytes (80H through FFH)are called
SFRs.
The 128-byte internal RAM is organized into three distinct areas.
Register banks
Bit addressable RAM
General purpose RAM
32 bytes from address 00h to 1Fh are made up of 32 working registers. These registers are organized as
four banks of 8 registers each. The four register banks are numbered 0 to 3 and registers named R0 to R7.
Each register can be addressed by name (when bank is selected) or by its RAM address. Bits RS0 and
RS1 in the PSW determine which bank of registers is currently in use. Bank 0 is selected on reset.
5
Page
6
There are 4 register banks from 00 to 1F each containing R0 through R7 registers. Each of
these registers is 8 bit wide. At any time only one register bank can be selected by
appropriate setting of bits in PSW.
A bit addressable area of 16 bytes occupies RAM byte addresses 20h to 2Fh, forming a
6
Page
total of 128 addressable bits. An addressable bit may be specified by the bit address of 00h
to 7Fh, or 8 bits from any byte address from 20h to 2Fh.
7
A general-purpose RAM area above the bit area from 30h to 7Fh addressable as byte.
The 8051 operations that do not use the internal 128-byte RAM addresses from 00h to 7Fh are done by a
group of specific internal registers called Special Function Register (SFR) which may be addressed like
RAM. SFRs may be bit addressable or byte addressable. The following table gives the SFRs and their
addresses.
INTERNAL RAM
NAME FUNCTION
ADDRESS
A Accumulator 0E0
B Arithmetic 0F0
SP Stack pointer 81
The 8051 has two 16 – bit Timer/Counter registers – Timer 0 - T0 and Timer 1 – T1. The timers have
three general functions
Page
Counting events
8
E.g. If the oscillator frequency is 11.0592MHz, the timer frequency is 11.0592/12= 921.6 kHz
The time taken count from 0 to 50,000 is = 50000 x 1/ 921.6K Hz = 0.0542 s
GATE:
Every timer has a means of starting and stopping. This may be done either by hardware or by software
controls. The hardware way of starting and stopping the timer by an external source is achieved by
making GATE = 1. If the GATE = 0, the start and stop of the timer is done by software.
C/T:
This bit is used to decide whether the timer is used as delay generator (timer) or as an event counter
(counter). If C/T is = 0, it is used as timer. If C/T =1 it is used as counter.
M1,M0:
These two bits set the mode of operation of timers
INTERRUPTS:
The interrupt is a process of data transfer by an external device that informs the CPU that it is
ready for communication and requests attention.
The Timer and Serial interrupts are internally generated by the microcontroller, whereas the external
interrupts are generated by additional interfacing devices or switches that are externally connected to the
microcontroller. The interrupts are enabled or disabled by the Interrupt Enable (IE) register:
This register is responsible for enabling and disabling the interrupt. It is a bit addressable register
in which EA must be set to one for enabling interrupts. In these five interrupts, if anyone interrupt is to be
activated, the corresponding bit must be set. The bit format of IE register is as shown below.
9
Page
10
Interrupt priority:
It is desirable to set priorities among competing interrupts that may occur simultaneously. If two interrupts
with same priority occur at the same time, then they have the following ranking:
1. IE0
2. TF0
3. IE1
4. TF1
5. Serial = RI OR TI
The priority levels of the interrupts can be changed by changing the corresponding bit in the Interrupt
Priority (IP) register. The priority of interrupts can be set in the IP register. The bit format of IP register
is as shown below.
INTERRUPT DESTINATION
Interrupt Address (Hex)
The addresslocations of various
IE0 0003 interrupts are as shown below. These
are called vectored interrupts
TF0 000B
10
IE1 0013
Page
.
TF1 001B
SERIAL 0023
11
EA (Pin 31)
External Access pin, when held high executes instruction from the internal program memory till address 0FFFH;
beyond this address, the instruction is fetched from external program memory. If this pin is low, all the
instructions are fetched from the external memory.
The heart of the 8051 is the circuitry that generates the clock pulses which are required for all internal
operations. The two Capacitors and crystal oscillator are related to the clock circuitry which
produces the system clock frequency. This crystal oscillator is used to generate clock pulses
required for the synchronization of all the internal operations. For each instruction fetching,
decoding, executing, and storing, controller requires clock pulse. (which is 1MHz to 16MHz).
One complete oscillation of the clock source is called a pulse. Two pulses forms a state and six states
forms one machine cycle. Also note that, two pulses of ALE are available for 1 machine cycle.
Program instructions may require one or more machine cycles depending upon type of instructions. The
time for execution of any instruction is calculated using the relation
Cx 12 d
T= crystalfrequency
12
I/O PORTS
13
A Port is a pin where data can be transferred between 8051 and an external device. 8051 has 4, 8 bit ports:
P0, P1, P2 and P3. Apart from being used for input / output functions some of the pins have alternate
functions.
This is an 8 bit, bit addressable input or
PORT 0 output port. P0 has a dual role. Apart
from being used as an I/O port, it is
also used as a bidirectional lower order
address and data bus for external
memory.
PORT 1:
This is an 8 bit, bit addressable input or output port. In contrast to Port 0, this port does not need any pull-
up resistors since it already has pull-up resistors internally. Upon reset, Port 1 is configured as an input
port.
If Port 1 has been configured as an output port, to make it an input port again, it must be programmed as
such by writing 1 to all its bits.
PORT 2:
This is an 8 bit, bit addressable input or output port. It has built-in pull-up resistors like in port 1. It must
first be programmed by writing 1 to all bits required to be an input.
Dual role of port 2:
In many systems based on 8051, P2 is used as simple I/O. When 8051 is connected to external memory,
P2 is used for the upper 8 bits of the external address and it cannot be used for I/O operations.
PORT 3:
Port 3 occupies a total of 8 pins. it can be used as input or output. Port 3 does not need pull-up resistors.
Although port 3 is configured as an input port upon reset, this is not the way it is most commonly used.
Port 3 has additional function of providing interrupt signals. The alternate functions of port 3 is listed
below.