-
Notifications
You must be signed in to change notification settings - Fork 16
/
Esp32_Correct_DA2.ino
146 lines (112 loc) · 3.35 KB
/
Esp32_Correct_DA2.ino
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
// (C) 2019 Helmut Weber
//
// Build a LookUpTable for ESP32-ADC to get corrected values
// Uncomment to get Text for including in programm:
//#define GRAPH
#include <driver/dac.h>
float Results[4096];
float Res2[4095*5];
void Dotest(int j) {
unsigned int a1;
//Serial.print("DoTest "); Serial.println(j);
for (int i=0;i<256;i++) {
dac_output_voltage(DAC_CHANNEL_1, (i) & 0xff);
delayMicroseconds(100);
adcStart(35);
delayMicroseconds(100);
a1 = adcEnd(35);
Results[i*16]=0.9*Results[i*16] + 0.1*a1;
//Results[i*16]=a1;
}
}
void setup() {
dac_output_enable(DAC_CHANNEL_1); // pin 25
dac_output_voltage(DAC_CHANNEL_1, 0);
adcAttachPin(35);
adcStart(35);
Serial.begin(921600);
}
void loop() {
float r;
int x, delta, deltaMin, deltaMinIndex;
Serial.println("Test Liniarity ...");
for (int i=0; i<500; i++) {
if ((i%100)==0) Serial.println(i);
Dotest(i);
}
for (int i=0; i<256; i++) {
r=Results[i*16]+0.5;
x=(int)r;
Serial.print(i*16); Serial.print(" "); Serial.println(Results[i*16]);
}
Serial.println("Calculate interpolated values ..");
for (int i=0; i<256; i++) {
for (int j=1; j<16; j++) {
Results[i*16+j] = Results[i*16] + (Results[(i+1)*16] - Results[(i)*16])*(float)j / (float)16.0;
}
}
for (int i=0; i<4096; i++) {
Serial.print(i); Serial.print(" "); Serial.println(Results[i]);
}
for (int i=0; i<4096; i++) {
Results[i]=0.5 + Results[i];
}
for (int i=0; i<4096; i++) {
Serial.print(i); Serial.print(" "); Serial.println(Results[i],6);
}
for (int i=0; i<4096; i++) {
for (int j=0; j<5; j++) {
Res2[i*5+j] = Results[i] + (Results[(i+1)] - Results[i])*(float)j / (float)10.0;
}
}
for (int i=0; i<(5*4096); i++) {
Serial.print(i); Serial.print(" "); Serial.println(Res2[i],6);
}
for (int i=1; i<4096; i++) {
float diff, minDiff;
int index;
minDiff=99999;
for (int j=0; j<(5*4096); j++) {
diff=abs((float)(i) - Res2[j]);
if(diff<minDiff) {
minDiff=diff;
index=j;
}
}
Results[i]=index;
}
for (int i=0; i<(4096); i++) {
Serial.print(i); Serial.print(" "); Serial.println(Results[i]/5,6);
Results[i]/=5;
}
#ifdef GRAPH
while(1) {
int a1;
for (int i=2; i<256;i++) {
dac_output_voltage(DAC_CHANNEL_1, (i) & 0xff);
delayMicroseconds(100);
adcStart(35);
delayMicroseconds(100);
a1 = adcEnd(35);
float r= Results[a1];
Serial.print(i*16); Serial.print(" "); Serial.println(r);
}
}
#else
for (int i=0; i<10; i++) Serial.println();
Serial.println(" float ADC_LUT[4096] = { 0,");
for (int i=1; i<4095; i++) {
Serial.print(Results[i],4); Serial.print(",");
if ((i%16)==0) Serial.println();
}
Serial.println(Results[4095]); Serial.println("} ;");
for (int i=0; i<10; i++) Serial.println();
Serial.println(" int ADC_LUT[4096] = { 0,");
for (int i=1; i<4095; i++) {
Serial.print((int)Results[i]); Serial.print(",");
if ((i%16)==0) Serial.println();
}
Serial.println((int)Results[4095]); Serial.println("} ;");
while(1);
#endif
}