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
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
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
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.
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.
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
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.
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).
- While the algorithm runs on servers, edge devices (like smart TVs) use MCUs (e.g., Amlogic S905) to:
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:
Hardware Setup:
- Connect Port 1 to LEDs (with current-limiting resistors).
- Use Timer 0 for delays (1s interrupts).
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... }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
8. Exam Tips
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."
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."
- Practice converting hex to decimal (e.g.,
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."
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."
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
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.
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.
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.
- "Design a circuit to read a temperature sensor (0–5V output) using an 8051. Which port and peripheral will you use?"
Answer:
11. Further Reading
- Books:
- "The 8051 Microcontroller and Embedded Systems" by Mazidi.
- "Embedded Systems with ARM Cortex-M" by Yifeng Zhu.
- Online:
- 8051 Datasheet (Intel)
- Arduino Project Hub (for practical examples).
Based on the TU BSc CSIT syllabus for Microprocessor Based Design, unit 1.
Discussion
Loading…