### CPU Fundamentals - **Definition:** The "brain" of the computer, responsible for executing instructions. - **Components:** - **Arithmetic Logic Unit (ALU):** Performs arithmetic operations (add, subtract) and logical operations (AND, OR, NOT). - **Control Unit (CU):** Manages and coordinates the flow of data and instructions within the CPU and to other components. - **Registers:** Small, fast storage locations within the CPU used to hold data, instructions, and addresses during processing. - **Program Counter (PC):** Stores the address of the next instruction to be fetched. - **Instruction Register (IR):** Holds the instruction currently being executed. - **Memory Address Register (MAR):** Stores the address of the memory location to be accessed. - **Memory Data Register (MDR):** Holds data fetched from or to be written to memory. - **Accumulator (ACC):** Stores the results of ALU operations. - **Clock Speed:** Measured in Hertz (Hz), indicates the number of cycles per second. Higher clock speed generally means faster instruction execution. ### Fetch-Decode-Execute Cycle The fundamental operation of the CPU: 1. **Fetch:** - Address in PC is copied to MAR. - Instruction at MAR address is fetched from memory and copied to MDR. - MDR content is copied to IR. - PC is incremented to point to the next instruction. 2. **Decode:** - The CU interprets the instruction in the IR. - Determines what operation needs to be performed and identifies operands. 3. **Execute:** - The ALU performs the operation specified by the instruction. - Data is processed, and results are stored (e.g., in the accumulator or memory). ### Instruction Sets - **Definition:** The complete set of commands (instructions) that a CPU can understand and execute. - **Types:** - **CISC (Complex Instruction Set Computer):** - Many complex instructions, each performing multiple low-level operations. - Fewer instructions needed per program, but each instruction takes longer. - Example: x86 architecture. - **RISC (Reduced Instruction Set Computer):** - Fewer, simpler instructions, each performing a single, basic operation. - More instructions needed per program, but each instruction executes very quickly (often in one clock cycle). - Pipelining is easier. - Example: ARM processors. - **Addressing Modes:** Ways of specifying the operand of an instruction. - **Immediate:** Operand is part of the instruction itself. - **Direct:** Instruction contains the exact memory address of the operand. - **Indirect:** Instruction contains the address of a memory location that holds the address of the operand. - **Register:** Operand is stored in a CPU register. - **Indexed:** Effective address is calculated by adding a constant offset to the content of an index register. ### Pipelining and Parallelism - **Pipelining:** Overlapping the execution of multiple instructions. While one instruction is in the execute stage, the next is in the decode stage, and another is being fetched. Improves throughput. - **Parallel Processing:** Executing multiple instructions or processes simultaneously. - **Multi-core Processors:** Multiple independent CPUs (cores) on a single chip. - **SIMD (Single Instruction, Multiple Data):** A single instruction operates on multiple data items simultaneously (e.g., for graphics processing). - **MIMD (Multiple Instruction, Multiple Data):** Multiple processors execute different instructions on different data simultaneously (e.g., supercomputers). ### Interrupts - **Definition:** Signals to the CPU indicating that an event requires immediate attention. - **Purpose:** Allows the CPU to handle I/O devices, errors, or time-critical events without constantly polling. - **Process:** 1. Interrupt signal received. 2. CPU completes current instruction. 3. Current CPU state (e.g., PC, registers) is saved (pushed onto stack). 4. CPU jumps to an Interrupt Service Routine (ISR) or handler. 5. ISR executes to handle the event. 6. CPU state is restored (popped from stack). 7. CPU resumes execution from where it left off. - **Types:** - **Hardware Interrupts:** From I/O devices (e.g., keyboard press, disk read complete). - **Software Interrupts:** Generated by programs (e.g., system calls, division by zero). ### Sensors Overview - **Definition:** Devices that detect and respond to physical input from the environment and convert it into electrical signals. - **Purpose:** Provide data about the real world to computer systems. - **Analog vs. Digital:** - **Analog:** Output is a continuous signal (e.g., voltage) proportional to the physical quantity. Requires an Analog-to-Digital Converter (ADC) for computer processing. - **Digital:** Output is discrete ON/OFF states or a direct digital value. ### Common Sensor Types - **Temperature Sensors:** - **Thermistor:** Resistance changes significantly with temperature. - **Thermocouple:** Generates a voltage proportional to temperature difference. - **RTD (Resistance Temperature Detector):** Resistance changes with temperature, often platinum. - **Semiconductor-based (e.g., LM35):** Output voltage directly proportional to temperature. - **Light Sensors:** - **Photoresistor (LDR - Light Dependent Resistor):** Resistance decreases as light intensity increases. - **Photodiode:** Generates current proportional to light intensity. - **Phototransistor:** More sensitive than photodiode, acts as a light-controlled switch or amplifier. - **Motion/Proximity Sensors:** - **PIR (Passive Infrared) Sensor:** Detects changes in infrared radiation (human body heat) for motion detection. - **Ultrasonic Sensor:** Emits sound waves and measures time for reflection to determine distance. - **Infrared (IR) Proximity Sensor:** Emits IR light and detects reflection for short-range object detection. - **Hall Effect Sensor:** Detects magnetic fields; used for position sensing, speed detection. - **Pressure Sensors:** Measures pressure of gases or liquids. Often uses diaphragms that deform under pressure, changing electrical properties. - **Humidity Sensors:** Measures moisture content in the air. - **Capacitive:** Dielectric constant of material changes with humidity. - **Resistive:** Electrical resistance of material changes with humidity. - **Accelerometers & Gyroscopes:** - **Accelerometer:** Measures acceleration (rate of change of velocity). Detects tilt, vibration, and freefall. - **Gyroscope:** Measures angular velocity (rate of rotation). Detects orientation and rotation. - **IMU (Inertial Measurement Unit):** Combines accelerometers and gyroscopes (and sometimes magnetometers) to provide comprehensive motion data. ### Sensor Interfaces - **Analog-to-Digital Converter (ADC):** Converts analog sensor signals into digital values that the CPU can process. - **Resolution:** Number of bits (e.g., 8-bit, 10-bit, 12-bit); higher resolution means finer measurement steps. - **Sampling Rate:** How often the analog signal is measured and converted. - **Digital Interfaces:** - **I2C (Inter-Integrated Circuit):** Two-wire serial bus (SDA - data, SCL - clock) for short-distance communication between master and multiple slave devices. - **SPI (Serial Peripheral Interface):** Four-wire serial bus (MOSI, MISO, SCK, SS) for full-duplex, high-speed communication between master and one or more slave devices. - **UART (Universal Asynchronous Receiver/Transmitter):** Asynchronous serial communication (Tx, Rx) for point-to-point communication. - **GPIO (General Purpose Input/Output):** Digital pins on a microcontroller that can be configured as inputs or outputs to read simple digital signals or control devices. ### Embedded Systems - **Definition:** Computer systems designed to perform dedicated functions within a larger mechanical or electrical system. - **Characteristics:** - Often real-time constraints. - Limited resources (memory, processing power). - Low power consumption. - Reliability and stability are critical. - **Role of Sensors:** Embedded systems heavily rely on sensors to gather data from their environment and actuators to interact with it. - **Examples:** Smart home devices, automotive control systems, medical equipment, industrial control.