Microcontroller 8051 Notes
Microcontroller 8051 Notes
The microcontroller has built in ROM, RAM, Input Output ports, Serial Port, timers, interrupts
and clock circuit. A microcontroller is an entire computer manufactured on a single chip. Microcontrollers are usually dedicated devices embedded within an application. For
example, microcontrollers are used as engine controllers in automobiles and as exposure and focus controllers in cameras. In order to serve these applications, they have a high
concentration of on-chip facilities such as serial ports, parallel input output ports, timers, counters, interrupt control, analog-to-digital converters, random access memory, read
only memory, etc. The I/O, memory, and on-chip peripherals of a microcontroller are selected depending on the specifics of the target application. Since microcontrollers are
powerful digital processors, the degree of control and programmability they provide significantly enhances the effectiveness of the application. The 8051 is the first
microcontroller of the MCS-51 family introduced by Intel Corporation at the end of the 1970s.
CRITERIA FOR SELECTION OF A MICROCONTROLLERIN EMBEDDED SYSTEM
Criteria for selection of microcontroller in any embedded system is as following:
(a) Meeting the computing needs of task at hand efficiently and cost effectively
Speed of operation
Packing
Power consumption
Amount of RAM and ROM on chip
No. of I/O pins and timers on chip Cost
(b) Availability of software development tools such as compiler, assembler and debugger
Bus System
A bus is a common electrical pathway between multiple devices
OR
A Bus is a set of physical connections used for communication between CPU and peripherals.
• Can be internal to the CPU to transport data to and from the ALU.
• Can be external to the CPU, to connect it to memory or to I/O devices.
Data Bus
Function of a data bus is to send data from one device to another
Data is passed in parallel or serial manner.
It is bidirectional.
Microcontroller 8051 has 8 bits of the data bus.
Address Bus
CPU needs to read an instruction (data) from a given location in memory. It is used to address memory locations and to transfer the address from CPU to Memory of the
microcontroller.
Identify the source or destination of data
Bus width determines maximum memory capacity of system
It is unidirectional.
Microcontroller 8051 has a 16-bit address bus
Control Bus
The control bus is used for sending control signals to the memory and I/O devices. It provides read or write signals to the device to indicate that the CPU is asking for information
or sending it information.
Identify the source or destination of data Data is passed in parallel or serial manner.
It is unidirectional. It is bidirectional.
Microcontroller 8051 has a 16-bit address bus Microcontroller 8051 has 8 bits of the data bus.
Program Memory
It is used for storing the hexadecimal codes of the program to be executed i.e., instructions. In 8051 Microcontroller, the code or instructions to be executed are stored in the
Program Memory, which is also called as the ROM of the Microcontroller. The original 8051 Microcontroller by Intel has 4KB of internal ROM.
Program memory is generally non-volatile memory, meaning it holds information over time without a need of power source.
Data memory, which is required for temporary data and intermediate results storage while the instructions are being executed. Data memory is volatile, meaning the data it holds
is temporary and is only maintained if the device is connected to a power source. Original Intel’s 8051 Microcontroller had 128Bytes of internal RAM. But almost all modern
variants of 8051 Microcontroller have 256Bytes of RAM.
Program Memory Data memory
The code or instructions to be executed are stored in the Program Memory. Temporary data and intermediate results are stored in the Program Memory.
The original 8051 Microcontroller by Intel has 4KB of internal ROM. The original 8051 Microcontroller by Intel has 128Bytes of RAM.
Program memory is generally non-volatile memory. Data memory is generally non-volatile memory.
The Von Neumann Architecture follows the concept of a stored-program computer concept.
The Von Neumann's architecture uses single memory for their instruction and data.
It requires single bus for both instruction and data.
The CPU is not able to read/write data and access instructions at the same time.
Instructions and data have to be fetched in sequential order limiting the operation bandwidth.
Harvard Architecture
Harvard Architecture is a modern type of computer architecture based on the relay-based model by
Harvard Mark I.
The Harvard Architecture uses two separate memories for their instruction and data.
It uses separate buses for the transfer of both data and instructions.
The CPU is able to read/write data and access instructions at the same time.
Instructions and data can be fetched simultaneously.
Microprocessor Microcontroller
A Microprocessors is the heart of a computer system. A Microcontrollers is the heart of an embedded system.
It has only a memory so its memory and I/O components It has a processor along with its internal memory and the
required to connect externally I/O components.
It has not any ROM, RAM, and other peripherals. It has ROM, RAM, and other peripherals.
Large Circuit Smaller Circuit
Not compatible with compact systems Compatible with compact systems.
Higher cost Lower Cost
High Power Consumption Low Power Consumption
Mostly don’t have power Saving features Mostly have power saving features.
Mainly present in personal computers. Mainly present in washing machines, music players, and
embedded systems.
Less number of registers. More number of registers.
Made on a silicon-based integrated chip. By product microprocessors and peripherals.
Has a high speed. Speed depends on the architecture.
Ideal for general purpose to handle
Ideal for the specific applications.
more data.
Requires more instructions Requires less instructions
Accumulator: ACC is an 8-bit special function register. It acts an operand register. Result is temporarily stored in this register. It is used in parallel I/O transfer.
B Register: B register is 8-bit SFR. This register is used in only two instructions MUL AB and DIV AB. For other operations, it can be used as a scratch pad register.
PC-Program Counter: Points to the address of next instruction to be executed from ROM
• It is 16-bit register means the 8051 can access program address from 0000H to FFFFH. (64K bytes code)
Initial value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 (0000H)
Final value 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 (FFFFH)
• Initially PC has 0000H
• ORG instruction is used to initialize the PC ORG 0000H means PC initialize by 0000H
• PC is incremented after each instruction
Program Status Word (PSW): PSW register is 8-bit SFR. It contains program status information. It is also used to select any one of the required register banks.
Stack Pointer (SP): It is 8-bit register. It is used to point the stack memory. The stack may reside in anywhere in on-chip memory. It is incremented before data is stored
during PUSH & CALL instructions. After reset SP is initialized to 07h. This causes the stack begin at location 08h.
Data Pointer (DPTR): It is 16-bit register. It may be manipulated as a 16 bit register or as two independents 8-bit registers. Its function is to hold a 16-bit address.
This register is used for external reference. DPH for Higher order 8 bits, DPL for lower order 8 bit
Port 0 to Port 3: Each port contains separate address. Using this address, User can communicate with these ports. Each port contains latch, output driver & input buffer
Serial Data Buffer: Serial data buffer contains two independent registers of a transmit buffer register and a receiver buffer register.
Timer Registers: Register pairs (TH0, TL0) & (TH1, TL1) are the two 16-bit counting registers for Timer/Counter 0 and 1 respectively
Control Registers: The special function registers IP, IE, TMOD, TCON SCON and PCON contain control and status information for interrupts, timer/counters and serial
port.
Timing & Control Unit: This unit derives a necessary timing and control signals required for the internal operations of the circuit. It derives control signals required for
controlling the external system bus. The interrupt, serial port and timer circuits are controlled by the control signals generated by timing & control unit.
Oscillator: This circuit generates the basic timing clock signal for the operation of the circuit using crystal oscillator.
Instruction Register: This register decodes the opcode of an instruction to be executed and gives information to the timing & control unit, and to generate necessary
signals for the execution of the instruction.
ALU: The Arithmetic and Logic Unit performs 8 bits arithmetic and logical operations over the operands held by the temporary registers TMP 1 and TMP 2. User cannot
access these temporary registers
SFR Register Banks: It is a set of registers, which can be addressed using their respective addresses which lie in the range 80h to FFh.
Interrupts
Interrupts are defined as requests because they can be refused (masked) if they are not used, that is when an interrupt is acknowledged. A special set of events or
routines are followed to handle the interrupts. These special routines are known as interrupt handler or interrupt service routines (ISR). These are located at a special
location in memory.
INT0 and INT1 are the pins for external interrupts.
Pin 9: RESET pin is used to set the 8051 microcontrollers to its initial values, while the microcontroller is working or at the initial start of application. To reset the
microcontroller, the reset pin must be set high for 2 machine cycles. Program Counter value will be set to 0000H after reset signal is applied.
Port Pin Alternate Signal SFR P3 Bit Description
10 RXD SBUF P3.0 Serial Data Input
11 TXD SBUF P3.1 Serial Data Output
12 𝐈𝐍𝐓𝟎 TCON.1 P3.2 External Interrupt 0
13 𝐈𝐍𝐓𝟏 TCON.3 P3.3 External Interrupt 1
14 T0 TMOD P3.4 Timer/ Counter 0 external input
15 T1 TMOD P3.5 Timer/ Counter 1 external input
16 𝐖𝐑 - P3.6 Signal for writing to external memory
17 𝐑𝐃 - P3.7 Signal for reading from external memory
Pin-20: GND
It is a ground pin. The supply is connected with respect to this ground pin.
Pin-30: ALE (Address Latch Enable) is used to demultiplex the address and data signal of port 0 (for external memory interfacing). When ALE=1, Port 0 is providing
lower order address (A0 - A7). When ALE=0, Port 0 is used as data lines.
Alternate function of this pin is 𝐏𝐑𝐎𝐆. It is a program input pulse used during on-chip flash memory programming. After reset, the 8051 checks this pin, if it is connected
to logic ‘0’ externally, it enters into flash programming mode.
Pin-31: 𝐄𝐀 (External Access) input is used to enable or disable external memory interfacing. It is low enable pin. When pin 30 is connected to ground, microcontroller
access External ROM memory. When pin30 is connected to +VCC, microcontroller access Internal ROM.
Alternate function of this pin is VPP it receives 12 V programming enable voltage during flash memory programming for chips that requires 12 V VPP.
Pin-40: VCC
It is a positive supply voltage pin. DC power supply +5V is connected to this pin.
Derivatives of 8051
Boolean Processor
The 8051 processor contains a complete Boolean processor for single-bit operations.
The internal RAM contains 128 addressable bits, and the SFR space supports up to 128 other addressable bits.
All port lines are bit-addressable, and each can be treated as a separate single-bit port.
The instructions that access these bits are not only conditional branches but also a complete set of set, clear, complement, OR, & AND instructions.
The 8051-instruction set is optimized for the one-bit operations.
The Boolean processor provides direct support for bit manipulation and testing of individual bit allows the use of single bit variable to perform logical operations
therefore 8051 can be used to solve Boolean expression. Bits may be set or cleared in a single instruction.
E.g.: CLR C means clear the carry bit SETB 20h means set the memory bit with bit address 20h.
It consists of Boolean processor which performs bit, set, test, clear and compliment.
Stack
A stack is a last in first out memory. The stack is a section of RAM used by the CPU to store information temporarily. RAM locations from 08H to 1FH can be used as
stack. Stack is used to store the data temporarily. This information could be data or an address. The register used to access the stack is called the SP (stack pointer)
register. The stack pointer in the 8051 is only 8 bits wide, which means that it can take value of 00 to FFH. When the 8051 is powered up, the SP register contains value
07H. RAM location 08 is the first location begin used for the stack by the 8051.
Instructions PUSH and POP are used for stack operations. The storing of a CPU register in the stack is called a PUSH. Loading the contents of the stack back into a CPU
register is called a POP.
When a data is to be placed on the stack, the stack pointer increments before storing the data on the stack so that the stack grows up as data is stored (pre-increment). As
the data is retrieved from the stack the byte is read from the stack, and then SP decrements to point the next available byte of stored data (post decrement).
[SP]=07 //CONTENT OF SP IS 07 (DEFAULT VALUE)
8051 has 128 bytes of internal data memory (RAM) for data storage. RAM consists of Register Banks, Stack memory to handle interrupt operations. The 7 bits (or lines)
of address bus are changed or used to address up to 27 = 128 bytes of memory. The address location ranges from 00H to 7FH. Each memory location can hold maximum
of 1 byte of data.
The 128 bytes of RAM in 8051 is divided into three blocks as shown in Fig.
1. Register Banks
The lower block (32 bytes) of RAM is used to accommodate general purpose register banks. 8051 has 4 register banks and are designated as RB0, RB1, RB2 and RB3.
Each bank has 8 general purpose registers named R0 to R7. These registers may be used to store data during arithmetic and logic operation, data movement and
manipulation, memory pointers or counter registers. Only one register bank can be accessed by 8051 at a time. Bits RS0 and RS1 of Program. Status Word (PSW) register
are used to select any one of the four registers banks.
Bit 7 – SMOD
In Power Down mode, the oscillator clock provided to the system is OFF i.e., CPU and peripherals clock remains inactive in this mode. While the microcontroller is in
Power Down mode, the state of all SFR registers and I/O ports remains unchanged. By setting it back into the normal mode, the contents of the SFR register is lost, but
the content of internal RAM is saved. Hardware reset can cancel this mode.
In Idle Mode, only the clock provided to the CPU gets deactivated, whereas the peripherals clock will remain active in this mode. Interrupt and H/W reset can cancel this
mode. Hence power saved in power-down mode is more than in idle mode.