forked from marko-pi/MCP4728
-
Notifications
You must be signed in to change notification settings - Fork 1
/
MCP4728_zynaptik_test.py
executable file
·112 lines (83 loc) · 4.32 KB
/
MCP4728_zynaptik_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import time
from ctypes import cdll, c_int, c_float, c_bool, c_void_p, POINTER
###### Wrapping C library ######
MCP4728 = cdll.LoadLibrary("./libMCP4728.so")
"""
# object = initialise(sda, scl, ldac, address)
Creates an instance of the chip. The initialisation type depends on the SDA, SCL and LDAC pins provided. If LDAC is irregular, reading and writing the chip address is disabled. If SDA and SCL do not match the predefined I2C pins, changing the output voltages is disabled. Both i2c-0 and i2c-1 busses can be used. Do not forget to provide pull-up resistors for the LDAC line and the I2C-0 lines.
Examples:
chip = initialize(2,3,-1,0x61) # opens an instance of the chip at address 0x61 and allows changing the output voltages over the i2c-1 bus (the chip address is mandatory)
chip = initialize(16,20,21,-1) # opens an instance of the chip at an unknown address and allows reading/writing the address
chip = initialize(0,1,16,-1) # opens an instance of the chip at an unknown address and allows both reading/writing the address and changing the output voltages over the i2c-0 bus
"""
initialize = MCP4728.mcp4728_initialize
initialize.restype = c_void_p
initialize.argtypes = [c_int, c_int, c_int, c_int]
"""
deinitialise(object)
Removes the instance of the chip. Recommended but not mandatory at the end of the program.
"""
deinitialize = MCP4728.mcp4728_deinitialize
deinitialize.argtypes = [c_void_p]
"""
address = getaddress(object)
Reads the current chip address. The obtained address is automatically stored by the library. LDAC mandatory.
"""
getaddress = MCP4728.mcp4728_getaddress
getaddress.argtypes = [c_void_p]
"""
setaddress(object, address)
Writes the new chip address. The current chip address must be supplied by the programmer or read with getaddress beforehand. LDAC mandatory.
"""
setaddress = MCP4728.mcp4728_setaddress
setaddress.argtypes = [c_void_p, c_int]
"""
singleinternal(object, channel, voltage, eeprom)
Sets the absolute voltange on the channel using the internal voltage reference. The eeprom indicates whether the voltage is written to the EEPROM of the chip. SDA/SLC must be 0/1 or 2/3.
"""
singleinternal = MCP4728.mcp4728_singleinternal
singleinternal.argtypes = [c_void_p, c_int, c_float, c_bool]
"""
singleexternal(object, channel, voltage, eeprom)
Sets the relative voltange on the channel using the external voltage reference. The eeprom indicates whether the voltage is written to the EEPROM of the chip. SDA/SLC must be 0/1 or 2/3.
"""
singleexternal = MCP4728.mcp4728_singleexternal
singleexternal.argtypes = [c_void_p, c_int, c_float, c_bool]
"""
multipleinternal(object, [voltages], eeprom)
Sets the absolute voltanges on all four channels using the internal voltage reference. The eeprom indicates whether the voltages are written to the EEPROM of the chip. SDA/SLC must be 0/1 or 2/3.
"""
multipleinternal = MCP4728.mcp4728_multipleinternal
multipleinternal.argtypes = [c_void_p, POINTER(c_float), c_bool]
"""
multipleexternal(object, [voltages], eeprom)
Sets the relative voltanges on all four channels using the external voltage reference. The eeprom indicates whether the voltages are written to the EEPROM of the chip. SDA/SLC must be 0/1 or 2/3.
"""
multipleexternal = MCP4728.mcp4728_multipleexternal
multipleexternal.argtypes = [c_void_p, POINTER(c_float), c_bool]
###### Test Zynthian Zynaptik ######
# creates instances of chip with unknown addresses, changing the output voltages over the i2c-1 bus allowed.
chip = initialize(2,3,-1,0x60)
# gets address
#res = getaddress(chip)
#print('MCP2748 I2C address: 0x%x' % (res))
# sets four absolute voltages on the chip
voltages=[1.0,1.5,2.0,2.5]
floats = (c_float*4)(*voltages) # voltage list has to be trasferred in the C compatible array
res=multipleinternal(chip,floats,False)
if res==0:
print("Multiple voltage values sent to MCP4728.")
else:
print("ERROR {} sending multiple voltage values to MCP4728.".format(res))
# sets one absolute voltage on the chip on channel 4
res=singleinternal(chip,4,3.0,False)
if res==0:
print("Single voltage value sent to MCP4728.")
else:
print("ERROR {} sending single voltage value to MCP4728.".format(res))
# removes instances of two chips
deinitialize(chip)
# this should be added if voltages are written to the EEPROM accoring to the manual
time.sleep(0.05)