In [10]:
import numpy as np
from scipy import linalg
In [11]:
digitalInput = 160
R = 1200
In [12]:
# resistor to resistor network for 8 bit Digital to Analog Coverter
# Create a Matrix with coefficients for system of 8 equations.
r2r = np.matrix([
[-5*R, 2*R, 0, 0, 0, 0, 0, 0],
[2*R, -5*R, 2*R, 0, 0, 0, 0, 0],
[0, 2*R, -5*R, 2*R, 0, 0, 0, 0],
[0, 0, 2*R, -5*R, 2*R, 0, 0, 0],
[0, 0, 0, 2*R, -5*R, 2*R, 0, 0],
[0, 0, 0, 0, 2*R, -5*R, 2*R, 0],
[0, 0, 0, 0, 0, 2*R, -5*R, 2*R],
[0, 0, 0, 0, 0, 0, 2*R, -4*R]
], dtype=np.float)
In [13]:
print(r2r)
[[-6000.  2400.     0.     0.     0.     0.     0.     0.]
 [ 2400. -6000.  2400.     0.     0.     0.     0.     0.]
 [    0.  2400. -6000.  2400.     0.     0.     0.     0.]
 [    0.     0.  2400. -6000.  2400.     0.     0.     0.]
 [    0.     0.     0.  2400. -6000.  2400.     0.     0.]
 [    0.     0.     0.     0.  2400. -6000.  2400.     0.]
 [    0.     0.     0.     0.     0.  2400. -6000.  2400.]
 [    0.     0.     0.     0.     0.     0.  2400. -4800.]]

In [14]:
binInput = bin(digitalInput)[2:].zfill(8)

print("----  Input  ", digitalInput, "  ----")
print("----  Binary  ", binInput, "  ----")
----  Input   160   ----
----  Binary   10100000   ----

In [15]:
# Make List of voltages for each loop based on binary input
# v[0] = voltage drop, rise or no change from bit 6 to bit 7
# v[1] = voltage drop, rise or no change from bit 5 to bit 6
# repeat for v[2] .. v[6]
v = [] #List to contain voltages for each loop
vL = [0, -5, 5, 0]
v7 = 0
for b in range(7):
    volt = vL[int(binInput[b]+binInput[b+1], 2)]
    v7 += volt
    v.append([volt])
# v[7] = voltage rise or no change from v=0 to bit 0 
volt = vL[int(binInput[7]+"0", 2)] # Voltage on bit 0 input
v7 += volt
v.append([volt])

print("V: ", v, "\n")

V = np.matrix(v) # Make numPy Matrix from list of voltages

# print("V matrix: ", V)
print("-"*80, "\n")
V:  [[5], [-5], [5], [0], [0], [0], [0], [0]] 

-------------------------------------------------------------------------------- 


In [16]:
schematic = [
" " * 6  + "".join(["     V" + str(r) for r in range(7, -1, -1)]),
" " * 6  + "".join([("     " if Vn[0] >= 0 else "    ") + str(Vn[0]) + "V" for Vn in v]),
"\t" + "o--||--" * 8,
"\t" + "|      " * 8 + "|",
"\t" + "\      " * 8 + "|",
"  " + "".join(["   R" + str(r) + " /" for r in range(7, -1, -1)]) + "      |",
"\t" + "\      " * 8 + "|",
"\t" + "/      " * 8 + "|",
"\t" + "|      " * 8 + "|",
"    o---" + "--===--" * 8 + "----|GND",
"        " + ("  " + str(R / 1000) + "K ") * 7 + "  " + str(2 * R / 1000) + "K ",
" \n    R7..R0 = " + str(2 * R / 1000) + "K\n"
]
In [17]:
IL = r2r.I @ V # Multiply r2r Inverse Matrix X Voltage Matrix
I7 = IL.tolist()[0][0] # Extract I7 Current from Current Matrix
In [18]:
print("\n\n")
for c in schematic:
    print(c)
print(" Currents: \n", IL, "\n")
print("-"*80, "\n")
print("----  Input  ", digitalInput, "  ----\n")
print("----  Binary  ", binInput, "  ----\n")
print("R7 Current: ", I7)
print("\nV7 voltage: ", v7)
print("\nR7 Volts: ", I7 * 2 * R) # Calculate Voltage on R
print("\nVout: ", v7 + I7 * 2 * R, "\n\n") # Add signed voltage to V7



           V7     V6     V5     V4     V3     V2     V1     V0
           5V    -5V     5V     0V     0V     0V     0V     0V
	o--||--o--||--o--||--o--||--o--||--o--||--o--||--o--||--
	|      |      |      |      |      |      |      |      |
	\      \      \      \      \      \      \      \      |
     R7 /   R6 /   R5 /   R4 /   R3 /   R2 /   R1 /   R0 /      |
	\      \      \      \      \      \      \      \      |
	/      /      /      /      /      /      /      /      |
	|      |      |      |      |      |      |      |      |
    o-----===----===----===----===----===----===----===----===------|GND
          1.2K   1.2K   1.2K   1.2K   1.2K   1.2K   1.2K   2.4K 
 
    R7..R0 = 2.4K

 Currents: 
 [[ -7.81250000e-04]
 [  1.30208333e-04]
 [ -9.76562500e-04]
 [ -4.88281250e-04]
 [ -2.44140625e-04]
 [ -1.22070313e-04]
 [ -6.10351563e-05]
 [ -3.05175781e-05]] 

-------------------------------------------------------------------------------- 

----  Input   160   ----

----  Binary   10100000   ----

R7 Current:  -0.00078125

V7 voltage:  5

R7 Volts:  -1.875

Vout:  3.125 



In [18]: