-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAD7490.cpp
More file actions
192 lines (158 loc) · 3.82 KB
/
AD7490.cpp
File metadata and controls
192 lines (158 loc) · 3.82 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
/**
* @file AD7490.cpp
* @brief Arduino library for AD7490 (12-bit, 16-channel ADC).
* @author Victor Gomes
*/
#include "AD7490.h"
void AD7490::begin() {
pinMode(CSPin, OUTPUT);
digitalWrite(CSPin, HIGH);
SPI.begin(
SCLKPin,
DOUTPin,
DINPin,
CSPin
);
reset();
}
uint16_t AD7490::read(uint8_t channel) {
uint16_t command = generateCommand(channel);
write(command);
// We only receive the requested data on the next write, so we have to send a blank request.
uint16_t response = write(0);
// Response is composed of ADDRESS+DATA, so we remove the ADDRESS.
response &= 0x0FFF;
return response;
}
void AD7490::validate() {
for (uint8_t channel = 0; channel < 16; channel++) {
uint16_t reading = read(channel);
Serial.print("CH");
Serial.print(channel);
Serial.print(": ");
Serial.print(reading);
Serial.print("|");
}
Serial.println("\n");
}
uint16_t AD7490::generateCommand(uint8_t address) {
// Uses bitwise operators to assemble the required command to read data from AD7490.
// For more information on that, read AD74901's datasheet.
return ((
(CRWriteValue <<11) |
(CRSEQValue <<10) |
((uint16_t)address <<6) |
(CRPMValue <<4) |
(CRShadowValue <<3) |
(CRWeakValue <<2) |
(CRRangeValue <<1) |
(CRCodingValue)
)<<4);
}
uint16_t AD7490::write(uint16_t command) {
digitalWrite(CSPin, LOW);
SPI.beginTransaction(
SPISettings(
clockFrequency,
SPI_BIT_ORDER,
SPI_MODE
)
);
uint16_t response = SPI.transfer16(command);
SPI.endTransaction();
digitalWrite(CSPin, HIGH);
return response;
}
void AD7490::reset() {
// For more information on that, see AD7490's datasheet on page 21.
write(UINT16_MAX);
delay(10);
write(UINT16_MAX);
delay(10);
uint16_t command = generateCommand(0);
write(command);
}
void AD7490::setSCLKPin(uint8_t pin) {
SCLKPin = pin;
}
void AD7490::setDOUTPin(uint8_t pin) {
DOUTPin = pin;
}
void AD7490::setDINPin(uint8_t pin) {
DINPin = pin;
}
void AD7490::setCSPin(uint8_t pin) {
CSPin = pin;
}
void AD7490::setPins(uint8_t sclk, uint8_t dout, uint8_t din, uint8_t cs) {
setSCLKPin(sclk);
setDOUTPin(dout);
setDINPin(din);
setCSPin(cs);
}
void AD7490::setClockFrequency(uint32_t frequency) {
clockFrequency = frequency;
}
void AD7490::setSequencer(uint8_t value) {
if (value <= 1) {
CRSEQValue = value;
read(0);
}
}
void AD7490::setPowerMode(uint8_t value) {
if (value <= 3) {
CRPMValue = value;
read(0);
}
}
void AD7490::setShadow(uint8_t value) {
if (value <= 1) {
CRShadowValue = value;
read(0);
}
}
void AD7490::setWeak(uint8_t value) {
if (value <= 1) {
CRWeakValue = value;
read(0);
}
}
void AD7490::setRange(uint8_t value) {
if (value <= 1) {
CRRangeValue = value;
read(0);
}
}
void AD7490::setCoding(uint8_t value) {
if (value <= 1) {
CRCodingValue = value;
read(0);
}
}
void AD7490::setCR(uint8_t seq, uint8_t pm, uint8_t shadow, uint8_t weak, uint8_t range, uint8_t coding) {
setSequencer(seq);
setPowerMode(pm);
setShadow(shadow);
setWeak(weak);
setRange(range);
setCoding(coding);
}
void AD7490::begin(uint8_t sclk, uint8_t dout, uint8_t din, uint8_t cs) {
setPins(
sclk,
dout,
din,
cs
);
begin();
}
void AD7490::begin(uint8_t sclk, uint8_t dout, uint8_t din, uint8_t cs, uint32_t frequency) {
setPins(
sclk,
dout,
din,
cs
);
setClockFrequency(frequency);
begin();
}