I'm trying out a TI chip that has 8 8-bit DACs, with the chip number being TLV5628CN. Looking at the datasheet, I thought I could use the SPI commands.
But the following code gives the results that follow in the second code section. The numbers should match, but don't.
// include the SPI library:
#include <SPI.h>
#define dacInputPin 4
// set pin 9 as the slave select for the DAC:
const int dacSelectPin = 9;
int dacValue;
void setup() {
// start serial port at 9600 bits per second
Serial.begin(9600);
// set the slaveSelectPin as an output:
pinMode (dacSelectPin, OUTPUT);
// initialize SPI:
SPI.begin();
}
void loop() {
// change the resistance from min to max:
for (int level = 0; level < 256; level = level + 1) {
dacWrite(level);
Serial.print(" ");
Serial.print(level);
Serial.print("\t");
dacValue = analogRead(dacInputPin)/4;
Serial.print(" ");
Serial.print(dacValue);
Serial.print("\n");
delay(10);
}
}
int dacWrite(int value) {
unsigned char commandMask, byteOne, byteTwo;
commandMask = 0x0;
byteOne = commandMask;
byteTwo = value;
// take the SS pin high to write to the chip:
digitalWrite(dacSelectPin,HIGH);
// send in the value via SPI:
SPI.transfer(byteOne);
SPI.transfer(byteTwo);
// take the SS pin low to write into DAC:
digitalWrite(dacSelectPin,LOW);
// take the SS pin high to finish the write:
digitalWrite(dacSelectPin,HIGH);
}
0 1
1 4
2 7
3 10
4 13
5 16
6 19
7 22
8 25
9 28
10 31
11 34
12 37
13 40
14 43
15 46
16 49
17 52
18 55
19 58
20 61
21 64
22 67
23 70
24 72
25 75
26 78
27 81
28 84
29 87
30 90
31 93
32 96
33 99
34 102
35 105
36 108
37 111
38 114
39 117
40 120
41 123
42 126
43 129
44 132
45 135
46 138
47 141
48 144
49 147
50 150
51 153
52 156
53 159
54 162
55 165
56 168
57 171
58 174
59 177
60 180
61 183
62 186
63 189
64 192
65 195
66 198
67 201
68 204
69 207
70 210
71 212
72 215
73 219
74 221
75 224
76 227
77 230
78 233
79 236
80 239
81 242
82 245
83 248
84 251
85 254
86 254
87 254
88 254
89 254
90 254
91 254
92 254
93 254
94 254
95 254
96 254
97 254
98 254
99 254
100 254
101 254
102 254
103 254
104 254
105 254
106 254
107 254
108 254
109 254
110 254
111 254
112 254
113 254
114 254
115 254
116 254
117 254
118 254
119 254
120 254
121 254
122 254
123 254
124 254
125 254
126 254
127 254
128 1
129 4
130 7
131 10
132 13
133 16
134 19
135 22
136 25
137 28
138 31
139 34
140 37
141 40
142 43
143 46
144 49
145 52
146 55
147 58
148 61
149 64
150 67
151 70
152 72
153 75
154 78
155 81
156 84
157 87
158 90
159 93
160 96
161 99
162 102
163 105
164 108
165 111
166 114
167 117
168 120
169 123
170 126
171 129
172 132
173 135
174 138
175 141
176 144
177 147
178 150
179 153
180 156
181 159
182 162
183 165
184 168
185 171
186 174
187 177
188 180
189 183
190 186
191 189
192 192
193 195
194 198
195 201
196 204
197 207
198 210
199 213
200 216
201 218
202 222
203 225
204 227
205 230
206 233
207 236
208 239
209 242
210 245
211 248
212 251
213 254
214 254
215 254
216 254
217 254
218 254
219 254
220 254
221 254
222 254
223 254
224 254
225 254
226 254
227 254
228 254
229 254
230 254
231 254
232 254
233 254
234 254
235 254
236 254
237 254
238 254
239 254
240 254
241 254
242 254
243 254
244 254
245 254
246 254
247 254
248 254
249 254
250 254
251 254
252 254
253 254
254 254
255 254
It seems like the problem should be obvious, but I'm not finding it. Anyone else see what is happening?