BEC405A Module 1 Akshaya
BEC405A Module 1 Akshaya
Prepared by:
Dr. Vijaya Kumar H R
Associate Professor
Department of ECE.
Tumakuru
Microcontrollers Semester 4
8051 Microcontroller
Module 1: Introduction
1. Consumer electronics products: Toys, Camera, Robots, washing machine, Microwave ovens
2. Instrumentation and process control: Oscilloscopes, Multimeter, Leakage current tester, data acquisition and control.
3. Medical instruments: ECG, Blood measurements.
4. Communication: Cell phones, telephone sets, communication machines.
5. Office: Security system, FAX Machine, copier, printer, paging, intercom.
6. Auto: Engine control, air bag, security system, center locking system.
7. Others: Cellular phones, traffic controller, musical instruments, camera, E voting machine etc.
The "B" Register: The "B" register is very similar to the Accumulator in the sense that
it may hold an 8-bit (1-byte) value. The "B" register is used only by two 8051
instructions: MUL AB and DIV AB. To quickly and easily multiply or divide A by another
number, you may store the other number in "B" and make use of these two
instructions. Apart from using MUL and DIV instructions, the "B" register is often used
as yet another temporary storage register, much like a ninth R register.
The Program Counter: The Program Counter (PC) is a 2-byte address which tells the 8051 where the next
instruction to execute can be found in the memory. PC starts at 0000h when the 8051 initializes and is
incremented every time after an instruction is executed. PC is not always incremented by 1. Some instructions
may require 2 or 3 bytes; in such cases, the PC will be incremented by 2 or 3.
The Stack Pointer (SP): The Stack Pointer, like all registers except DPTR and PC, may hold an 8-bit (1-byte) value.
The Stack Pointer tells the location from where the next value is to be removed from the stack. When a value is
pushed onto the stack, the value of SP is incremented and then the value is stored at the resulting memory
location. When a value is popped off the stack, the value is returned from the memory location indicated by SP,
and then the value of SP is decremented.
This order of operation is important. SP will be initialized to 07h when the 8051 is initialized. If a value is pushed
onto the stack at the same time, the value will be stored in the internal RAM address 08h because the 8051 will
first increment the value of SP (from 07h to 08h) and then will store the pushed value at that memory address
(08h). SP is modified directly by the 8051 by six instructions: PUSH, POP, ACALL, LCALL, RET, and RETI.
. Dr. Vijaya Kumar H R
Associate. Professor, Dept. of ECE, AIT, Tumakuru
8051 Flag Bits and PSW Register
The program status word (PSW) register is an 8-bit register, also known as flag register. It is of 8-bit wide but only 6-
bit of it is used. The two unused bits are user-defined flags. Four of the flags are called conditional flags, which
means that they indicate a condition which results after an instruction is executed. These four
are CY (Carry), AC (auxiliary carry), P (parity), and OV (overflow). The bits RS0 and RS1 are used to change the
bank registers. The following figure shows the program status word register.
•CY, the carry flag − This carry flag is set (1) whenever there is
a carry out from the D7 bit. It is affected after an 8-bit addition or
subtraction operation. It can also be reset to 1 or 0 directly by an
instruction such as "SETB C" and "CLR C" where "SETB"
stands for set bit carry and "CLR" stands for clear carry.
•AC, auxiliary carry flag − If there is a carry from D3 and D4
during an ADD or SUB operation, the AC bit is set; otherwise, it
is cleared. It is used for the instruction to perform binary coded
decimal arithmetic.
•P, the parity flag − The parity flag represents the number of 1's
in the accumulator register only. If the A register contains odd
number of 1's, then P = 1; and for even number of 1's, P = 0.
•OV, the overflow flag − This flag is set whenever the result of
a signed number operation is too large causing the high-order bit
to overflow into the sign bit. It is used only to detect errors in
signed arithmetic operations.
Three parts:
1. 4 Register bank.
2. Bit-addressable location or Special
function registers
3. General purpose or Scratch pad
registers.
•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. It is often referred as
“power-on-reset” pin because it is used to reset the microcontroller
to it’s initial values when power is on (high).
•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 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.
.
Dr. Vijaya Kumar H R
Associate. Professor, Dept. of ECE, AIT, Tumakuru
•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 is active low pin and 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.
8051 doesn't have internal pull-up
resistor. Generally we use external pull up
resistor on Port0 of 8051 microcontroller. We
use resistor of 10k connected to pins and
VCC. Or we use 10k resistor array to do that,
the reason is because 8051 microcontroller
doesn't have internal pull up resistor on port0
Dr. Vijaya Kumar H R
Associate. Professor, Dept. of ECE, AIT, Tumakuru
Dr. Vijaya Kumar H R
Associate. Professor, Dept. of ECE, AIT, Tumakuru
Interfacing External Memory with 8051 Microcontroller
• It is always good to have an option to expand the capabilities of a Microcontroller, whether it is in terms of
Memory or IO or anything else. Such expansion will be useful to avoid design throttling. We have seen that a
typical 8051 Microcontroller has 4KB of ROM and 128B of RAM (most modern 8051 Microcontroller variants
have 8K ROM and 256B of RAM).
• The designer of an 8051 Microcontroller based system is not limited to the internal RAM and ROM present in
the 8051 Microcontroller. There is a provision of connecting both external RAM and ROM i.e., Data Memory
and Program.
• The reason for interfacing external Program Memory or ROM is that complex programs written in high – level
languages often tend to be larger and occupy more memory.
• Another important reason is that chips like 8031 or 8032, which doesn’t have any internal ROM, have to be
interfaced with external ROM.
• A maximum of 64KB of Program Memory (ROM) and Data Memory (RAM) each can be interface with the
8051 Microcontroller.
• An important point to remember when interfacing external memory with 8051 Microcontroller is that Port 0
(P0) cannot be used as an IO Port as it will be used for multiplexed address and data bus (A0 – A7 and D0 –
D7). Not always, but Port 2 may be used as higher byte of the address bus.
• In this tutorial, we have seen the 8051 Microcontroller Memory Organization, Program Memory, Data
Memory, Internal ROM and RAM and how to interface external Memory (ROM and RAM) with 8051
Microcontroller. Dr. Vijaya Kumar H R
Associate. Professor, Dept. of ECE, AIT, Tumakuru
Dr. Vijaya Kumar H R
Associate. Professor, Dept. of ECE, AIT, Tumakuru
Dr. Vijaya Kumar H R
Associate. Professor, Dept. of
ECE, AIT, Tumakuru
• The memory types are illustrated in the following graphic. They are: On-Chip Memory, External Code Memory, and External RAM.
• On-Chip Memory refers to any memory (Code (4K), RAM(128)BYTES, or other) that physically exists on the microcontroller itself. On-chip
memory can be of several types, but we'll get into that shortly.
• External Code Memory is code (or program) memory that resides off-chip. This is often in the form of an external EPROM.
• External RAM is RAM memory that resides off-chip. This is often in the form of standard static RAM or flash RAM.
• Code Memory: Code memory is the memory that holds the actual 8051 program that is to be run. This memory is limited to 64K and
comes in many shapes and sizes: Code memory may be found on-chip, either burned into the microcontroller as ROM or EPROM. Code
may also be stored completely off-chip in an external ROM or, more commonly, an external EPROM. Flash RAM is also another popular
method of storing a program. Various combinations of these memory types may also be used--that is to say, it is possible to have 4K of
code memory on-chip and 64k of code memory off-chip in an EPROM. When the program is stored on-chip the 64K maximum is often
reduced to 4k, 8k, or 16k. This varies depending on the version of the chip that is being used. Each version offers specific capabilities and
one of the distinguishing factors from chip to chip is how much ROM/EPROM space the chip has. However, code memory is most
commonly implemented as off-chip EPROM. This is especially true in low-cost development systems and in systems developed by
students. Programming Tip: Since code memory is restricted to 64K, 8051 programs are limited to 64K. Some assemblers and compilers
offer ways to get around this limit when used with specially wired hardware. However, without such special compilers and hardware,
programs are limited to 64K.
• External RAM As an obvious opposite of Internal RAM, the 8051 also supports what is called External RAM. As the name suggests,
External RAM is any random access memory which is found off-chip. Since the memory is off-chip it is not as flexible in terms of accessing,
and is also slower. For example, to increment an Internal RAM location by 1 requires only 1 instruction and 1 instruction cycle. To
increment a 1-byte value stored in External RAM requires 4 instructions and 7 instruction cycles. In this case, external memory is 7 times
slower! What External RAM loses in speed and flexibility it gains in quantity. While Internal RAM is limited to 128 bytes (256 bytes with an
8052), the 8051 supports External RAM up to 64K
Dr. Vijaya Kumar H R
Associate. Professor, Dept. of ECE, AIT, Tumakuru
•The program fetches to addresses 0000H through
OFFFH are directed to the internal ROM in the 8051
when the EA pin is attached to Vcc, and program
fetches to addresses 1000H through FFFFH are directed
to the external ROM/EPROM. When the EA pin is
grounded, all addresses fetched by the program (0000H
to FFFFH) are led to it.. ROM/EPROM that is external to
the device.
•As seen in Fig. NEXT PAGES, the PSEN signal is used to
trigger output e external ROM/EPROM. Port 0 is used
as a multiplexed address/bus, as seen in Fig.
•In the initial T-cycle, it provides a lower order 8-bit
address, and later it is used as a data bus. The external
latch and the ALE signal provided by the 8051 are used
to latch the 8-bit address.
•Remote ROM/EPROM(Read Only Memory/Electronic
Programmable ROM/Electronic Programmable
ROM/Electronic Program The PSEN signal is used to activate
output e external ROM/EPROM.
•ort 0 is used as a multiplexed address/bus. It supplies a
Dr. Vijaya Kumar H R lower-order 8-bit address in the first T-cycle and later serves
Associate. Professor, Dept. of ECE, AIT, Tumakuru as a data bus. The 8-bit address is latched using the external
latch and the ALE signal given by the 8051.
External data memory (RAM)
• External data memory is read or write memory. Since external data memory is indirectly accessed through a
data pointer register, DPTR (which must be loaded with an address), it is a slower process compared to
accessing the internal data memory.
• Note that the additional 128 bytes of memory is interfaced with RAM by the use of RD read signal, RD = 1 (is
active) when reading bytes from the external data memory (RAM). The command used to access data from
external RAM is,
p2
Dr. Vijaya Kumar H R
Associate. Professor, Dept. of ECE, AIT, Tumakuru
Dr. Vijaya Kumar H R
Associate. Professor, Dept. of ECE, AIT, Tumakuru
Dr. Vijaya Kumar H R
Associate. Professor, Dept. of ECE, AIT, Tumakuru
Dr. Vijaya Kumar H R
Associate. Professor, Dept. of ECE, AIT,
Tumakuru
Dr. Vijaya Kumar H R
Associate. Professor, Dept. of ECE, AIT, Tumakuru
Dr. Vijaya Kumar H R
Associate. Professor, Dept. of ECE, AIT, Tumakuru
Dr. Vijaya Kumar H R
Associate. Professor, Dept. of ECE, AIT, Tumakuru
END OF MODULE 1