ECOR 1032: Mech Lecture 3 Logic Gates
Logic in Embedded Systems
Course Overview
- Course Title: Logic in Embedded Systems
- Course Code: ECOR 1032
- Module Focus: Circuits and Mechatronics
Bit Recap From ADC to Bits: The Binary Bridge
- Concept of Bits:
- A bit represents the simplest decision unit in binary logic.
- Binary Values Include:
- Yes/No: Represents approval or denial.
- On/Off: Represents the state of a switch.
- Voltage States:
- 0 (LOW): Represents 0V.
- 1 (HIGH): Represents 3.3V/5V.
- Importance: Foundation for all logic gates and decisions.
Sources of 1-Bit Signals
- Two Primary Sources:
- Natural Binary Sources:
- Devices that inherently emit one bit of information (e.g., a push button).
- States:
- Pressed/Not Pressed (On/Off).
- Thresholded Analog Sources:
- Continuous signals (e.g., from sensors) that are converted to binary outputs via thresholding.
- Behavior of Thresholding:
- HIGH if above threshold.
- LOW if below threshold.
Bits From Thresholding Analog Signals
- 1-Bit Quantization:
- Threshold is set at midpoint (e.g., 100).
- Output changes to 1 (HIGH) above threshold and 0 (LOW) below threshold.
- Visual Representation:
- Time Series:
- Time (ms): 0 to 500
- Amplitude/Bit Value: Ranges from 0 to 200.
- Threshold: 100 (output shifts)
- Output Pattern:
- HIGH = 1
- LOW = 0
Reed Switch Float Sensor Mechanism
- Float Sensor for Half-Full Indication:
- Mechanism Description:
- Float Movement:
- A cylindrical float containing a permanent magnet slides along a vertical guide rod.
- Always rests on the water’s surface.
- Reed Switch:
- Sealed reed switch is mounted at the critical "Half Full" level.
- Trigger Condition:
- As water level rises and float's magnet aligns with reed switch, circuit closes.
- Circuit Action:
- Closes circuit electromagnetically, providing an electrical signal to an external indicator.
- Indication of Tank Level:
- External LED illuminates when tank reaches set threshold, providing visual confirmation.
Physical Thresholding: The Float Sensor
- Real-World Thresholding Operation:
- Input Signal: Continuous analog signal representing water level in tank.
- Physical Threshold: Set by the reed switch’s location (halfway mark).
- Digital Output: Represents the reed switch status (Open or Closed).
- Embedded System Input:
- Single bit output from sensor acts as a direct GPIO input to the Raspberry Pi for logical decision-making.
Python Code: Water Level Monitor
- Sensor and Output Configuration:
- Reed Switch: Acts as a digital input source.
- GPIO Behavior:
- When water level is ≥ Halfway: Switch closes, GPIO reads HIGH (1).
- When water level is < Halfway: Switch opens, GPIO reads LOW (0).
- Pin Setup: Using BCM numbering convention.
python
SENSOR_PIN = 17
LED_PIN = 27
- Code Logic Implementation:
- Initiating Infinite Loop:
python
while True:
sensor_state = GPIO.input(SENSOR_PIN)
if sensor_state == GPIO.HIGH:
GPIO.output(LED_PIN, GPIO.HIGH)
else:
GPIO.output(LED_PIN, GPIO.LOW)
time.sleep(0.1)
- Control Logic:
- Implements simple combinatorial logic:
- IF Input is HIGH, THEN Output is ON.
Inverting the Logic
- New Requirement Example: Tank Empty Warning.
- Objective: LED should be ON when tank level is BELOW halfway, OFF at or above halfway.
Python Code: Tank Empty Warning Monitor
- Reed Switch Configuration:
- GPIO behavior remains the same (HIGH when tank is ≥ Halfway; LOW when < Halfway).
- Pin Setup:
python
SENSOR_PIN = 17
LED_PIN = 27
- Code Logic Change:
- Implementing Inverted Logic:
python
if sensor_state == GPIO.LOW:
GPIO.output(LED_PIN, GPIO.HIGH)
else:
GPIO.output(LED_PIN, GPIO.LOW)
- Control Logic:
- Implements inverted combinatorial logic:
- IF Input is LOW, THEN Output is ON.
Python Code: Tank Empty Warning Monitor (NOT Logic)
- Reed Switch: Functions the same as before.
- **Pin Setup: **Same as previous.
- Code Logic Using NOT Operator:
python
if not (sensor_state == GPIO.HIGH):
GPIO.output(LED_PIN, GPIO.HIGH)
else:
GPIO.output(LED_PIN, GPIO.LOW)
- Control Logic Behavior:
- Logic evaluates: IF NOT (Input is HIGH), THEN Output is ON.
- Similar functionality as checking for LOW, but uses Boolean NOT operation.
The Limits of Software Control
- Scenario: Fixed software control requiring HIGH to mean "turn light ON."
- Hardware Constraint:
- Need to make light turn ON when tank is LOW (switch open) without changing existing code.
- Solution Focus:
- Manipulate external signal to ensure the input signals match software expectations.
Signal Path Visualization
- External Logic Inversion:
- Using a NOT Gate (Inverter) circuit to flip sensor signal before it reaches Raspberry Pi GPIO pin.
- Functionality Assurance:
- Ensuring that when the sensor is LOW, the Pi receives a HIGH signal.
Commercial Hardware Solution: The Hex Inverter
- Device Source: Texas Instruments.
- Key Features:
- Controlled Baseline with single assembly site.
- Extended temperature performance from -55°C to 125°C.
- Enhanced DMS support and product-change notifications.
- Device Characteristics:
- Unbuffered Outputs and Latch-Up Performance Exceeds 100 mA.
- Operating Voltage Range: 2V to 5.5V Vcc.
Software vs. Hardware Logic
- Rationale for Hardware Implementation:
- Flexibility and Speed: Hardware logic is faster and does not suffer from software latencies.
- Considerations:
- Since hardware can implement logic faster and more reliably, it can be superior in time-sensitive applications.
Limitations of Embedded Processors (e.g., Raspberry Pi)
- Key Limitations Include:
- Cost and Scale:
- Raspberry Pi costs $35+. For mass production, cheaper reliance on combinatorial logic circuits increases profit.
- Physical Footprint (Space):
- Raspberry Pi is large compared to combinatorial logic circuits that fit into smaller spaces.
- Power Consumption:
- Requires significant power (1W to 5W) and voltage regulation. Logic gates consume much less power.
- Processing Speed and Latency:
- As Pi runs an OS, this introduces latency. Combinatorial logic reacts immediately to input signals without delays.
- GPIO Limits:
- Limited number of GPIO pins available on the Pi. Logic gates can combine multiple inputs, conserving GPIO resources.
- Reliability:
- Software can crash or introduce bugs, while dedicated hardware logic is more robust for continuous operations.
Software vs Hardware Execution Time
- Code Logic Execution:
python
try:
while True:
sensor_state = GPIO.input(SENSOR_PIN)
if not(sensor_state == GPIO.HIGH):
GPIO.output(LED_PIN, GPIO.HIGH)
else:
GPIO.output(LED_PIN, GPIO.LOW)
time.sleep(0.1)
- Execution Time Analysis:
- Loop time influenced by GPIO calls.
- Typical Loop Latency (Raspberry Pi):
- Single GPIO Call: approximately $20$ to $100$ µs
- Total Loop Time: approximately $40$ to $200$ µs
- Maximum Frequency (Latency): approximately $5$ to $25$ kHz
- Hardware Response Time:
- NOT Gate (7404 IC) executes the logic in approximately $5$ to $20$ ns (10,000× faster than software).
Multi-Channel Monitoring: Two Sensors, Two Outputs
- System Description:
- Each tank is monitored with an independent reed switch.
- Pi reads multiple GPIO input pins corresponding to each float sensor.
- Control Mechanism:
- The Pi controls individual GPIO output pins for LED indicators for each tank.
- Outputs Are Binary:
- Two independent outputs indicating the state for each sensor based on floating level detection.
Python Code: Dual Level Monitor
- Monitoring Two Inputs:
- Code for monitoring two sensors is implemented with frame separation for each condition.
- Pin Setup:
python
SENSOR_1_PIN = 17
SENSOR_2_PIN = 18
LED_1_PIN = 27
LED_2_PIN = 22
- Code Logic Implementation:
python
try:
while True:
state_1 = GPIO.input(SENSOR_1_PIN)
state_2 = GPIO.input(SENSOR_2_PIN)
if state_1 == GPIO.HIGH:
GPIO.output(LED_1_PIN, GPIO.HIGH)
else:
GPIO.output(LED_1_PIN, GPIO.LOW)
if state_2 == GPIO.HIGH:
GPIO.output(LED_2_PIN, GPIO.HIGH)
else:
GPIO.output(LED_2_PIN, GPIO.LOW)
time.sleep(0.1)
Control Logic (Parallel)
- Operation:
- The code implements two independent control loops for monitoring two tanks.
- Conditions:
- If SENSOR1 is HIGH, then LED1 is ON.
- If SENSOR2 is HIGH, then LED2 is ON.
Combinatorial Logic: Two Sensors → One Output
- New Requirement:
- Implementing Logical OR functionality where a single LED turns ON if either Tank 1 or Tank 2 is above the halfway mark.
Python Code: Logical OR Monitor
- Pin Setup for OR Logic:
python
SENSOR_1_PIN = 17
SENSOR_2_PIN = 18
LED_PIN = 27
- Control Logic Implementation:
python
while True:
state_1 = GPIO.input(SENSOR_1_PIN)
state_2 = GPIO.input(SENSOR_2_PIN)
if (state_1 == GPIO.HIGH) or (state_2 == GPIO.HIGH):
GPIO.output(LED_PIN, GPIO.HIGH)
else:
GPIO.output(LED_PIN, GPIO.LOW)
time.sleep(0.1)
Combinatorial Logic: Two Sensors → One Output (AND)
- New Requirement:
- Implementing Logical AND functionality where LED turns ON ONLY when both Tank 1 and Tank 2 are above halfway.
- Control Logic Implementation:
python
while True:
if (state_1 == GPIO.HIGH) and (state_2 == GPIO.HIGH):
GPIO.output(LED_PIN, GPIO.HIGH)
else:
GPIO.output(LED_PIN, GPIO.LOW)
time.sleep(0.1)
Logical Operators: Software and Hardware
- Operators Used:
- NOT, OR, AND in Python code.
- Hardware implementation of NOT logic using a physical gate.
- Common Concept:
- Logical operations act upon binary values, independent of medium—be it software or hardware.
Software vs. Hardware: The Trade-Off Revisited
- Processor Approach:
- Offers flexibility, debugging capability, and centralized control but at the cost of latency.
- Gates Approach:
- Provides zero flexibility and harder debugging but achieves ultra-low latency and distributed control, advantageous in critical applications.
- Key Decision Points:
- If logic is simple, time-sensitive, or needs reliability (e.g., safety), hardware is the preferred solution.
Case Study: Hardware Logic for Safety
- Critical Example:
- A pump should shut off if pressure is LOW or temperature is HIGH.
- Approaches Considered:
- Software Approach:
- Pi decides, potentially resulting in latency (∼100 µs).
- Hardware Approach:
- Sensors connect to an OR gate controlling a relay; latency is approximately $10$ ns.
- Takeaway:
- Hardware offers faster response, less risk, and lower complexity for certain tasks.
Origins of Boolean Algebra
- Historical Context:
- Developed by George Boole in the mid-1800s.
- Expressed logical statements symbolically, establishing algebraic manipulation rules.
- Fundamental Principles:
- Boolean Algebra Framework: Variables can be $0$ or $1$, with operations producing new outputs.
- Rules include validity for commutativity, associativity, distributions among others.
Logical Operators We Can Implement
- Core Operators:
- Already utilized: NOT, AND, OR.
- Additional Operators: NAND, NOR, XOR, XNOR, each realizable in software and hardware platforms.
Software vs. Hardware Implementations
- Normal Execution in Software:
- Instantiated as processor instructions.
- Execution in Hardware:
- Realized as voltage-controlled circuits.
- Factors Influencing Choice:
- Speed, reliability, and complexity of the overall architecture.
Introducing Combinatorial Logic
- Definition:
- Digital logic where output solely depends on input states; no memory or timing involved.
- Building Blocks:
- Logic gates implementing Boolean operations are foundational to combinatorial logic.
Standard Logic Gates
- Transition from General to Specific:
- Gates Include:
- Buffer Gate:
- Symbol: A ➜ Y
- Passes input unchanged.
- NOT Gate:
- Symbol: ¬A ➜ Y
- Inverts signal; essential for corrective actions in circuits.
- AND Gate:
- Symbol: A ∧ B ➜ Y
- Outputs true only if all inputs are true.
- OR Gate:
- Symbol: A ∨ B ➜ Y
- Outputs true if any input is true.
- NAND Gate:
- Symbol: ¬(A ∧ B) ➜ Y
- Universal gate; inverts AND output.
- NOR Gate:
- Symbol: ¬(A ∨ B) ➜ Y
- Universal gate; inverts OR output.
- XOR Gate:
- Symbol: A ⊕ B ➜ Y
- Outputs true if inputs differ; applications in error detection.
- XNOR Gate:
- Symbol: ¬(A ⊕ B) ➜ Y
- Checks for input equality; utilized in matching state conditions.