Skip to content

Commit f6329ef

Browse files
committed
saving progress
1 parent 401bd85 commit f6329ef

File tree

3 files changed

+218
-13
lines changed

3 files changed

+218
-13
lines changed

adafruit_mcp4728.py

Lines changed: 155 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,43 @@
5555

5656
_MCP4728_DEFAULT_ADDRESS = 0x60
5757

58-
_MCP4728_CH_A_SINGLE = 0x58
59-
_MCP4728_CH_B_SINGLE = 0x5A
60-
_MCP4728_CH_C_SINGLE = 0x5C
61-
_MCP4728_CH_D_SINGLE = 0x5E
58+
"""
59+
# DAC1, DAC0 DAC Channel Selection bits:
60+
# 00 = Channel A
61+
# 01 = Channel B
62+
# 10 = Channel C
63+
# 11 = Channel D
64+
65+
0 = slave addr(auto)
66+
0 1 0 0 0 DAC1 DAC0 UDAC[A]
67+
01000 + 00 + UDAC =
68+
0b01000000 = 0x40 (+2 for each successive)
69+
VREF PD1 PD0 Gx D11 D10 D9 D8 [A]
70+
D7 D6 D5 D4 D3 D2 D1 D0 [A]
71+
72+
0 1 0 0 0 Multi-Write for DAC
73+
0 1 0 1 0 Sequential Write for DAC Input Registers and EEPROM
74+
0 1 0 1 1 Single Write for DAC Input Register and EEPROM
75+
76+
1 0 0 0 0 Write Reference (VREF) selection bits to Input Registers
77+
1 1 0 0 0 Write Gain selection bits to Input Registers
78+
1 0 1 0 0 Write Power-Down bits to Input Registers
79+
"""
80+
81+
'0b 010 00 000'
82+
_MCP4728_CH_A_MULTI_IB = 0x40
83+
_MCP4728_CH_B_MULTI_IB = 0x42
84+
_MCP4728_CH_C_MULTI_IB = 0x44
85+
_MCP4728_CH_D_MULTI_IB = 0x46
86+
87+
'0b 010 11 000'
88+
_MCP4728_CH_A_SINGLE_EEPROM = 0x58
89+
_MCP4728_CH_B_SINGLE_EEPROM = 0x5A
90+
_MCP4728_CH_C_SINGLE_EEPROM = 0x5C
91+
_MCP4728_CH_D_SINGLE_EEPROM = 0x5E
92+
93+
'0b 010 10 000'
94+
_MCP4728_CH_A_MULTI_EEPROM = 0x50
6295

6396
class MCP4728:
6497
"""Helper library for the Microchip MCP4728 I2C 12-bit Quad DAC.
@@ -67,11 +100,17 @@ class MCP4728:
67100
:param address: The I2C slave address of the sensor
68101
69102
"""
103+
_channel_a_single_write_eeprom = UnaryStruct(_MCP4728_CH_A_SINGLE_EEPROM, ">H")
104+
_channel_b_single_write_eeprom = UnaryStruct(_MCP4728_CH_B_SINGLE_EEPROM, ">H")
105+
_channel_c_single_write_eeprom = UnaryStruct(_MCP4728_CH_C_SINGLE_EEPROM, ">H")
106+
_channel_d_single_write_eeprom = UnaryStruct(_MCP4728_CH_D_SINGLE_EEPROM, ">H")
107+
108+
_channel_a_multi_write = UnaryStruct(_MCP4728_CH_A_MULTI_IB, ">H")
109+
_channel_b_multi_write = UnaryStruct(_MCP4728_CH_B_MULTI_IB, ">H")
110+
_channel_c_multi_write = UnaryStruct(_MCP4728_CH_C_MULTI_IB, ">H")
111+
_channel_d_multi_write = UnaryStruct(_MCP4728_CH_D_MULTI_IB, ">H")
70112

71-
_channel_a_single_write = UnaryStruct(_MCP4728_CH_A_SINGLE, ">H")
72-
_channel_b_single_write = UnaryStruct(_MCP4728_CH_B_SINGLE, ">H")
73-
_channel_c_single_write = UnaryStruct(_MCP4728_CH_C_SINGLE, ">H")
74-
_channel_d_single_write = UnaryStruct(_MCP4728_CH_D_SINGLE, ">H")
113+
_multi_write_channel_a_start = UnaryStruct(_MCP4728_CH_A_MULTI_EEPROM, ">HHHH")
75114

76115
def __init__(self, i2c_bus, address=_MCP4728_DEFAULT_ADDRESS):
77116
self.i2c_device = i2c_device.I2CDevice(i2c_bus, address)
@@ -83,7 +122,7 @@ def channel_a(self):
83122

84123
@channel_a.setter
85124
def channel_a(self, value):
86-
self._channel_a_single_write = value
125+
self._channel_a_multi = value
87126

88127

89128
@property
@@ -93,7 +132,7 @@ def channel_b(self):
93132

94133
@channel_b.setter
95134
def channel_b(self, value):
96-
self._channel_b_single_write = value
135+
self._channel_b_multi = value
97136

98137
@property
99138
def channel_c(self):
@@ -102,7 +141,7 @@ def channel_c(self):
102141

103142
@channel_c.setter
104143
def channel_c(self, value):
105-
self._channel_c_single_write = value
144+
self._channel_c_multi = value
106145

107146
@property
108147
def channel_d(self):
@@ -111,4 +150,108 @@ def channel_d(self):
111150

112151
@channel_d.setter
113152
def channel_d(self, value):
114-
self._channel_d_single_write = value
153+
self._channel_d_multi = value
154+
155+
156+
def write_init(self, register_address, struct_format):
157+
self.format = struct_format
158+
self.address = register_address
159+
160+
# def read(self, obj):
161+
# buf = bytearray(1+struct.calcsize(self.format))
162+
# buf[0] = self.address
163+
# with self.i2c_device as i2c:
164+
# i2c.write_then_readinto(buf, buf, out_end=1, in_start=1)
165+
# return struct.unpack_from(self.format, buf, 1)[0]
166+
167+
# def write(self, obj, value):
168+
# buf = bytearray(1+struct.calcsize(self.format))
169+
# buf[0] = self.address
170+
# struct.pack_into(self.format, buf, 1, value)
171+
# with self.i2c_device as i2c:
172+
# i2c.write(buf)
173+
def chunks(self, l, n):
174+
# For item i in a range that is a length of l,
175+
for i in range(0, len(l), n):
176+
# Create an index range for l of n items:
177+
yield l[i:i+n]
178+
179+
def get_flags(self, high_byte):
180+
vref = (high_byte & 1<<7) > 0
181+
gain = (high_byte & 1<<4) > 0
182+
pd = (high_byte & 0b011<<5)>>5
183+
return (vref, gain, pd)
184+
185+
def read_registers(self):
186+
buf = bytearray(24)
187+
188+
with self.i2c_device as i2c:
189+
i2c.readinto(buf)
190+
index = 0
191+
for index, value in enumerate(buf):
192+
if index %3 is 0:
193+
print("\n%4s\t"%index, end="")
194+
print("%s %s "%( format(value, '#010b'), hex(value)), end="")
195+
print()
196+
# stride is 6 because we get 6 bytes for each channel; 3 for the output regs
197+
# and 3 for the eeprom. here we only care about the output buffer
198+
current_values = []
199+
for header, high_byte, low_byte, na_1, na_2, na_3 in self.chunks(buf,6):
200+
value = (high_byte & 0b00001111) << 8 | low_byte
201+
vref, gain, pd = self.get_flags(high_byte)
202+
current_values << (value, vref, pd, gain)
203+
204+
return current_values
205+
# ch_a_header, ch_a_hb, ch_a_lb = buf[0:3]
206+
# ch_aee_header, ch_aee_hb, ch_aee_lb = buf[3:6]
207+
# # ch_c_header, ch_c_hb, ch_c_lb = buf[6:9]
208+
# # ch_d_header, ch_d_hb, ch_d_lb = buf[9:12]
209+
210+
# ch_a_val =
211+
212+
# print("%s %s %s"%( self.b(ch_a_header),self.b(ch_a_hb), self.b(ch_a_lb) ))
213+
# print("%s %s %s"%( self.b(ch_b_header),self.b(ch_b_hb), self.b(ch_b_lb) ))
214+
# # print("%s %s %s"%( self.b(ch_c_header),self.b(ch_c_hb), self.b(ch_c_lb) ))
215+
# # print("%s %s %s"%( self.b(ch_d_header),self.b(ch_d_hb), self.b(ch_d_lb) ))
216+
217+
def b(self, byte_val):
218+
return format(byte_val, '#010b')
219+
220+
def write_multi_eeprom(self, byte_list, start=0):
221+
buffer_list = [_MCP4728_CH_A_MULTI_EEPROM]
222+
buffer_list += byte_list
223+
print("Byte List:")
224+
print(buffer_list)
225+
buf = bytearray(buffer_list)
226+
# struct.pack_into(self.format, buf, 1, value)
227+
with self.i2c_device as i2c:
228+
i2c.write(buf)
229+
230+
231+
232+
233+
234+
235+
@property
236+
def ch_a(self):
237+
return "poo"
238+
239+
@ch_a.setter
240+
def ch_a(self, value):
241+
self._channel_a_multi = value
242+
243+
"""
244+
# set the gain for a channel
245+
cache each, set all?
246+
247+
# set reference source for a channel
248+
cache each, set all?
249+
250+
# save settings to eeprom
251+
252+
# write a channel
253+
254+
# write all channels
255+
256+
# latching?
257+
"""

examples/mcp4728_sine_wave.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def sin_2_bits(sin_val):
2626
while True:
2727
for i in range(0,10000):
2828
bits = bits_array[i%45]
29-
mcp4728.channel_a = bits
29+
mcp4728.ch_a = bits
3030
# mcp4728.channel_b = bits
3131
# mcp4728.channel_c = bits
3232
# mcp4728.channel_d = bits

examples/read_test.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import time
2+
import board
3+
import busio
4+
import adafruit_mcp4728
5+
6+
i2c = busio.I2C(board.SCL, board.SDA)
7+
mcp4728 = adafruit_mcp4728.MCP4728(i2c)
8+
9+
def print_regs():
10+
buf = bytearray(24)
11+
12+
with mcp4728.i2c_device as i2c:
13+
i2c.readinto(buf)
14+
15+
for index, value in enumerate(buf):
16+
if index %3 is 0:
17+
print("\n%4s\t"%index, end="")
18+
print("%s %s "%( format(value, '#010b'), hex(value)), end="")
19+
print()
20+
21+
# Create a function called "chunks" with two arguments, l and n:
22+
def chunks(l, n):
23+
# For item i in a range that is a length of l,
24+
for i in range(0, len(l), n):
25+
# Create an index range for l of n items:
26+
yield l[i:i+n]
27+
# Create a list that from the results of the function chunks:
28+
29+
# mcp4728._channel_a_multi_write = 0b0000111111111111 # Voltage = VCC
30+
# mcp4728._channel_b_multi_write = 0b0000111111111111
31+
# mcp4728._channel_c_multi_write = 0b0000111111111111
32+
# mcp4728._channel_d_multi_write = 0b0000111111111111
33+
34+
# mcp4728.write_multi_eeprom([
35+
# 0b00001111, 0b11111111,
36+
# 0b00001111, 0b11111111,
37+
# 0b00001111, 0b11111111,
38+
# 0b00001111, 0b11111111
39+
# ])
40+
EEPROM_WAIT = 0.015 # 100ms
41+
time.sleep(0.100)
42+
# print_regs()
43+
44+
mcp4728.read_registers()
45+
46+
time.sleep(2)
47+
val = 0b111000000111
48+
val = 0
49+
mcp4728._channel_d_single_write_eeprom = 0b1110111100000000
50+
time.sleep(EEPROM_WAIT)
51+
mcp4728._channel_c_single_write_eeprom = 99
52+
time.sleep(EEPROM_WAIT)
53+
mcp4728._channel_b_single_write_eeprom = 102
54+
time.sleep(EEPROM_WAIT)
55+
mcp4728._channel_a_single_write_eeprom = 1234
56+
time.sleep(EEPROM_WAIT)
57+
58+
mcp4728.read_registers()
59+
# mcp4728.channel_a = 0b0000110011001100 # Voltage = VCC
60+
# mcp4728.channel_b = 0b0000110011001100
61+
# mcp4728.channel_c = 0b0000110011001100
62+
# mcp4728.channel_d = 0b0000110011001100

0 commit comments

Comments
 (0)