Introduction to 8051 Microcontroller Architecture and Programming

8051 Microcontroller Architecture

izS8+bC1cLL54n+z855rdSIHrn0AAAAABJRU5ErkJggg== The 8051 microcontroller includes the following components:

Processor Components

  • Arithmetic and Logic Unit (ALU)
  • Oscillator Timing & Control Unit
  • Program and Data Memory
  • Instruction Decoder
  • Registers (Accumulator, Status Register (PSW), Data Pointer, etc.)
  • Serial Port Data Buffer
  • Timer Block
  • Instruction Register
  • Program Address Register
  • RAM Address Register

ALU

The ALU performs arithmetic and logical operations. The accumulator is an 8-bit register, and one of the operands in arithmetic and logical operations is stored in the A register.

Oscillator

The oscillator provides the clock signal for the microcontroller.

Timing and Control Unit

This unit generates control signals required for communication between the processor and peripherals.

Program Memory

Program memory stores the program code. The 8051 typically uses ROM/EPROM, with 4 Kbytes of internal ROM.

Data Memory

Data memory stores data. The 8051 has 128 bytes of internal RAM, and external RAM can be added.

Stack Pointer

The 8-bit stack pointer contains the address of the data item on top of the stack. It is incremented before data is stored and initialized to 07H after reset.

Program Counter

The 16-bit program counter specifies the address of the next instruction to be executed. After reset, the PC is set to 0000H. The 8051 fetches instructions one byte at a time and increments the PC by 1 after each fetch.

Special Function Register

On-chip RAM locations from 80H to FFH are reserved for special functions. These locations can be read and written by the user.

Data Pointer (DPTR)

The DPTR consists of two 8-bit registers: DPH (high byte) and DPL (low byte). It forms a 16-bit address used to provide address information for external program memory and external data memory.

Serial Port Data Buffer

This register is used for serial data transfer and consists of a Transmit Buffer and a Receive Buffer.

Timer Block

The 8051 has two 16-bit registers for timing and counting: TMOD (Timer Mode) and TCON (Timer Control). These registers allow for pulse width measurement (0-65ms), time delay generation, and time interval generation.

Instruction Decoder and Control

When an instruction is fetched, it is loaded into the instruction register. The decoder decodes the instruction and establishes the sequence of events to follow.


Addressing Modes of 8051

The 8051 microcontroller supports several addressing modes:

Immediate Addressing Mode

The data is provided in the instruction itself, immediately after the opcode. Example: MOV R3, #45H;

Register Addressing Mode

The source or destination data is present in a register (R0 to R7). Example: MOV A, R5;

Direct Addressing Mode

The source or destination address is specified using an 8-bit data value in the instruction. Only internal data memory can be accessed in this mode. Example: MOV R2, 45H;

Register Indirect Addressing Mode

The source or destination address is stored in a register. Both internal and external addresses can be accessed. R0 and R1 are used for 8-bit addresses, and DPTR is used for 16-bit addresses. Example: MOV @R1, 80H

Indexed Addressing Mode

The source memory can only be accessed from program memory. The destination operand is always the accumulator (A register). Example: MOVC A, @A+DPTR;

Implied Addressing Mode

There is only a single operand. Example: RLA;


3f, 06, 5b, 4f, 66, 6d, 7d, 07, 7f, 67


Interfacing a Stepper Motor with the 8051 Microcontroller

A stepper motor is an electromechanical device that converts electrical pulses into discrete mechanical movements or steps. Unlike conventional motors that rotate continuously, stepper motors move in fixed angular increments. This characteristic makes them ideal for applications requiring precise positioning and speed control.

Stepper Motor Basics

  • Step Angle: The minimum degree of rotation for a single step (e.g., 0.9 or 1.8 degrees).
  • Steps per Revolution: The number of steps required for a full 360-degree rotation (e.g., 400 or 200 steps).
  • Speed Control: The motor’s speed is determined by the time delay between consecutive steps.
  • Advantages: Precise positioning, easy control of direction and movement, holding torque.

Construction

A typical stepper motor consists of:

  • Rotor (Shaft): The rotating component, often a permanent magnet.
  • Stator: The stationary component surrounding the rotor, typically with four windings paired and center-tapped (four-phase or unipolar stepper motor).

Calculating Steps

  • Steps per Revolution = 360° / Step Angle
  • Example: For a step angle of 2°, the steps per revolution would be 180 (360° / 2° = 180).
  • Steps per Second = RPM x Steps per Revolution / 60

Example Code: Controlling Stepper Motor Direction with a Switch

This C code demonstrates how to control the direction of a stepper motor connected to an 8051 microcontroller using a switch connected to pin P2.7. If the switch is pressed (SW=0), the motor rotates clockwise; if the switch is not pressed (SW=1), the motor rotates counterclockwise.

#include <reg51.h>

void delay(void);
sbit SW = P2^7;

void main() {
    SW = 1; // Configure SW as input pin

    while(1) {
        if(SW == 0) {
            P1 = 0x06; 
            delay();
            P1 = 0x03;
            delay();
            P1 = 0x09; 
            delay();
            P1 = 0x0C; 
            delay(); 
        }

        if(SW == 1) {
            P1 = 0x06; 
            delay();
            P1 = 0x0C; 
            delay();
            P1 = 0x09; 
            delay();
            P1 = 0x03; 
            delay();
        }
    } 
}

void delay(void) {
    unsigned char cnt, cnt1;
    for(cnt = 0; cnt < 255; cnt++) {
        for(cnt1 = 0; cnt1 < 255; cnt1++);
    }
}

Example Code: Rotating Stepper Motor 640 Steps Clockwise

This code snippet demonstrates how to rotate a stepper motor 640 steps clockwise, assuming a step angle of 2 degrees.

#include <reg51.h>

void delay(void);

void main() {
    unsigned char step;

    // Calculate the number of steps needed for 640 degrees of rotation
    // Assuming a step angle of 2 degrees: 640 / 2 = 320 steps

    for (step = 0; step < 320; step++) {
        // Code to generate one clockwise step
        // ...
        delay(); // Adjust delay for desired speed
    }
}

void delay(void) {
    // Delay function implementation
    // ...
}

Interfacing a DC Motor with the 8051 using an L293 Driver

QCRsyI8kTIIOzRKHJW3T5lyAWtqzwIaxsqVxZB0zsfzPfvYsbrn1VpETxpWfyZfLvl4S1qTOnN5aXtdyqDw88P9FnYLIbRbd7wAAAABJRU5ErkJggg==

The L293 is a popular integrated circuit (IC) designed to drive DC motors. It acts as a dual H-bridge, allowing for bidirectional control of two DC motors simultaneously. Each H-bridge consists of four transistors arranged in a way that enables the motor to rotate in both directions.

L293 Features

  • Quadruple Half H-Bridge: Can drive two DC motors independently.
  • Current Capacity: 600mA per channel.
  • Supply Voltage: 4.5V to 36V DC.
  • Widely Available: Common and readily available IC.

Interfacing with 8051

The L293 can be easily interfaced with the 8051 microcontroller to control the speed and direction of DC motors. The following code provides an example of how to interface an L293 with an 8051 to control a single DC motor.

#include <reg51.h>

sbit in1 = P2^0;
sbit in2 = P2^1;
sbit en1 = P2^2;
sbit en2 = P2^3;
sbit in3 = P2^4;
sbit in4 = P2^5;

void forward();
void reverse();

void main() {
    en1 = 1;
    en2 = 1;

    while(1) {
        forward();
        reverse();
    } 
}

void forward() {
    in1 = 1;
    in2 = 0; 
}

void reverse() {
    in3 = 0;
    in4 = 1; 
}

Note: This code provides a basic framework. You will need to adjust pin connections, delay values, and potentially add PWM (Pulse Width Modulation) control for speed regulation based on your specific hardware configuration and application requirements.