-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArduino_Multimeter.ino
More file actions
408 lines (319 loc) · 8.48 KB
/
Arduino_Multimeter.ino
File metadata and controls
408 lines (319 loc) · 8.48 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <avr/interrupt.h>
const unsigned long R1_Value = 1655000ul;
const unsigned long R2_Value = 218000ul;
const unsigned long R3_Value = 18070ul;
const unsigned long R4_Value = 467ul;
const unsigned long R_Values[] = {R1_Value, R2_Value, R3_Value, R4_Value};
const int R1_PIN = 3;
const int R2_PIN = 4;
const int R3_PIN = 5;
const int R4_PIN = 8;
const int R_PINS[] = {R1_PIN, R2_PIN, R3_PIN, R4_PIN};
const int Mode1_PIN = 9;
const int Mode2_PIN = 2;
const int MeasPos_PIN = A1;
const int MeasNeg_PIN = A2;
const int Comp1_PIN = 6;
const int Comp2_PIN = 7;
int selected_R =0;
//define Custom Characters
LiquidCrystal_I2C lcd(0x27,20,4);
uint8_t ohmCahr[8] = {0x00, 0x00, 0x0E, 0x11, 0x11, 0x11, 0x0A, 0x1B};
uint8_t susCahr[8] = {0x00, 0x00, 0x0E, 0x18, 0x1E, 0x0E, 0x0A, 0x00};
uint8_t capCahr[8] = {0x04, 0x04, 0x04, 0x1F, 0x00, 0x1F, 0x04, 0x04};
uint8_t resCahr[8] = {0x04, 0x04, 0x0E, 0x0A, 0x0A, 0x0E, 0x04, 0x04};
uint8_t volCahr[8] = {0x00, 0x00, 0x11, 0x11, 0x1B, 0x0A, 0x0E, 0x04};
uint8_t Load1Cahr[8] = {0x01, 0x03, 0x02, 0x02, 0x02, 0x02, 0x03, 0x01};
uint8_t Load2Cahr[8] = {0x10, 0x18, 0x08, 0x08, 0x08, 0x08, 0x18, 0x10};
uint8_t Load3Cahr[8] = {0x1F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1F};
uint8_t Load4Cahr[8] = {0x1F, 0x00, 0x1F, 0x1F, 0x1F, 0x1F, 0x00, 0x1F};
uint8_t Load5Cahr[8] = {0x1F, 0x00, 0x1F, 0x1F, 0x1F, 0x1F, 0x00, 0x1F};
//Timer
volatile int ovfCount= 0;
volatile unsigned long count = 0;
volatile bool inSetup = true;
volatile bool CaptFlag = false;
ISR(TIMER1_CAPT_vect){
// for some reason interrupt triggers after starting timer
if (inSetup) return;
if (!digitalRead(MeasPos_PIN)) return;
count = (((unsigned long)ovfCount)*65536ul) + ((unsigned int)ICR1);
ovfCount = 0;
CaptFlag = true;
DisableCount();
}
ISR(TIMER1_OVF_vect){
ovfCount++;
}
unsigned long getCurrentCount(){
return (((unsigned long)ovfCount)*65536ul) + ((unsigned int)TCNT1);
}
void Init_Comparator_Timer(){
TCCR1A = 0;
TCCR1B = (1<<ICNC1);//|(1<<ICES1); //prescaler of 8
PORTB &=~((1<<PD6)|(1<<PD7));
DDRB &=~((1<<PD6)|(1<<PD7));
ACSR |= (1<<ACIC);
EnableCount();
delay(1);
DisableCount();
inSetup = false;
}
void EnableCount(){
cli();
ovfCount= 0;
TCNT1= 0;
TIFR1 =0;
TCCR1B |= (1<<CS11);
TIMSK1 |= (1<<ICIE1) |(1<<TOIE1);
TIFR1 =0;
sei();
}
void DisableCount(){
TIMSK1 &= ~(1<<ICIE1);
TCCR1B &= ~(1<<CS11);
}
void setup(){
//setup inputs
pinMode(MeasPos_PIN, INPUT);
pinMode(MeasNeg_PIN, INPUT);
pinMode(Mode1_PIN, INPUT_PULLUP);
pinMode(Mode2_PIN, INPUT_PULLUP);
pinMode(R1_PIN, INPUT);
pinMode(R2_PIN, INPUT);
pinMode(R3_PIN, INPUT);
pinMode(R4_PIN, INPUT);
Init_Comparator_Timer();
Serial.begin(9600);
while (!Serial) {
;
}
//setup LCD
lcd.init();
lcd.backlight();
lcd.createChar(0, ohmCahr);
lcd.createChar(1, susCahr);
lcd.createChar(2, capCahr);
//lcd.createChar(2, resCahr);
lcd.createChar(3, Load1Cahr);
lcd.createChar(4, Load2Cahr);
lcd.createChar(5, Load3Cahr);
lcd.createChar(6, Load4Cahr);
lcd.home();
lcd.setCursor(0, 1);
lcd.write(1);
lcd.write(1);
lcd.setCursor(14, 1);
lcd.write(1);
lcd.write(1);
lcd.setCursor(0, 0);
lcd.write(2);
lcd.setCursor(15, 0);
lcd.write(2);
}
bool firstPass = true;
void loop(){
lcd.setCursor(14, 0);
lcd.print((char)(selected_R+49));
if (!digitalRead(Mode1_PIN)){
lcd.createChar(2, capCahr);
pinMode(MeasNeg_PIN, OUTPUT);
digitalWrite(MeasNeg_PIN, LOW);
pinMode(MeasPos_PIN, INPUT);
pinMode(R1_PIN, INPUT);
pinMode(R2_PIN, INPUT);
pinMode(R3_PIN, INPUT);
pinMode(R4_PIN, OUTPUT);
digitalWrite(R4_PIN, LOW);
//Wait until the cap is discharged
int inVolt =0;
while((inVolt = analogRead(MeasPos_PIN)) > 1){
displayILoadingBar((1024-inVolt));
}
displayILoadingBar(1024);
delay(1);
pinMode(R4_PIN, INPUT);
//Load cap and time loading time
EnableCount();
pinMode(R_PINS[selected_R], OUTPUT);
digitalWrite(R_PINS[selected_R], HIGH);
while(!CaptFlag){
int aread = analogRead(MeasPos_PIN);
displayLoadingBar(aread*3/2);
// change resister if too big and restart measurement
if(firstPass && (getCurrentCount()>800000ul)){
firstPass = false;
selected_R = 3;
DisableCount();
return;
}
}
CaptFlag = false;
displayLoadingBar(1024);
double cval = (count/ (1.0986122886681096913952452369225*2000000ul))/R_Values[selected_R];
printValue(cval);
lcd.print("F");
Serial.println("F");
if (count < 2000000ul){//change resister if too small
if (selected_R > 0)
selected_R--;
else
firstPass = true;
}
}
else if (!digitalRead(Mode2_PIN)){
firstPass = true;
lcd.createChar(2, resCahr);
pinMode(R1_PIN, INPUT);
pinMode(R2_PIN, INPUT);
pinMode(R3_PIN, INPUT);
pinMode(R4_PIN, INPUT);
pinMode(MeasNeg_PIN, OUTPUT);
digitalWrite(MeasNeg_PIN, LOW);
pinMode(MeasPos_PIN, INPUT);
pinMode(R_PINS[selected_R], OUTPUT);
digitalWrite(R_PINS[selected_R], HIGH);
delay(1);
int sum = 0;
for(int i = 0; i<8; i++){
sum+= analogRead(MeasPos_PIN);
}
displayLoadingBar(sum/8);
double rVal = R_Values[selected_R]*(1023*8.0/(1023*8-sum)-1);
printValue(rVal);
lcd.write(0);
Serial.println("Ohm");
//Adjust resister
if (sum > 1024ul*8*9/10){
if (selected_R>0) selected_R--;
}
else if (sum < 1024ul*8/10){
if (selected_R<3) selected_R++;
}
delay(100);
}
else {
firstPass = true;
lcd.createChar(2, volCahr);
pinMode(R1_PIN, INPUT);
pinMode(R2_PIN, INPUT);
pinMode(R3_PIN, INPUT);
pinMode(R4_PIN, INPUT);
pinMode(MeasNeg_PIN, OUTPUT);
digitalWrite(MeasNeg_PIN, LOW);
pinMode(MeasPos_PIN, INPUT);
delay(1);
int sum = 0;
for(int i = 0; i<8; i++){
sum+= analogRead(MeasPos_PIN);
}
displayLoadingBar(sum/8);
double vval = 5.0 * sum /(1023*8);
printValue(vval);
lcd.write('V');
Serial.println("V");
}
}
void displayLoadingBar( int adcVal){
int temp = (adcVal*50ul+512)/1024;
char customload = (0xFE<<4-(temp%5));
Load5Cahr[2] = customload;
Load5Cahr[3] = customload;
Load5Cahr[4] = customload;
Load5Cahr[5] = customload;
lcd.createChar(7, Load5Cahr);
lcd.setCursor(2, 1);
lcd.write(3);
adcVal -= 51;
for(int i=0; i<10; i++){
if (temp>= 5)
lcd.write(6);
else if(temp>0)
lcd.write(7);
else
lcd.write(5);
temp-=5;
}
lcd.write(4);
}
void displayILoadingBar( int adcVal){
int temp = (adcVal*50ul+512)/1024;
char customload = ~(0xFE<<4-(temp%5));
Load5Cahr[2] = customload;
Load5Cahr[3] = customload;
Load5Cahr[4] = customload;
Load5Cahr[5] = customload;
lcd.createChar(7, Load5Cahr);
lcd.setCursor(2, 1);
lcd.write(3);
adcVal -= 51;
for(int i=0; i<10; i++){
if (temp>= 5)
lcd.write(5);
else if(temp>0)
lcd.write(7);
else
lcd.write(6);
temp-=5;
}
lcd.write(4);
}
//not fast in any way
void printValue(double d){
char unit = 0;
if(d > 1000){
d/=1000;
if(d > 1000){
d/=1000;
if(d > 1000){
d/=1000;//Giga
unit= 'G';
}
else{//Mega
unit= 'M';
}
}
else{//kilo
unit= 'k';
}
}
else if (d<1){
d*= 1000;
if(d>=1){
unit = 'm';
}
else{
d*= 1000;
if(d>=1){
unit = 'u';
}
else{
d*= 1000;
if(d>=1){
unit = 'n';
}
else{
d*= 1000;
if(d>=1){
unit = 'p';
}
else{
d*= 1000;
unit = 'f';
}
}
}
}
}
lcd.setCursor(2, 0);
if (unit==0) lcd.print(" ");
char sbuff[] = {0,0,0,0,0,0,0,0,0,0,0,0};
dtostrf(d,7,3,sbuff);
lcd.print(sbuff);
Serial.print(sbuff);
sprintf(sbuff," %c",unit);
lcd.print(sbuff);
Serial.print(sbuff);
}