Elective Microprocessor Based Design

Microprocessor Based DesignUnit 111 min read

Microcontrollers: Basics, Architecture & Applications

Unit 1 of Microprocessor Based Design introduces microcontrollers (MCUs), their core components, differences from microprocessors, and real-world applications in embedded systems. Covers architecture, programming models, and design considerations for students preparing for TU exams.

TAKEAWAYS:

  • A microcontroller is a self-contained system-on-chip combining CPU, memory, I/O, and peripherals, unlike microprocessors that require external components.
  • Key components include CPU, memory (RAM/ROM), I/O ports, timers, ADC, UART, and interrupts, all integrated on a single chip.
  • Microcontrollers are energy-efficient, cost-effective, and ideal for real-time control in embedded systems, while microprocessors excel in general-purpose computing.
  • Programming models (Harvard vs. von Neumann) and instruction sets (RISC vs. CISC) define how MCUs execute tasks and interact with hardware.
  • Applications span from home automation (eSewa’s payment terminals) to medical devices (pulse oximeters) and automotive systems (engine control units).
  • Exam focus: Compare MCUs vs. MPs, explain architecture diagrams, and solve problems involving clock speed, memory types, and I/O configurations.

1. What is a Microcontroller?

A microcontroller (MCU) is a compact, integrated circuit designed to perform specific tasks in embedded systems. Unlike general-purpose microprocessors (MPs), MCUs include:

  • CPU core (for executing instructions)
  • Memory (RAM for volatile data, ROM/Flash for programs)
  • Peripheral interfaces (timers, ADC, UART, SPI, I2C)
  • Clock and power management

Why Use Microcontrollers?

Feature Microcontroller (MCU) Microprocessor (MP)
Integration All components on a single chip Requires external memory, I/O chips
Power Consumption Low (mA to µA) Higher (mA to A)
Cost Low (ideal for mass production) Higher (general-purpose)
Use Case Embedded systems (sensors, actuators) PCs, servers, high-performance computing
Programming Assembly/C (direct hardware control) High-level languages (C++, Java)

2. Core Components of a Microcontroller

02467Bit 71 bitsBit 61 bitsBit 51 bitsBit 41 bitsBit 31 bitsBit 21 bitsBit 11 bitsBit 01 bits
8-bit Port Structure (e.g., P1 Port)

A. Central Processing Unit (CPU)

  • Executes instructions from memory.
  • Features:
    • ALU (Arithmetic Logic Unit): Performs math/logic operations.
    • Control Unit: Manages instruction fetch-decode-execute cycle.
    • Registers: Temporary storage (e.g., Accumulator, Program Counter (PC), Stack Pointer (SP)).

B. Memory

Type Function Example (8051)
ROM Stores firmware (program instructions) 4KB (mask ROM)
RAM Volatile memory for runtime data 128–256 bytes
EEPROM Non-volatile, rewritable (for settings) 256 bytes
Flash Reprogrammable (modern MCUs) 32KB–1MB+

Worked Example: Memory Allocation in an 8051 An 8051 MCU has:

  • 4KB ROM (for program storage)
  • 128 bytes RAM (for variables)
  • 256 bytes EEPROM (for user settings like a thermostat’s target temperature).

If a program uses 512 bytes of RAM for sensor data and 3KB of ROM for code, calculate the remaining memory:

  • Remaining RAM: 128 – 512 = Error! (Overuse → Exam Tip: Always check memory limits.)
  • Remaining ROM: 4096 – 3072 = 1024 bytes (for future expansions).

C. Input/Output (I/O) Ports

MCUs communicate with external devices via parallel (ports) or serial (UART, SPI, I2C) interfaces. Example: 8051 Ports

  • Port 0 (P0): 8-bit bidirectional (open-drain, needs pull-ups).
  • Port 1 (P1): 8-bit quasi-bidirectional (push-pull).
  • Port 2 (P2): 8-bit (used for higher address lines in expanded memory).
  • Port 3 (P3): Multiplexed (I/O + serial communication).

Mermaid Diagram: 8051 Port Functions

Port 0Open-drain, Data/Address BusPort 1Push-pull, General I/OPort 2High Address Lines (Memory Expansion)Port 3Multiplexed (I/O + Serial/Interrupts)
8051 Port Functions: Usage and Characteristics

3. Programming Models: Harvard vs. von Neumann

Feature Harvard Architecture von Neumann Architecture
Memory Separation Program data and instructions in separate memory Single memory for both
Speed Faster (parallel access) Slower (sequential access)
Example MCUs 8051, AVR (Arduino), PIC ARM Cortex-M (some models)
Use Case Embedded systems (real-time control) General-purpose computing

Real-World Example: Arduino (AVR MCU) Arduino uses Harvard architecture, allowing simultaneous access to program and data memory. This is why it can read a sensor (ADC) while executing code without delays.


4. Instruction Sets: RISC vs. CISC

Feature RISC (Reduced Instruction Set) CISC (Complex Instruction Set)
Instruction Types Simple, single-cycle instructions Complex (e.g., multiply-add in one op)
Clock Cycles Fewer per instruction More (but fewer total instructions)
Power Efficiency Higher (used in mobile/embedded) Lower (used in PCs)
Example MCUs ARM Cortex-M, AVR 8051, x86 (not MCUs)

Exam Tip: The 8051 uses CISC, so its instructions (e.g., MOV, ADD, JMP) may take multiple clock cycles.


5. Real-World Applications of Microcontrollers

A. Nepalese Examples

  1. eSewa Payment Terminals

    • MCU Used: STM32 or AVR (8-bit/32-bit).
    • How It Works:
      • Reads card data via UART/SPI.
      • Processes payment authentication (cryptographic operations in hardware).
      • Sends transaction logs to a server via Wi-Fi (ESP8266 module).
    • Key MCU Features Used:
      • UART for card reader communication.
      • Timers for debouncing buttons.
      • ADC for battery voltage monitoring.
  2. Khalti’s POS Machines

    • MCU: Raspberry Pi Pico (RP2040) or ESP32.
    • Application:
      • QR code scanning (camera module + image processing).
      • Secure storage of merchant keys in EEPROM.
      • Real-time clock (RTC) for transaction timestamps.
  3. NTC’s Smart Meters

    • MCU: 8051 or STM32.
    • Functions:
      • ADC measures voltage/current.
      • UART sends data to the NTC server.
      • Interrupts trigger alerts for power outages.

B. Global Examples

  1. Pathao Driver App (Ride-Hailing)

    • MCU in Hardware: ESP32 (for GPS modules in bikes).
    • Role:
      • GPS data parsing (UART from NEO-6M module).
      • Battery management (ADC for voltage levels).
      • Bluetooth Low Energy (BLE) for rider-driver pairing.
  2. YouTube’s Recommendation Algorithm (Indirect MCU Use)

    • While the algorithm runs on servers, edge devices (like smart TVs) use MCUs (e.g., Amlogic S905) to:
      • Decode video streams (DMA transfers).
      • Manage remote control inputs (IR receiver via GPIO).

6. Electromagnetic Interference (EMI) Basics

MCUs are sensitive to EMI, which can corrupt data or cause resets. Solutions:

  • Shielding: Use grounded metal enclosures.
  • Decoupling Capacitors: Place 0.1µF capacitors near MCU power pins.
  • Twisted-Pair Wiring: For sensitive signals (e.g., UART).
  • Grounding: Star topology (all grounds meet at one point).

7. Worked Example: Designing a Traffic Light Controller

Requirements:

  • 3 lights (Red, Yellow, Green) for one direction.
  • Cycle: Red (30s) → Green (25s) → Yellow (5s).
  • Use 8051 MCU with timers and ports.

Solution:

  1. Hardware Setup:

    • Connect Port 1 to LEDs (with current-limiting resistors).
    • Use Timer 0 for delays (1s interrupts).
  2. Pseudocode:

    void main() {
        P1 = 0x01; // Red ON (binary 00000001)
        delay(30); // 30s
        P1 = 0x04; // Green ON (00000100)
        delay(25);
        P1 = 0x02; // Yellow ON (00000010)
        delay(5);
        // Repeat...
    }
    
  3. Timer 0 Configuration (8051):

    • TH0 = TL0 = 65536 – (20000000/12/1024) (for 1s interrupt at 12MHz clock).
    • Enable interrupts via ET0 = 1; EA = 1;.

Mermaid Diagram: Traffic Light State Machine

Red (30s)Red Light ONGreen (25s)Green Light ON(after 30s)Yellow (5s)Yellow Light ON(after 55s)Red (30s)Cycle Restart(after 60s)
Traffic Light Controller State Timeline (12MHz 8051 Timer 0)

8. Exam Tips

  1. Compare MCUs vs. MPs:

    • Always highlight integration, power, and cost in answers.
    • Example: "An 8051 MCU is cheaper than a microprocessor like the Intel 8086 because it integrates memory and I/O."
  2. Memory Calculations:

    • Practice converting hex to decimal (e.g., 0xFF = 255 bytes).
    • Example: "If an 8051 has 4KB ROM and your program is 3.5KB, remaining space is 512 bytes."
  3. Port Configurations:

    • Know bidirectional vs. quasi-bidirectional ports (e.g., P0 vs. P1 in 8051).
    • Example: "Port 0 of 8051 requires external pull-up resistors because it’s open-drain."
  4. Real-World Scenarios:

    • Relate timers to delays, ADC to sensors, and UART to communication.
    • Example: "In a smart water meter, the MCU uses ADC to read voltage from a flow sensor and UART to send data to a cloud server."
  5. Common Pitfalls:

    • Floating inputs: Always enable pull-up/down resistors on unused GPIO pins.
    • Stack overflow: Ensure your program doesn’t exceed 8051’s 8-level stack.

9. Summary Checklist for Exams

Before answering, ensure you cover: ✅ Definition: MCU = integrated CPU + memory + I/O. ✅ Components: CPU, memory (ROM/RAM/EEPROM), I/O ports, timers. ✅ Architecture: Harvard vs. von Neumann (8051 is Harvard). ✅ Programming: Instruction sets (CISC in 8051), registers (PC, SP). ✅ Applications: Sensors, actuators, communication (UART, SPI). ✅ Real-World Tie: eSewa (UART + timers), Khalti (ADC + Wi-Fi), NTC meters (ADC + UART).


10. Practice Questions

  1. Short Answer:

    • "Why does an 8051 MCU use separate program and data memory?" Answer: Harvard architecture allows parallel access, improving speed for embedded real-time tasks.
  2. Calculation:

    • "An 8051 program uses 2KB ROM and 64 bytes RAM. What’s the remaining memory?" Answer: ROM: 4096 – 2048 = 2048 bytes; RAM: 128 – 64 = 64 bytes.
  3. Design:

    • "Design a circuit to read a temperature sensor (0–5V output) using an 8051. Which port and peripheral will you use?" Answer:
      • ADC0804 (8-bit ADC) connected to P1.0.
      • 8051’s ADC peripheral (if available) or external ADC with SPI/I2C.

11. Further Reading

Based on the TU BSc CSIT syllabus for Microprocessor Based Design, unit 1.

Discussion

Loading…