MC_Module_1 (2)
MC_Module_1 (2)
Module 1
8051 Microcontroller: Microprocessor Vs Microcontroller, Embedded Systems, Embedded
Microcontrollers, 8051 Architecture- Registers, Pin diagram, I/O ports functions,
Internal Memory organization. External Memory (ROM & RAM) interfacing.
Introduction to Microcontrollers
Embedded Systems:
An embedded system is a smart system that consists of both hardware and software designed to
perform a set of tasks.
Example of embedded system
Mobile phones: These consist of many embedded systems, including GUI software and hardware,
operating systems (OS), cameras, microphones, and USB (Universal Serial Bus) I/O (input/output)
modules.
Microprocessor
Microprocessors are the core of any modern computer: they are single chip processing units
responsible for computing each operation stored in the program.
Microcontroller
A microcontroller is a small and low-cost microcomputer, which is designed to perform the
specific tasks of embedded systems.
Microprocessors are different than microcontrollers in their design.
Microprocessors have the only CPU inside them and no in-memory support while
Microcontrollers, on the other side, has CPU, RAM, ROM and other peripherals which are all
embedded on the chip.
Microprocessor Microcontroller
Since memory and I/O are connected Since memory and I/O are present together,
externally, the circuit becomes large in size. the internal circuit is small in size.
It is not suitable for devices that run on It can be used on devices that use stored
stored power since total power consumption power since total power consumption is
is high due to external components. low due to less external components.
Complex and expensive due to a large Simple and inexpensive due to less number
number of instructions to process. of instructions to process.
Microcontroller Components:
Types of Memory:
Three types of memories are commonly used in modern microcontrollers.
1. Random Access Memory (RAM)
• It is a volatile memory, which means it can only be accessed when the system
is powered-up.
• It has a faster access time, but this is also expensive when compared with the
other ones.
• It is mainly used to store temporary data, variables, and constants during the
execution of user instructions.
2. Electrically Erasable Programmable Read-Only Memory (EEPROM)
• It is a non-volatile memory, which means that contents in such a memory
remain intact even when the power is removed.
• The main use of EEPROM in the microcontroller is to store user instructions
and data.
• The microcontroller then accesses the instructions one-by-one and
sequentially executes them.
3. Flash Memory
• Flash Memory is also non-volatile memory.
• Unlike EEPROM, it can only program a whole sector or block of bytes.
Central Processing Unit (CPU):
CPU is a component of the microcontroller that fetches instructions from the memory,
decodes them, and then executes them.
1. Control Unit
• A control unit controls the operations of all parts of the computer but it does
not carry out any data processing operations.
• Some main functions of the control unit are listed below:
o Controlling of data and transfer of data and instructions is done by the
control unit among other parts of the computer.
o The control unit is responsible for managing all the units of the
computer.
o The main task of the control unit is to obtain the instructions or data
which is input from the memory unit, interprets them, and then directs
the operation of the computer according to that.
o The control unit is responsible for communication with input and
output devices for the transfer of data or results from memory.
o The control unit is not responsible for the processing of data or storing
data.
2. ALU (Arithmetic Logic Unit)
• ALU (Arithmetic Logic Unit) is responsible for performing arithmetic and
logical functions or operations.
• It consists of two subsections, which are:
1. Arithmetic Section
2. Logic Section
3. Registers
• Registers are temporary storage areas within a CPU.
• They are used to store the information about the program flow, program
status, instruction results, and the hardware configuration.
• They are much faster to access than the memory.
• This is the single main reason why CPU prefers registers over the memory for
operations involving immediate data access.
4. Clock
• You need a Clock Signal which can synchronize each activity of the CPU with
a rising or falling edge.
• Moreover, the frequency of the clock signal will specify the execution period
for each instruction.
• Higher the frequency, lower the execution time.
5. I/O Ports
• Input-Output ports in microcontroller to read dynamic user inputs, or to
control external devices.
• I/O Ports provide the user input data from input devices to the CPU.
• I/O Ports output the desired information to the user by interfacing with the
LCD or LED display.
• The 32-bit microcontroller uses the 32-bit instructions to perform the arithmetic and
logic operations.
• These are used in automatically controlled devices including implantable medical
devices, engine control systems, office machines, appliances, and other types of
embedded systems.
Microcontroller Types based on Memory
• Based on memory, microcontrollers are divided into two types:
1. memory microcontrollers and
2. embedded memory microcontrollers.
1. External Memory: In the case of external memory microcontrollers, memory
is not interfaced within the microcontroller.
Rather, the user needs to connect an external memory to store
instructions/data.
2. Embedded Memory: For embedded memory microcontrollers, memory is
embedded inside the microcontroller.
Microcontroller Types based on the Instruction set
• Based on instruction set, microcontrollers are classified into two types i.e CISC-CISC
and RISC-RISC.
1. Complex instruction set computer (CISC): CISC computers have small
programs. It has a huge number of compound instructions, which takes a long
time to perform.
2. Reduced instruction set computer (RISC): RISC is a computer that only uses
simple commands that can be divided into several instructions that achieve
low-level operation within a single CLK cycle.
RISC CISC
It is a hard wired unit of programming in the RISC Microprogramming unit in CISC Processor.
Processor.
It requires multiple register sets to store the It requires a single register set to store the
instruction. instruction.
RISC has simple decoding of instruction. CISC has complex decoding of instruction.
Uses of the pipeline are simple in RISC. Uses of the pipeline are difficult in CISC.
It uses a limited number of instruction that requires It uses a large number of instruction that
less time to execute the instructions. requires more time to execute the instructions.
It uses LOAD and STORE that are independent It uses LOAD and STORE instruction in the
instructions in the register-to-register a program's memory-to-memory interaction of a program.
interaction.
RISC has more transistors on memory registers. CISC has transistors to store complex
instructions.
The execution time of RISC is very short. The execution time of CISC is longer.
RISC architecture can be used with high-end CISC architecture can be used with low-end
applications like telecommunication, image applications like home automation, security
processing, video processing, etc. system, etc.
The program written for RISC architecture needs to Program written for CISC architecture tends
take more space in memory. to take less space in memory.
Example of RISC: ARM, PA-RISC, Power Examples of CISC: VAX, Motorola 68000
Architecture, Alpha, AVR, ARC and the SPARC. family, System/360, AMD and the Intel x86
CPUs.
• Ports:
o The 8051 microcontroller has four parallel I/O ports, each of 8-bits.
o So, it provides the user 32 I/O lines for connecting the microcontroller to the
peripherals.
o The four ports are P0 (Port 0), P1(Port1) ,P2(Port 2) and P3 (Port3).
o Upon reset all the ports are output ports.
o In order to make them input, all the ports must be set i.e a high bit must be
sent to all the port pins.
• Special function Registers:
o A set of special function registers, which can be addressed using their
respective addresses which lie in the range 80H to FFH .
• System Timing and Control Device
o The interrupt, serial port and timer units control and perform their specific
function under the control of the timing and control unit.
Pin Diagram of 8051:
• 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.
IO Port Functions:
• 8051 microcontrollers have 4 I/O ports each of 8-bit, which can be configured as input
or output.
• Hence, total 32 input/output pins allow the microcontroller to be connected with the
peripheral devices.
• Pin configuration, i.e. the pin can be configured as 1 for input and 0 for output as per
the logic state.
Port 0:
• Physical Address of port 0 is 80H
Construction:
• Port 0 has a D-type latch, unidirectional buffer, and 2 FETs at each pin.
• It does not have an internal pull-up resistor. An external pull-up resistor is needed
when Port 0 is defined as an output port.
Working of port 0
• To configure port 0 as an input port the internal bus writes 1 to the D flip flop and
the control pin is set to 0(Upper FET is OFF).
• The mux is connected to Q'(0) of the D flip flop as the control pin is 0. Due to this, the
pin is connected to the input buffer which can be read to get the input data.
Port 0 of the 8051 has two main functions:
1) To be used as a simple input-output port and
2) To access external memory in conjunction with Port 2.
2. When we use Port 0 as an input port, the internal latch should know that it’s
being used for input, and thus, a digital 1 (FFH) is written at the port address
of 80H.
3. This turns off the transistors causing the pin to float in high impedance state
connecting it to the input buffer.
4. We can read data from ‘Read Pin Data’/’Read Latch Bit.’
• Port 0 use as output:
1. When we use Port 0 as an output port, the latch programmed to 0 will turn on.
Consequently, the FET will connect to GND.
2. We will require an external pull up resistor (10k Ohm) here to give a logic ‘1’
for using Port 0 as an output port.
Port 1
• Address is 90H
Construction:
• Port 1 has one D latch, two unidirectional buffers, 1 FET, and one internal pull-up
resistor at each pin.
• It has only one function – to act as an Input-Output port.
The function of Port 1 – I/O port:
Input:
• When Port 1 is functioning in the capacity of an input port, a digital ‘1’ (FFH) is
written to the latch.
• At 90H. This turns off the transistor, and the pin floats in a high impedance state.
Consequently, it connects to the input buffer.
Output:
• When Port 1 is functioning in the capacity of an output port, the latch is given a
‘LOW’ signal (00H).
• This turns the FER (Field Effect Transistor) o. The pull-up resistor is OFF, and the
port is used as an output port.
Port 2
• Address is 10H
Construction:
• Port 2 has a D type latch, 1 FET, an internal pull-up resistor, two unidirectional
buffers, and a Control Logic block.
• Its main functions are kind of similar to those of Port 0. It can be used as an input-
output port. And can access external memory in conjunction with Port 0.
Functions of Port 2
I/O port:
• Similar to Port 0. The only difference here is that in Port 2, we use one FET with an
internal pull-up resistor instead of the two FETs we saw in Port 0.
Memory Access:
• Port 2 is used in conjunction with Port 0 to generate the upper address of the external
memory location that needs to be accessed.
• However, one key difference is that it doesn’t need to turnaround and get a 1 in the
latch immediately for input as in Port 0. It can remain stable.
Port 3
• Address is B0H
Construction:
• The third Port of 8051 has a D-type latch. In addition to that, it has three
unidirectional buffers.
• A FET with an internal pull-up resistor. Additionally, it also has a NAND gate
connected to the FET.
• Port 3 performs two main functions, as we will see below.
Functions of Port 3:
I/O port:
• Just like Port 2, Port 3 can function as an input-output port.
Alternate SFR function:
• The input to SFR 1, we get the output of latch as 1, which turns on the NAND gate,
and depending on the value of ‘Alternate Output Pin,’ FET will be wither ON/OFF.
1. RXD: this is used for a serial input port
2. TXD: this is used for serial output port
3. INT0: this used for an external interrupt 0
4. INT1: this used for external interrupt 1
5. T0: Timer 0 external input
6. T1: Timer 1 external input
7. WR: external data memory write strobe
8. RD: external data memory Read strobe
• The program status word (PSW) register is an 8-bit register, it is also referred to as
the flag register
• The PSW register is 8-bit wide only 6-bit of it are used by the 8051,
• Two unused bits are user-definable flags,
• Four of the flags are called conditional flags, meaning that they indicate some
conditions that result after an instruction is executed. They are,
• Carry flag (CY)
After ALU operation, Cy = 1, if carry generate from bit 6 to bit
7
Cy=0, if no carry generate.
• Auxiliary carry(AC)
AC=1, if carry generate from bit 3 (lower nibble) to bit 4( upper
nibble). AC =0, If no carry is generate.
• Parity(P)
After ALU operation, result contain odd number of 1’s then
P=1, otherwise P=0.
• Overflow(OV)
After ALU operation,
• The remaining two bits PSW.3 and PSW.4 are designated as RS0 and RS1
register selection bit and are used to change the bank register.
• The 32, 8-bit registers are divided into four groups of 8 registers each,
called register banks.
a. If RS1=0, RS0=0, then the register bank selected is register bank 0.
b. If RS1=0, RS0=1, then the register bank selected is register bank 1.
c. If RS1=1, RS0=0, then the register bank selected is register bank 2.
d. If RS1=1, RS0=1, then the register bank selected is register bank 3.
Memory Organization:
The 8051 microcontroller’s memory is divided into Program Memory and Data Memory.
• Program Memory (ROM) is used for permanent saving program being executed.
• Data Memory (RAM) is used for temporarily storing and keeping intermediate
results and variables.
Program Memory (ROM):
• The 8051 microcontroller has 4kB of on chip ROM but it 8051 Microcontroller
can be extended up to 64kB.
• This ROM is also called program memory or code memory.
• The CODE segment is accessed using the program counter (PC) for opcode
fetches and by DPTR for data.
• The external ROM is accessed when the EA(active low) pin is connected to ground
or the contents of program counter exceeds 0FFFH.
• When the Internal ROM address is exceeded the 8051 automatically fetches the
code bytes from the external program memory.
1. Four register banks (Bank0,Bank1, Bank2 and Bank3) each of 8bits (total 32 ).
• The default bank register is Bank0.
• The remaining Banks are selected with the help of RS0 and RS1 bits of
PSW Register.
• 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 to R7 register can be addressed by its
name or by its RAM address.
• Memory locations from 80H to FFH, or the upper 128B of the RAM, are
reserved for Special Function Registers (SFRs).
• Specific 8051 Microcontroller functionalities are controlled via SFRs.
• I/O Port Registers (P0, P1, P2, and P3), PSW (Program Status Word), A
(Accumulator), IE (Interrupt Enable), PCON (Power Control), and other
SFRs are a few examples of SFRs.
• Memory addresses in SRFs can only be accessed directly.
• The addresses between 80H and FFH cannot be used as additional RAM space
even though some of them have no SFR assigned to them.
• There may be an additional 128B of RAM in some microcontrollers, which
shares the memory address (80H to FFH) with SFRs.
• However, the only way to reach this extra RAM block is through indirect
addressing.
5. Cost-effective:
o External memory devices such as RAM and ROM are relatively inexpensive,
making it cost-effective to interface them with the microcontroller instead of
using more expensive on-chip memory.