Unit 3
Unit 3
The 8051 microcontroller is a Harvard architecture 8-bit microcontroller. It has a RISC (Reduced
Instruction Set Computing) architecture. It features a single accumulator, 16-bit program counter, and
8-bit data bus. It has 4KB of on-chip program memory (ROM). It also haves 128 bytes of on-chip data
memory (RAM) with a built-in memory controller.
It has four 8-bit I/O ports that can be programmed to be input or output. It also has two 16-bit
timers/counters, a serial communication port, and an interrupt controller. The architecture supports direct
addressing, register indirect addressing, and immediate addressing modes.
CPU (Central Processing Unit): CPU act as a mind of any processing machine. It synchronizes and
manages all processes that are carried out in microcontroller. User has no power to control the
functioning of CPU. It interprets the program stored in ROM and carries out from storage and then
performs it projected duty. CPU manage the different types of registers available in 8051
microcontroller.
Interrupts: Interrupts is a sub-routine call that given by the microcontroller when some other program
with high priority is request for acquiring the system buses the n interrupts occur in current running
program.
Interrupts provide a method to postpone or delay the current process, performs a sub-routine task and
then restart the standard program again.
Memory: For operation Micro-controller required a program. This program guides the microcontroller
to perform the specific tasks. This program installed in microcontroller required some on chip memory
for the storage of the program.
Microcontroller also required memory for storage of data and operands for the short duration. In
microcontroller 8051 there is code or program memory of 4 KB that is it has 4 KB ROM and it also
comprise of data memory (RAM) of 128 bytes.
Bus : Bus is a group of wires which uses as a communication canal or acts as means of data transfer.
The different bus configuration includes 8, 16 or more cables. Therefore, a bus can bear 8 bits, 16 bits
all together.
Address Bus: 8051 microcontrollers is consisting of 16 bit address bus. It is generally be used for
transferring the data from Central Processing Unit to Memory.
Data bus: 8051 microcontroller is consisting of 8 bits data bus. It is generally be used for transferring
the data from one peripherals position to other peripherals.
Oscillator:
To perform timer operation inside microcontroller it required externally connected or on-chip oscillator.
Microcontroller is used inside an embedded system for managing the function of devices. Therefore,
8051 uses the two 16 bit counters and timers. For the operation of this timers and counters the oscillator
is used inside microcontroller.
I/O Ports: It typically includes four 8-bit I/O ports (P0, P1, P2, and P3), totaling 32 I/O lines which can
be used for input or output operations. These ports are bit-addressable, offering flexibility in interfacing
with external devices.
Timers/Counters: The 8051 includes two 16-bit timers/counters (Timer 0 and Timer 1) that can be used
in various modes to perform tasks like time delays or counting external events.
Serial Communication Control: It supports serial communication through its serial port, allowing for
easy data exchange with other devices over serial protocols like RS-232.
Special Function Registers (SFRs): These registers control various aspects of the microcontroller's
operation, including I/O port configuration, timer control, serial communication control, and interrupt
handling.
Pin Diagram of 8051 Microcontroller
• VCC: This pin is connected to the power supply and provides a voltage of +5V to the
microcontroller.
• GND: This pin is connected to the ground and serves as the reference voltage for the
microcontroller.
• XTAL1 and XTAL2: These pins are used for connecting an external crystal oscillator that
provides the clock signal for the microcontroller.
• Reset: This pin is used for resetting the microcontroller. A high pulse on this pin resets the
microcontroller to its initial state.
• Port 1: This is an 8-bit bidirectional input/output port that can be used for interfacing with
external devices.
• Port 2: This is an 8-bit bidirectional input/output port that can be used for interfacing with
external devices.
• Port 3: This is an 8-bit bidirectional input/output port that can be used for interfacing with
external devices.
• Port 4: This is an 8-bit bidirectional input/output port that can be used for interfacing with
external devices.
• INT0: This is an external interrupt 0 input pin.
• INT1: This is an external interrupt 1 input pin.
• T0: This is an external timer 0 input pin.
• T1: This is an external timer 1 input pin.
• WR: This is the write signal for external memory.
• RD: This is the read signal for external memory.
• ALE: This is the address latch enable signal that is used to latch the address for external memory.
• PSEN: This is the program store enable signal that is used for accessing the program memory.
• 17-24. Address bus: These pins are used for transmitting the address information to the external
memory.
• 25-32. Data bus: These pins are used for transmitting the data between the microcontroller and
external memory.
• RST: This is the output signal that indicates the microcontroller is being reset.
• EA: This is the external access enable signal that is used for selecting the program memory.
• VPP: This pin is used for programming the microcontroller.
• 36-39. XTAL: These pins are used for connecting an external crystal oscillator.
• P1.0 (AD0) – P1.7 (AD7): These pins are used for interfacing with external analog devices.
Pin 1 to Pin 8 (Port 1): Pin 1 to Pin 8 are assigned to Port 1 for simple I/O operations. They can be
configured as input or output pins depending on the logic control i.e. if logic zero (0) is applied to the
I/O port it will act as an output pin and if logic one (1) is applied the pin will act as an input pin. These
pins are also referred to as P1.0 to P1.7 (where P1 indicates that it is a pin in port 1 and the number after
‘.’ tells the pin number i.e. 0 indicates first pin of the port. So, P1.0 means first pin of port 1, P1.1 means
second pin of the port 1 and so on). These pins are bidirectional pins.
Pin 9 (RST) : Reset pin. It is an active-high, input pin. Therefore if the RST pin is high for a minimum
of 2 machine cycles, the microcontroller will reset i.e. it will close and terminate all activities.
Pin 10 to Pin 17 (Port 3) :Pin 10 to pin 17 are port 3 pins which are also referred to as P3.0 to P3.7.
These pins are similar to port 1 and can be used as universal input or output pins. These pins are
bidirectional pins
These pins also have some additional functions which are as follows:
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.
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.
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.
Pin 20 (GND): This pin is connected to the ground. It has to be provided with 0V power supply. Hence
it is connected to the negative terminal of the power supply.
Pin 21 to Pin 28 (Port 2) : Pin 21 to pin 28 are port 2 pins also referred to as P2.0 to P2.7. When
additional external memory is interfaced with the 8051 microcontroller, pins of port 2 act as higher-
order address bytes. These pins are bidirectional.
Pin 29 (PSEN) : PSEN stands for Program Store Enable. It is output, active-low pin. This is used to
read external memory. In 8031 based system where external ROM holds the program code, this pin is
connected to the OE pin of the ROM.
Pin 30 (ALE/ PROG) :ALE stands for Address Latch Enable. It is input, active-high pin. This pin is
used to distinguish between memory chips when multiple memory chips are used. It is also used to de-
multiplex the multiplexed address and data signals available at port 0. During flash programming i.e.
Programming of EPROM, this pin acts as program pulse input (PROG).
Pin 31 (EA/ VPP): EA stands for External Access input. It is used to enable/disable external memory
interfacing. In 8051, EA is connected to Vcc as it comes with on-chip ROM to store programs. For other
family members such as 8031 and 8032 in which there is no on-chip ROM, the EA pin is connected to
the GND.
Pin 32 to Pin 39 (Port 0): Pin 32 to pin 39 are port 0 pins also referred to as P0.0 to P0.7. They are
bidirectional input/output pins. They don’t have any internal pull-ups. Hence, 10 K? pull-up registers
are used as external pull-ups. Port 0 is also designated as AD0-AD7 because 8051 multiplexes address
and data through port 0 to save pins.
Pin 40 (VCC) : This pin provides power supply voltage i.e. +5 Volts to the circuit.
The clock frequency is12MHz, so 64 instruction types are executed in just 1 µs, and rest are just 2 µs.
The Multiplication and Division operations take 4 µsto to execute.
• Immediate AddressingMode
• Register AddressingMode
• Direct AddressingMode
• Register IndirectAddressing Mode
• Indexed AddressingMode
• Implied AddressingMode
Immediate Addressing Mode
In this Immediate Addressing Mode, the data is provided in the instruction itself. The data is provided
immediately after the opcode. These are some examples of Immediate Addressing Mode.
MOVA, #0AFH;
MOVR3, #45H;
In these instructions, the # symbol is used for immediate data. In the last instruction, there is DPTR. The
DPTR stands for Data Pointer. Using this, it points the external data memory location. In the first
instruction, the immediate data is AFH, but one 0 is added at the beginning. So when the data is starting
with A to F, the data should be preceded by 0.
In 8051, there is no instruction like MOVR5, R7. But we can get the same result by using this instruction
MOV R5, 07H, or by using MOV 05H, R7. But these two instructions will work when the selected
register bank is RB0. To use another register bank and to get the same effect, we have to add the starting
address of that register bank with the register number. For an example, if the RB2 is selected, and we
want to access R5, then the address will be (10H + 05H = 15H), so the instruction will look like this
MOV 15H, R7. Here 10H is the starting address of Register Bank 2.
MOV80H, R6;
MOVR2, 45H;
MOVR0, 05H;
The first instruction will send the content of registerR6 to port P0 (Address of Port 0 is 80H). The second
one is forgetting content from 45H to R2. The third one is used to get data from Register R5 (When
register bank RB0 is selected) to register R5.
Register indirect addressing Mode
In this mode, the source or destination address is given in the register. By using register indirect
addressing mode, the internal or external addresses can be accessed. The R0 and R1 are used for 8-bit
addresses, and DPTR is used for 16-bit addresses, no other registers can be used for addressing purposes.
Let us see some examples of this mode.
MOV0E5H, @R0;
MOV@R1, 80H
In the instructions, the @ symbol is used for register indirect addressing. In the first instruction, it is
showing that theR0 register is used. If the content of R0 is 40H, then that instruction will take the data
which is located at location 40H of the internal RAM. In the second one, if the content of R1 is 30H,
then it indicates that the content of port P0 will be stored at location 30H in the internal RAM.
MOVXA, @R1;
MOV@DPTR, A;
In these two instructions, the X in MOVX indicates the external data memory. The external data memory
can only be accessed in register indirect mode. In the first instruction if the R0 is holding 40H, then A
will get the content of external RAM location40H. And in the second one, the content of A is overwritten
in the location pointed by DPTR.
This addressing mode is used to read the data from the external memory or ROM memory. All addressing
modes cannot read the data from the code memory. The code must read through the DPTR register. The
DPTR is used to point the data in the code or external memory.
In the indexed addressing mode, the source memory can only be accessed from program memory only.
The destination operand is always the register A. These are some examples of Indexed addressing mode.
The assembly language is a low-level programming language used to write program code in terms of
mnemonics.
The assembly language mnemonics are in the form of op-code, such as MOV, ADD, JMP, and so on,
which are used to perform the operations.
Op-code: The op-code is a single instruction that can be executed by the CPU. Here the op-code is a
MOV instruction.
Operands: The operands are a single piece of data that can be operated by the op-code. Example,
multiplication operation is performed by the operands that are multiplied by the operand.
The Elements of an Assembly Language Programming:
Assembler Directives
Instruction Set
Addressing Modes
Assembler Directives:
The assembling directives give the directions to the CPU. The 8051 microcontroller consists of various
kinds of assembly directives to give the direction to the control unit.
The most useful directives are 8051 programming, such as:
• ORG
• DB
• EQU
• END
ORG(origin): This directive indicates the start of the program. This is used to set the register address
during assembly. For example; ORG 0000h tells the compiler all subsequent code starting at address
0000h.
Syntax:
ORG 0000h
MOV a, #00h
————-
————-
DB”EDGEFX”
EQU (equivalent): The equivalent directive is used to equate address of the variable.
Syntax:
reg equ,09h
—————–
—————–
MOV reg,#2h
END: The END directive is used to indicate the end of the program.
Syntax:
reg equ,09h
—————–
—————–
MOV reg,#2h
END