-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcp4922.py
More file actions
executable file
·225 lines (199 loc) · 7.02 KB
/
Copy pathmcp4922.py
File metadata and controls
executable file
·225 lines (199 loc) · 7.02 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
Python Library for MCP4922 DAC using Raspberry Pi 3 Model B+
2 Channels, 12 Bit
Currently only supports Hardware SPI
Requires: RPi.GPIO & spidev libraries
Wiring:
MCP4922 =======> Raspberry Pi
CS -------> GPIO08 Physical Pin 24 (SPI0 CE0) => Can be changed
SDI -------> GPIO10 Physical Pin 19 (SPI0 MOSI) => cannot be changed in hardware SPI MODE
SCK -------> GPIO11 Physical Pin 23 (SPI0 SCLK) => cannot be changed in hardware SPI MODE
MIT License
Copyright (c) 2017 mrwunderbar666
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
import hardDependencies
if hardDependencies.Raspberry:
import RPi.GPIO as GPIO
import spidev
import traceback
class MCP4922(object):
""" Class for the Microchip MCP4922 digital to analog converter
"""
spi = spidev.SpiDev()
def __init__(self,
spibus=None,
spidevice=None,
cs=None
):
""" Initialize MCP4922 device with hardware SPI
Chipselect default value is BCM Pin 8 (Physical Pin: 24)
Select the bus and device number. Default values are:
Bus = 0 ; Device = 1
If you're not sure, just leave it default
"""
mode = GPIO.getmode()
if mode == GPIO.BOARD:
default_cs = 24
elif mode == GPIO.BCM:
default_cs = 8
else:
raise ValueError(
"You haven't selected a GPIO Mode? Use: e.g. GPIO.setmode(GPIO.BCM)")
if cs is None:
self.cs = default_cs
else:
self.cs = cs
if spibus is None:
self.spibus = 0
else:
self.spibus = spibus
if spidevice is None:
self.spidevice = 1
else:
self.spidevice = spidevice
GPIO.setup(self.cs, GPIO.OUT)
GPIO.output(self.cs, 1)
# As soon as MCP4922 object is created spidev bus and device are opened
# otherwise causes memory leak and creates Errno 24
self.spi.open(self.spibus, self.spidevice)
def setVoltage(self, channel, value):
"""
Regular setVoltage Function
Select your channel 0 or 1
Select Voltage value 0 to 4095
"""
if channel == 0:
output = 0x3000
elif channel == 1:
output = 0xb000
else:
raise ValueError(
'MCP4922 Says: Wrong Channel Selected! Chose either 0 or 1!')
if value > 4095:
value = 4095
if value < 0:
value = 0
#self.spi.open(self.spibus, self.spidevice)
output |= value
buf0 = (output >> 8) & 0xff
buf1 = output & 0xff
GPIO.output(self.cs, 0)
self.spi.writebytes([buf0, buf1])
GPIO.output(self.cs, 1)
#self.spi.close
return
def setVoltage_gain(self, channel, value):
"""
The MCP4922 has the ability to output the double of the reference Voltage
Reference Voltage is measured by the MCP4922 at pin 13 (VrefA) for Channel A and pin 11 (VrefB) for Channel B
Note that the output voltage cannot exceed the supply voltage from pin 1 (VDD)
"""
if channel == 0:
output = 0x1000
elif channel == 1:
output = 0x9000
else:
raise ValueError(
'MCP4922 Says: Wrong Channel Selected! Chose either 0 or 1!')
if value > 4095:
value = 4095
if value < 0:
value = 0
#self.spi.open(self.spibus, self.spidevice)
output |= value
buf0 = (output >> 8) & 0xff
buf1 = output & 0xff
GPIO.output(self.cs, 0)
self.spi.writebytes([buf0, buf1])
GPIO.output(self.cs, 1)
#self.spi.close
return
def setVoltage_buffered(self, channel, value):
"""
Using the buffer feature of the MCP4922,
refer to the datasheet for details
"""
if channel == 0:
output = 0x7000
elif channel == 1:
output = 0xF000
else:
raise ValueError(
'MCP4922 Says: Wrong Channel Selected! Chose either 0 or 1!')
if value > 4095:
value = 4095
if value < 0:
value = 0
#self.spi.open(self.spibus, self.spidevice)
output |= value
buf0 = (output >> 8) & 0xff
buf1 = output & 0xff
GPIO.output(self.cs, 0)
self.spi.writebytes([buf0, buf1])
GPIO.output(self.cs, 1)
#self.spi.close
return
def shutdown(self, channel):
"""
Completely shutdown selected channel for power saving
Sets the output of selected channel to 0 and 500K Ohms.
Read Datasheet (SHDN) for details
"""
if channel == 0:
output = 0x2000
elif channel == 1:
output = 0xA000
else:
raise ValueError(
'MCP4922 Says: Wrong Channel Selected! Chose either 0 or 1!')
#self.spi.open(self.spibus, self.spidevice)
buf0 = (output >> 8) & 0xff
buf1 = output & 0xff
GPIO.output(self.cs, 0)
self.spi.writebytes([buf0, buf1])
GPIO.output(self.cs, 1)
#self.spi.close
return
def close(self):
"""
Closes the device
"""
self.spi.close
return
def open(self):
"""
Manually Open the device
"""
self.spi.open(self.spibus, self.spidevice)
return
if __name__ == "__main__":
GPIO.setmode(GPIO.BOARD)
dac = MCP4922(cs=26)
dac.setVoltage(0,0)
while True:
try:
relay = input("Voltage (0..4095)")
dac.setVoltage(0,int(relay))
except KeyboardInterrupt:
break
except:
traceback.print_exc()
continue
dac.close