forked from bobhart/AD5668-Library
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAD5668.cpp
More file actions
183 lines (160 loc) · 5.4 KB
/
Copy pathAD5668.cpp
File metadata and controls
183 lines (160 loc) · 5.4 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
/*
AD5668.cpp, version 1.1, written by Robert Hart, October 15, 2015
This library implements both hardware-SPI and software SPI constructors.
This is an Arduino library for the Analog Devices AD5668 16-bit, 8 Channel
Digital-to-Analog converter chip. It should also be usable with the Texas
Instruments DAC8568, a pin and command compatible part. However, to maintain
full compatibility with the AD5668, this library does not impliment the
"Flexible Mode" internal reference command write sequences of the DAC8568
(0b1001).
Portions of this code were based upon prior work by crx and Stephan Bergemann,
specifically in the structure of the writeDAC and init functions, as well as
the use of a command definition list. This library impliments all of the
command functions of the chip, and allows for tying the ~LDAC pin to ground.
When tying ~LDAC to ground, DO NOT use the toggleLDAC function. Also, use of the
toggleDAC function is probably only needed in conjuction with command function
writeChannel (chip command 0). For a full explanation of the various functions,
refer to the readme file and the examples.
The AD5668 library is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the Free
Software Foundation, version 3 of the License, which should be included in this
this distribution. If not, see <http://www.gnu.org/licenses/>.
*/
#include "AD5668.h"
#include <SPI.h>
// Hardware SPI Constructor, must use hardware SPI bus pins
AD5668::AD5668(uint8_t ssPin, uint8_t clrPin, int8_t ldacPin) {
_ssPin = ssPin;
_clrPin = clrPin;
_ldacPin = ldacPin;
pinMode(_ssPin, OUTPUT);
pinMode(_clrPin, OUTPUT);
if (_ldacPin > 0) {
pinMode(_ldacPin, OUTPUT);
}
_hwSPI = true;
}
// Software SPI Constructor, may use any digital pins
AD5668::AD5668(uint8_t mosiPin, uint8_t sclkPin, uint8_t ssPin, uint8_t clrPin, int8_t ldacPin) {
_mosiPin = mosiPin;
_sclkPin = sclkPin;
_ssPin = ssPin;
_clrPin = clrPin;
_ldacPin = ldacPin;
pinMode(_mosiPin, OUTPUT);
pinMode(_sclkPin, OUTPUT);
pinMode(_ssPin, OUTPUT);
pinMode(_clrPin, OUTPUT);
if (_ldacPin > 0) {
pinMode(_ldacPin, OUTPUT);
}
_hwSPI = false;
}
void AD5668::init() {
if (_hwSPI) {
SPI.begin();
SPI.setBitOrder(MSBFIRST);
}
else {
digitalWrite(_mosiPin, LOW);
digitalWrite(_sclkPin, LOW);
}
digitalWrite(_ssPin, LOW);
if (_ldacPin > 0) {
digitalWrite(_ldacPin, HIGH);
}
digitalWrite(_clrPin, LOW);
delayMicroseconds(1);
digitalWrite(_clrPin, HIGH);
delayMicroseconds(1);
}
void AD5668::writeDAC(uint8_t command, uint8_t address, unsigned int data, uint8_t function) {
byte b1 = command;
byte b2 = address << 4 | data >> 12; //4 address bits and 4 MSBs of data
byte b3 = data >> 4; // middle 8 bits of data
byte b4 = 0xF0 & (data << 4) >> 8 | function;
digitalWrite(_ssPin, LOW);
delayMicroseconds(1);
if (_hwSPI) {
SPI.transfer(b1);
SPI.transfer(b2);
SPI.transfer(b3);
SPI.transfer(b4);
}
else {
shiftOut(_mosiPin, _sclkPin, MSBFIRST, b1);
shiftOut(_mosiPin, _sclkPin, MSBFIRST, b2);
shiftOut(_mosiPin, _sclkPin, MSBFIRST, b3);
shiftOut(_mosiPin, _sclkPin, MSBFIRST, b4);
}
delayMicroseconds(1);
digitalWrite(_ssPin, HIGH);
}
void AD5668::writeChannel(uint8_t channel, unsigned int value) {
_channel = channel;
_value = value;
writeDAC(WRITE_INPUT_REGISTER, _channel, _value, 15);
}
void AD5668::updateChannel(uint8_t channel) {
_channel = channel;
writeDAC(UPDATE_OUTPUT_REGISTER, _channel, 0, 15);
}
void AD5668::writeChUpdateAll(uint8_t channel, unsigned int value) {
_channel = channel;
_value = value;
writeDAC(WRITE_INPUT_REGISTER_UPDATE_ALL, _channel, _value, 15);
}
void AD5668::writeUpdateCh(uint8_t channel, unsigned int value) {
_channel = channel;
_value = value;
writeDAC(WRITE_INPUT_REGISTER_UPDATE_N, _channel, _value, 15);
}
void AD5668::powerDAC_Normal(uint8_t channelsN) {
_channelsN = channelsN;
unsigned int modeChE_H = _channelsN >> 4;
byte chA_D = 0x0F & _channelsN;
writeDAC(POWER_DOWN_UP_DAC, 0, modeChE_H, chA_D);
}
void AD5668::powerDAC_Down1K(uint8_t channelsN) {
_channelsN = channelsN;
unsigned int modeChE_H = 0x10 | (_channelsN >> 4);
byte chA_D = 0x0F & _channelsN;
writeDAC(POWER_DOWN_UP_DAC, 0, modeChE_H, chA_D);
}
void AD5668::powerDAC_Down100K(uint8_t channelsN) {
_channelsN = channelsN;
unsigned int modeChE_H = 0x20 | (_channelsN >> 4);
byte chA_D = 0x0F & _channelsN;
writeDAC(POWER_DOWN_UP_DAC, 0, modeChE_H, chA_D);
}
void AD5668::powerDAC_DownTri(uint8_t channelsN) {
_channelsN = channelsN;
unsigned int modeChE_H = 0x30 | (_channelsN >> 4);
byte chA_D = 0x0F & _channelsN;
writeDAC(POWER_DOWN_UP_DAC, 0, modeChE_H, chA_D);
}
void AD5668::setClearCode(uint8_t clearCode) {
_clearCode = clearCode;
writeDAC(LOAD_CLEAR_CODE_REGISTER, 0, 0, _clearCode);
}
void AD5668::setSoftLDAC(uint8_t channelsN) {
_channelsN = channelsN;
unsigned int chE_H = _channelsN >> 4;
byte chA_D = 0x0F & _channelsN;
writeDAC(LOAD_LDAC_REGISTER, 0, chE_H, chA_D);
}
void AD5668::reset() {
writeDAC(RESET_POWER_ON, 0, 0, 0);
}
void AD5668::enableInternalRef() {
writeDAC(SETUP_INTERNAL_REF, 0, 0, 1);
}
void AD5668::disableInternalRef() {
writeDAC(SETUP_INTERNAL_REF, 0, 0, 0);
}
void AD5668::toggleLDAC() {
digitalWrite(_ldacPin, LOW);
delayMicroseconds(1);
digitalWrite(_ldacPin, HIGH);
delayMicroseconds(1);
}