× back

Microcontroller

8051 Microcontroller: Introduction and History

Difference Between Microprocessor and Microcontroller

Microprocessors and microcontrollers are fundamental components in the field of embedded systems and computing.

Features of 8051 Microcontroller

The 8051 microcontroller is widely used in embedded systems due to its numerous features and versatility. This transcript outlines its key features and applications.

Pin Diagram of 8051 Microcontroller

Block Diagram of 8051 Microcontroller

Important parts of 8051 Microcontroller

ALU

The Arithmetic and Logic Unit (ALU) is a part of the CPU. The 8051 microcontroller has an 8-bit ALU responsible for executing instructions along with arithmetic and logical operations. All arithmetic and logical operations inside the CPU are performed by the ALU.

Here are some examples for better understanding:

  • ADD A, R1: Adds the accumulator A with register R1, and the result is stored in A.
  • CPL P0.3: Complements bit 3 of port 0, demonstrating that the ALU can perform single-bit operations.

Accumulator

The accumulator (A) is an 8-bit register used for most arithmetic and logical operations in the 8051 microcontroller. Examples include:

  • ADD A, R0: Adds the accumulator A with register R0, and the result is stored in A.
  • ANL A, R1: Performs a logical AND operation between A and R1, and stores the result in A.

B Register

The B register is an 8-bit register dedicated to multiplication and division instructions in the 8051 microcontroller. Examples include:

  • MUL AB: Multiplies A and B, storing the 16-bit result in A (lower byte) and B (higher byte).
  • DIV AB: Divides A by B, storing the quotient in A and the remainder in B.

Program Counter

  • The program counter (PC) is a 16-bit register that points to the address of the next instruction to be executed.
  • The 8051 microcontroller follows the Harvard architecture, with separate memory for program (ROM) and data (RAM). The PC automatically increments after each instruction is fetched.

Data Pointer

  • The Data Pointer (DPTR) is a 16-bit register used to point to data in RAM.
  • It is divided into two 8-bit registers: DPH (higher byte) and DPL (lower byte). The DPTR can also be used as a pointer for a lookup table in ROM.

Stack Pointer

  • The stack pointer (SP) is an 8-bit register that holds the address of the top of the stack.
  • The stack operates on a Last In, First Out (LIFO) basis. The SP points to internal RAM addresses ranging from 00H to 7FH and is used with push and pop instructions, as well as for subroutine and interrupt handling.

Program Status Word (PSW)

  • The Program Status Word (PSW) is an 8-bit register that indicates the status of the program, such as carry, overflow, and other flags.
  • The PSW is bit-addressable, allowing bit-by-bit operations as well as operations on the entire register. The PSW flags change after each instruction execution.

Flag Register of 8051 Microcontroller

The flag register, also known as the Program Status Word (PSW), is an 8-bit register used to indicate the status of the processor. The PSW contains flags that represent the result of arithmetic and logical operations. The 8051 microcontroller's flag register is divided into various flags, each serving a specific purpose. Let's discuss each flag with simple examples:

Carry Flag (CY)

  • This flag is set if there is a carry out from the most significant bit (MSB) in an arithmetic operation. It is also affected by rotate operations.
  • Example: Addition of two 8-bit numbers resulting in a carry
    ; A = 1101 0101 (0xD5)
    ; B = 1001 0011 (0x93)
    ; A + B = 1 0110 1100 (0x1B8)
    ; Result = 0110 1100 (0x6C), CY = 1 (carry out from MSB)

Auxiliary Carry Flag (AC)

  • This flag is set if there is a carry out from the 4th bit (low nibble) to the 5th bit (high nibble) in an arithmetic operation. It is used in BCD (Binary-Coded Decimal) arithmetic operations.
  • Example: Addition of two BCD numbers
    ; A = 0000 1001 (0x09)
    ; B = 0000 0111 (0x07)
    ; A + B = 0001 0000 (0x10)
    ; Result = 0001 0000 (0x10), AC = 1 (carry out from low nibble)

Parity Flag (P)

  • This flag is set if the number of 1-bits in the accumulator is even. It is cleared if the number of 1-bits is odd. It reflects the parity of the accumulator.
  • Example: Checking the parity of a value
    ; A = 0000 0011 (0x03, two 1s, even parity)
    ; P = 1

Overflow Flag (OV)

  • This flag is set if there is a signed overflow, i.e., when the result of an arithmetic operation exceeds the range of -128 to +127.
  • Example: Addition causing overflow
    ; A = 0111 1111 (0x7F)
    ; B = 0000 0001 (0x01)
    ; A + B = 1000 0000 (0x80)
    ; Result = 1000 0000 (0x80), OV = 1 (signed overflow)

General Purpose Flag (F0)

  • This is a user-defined flag that can be set or cleared by software. It does not affect the operation of the ALU.
  • Example: Using general-purpose flag F0
    ; Set flag F0
    SETB PSW.5
    ; Clear flag F0
    CLR PSW.5
                            

Register Bank Select Flags (RS1 and RS0)

  • These flags are used to select one of the four register banks (Bank 0, Bank 1, Bank 2, Bank 3) in the internal RAM of the 8051 microcontroller.

  • Example: Selecting Register Bank 2
    ; Select Register Bank 2
    SETB PSW.4 ; RS1 = 1
    CLR PSW.3 ; RS0 = 0
    ; This selects Register Bank 2

Interrupts in 8051

Interrupts play a vital role in microcontroller systems like the 8051, allowing the processor to respond to external events promptly. This lecture delves into the intricacies of interrupts in the 8051 microcontroller, focusing on their types, vector addresses, priority management, and control mechanisms using the IE and IP registers.

Types of Interrupts in 8051:

Interrupts in the 8051 microcontroller help manage tasks efficiently.

  • Hardware Interrupts: These interrupts are triggered by external hardware events connected to the microcontroller.
    • Example: Imagine an 8051 microcontroller used in a security system. When a motion sensor connected to the microcontroller detects movement, it sends a signal to trigger a hardware interrupt. The microcontroller can then respond immediately to handle the security event, such as sounding an alarm or capturing images.
  • Software Interrupts: These interrupts are initiated by software instructions within the program running on the microcontroller.
    • Example: In an embedded system application, the software may need to perform periodic tasks such as updating a display or processing user inputs. A software interrupt can be programmed to occur at specific intervals to trigger these tasks. For instance, a software timer interrupt can be set up to update a real-time clock display every second.

Interrupt Service Routine (ISR) Execution:

When an interrupt occurs, the processor suspends the current program execution and jumps to the corresponding ISR location. The ISR handles the interrupt-specific tasks and then returns to the main program using the RETI (Return from Interrupt) instruction. The main program's address is stored on the stack during ISR execution.

Interrupt Enable (IE) Register:

The IE register controls the enable/disable status of interrupts in the 8051 microcontroller. It consists of several bit-addressable flags:

  • EA (Enable All): Enables all interrupts when set to 1.
  • ES (Enable Serial): Enables the serial communication interrupt (RI/TI).
  • ET1 (Enable Timer 1), EX1 (Enable External Interrupt 1), EX0 (Enable External Interrupt 0): Enable specific interrupts individually.

Enabling interrupts allows the processor to respond to external events based on their priority and handling requirements.

Interrupt Priority (IP) Register

The IP register manages interrupt priorities in the 8051 microcontroller. It assigns higher priority to certain interrupts based on bit settings:

  • PX1 (Priority for External Interrupt 1), PT1 (Priority for Timer 1), PT0 (Priority for Timer 0), PS (Priority for Serial): Setting these bits to 1 assigns higher priority to the corresponding interrupts.
Adjusting interrupt priorities ensures critical tasks are handled promptly, enhancing system responsiveness and efficiency.

Controlling Interrupts:

By configuring the IE and IP registers, developers can control interrupt behavior in the 8051 microcontroller. Enabling specific interrupts and assigning priorities facilitate seamless event handling, optimizing system performance in real-time applications.

References