Skip to content

Commit 813847d

Browse files
author
twhoekstra
committed
Updated DAC module to allow dual-channel use
1 parent fbe2b4e commit 813847d

1 file changed

Lines changed: 38 additions & 19 deletions

File tree

ports/rp2/modules/dac.py

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,52 @@
33

44
class DAC:
55

6-
def __init__(self, channel: str = 'A'):
7-
self.dac_A = not channel in ['B', 'b']
6+
def __init__(self):
7+
pass
8+
9+
def write(self, voltage_a: float = None, voltage_b: float = None):
10+
11+
if voltage_a is None and voltage_b is None:
12+
raise ValueError('Must specify either a voltage_a or voltage_b')
13+
elif voltage_a is not None and voltage_b is None:
14+
# Just use DAC A
15+
voltages = [voltage_a]
16+
selectors = [True]
17+
elif voltage_b is not None and voltage_a is None:
18+
# Just use DAC B
19+
voltages = [voltage_b]
20+
selectors = [False]
21+
else:
22+
# Use both DACs
23+
voltages = [voltage_a, voltage_b]
24+
selectors = [True, False]
825

9-
def write(self, voltage):
10-
waveform = DC(voltage)
26+
spi, CS, LDAC = _setup_spi()
27+
LDAC.value(True) # Set LDAC to HIGH to keep latch closed
1128

12-
wcr_array = _add_instr_to_wcr_array(waveform.get_wcr_array(),
13-
self.dac_A,
14-
waveform.gain_2)
29+
for voltage, sele in zip(voltages, selectors):
30+
waveform = DC(voltage)
1531

16-
spi, CS, LDAC = _setup_spi()
32+
wcr_array = _add_instr_to_wcr_array(waveform.get_wcr_array(),
33+
sele,
34+
waveform.gain_2)
35+
mv = memoryview(wcr_array)
36+
37+
CS.value(True)
1738

18-
mv = memoryview(wcr_array)
1939

20-
CS.value(True)
21-
LDAC.value(False)
40+
buffer = bytearray([0, 0])
2241

23-
buffer = bytearray([0, 0])
42+
# PREPARE ---------------------------
43+
CS.value(False)
44+
buffer[0] = mv[0]
45+
buffer[1] = mv[1]
2446

25-
# PREPARE ---------------------------
26-
CS.value(False)
27-
buffer[0] = mv[0]
28-
buffer[1] = mv[1]
47+
# WRITE POINT -----------------------------
48+
spi.write(buffer)
49+
CS.value(True) # Datasheet: Chip select to end before LDAC pulse
2950

30-
# WRITE POINT -----------------------------
31-
spi.write(buffer)
32-
CS.value(True) # Datasheet: Chip select to end before LDAC pulse
51+
LDAC.value(False) # Release latch to update simultaneously
3352

3453
return 1
3554

0 commit comments

Comments
 (0)