-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResistorMeasureBoard.cpp
159 lines (130 loc) · 3.17 KB
/
ResistorMeasureBoard.cpp
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
#include "ResistorMeasureBoard.h"
int RTDBoard::MAX_GAIN_NUMBER = 6;
double RTDBoard::MIN_UREF_VOLTAGE = 10.0;
int RTDBoard::GAIN_VOLTAGE[] {
6144,
4096,
2048,
1024,
512,
256
};
adsGain_t RTDBoard::GAIN_REGISTER[] {
GAIN_TWOTHIRDS,
GAIN_ONE,
GAIN_TWO,
GAIN_FOUR,
GAIN_EIGHT,
GAIN_SIXTEEN
};
RTDBoard::RTDBoard(double reference,int maxChannel) {
REFERENCE_RESISTOR_VALUE = reference;
CHANNEL_COUNT = maxChannel;
}
void RTDBoard::begin() {
Wire.begin();
}
double RTDBoard::readAdcUOut_AutoGain() {
double val;
int i;
for (i = 0; i < MAX_GAIN_NUMBER ; i++) {
adc.setGain(GAIN_REGISTER[i]);
val = (double)adc.getGainMV() * (double)((int16_t)adc.readADC_SingleEnded(0)) / 32767.0;
if ( i == MAX_GAIN_NUMBER || val >= GAIN_VOLTAGE[i + 1]) {
return val;
}
}
return 0;
}
double RTDBoard::readAdcUref_AutoGain() {
double val;
int i;
for (i = 0; i < MAX_GAIN_NUMBER ; i++) {
adc.setGain(GAIN_REGISTER[i]);
val = adc.readADC_Differential_0_1_mv();
val = (double)adc.getGainMV() * (double)((int16_t)adc.readADC_Differential_0_1()) / 32767.0;
if ( i == MAX_GAIN_NUMBER || val >= GAIN_VOLTAGE[i + 1]) {
return val;
}
}
return 0;
}
double RTDBoard::readAdcUrx_AutoGain() {
double val;
int i;
for (i = 0; i < MAX_GAIN_NUMBER ; i++) {
adc.setGain(GAIN_REGISTER[i]);
val = (double)adc.getGainMV() * (double)((int16_t)adc.readADC_SingleEnded(2)) / 32767.0;
if ( i == MAX_GAIN_NUMBER || val >= GAIN_VOLTAGE[i + 1]) {
return val;
}
}
return 0;
}
void RTDBoard::readAllValues(int channel) {
//read U0
data[channel].uOut = readAdcUOut_AutoGain();
data[channel].uOutRangemv = adc.getGainMV();
delay(25);
//read uRef
data[channel].uRef = readAdcUref_AutoGain();
data[channel].uRefRangemv = adc.getGainMV();
delay(25);
//read uRx
data[channel].uRx = readAdcUrx_AutoGain();
data[channel].uRxRangemv = adc.getGainMV();
delay(25);
if(data[channel].uRef > MIN_UREF_VOLTAGE){
// calc Rx
data[channel].iRef = data[channel].uRef * 1000.0f / REFERENCE_RESISTOR_VALUE;
data[channel].rx = data[channel].uRx / data[channel].iRef * 1000.0f ;
data[channel].error = false;
}else{
data[channel].iRef = 0;
data[channel].rx = 0;
data[channel].error = true;
}
}
void RTDBoard::readAllValues() {
for(int i=0; i < CHANNEL_COUNT; i++){
readAllValues(i);
}
}
String RTDBoard::toString() {
String out = String();
out += "Chan\t\t";
out += "uOut\t";
out += "Range\t";
out += "iRef\t";
out += "uRef\t";
out += "Range\t";
out += "uRx\t";
out += "Range\t";
out += "Rx\t\t";
out += "Error\t";
out += "\r\n";
for (int i = 0; i < CHANNEL_COUNT; i++) {
out += "Ch";
out += i;
out += "\t\t";
out += data[i].uOut;
out += "\t(";
out += data[i].uOutRangemv;
out += ")\t";
out += data[i].iRef;
out += "\t";
out += data[i].uRef;
out += "\t(";
out += data[i].uRefRangemv;
out += ")\t";
out += data[i].uRx;
out += "\t(";
out += data[i].uRxRangemv;
out += ")\t";
out += data[i].rx;
out += "\t\t";
out += data[i].error;
out += "\r\n";
}
return out;
}