80 lines
2.8 KiB
Python
80 lines
2.8 KiB
Python
# Include external libraries
|
|
import os
|
|
import sys
|
|
import time
|
|
|
|
|
|
# Include custom files
|
|
# Add the parent directory to the module search path
|
|
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
|
|
from interface_board_libs.adc_mcp3208 import MCP3208 # custom driver for ADC-IC
|
|
from interface_board_pins import (
|
|
# mapping of ADC channels to terminals + max value:
|
|
ADC_TERMINALS,
|
|
# mapping of ADC channels to terminals:
|
|
ADC_CHANNEL_T0__0_TO_3V3,
|
|
ADC_CHANNEL_T1__0_TO_3V3,
|
|
ADC_CHANNEL_T2__0_TO_5V,
|
|
ADC_CHANNEL_T3__0_TO_5V,
|
|
ADC_CHANNEL_T4__0_TO_12V,
|
|
ADC_CHANNEL_T5__0_TO_24V,
|
|
ADC_CHANNEL_T6__0_TO_20MA,
|
|
ADC_CHANNEL_T7__0_TO_20MA,
|
|
) # mapping of ADC channels to terminals
|
|
|
|
|
|
# create ADC instance
|
|
adc = MCP3208()
|
|
|
|
|
|
|
|
# local helper function to scale the adc value to an actual voltage
|
|
def adc2value(adc_value, max_value):
|
|
max_adc = 4095
|
|
return adc_value * max_value / max_adc
|
|
|
|
|
|
|
|
|
|
while True:
|
|
print("-" * 40)
|
|
|
|
# Read all available channels in a loop according to terminal order / map
|
|
values = []
|
|
for terminal, config in ADC_TERMINALS.items():
|
|
adc_channel = config["adc_channel"]
|
|
max_value = config["max_value"]
|
|
value = adc.read(adc_channel)
|
|
print(f"T{terminal}: ADC={value:04d} => U_ADC = {adc2value(value, max_value):5.3f}V")
|
|
|
|
|
|
# Read channels one by one using defined constants (more intuitive)
|
|
print("-" * 40)
|
|
value = adc.read(ADC_CHANNEL_T0__0_TO_3V3)
|
|
print(f"Terminal 0 (0 to 3.3V): ADC={value:04d} => Terminal={adc2value(value, 3.3):4.2f}V")
|
|
|
|
value = adc.read(ADC_CHANNEL_T1__0_TO_3V3)
|
|
print(f"Terminal 1 (0 to 3.3V): ADC={value:04d} => Terminal={adc2value(value, 3.3):4.2f}V")
|
|
|
|
value = adc.read(ADC_CHANNEL_T2__0_TO_5V)
|
|
print(f"Terminal 2 (0 to 5V): ADC={value:04d} => Terminal={adc2value(value, 5):4.2f}V")
|
|
|
|
value = adc.read(ADC_CHANNEL_T3__0_TO_5V)
|
|
print(f"Terminal 3 (0 to 5V): ADC={value:04d} => Terminal={adc2value(value, 5):4.2f}V")
|
|
|
|
value = adc.read(ADC_CHANNEL_T4__0_TO_12V)
|
|
print(f"Terminal 4 (0 to 12V): ADC={value:04d} => Terminal={adc2value(value, 12):05.2f}V")
|
|
|
|
value = adc.read(ADC_CHANNEL_T5__0_TO_24V)
|
|
print(f"Terminal 5 (0 to 24V): ADC={value:04d} => Terminal={adc2value(value, 24):05.2f}V")
|
|
|
|
value = adc.read(ADC_CHANNEL_T6__0_TO_20MA)
|
|
print(f"Terminal 6 (0 to 20mA): ADC={value:04d} => Terminal={adc2value(value, 20):05.2f}mA")
|
|
|
|
value = adc.read(ADC_CHANNEL_T7__0_TO_20MA)
|
|
print(f"Terminal 7 (0 to 20mA): ADC={value:04d} => Terminal={adc2value(value, 20):05.2f}mA")
|
|
|
|
print("-" * 40)
|
|
time.sleep(1) # Delay before next read cycle
|
|
|