MICROCONTROLLERS 18EE52/ 17EE52/ 15EE52
SUBJECT: MICROCONTROLLERS (18EE52 / 17EE52 / 15EE52)
Module 1 : 8051 Microcontrollers Basics
Syllabus: Definition of Microcontrollers, Difference between Microcontrollers
and Microprocessors, Embedded Processors, Block Diagram of 8051, PSW and
Flag Bits, 8051 Register Banks, Stack, Internal Memory Organization of 8051,
IO Port Usage in 8051, Types of Special Function Registers and their uses in
8051, Pins Of 8051, Memory Address Decoding, 8031/51 Interfacing With
External ROM And RAM, 8051 Addressing Modes
Definition of Microcontrollers:
A microcontroller is a small computer on a single metal-oxide-semiconductor
(MOS) integrated circuit (IC) chip.
A microcontroller contains a CPU (processor core) along with memory and
programmable input/output peripherals.
Examples of Microcontrollers are 8-bit microcontrollers are Intel 8031/8051, PIC1x
and Motorola MC68HC11 families.
Microcontrollers are used in Consumer Electronics Products such as Toys,
Cameras, Robots, Washing Machine, Microwave Ovens etc,
Instrumentation and Process Control such as Oscilloscopes, Multi-meter, Leakage
Current Tester, Data Acquisition and Control etc, Medical Instruments,
Communication, Office Equipment, Multimedia Application, Automobile
Difference between Microprocessors and Microcontrollers:
Micro Processors Microcontrollers
Microprocessor is a off chip device. Microcontroller is a on chip device.
RAM, ROM, Input output devices, RAM, ROM, Input output devices,
CPU is externally connected. CPU are internally connected.
General Purpose unit Specific purpose unit
High Cost Low Cost
High Power Consumption Low power consumption
Used for high scale applications Used for low scale applications
Von Neumann Architecture Harvard Architecture
High Processing power Low Processing Power
Relatively low in speed Relatively high speed
Access time for memory and input Access time for memory and input
devices are more devices are less
1
Page
Department of EEE, SVIT, Bangalore Prof Praveen B.R. Assistant Professor
MICROCONTROLLERS 18EE52/ 17EE52/ 15EE52
Embedded Processors:
A software inside a hardware is known as embedded processor
An embedded system has hardware, application software and Real Time
Operating system (RTOS) that supervises the application software.
An embedded system can be a Microcontroller based, software driven, and
reliable, real-time control system.
Block Diagram of 8051 Microcontroller:
Arithmetic and Logical Unit also known as CPU is a unit which performs
mathematical and logical operations.
Interrupt is a subroutine call that interrupts of the microcontrollers main operations
or work and causes it to execute any other program, which is more important at the
time of operation.
Bus is a collection of wires which work as a communication channel or medium for
transfer of Data.
An on-chip oscillator which works as a clock source for Central Processing Unit of
the microcontroller.
To connect it to other machines, devices or peripherals we require I/O interfacing
ports in the microcontroller.
Timers are used for measurement of intervals to determine the pulse width of
pulses.
Memory which is used to store the program of the microcontroller is known as READ
ONLY MEMORY (ROM).
Data memory of the 8051 is used to store data temporarily for operation is known
RANDOM ACCESS MEMORY (RAM).
8051 includes an on-chip serial port that can operate in four modes over a wide range
of frequencies.
2
Page
Department of EEE, SVIT, Bangalore Prof Praveen B.R. Assistant Professor
MICROCONTROLLERS 18EE52
52/ 17EE52/ 15EE52
Architecture of 8051 Microcontrollers:
Microcontrollers’ works on Harvard Architecture.
The Harvard architecture is computer architecture with separate storage
and signal pathways for instructions and data.
When data and code lie in different memory blocks, then the architecture is
referred as Harvard architecture
architecture. In case data and code lie in the same
memory block, then the architecture is referred as Von
Neumann architecture
architecture.
Features of 8051 Microcontroller
Microcontroller:
4KB bytes on-chip
chip program memory (ROM)
128 bytes on-chip
chip data memory (RAM)
Four register banks
128 user defined software flags
8-bit bidirectional data bus
16-bit unidirectional address bus
32 general purpose registers each of 8-bit
16 bit Timers (usually 2, but may have more or less)
Four 8-bit ports,(short
,(short model have two 88-bit ports)
16-bit program counter
ounter and data pointer
Program Status Word (PSW)
(PSW):
The Program Status Word ((PSW)) contains status bits that reflect the current CPU
state.
The program status word (PSW) register is an 8-bit register.
Although the PSW register is 8 bits wide, only 6 bits of it are used by the 8051.
The two unused bits are user
user-definable flags.
3
Page
Department of EEE, SVIT, Bangalore Prof Praveen B.R. Assistant Professor
MICROCONTROLLERS 18EE52
52/ 17EE52/ 15EE52
CY (carry flag):
This flag is set whenever there is a carry out from the D.7 bit
This flag bit is affected after 8 bit addition or subtraction
AC (Auxiliary flag):
If there is a carry from D3 to D4 during ADD or SUB operation this bit is set
otherwise it is cleared
This flag is used by instruction that perform BCD (binary coded decimal) arithmetic
P (parity flag):
The parity flag reflect
ect the number of 1's in the A (accumulator) register only
If the A register contains an odd number of 1's then p=1 therefore p=0 if A has an
even number 1's.
OV (overflow flag):
This flag is set whenever the result of a signed number operation is too larg
large causing
the high order bit to overflow into the sign bit
the carry flag is used to detect error in unsigned arithmetic operations
The overflow flag is only used to detect error in signed arithmetic operations.
Register Banks:
The bits RS0 and RS1 are used to change the bank registers.
4
Page
Department of EEE, SVIT, Bangalore Prof Praveen B.R. Assistant Professor
MICROCONTROLLERS 18EE52/ 17EE52/ 15EE52
Stacks:
Stack Memory Allocation in 8051 Microcontroller.
The stack is an area of random access memory (RAM) allocated to hold temporarily
all the parameters of the variables.
The stack is also responsible for reminding the order in which a function is called so
that it can be returned correctly.
The register used to access the stack is known as the stack pointer register.
The stack pointer in the 8051 is 8-bits wide, and it can take a value of 00 to FFH.
When the 8051 is initialized, the SP register contains the value 07H.
PUSH and POP are the two instructions used in Stack.
PUSH instruction loads the data into Stack from Accumulator and POP instruction
removes the data from stack.
Internal RAM:
There are 256 bytes of internal RAM on the 8051.
The first 128 locations (00H to 7FH) of internal RAM are used by the programmer
for storing data while the second 128 locations (80H to FFH) are the Special Function
Registers (SFRs)
Internal memory, also called "main or primary memory" refers to memory that stores
5
small amounts of data that can be accessed quickly while the computer is running.
Page
External memory, also called "secondary memory" refers to a storage device that can
Department of EEE, SVIT, Bangalore Prof Praveen B.R. Assistant Professor
MICROCONTROLLERS 18EE52/ 17EE52/ 15EE52
retain or store data persistently.
The 4 banks are named as Bank0, Bank1, Bank2 and Bank3.
Each Bank consists of 8 registers named as R0 – R7.
Each Register can be addressed in two ways: either by name or by address.
The next 16B of the RAM i.e. from 20H to 2FH are Bit – Addressable memory
locations.
There are totally 128 bits that can be addressed individually using 00H to 7FH or the
entire byte can be addressed as 20H to 2FH.
The final 80B of the internal RAM i.e. addresses from 30H to 7FH, is the general
purpose RAM area which are byte addressable.
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.
Input output Port usage in 8051:
The input/output port is a memory address used by software to communicate with
hardware on your computer.
The microcontroller 8051 has four ports P0, P1, P2 & P3.
The four ports are requiring for I/O operations.
Out of the 40 pin, 32 pins are set aside for the four ports P0, P1, P2 & P3.
Each port takes 8 pins.
Pin Configuration of 8051:
6
Page
Department of EEE, SVIT, Bangalore Prof Praveen B.R. Assistant Professor
MICROCONTROLLERS 18EE52/ 17EE52/ 15EE52
Pins 1 to 8 − these pins are known as Port 1. This port doesn’t serve any other
functions. It is internally pulled up, bi-directional I/O port.
Pin 9 − It is a RESET pin, which is used to reset the microcontroller to its initial
values.
Pins 10 to 17 − these pins are known as Port 3. This port serves some functions like
interrupts, timer input, control signals, serial communication signals RxD and TxD,
etc.
Pins 18 & 19 − these pins are used for interfacing an external crystal to get the
system clock.
Pin 20 − this pin provides the power supply to the circuit.
Pins 21 to 28 − these pins are known as Port 2. It serves as I/O port. Higher order
address bus signals are also multiplexed using this port.
Pin 29 − this is PSEN pin which stands for Program Store Enable. It is used to read a
signal from the external program memory.
Pin 30 − this is EA pin which stands for External Access input. It is used to
enable/disable the external memory interfacing.
Pin 31 − this is ALE pin which stands for Address Latch Enable. It is used to
demultiplex the address-data signal of port.
Pins 32 to 39 − these pins are known as Port 0. It serves as I/O port. Lower order
address and data bus signals are multiplexed using this port.
Pin 40 − this pin is used to provide power supply to the circuit.
Types of SFR’s in 8051:
The 8051 Microcontroller Special Function Registers are used to program and
control different hardware peripherals like Timers, Serial Port, I/O Ports etc.
In fact, by manipulating the 8051 Microcontroller Special Function Registers
(SFRs), you can assess or change the operating mode of the 8051 Microcontroller.
1. Accumulator or Register A:
It is the most important and most used 8051 Microcontroller SFRs.
The Register A is located at the address E0H in the SFR memory space.
Some of the operations where the Accumulator is used are:
i. Arithmetic Operations like Addition, Subtraction, Multiplication etc.
ii. Logical Operations like AND, OR, NOT etc.
iii. Data Transfer Operations (between 8051 and External Memory)
2. B (Register B)
The B Register is used along with the ACC in Multiplication and Division
operations.
These two operations are performed on data that are stored only in Registers A and B.
During Multiplication Operation, one of the operand (multiplier or multiplicand) is
stores in B Register and also the higher byte of the result.
7
Page
Department of EEE, SVIT, Bangalore Prof Praveen B.R. Assistant Professor
MICROCONTROLLERS 18EE52/ 17EE52/ 15EE52
In case of Division Operation, the B Register holds the divisor and also the
remainder of the result.
3. Program Status Word (PSW)
The PSW or Program Status Word Register is also called as Flag Register and is one
of the important SFRs.
The PSW Register consists of Flag Bits, which help the programmer in checking the
condition of the result and also make decisions.
4. Data Pointer (DPTR – DPL and DPH)
The Data Pointer is a 16-bit Register and is physically the combination of DPL (Data
Pointer Low) and DPH (Data Pointer High) SFRs.
The Data Pointer can be used as a single 16-bit register (as DPTR) or two 8-bit
registers (as DPL and DPH).
The DPTR Register is used by the programmer addressing external memory
5. Stack Pointer (SP)
SP or Stack Pointer points out to the top of the Stack and it indicates the next data
to be accessed.
The Stack Pointer is an 8-bit register and upon reset, the Stack Pointer is initialized
with 07H.
When writing a new data byte into the stack, the SP (Stack Pointer) is automatically
incremented by 1.
When reading data from stack, the data is retrieved from the Address in SP and after
that the SP is decremented by 1.
6. I/O Port Registers (P0, P1, P2 and P3)
The 8051 Microcontroller four Ports which can be used as Input and/or Output.
These four ports are P0, P1, P2 and P3.
7. PCON (Power Control)
The PCON or Power Control register, as the name suggests is used to control the
8051 Microcontroller’s Power Modes.
Using two bits in the PCON Register, the microcontroller can be set to Idle Mode
and Power Down Mode.
During Idle Mode, the Microcontroller will stop the Clock Signal to the ALU (CPU)
but it is given to other peripherals like Timer, Serial, Interrupts, etc.
In the Power Down Mode, the oscillator will be stopped and the power will be
reduced to 2V.
8. SCON (Serial Control)
The Serial Control or SCON SFR is used to control the 8051 Microcontroller’s
Serial Port.
Using SCON, you can control the Operation Modes of the Serial Port, Baud Rate
of the Serial Port and Send or Receive Data using Serial Port.
8
9. TCON (Timer Control)
Page
Timer Control or TCON Register is used to start or stop the Timers
Department of EEE, SVIT, Bangalore Prof Praveen B.R. Assistant Professor
MICROCONTROLLERS 18EE52/ 17EE52/ 15EE52
It also contains bits to indicate if the Timers have overflowed
10. TMOD (Timer Mode)
The TMOD or Timer Mode register or SFR is used to set the Operating Modes of
the Timers T0 and T1.
The lower four bits are used to configure Timer0 and the higher four bits are used
to configure Timer1.
11. IE (Interrupt Enable)
The IE or Interrupt Enable Register is used to enable or disable individual
interrupts.
If a bit is SET, the corresponding interrupt is enabled and if the bit is cleared, the
interrupt is disabled.
12. IP (Interrupt Priority)
The IP or Interrupt Priority Register is used to set the priority of the interrupt as
High or Low.
If a bit is CLEARED, the corresponding interrupt is assigned low priority and if the
bit is SET, the interrupt is assigned high priority.
13. SBUF (Serial Data Buffer)
The Serial Buffer or SBUF register is used to hold the serial data while
transmission or reception.
Addressing Modes in 8051:
An Addressing Mode is a way to locate a target Data, which is also called as Operand.
In 8051 there are six types of addressing modes.
1. Immediate Addressing Mode
2. Register Addressing Mode
3. Direct Addressing Mode
4. Register Indirect Addressing Mode
5. Indexed Addressing Mode
6. Implied Addressing Mode
Immediate addressing mode: In this Immediate Addressing Mode, the data is provided in
the instruction itself. The data is provided immediately after the opcode.
Ex: MOV R3, #45H
Register addressing mode: In the register addressing mode the source or destination data
should be present in a register (R0 to R7).
Ex: MOV A, R5
9
Page
Department of EEE, SVIT, Bangalore Prof Praveen B.R. Assistant Professor
MICROCONTROLLERS 18EE52/ 17EE52/ 15EE52
Direct Addressing mode: In the Direct Addressing Mode, the source or destination
address is specified by using 8-bit data in the instruction. Only the internal data
memory can be used in this mode.
Ex: MOV R2, 45H
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.
Ex: MOV @R1, 80H
Indexed addressing mode: In the indexed addressing mode, the source memory can only
be accessed from program memory only. The destination operand is always the register A.
Ex: MOVC A, @A+PC
Implied Addressing mode: In the implied addressing mode, there will be a single operand.
These types of instruction can work on specific registers only.
Ex: SWAP A
.
10
Page
Department of EEE, SVIT, Bangalore Prof Praveen B.R. Assistant Professor