-
Notifications
You must be signed in to change notification settings - Fork 1
/
Tool_I2CScanner_Teil_3.ino
446 lines (416 loc) · 12.9 KB
/
Tool_I2CScanner_Teil_3.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
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
// Tobias Kuch 2020 GPL 3.0 tobias.kuch@googlemail.com
// https://github.com/kuchto
#include <Wire.h>
#include <SoftWire.h>
//#include <AsyncDelay.h>
#include "SSD1306Ascii.h"
#include "SSD1306AsciiWire.h"
#define oLed_I2C_ADDRESS 0x3C
#define VSensor0 A0
#define VSensor1 A1
#define UniIOA 4
#define UniIOB 5
#define POTAConfig 6
#define POTBConfig 7
#define UpSwitch 8 // Pin Up Taster
#define DownSwitch 9 // Pin Down Taster
#define ModeSwitch 2 // Pin Modus Taster
#define Prescaler 256 // Mögliche Werte: 1,8,64,256,1024
#define CrystalFreq 16000000 // Quarzgeschwindigkeit des Arduinos: Standard: 16 mhz
#define UReference 4.5 // Spannungsreferenz für den Analogwandler (hier: 4.5 Volt)
#define ADResolution 1023
#define interval1 500 // Zeitraum in ms zwischen 2 analog Messungen der Spannung an A0 und A1
const uint8_t firstAddr = 1;
const uint8_t lastAddr = 0x7F;
SSD1306AsciiWire oled;
SoftWire sw(UniIOA, UniIOB);
SoftWire sw_rev(UniIOB, UniIOA);
//------------------------------------------------------------------------------
uint8_t I2C_Device_found_at_Address = 0;
uint16_t Round = 0;
uint16_t IntFreqStepOld = 0;
bool AlreadyScan = false;
bool Alreadypressed1 = false;
bool Alreadypressed2 = false;
bool Alreadypressed3 = false;
bool Modeswitched = false;
byte SelectedMode = 0;
unsigned int IntFreqStep = 1;
unsigned long previousMillis1 = 0;
unsigned long Frequency = 0;
float VoltageMultiplicator = 0;
ISR(TIMER1_COMPA_vect) { //Timer1 interrupt.
// Weitere Infos auf: https://www.mikrocontroller.net/articles/AVR-Tutorial:_Timer
digitalWrite(UniIOA, !(digitalRead(UniIOA)));
digitalWrite(UniIOB, !(digitalRead(UniIOB)));
}
void setup()
{
pinMode(ModeSwitch, INPUT_PULLUP);
pinMode(UpSwitch, INPUT_PULLUP);
pinMode(DownSwitch, INPUT_PULLUP);
pinMode(UniIOA, OUTPUT);
pinMode(UniIOB, OUTPUT);
pinMode(POTAConfig, OUTPUT);
pinMode(POTBConfig, OUTPUT);
digitalWrite (POTAConfig, HIGH);
digitalWrite (POTBConfig, HIGH);
cli(); // stoppe alle Interrupts
TCCR1A = 0; // set entire TCCR1A register to 0 TCCR - Timer/Counter Control Register
TCCR1B = 0; // Setze Timer/Counter Control Register TCCR1B auf 0
TCCR1B |= (1 << WGM12); // Schalte Clear Timer on Compare (CTC) Modus ein
if (Prescaler == 0)
{
TCCR1B |= (1 << CS10); // Setze Prescaler auf 0.
}
if (Prescaler == 8)
{
TCCR1B |= (1 << CS11); // Setze CS11 Bit auf 1 für den 8 Prescaler.
}
if (Prescaler == 64)
{
TCCR1B |= (1 << CS11) | (1 << CS10); // Setze CS10 und CS11 Bit auf 1 für den 64 Prescaler.
}
if (Prescaler == 256)
{
TCCR1B |= (1 << CS12); // Setze CS12 Bit auf 1 für den 256 Prescaler.
}
if (Prescaler == 1024)
{
TCCR1B |= (1 << CS12) | (1 << CS10); // Setze CS10 und CS12 Bit auf 1 für den 1024 Prescaler.
}
TCNT1 = 0; // Initialisiere Zähler/Zeitgeber Register Wert auf 0
OCR1A = 130; // Aufruffrequenz Timer 1 241 Hz * 2
TIMSK1 &= (0 << OCIE1A); // Sperre Timer Compare interrupt TIMSK - Timer/Counter Interrupt Mask Register
sei(); // erlaube interrupts
VoltageMultiplicator = float(UReference)/float(ADResolution);
Wire.begin();
Wire.setClock(400000L);
sw.setTimeout_ms(200); // Software I2C Bus Objekt 1 TimeOut
sw_rev.setTimeout_ms(200); // Software I2C Bus Objekt 2 TimeOut
oled.begin(&Adafruit128x32, oLed_I2C_ADDRESS);
oled.setFont(Adafruit5x7);
oled.clear();
oled.set1X();
oled.clear();
oled.setCursor(0, 0);
oled.print("Scanning I2C Bus...");
oled.setCursor(0, 1);
oled.print("Initital Scan.");
}
//------------------------------------------------------------------------------
bool scanI2C()
{
bool detected = false;
for (uint8_t addr = firstAddr; addr <= lastAddr; addr++)
{
uint8_t startResult = sw.llStart((addr << 1) + 1); // Signal a read
sw.stop();
if ((startResult == 0) & (I2C_Device_found_at_Address != addr)) // New I2C Device found
{
Round = 0;
detected = true;
I2C_Device_found_at_Address = addr;
AlreadyScan = false;
oled.set1X();
oled.setCursor(0, 0);
oled.print("I2C Device found at: ");
oled.setCursor(0, 1);
oled.set2X();
oled.setCursor(0, 1);
oled.print("-");
oled.print(addr, BIN);
oled.print("-b ");
oled.set1X();
oled.setCursor(0, 3);
oled.print("0x");
if (addr < 16) oled.print("0");
oled.print(addr, HEX);
oled.print(" HEX -- ");
if (addr < 10) oled.print("0");
oled.print(addr, DEC);
oled.print(" DEC ");
break;
} // Device Found END
} // Scan Round END
if (!detected)
{
I2C_Device_found_at_Address = 0;
Round++;
}
return detected;
} // Function END
bool scanI2C_rev()
{
bool detected = false;
for (uint8_t addr = firstAddr; addr <= lastAddr; addr++)
{
uint8_t startResult = sw_rev.llStart((addr << 1) + 1); // Signal a read
sw_rev.stop();
if ((startResult == 0) & (I2C_Device_found_at_Address != addr)) // New I2C Device found
{
Round = 0;
detected = true;
I2C_Device_found_at_Address = addr;
AlreadyScan = false;
oled.set1X();
oled.setCursor(0, 0);
oled.print("I2C Device found at: ");
oled.setCursor(0, 1);
oled.set2X();
oled.setCursor(0, 1);
oled.print("-");
oled.print(addr, BIN);
oled.print("-b ");
oled.set1X();
oled.setCursor(0, 3);
oled.print("0x");
if (addr < 16) oled.print("0");
oled.print(addr, HEX);
oled.print(" HEX -- ");
if (addr < 10) oled.print("0");
oled.print(addr, DEC);
oled.print(" DEC ");
break;
} // Device Found END
} // Scan Round END
if (!detected)
{
I2C_Device_found_at_Address = 0;
Round++;
}
return detected;
} // Function END
void CheckMode()
{
bool PinStatus1 = digitalRead(UpSwitch);
if ((PinStatus1 == LOW) && !(Alreadypressed1))
{
delay(200);
IntFreqStep = IntFreqStep + 1;
if (IntFreqStep > 254)
{
IntFreqStep = 254;
}
Alreadypressed1 = true;
} else if ((PinStatus1 == HIGH) && (Alreadypressed1))
{
Alreadypressed1 = false;
}
bool PinStatus2 = digitalRead(DownSwitch);
if ((PinStatus2 == LOW) && !(Alreadypressed2))
{
delay(200);
IntFreqStep = IntFreqStep - 1;
if (IntFreqStep < 1)
{
IntFreqStep = 1;
}
Alreadypressed2 = true;
} else if ((PinStatus2 == HIGH) && (Alreadypressed2))
{
Alreadypressed2 = false;
}
bool PinStatus3 = digitalRead(ModeSwitch);
if ((PinStatus3 == LOW) && !(Alreadypressed3))
{
delay(200);
SelectedMode++;
if (SelectedMode > 2) {
SelectedMode = 0;
}
Alreadypressed3 = true;
Modeswitched = true;
} else if ((PinStatus3 == HIGH) && (Alreadypressed3))
{
Alreadypressed3 = false;
}
}
void loop()
{
CheckMode();
if (SelectedMode == 0) // I2C Scanner Modus
{
if (Modeswitched)
{
cli(); //disable interrupts
TIMSK1 &= (0 << OCIE1A); // Sperre Timer Compare interrupt TIMSK - Timer/Counter Interrupt Mask Register
sei(); //allow interrupts
pinMode(POTAConfig, OUTPUT);
pinMode(POTBConfig, OUTPUT);
digitalWrite (POTAConfig, HIGH);
digitalWrite (POTBConfig, HIGH);
oled.clear();
oled.setCursor(0, 0);
oled.print("Scanning I2C Bus...");
}
Modeswitched = false;
if ((!scanI2C()) && (Round > 2))
{
if (!AlreadyScan)
{
AlreadyScan = true;
oled.clear();
oled.setCursor(0, 0);
oled.print("Scanning I2C Bus...");
}
Round = 0;
I2C_Device_found_at_Address = 0;
}
}
CheckMode();
if (SelectedMode == 0) // I2C Scanner Modus
{
if (Modeswitched)
{
cli(); //disable interrupts
TIMSK1 &= (0 << OCIE1A); // Sperre Timer Compare interrupt TIMSK - Timer/Counter Interrupt Mask Register
TCNT1 = 0; // Lösche Counter Value
sei(); //allow interrupts
pinMode(POTAConfig, OUTPUT);
pinMode(POTBConfig, OUTPUT);
digitalWrite (POTAConfig, HIGH);
digitalWrite (POTBConfig, HIGH);
oled.clear();
oled.setCursor(0, 0);
oled.print("Scanning I2C Bus...");
}
Modeswitched = false;
if ((!scanI2C_rev()) && (Round > 2))
{
if (!AlreadyScan)
{
AlreadyScan = true;
oled.clear();
oled.setCursor(0, 0);
oled.print("Scanning I2C Bus...");
}
Round = 0;
I2C_Device_found_at_Address = 0;
}
}
CheckMode();
if (SelectedMode == 1) // Square Wave Generator
{
if (Modeswitched)
{
digitalWrite(POTAConfig, LOW);
digitalWrite(POTBConfig, LOW);
pinMode(POTAConfig, INPUT);
pinMode(POTBConfig, INPUT);
pinMode(UniIOA, OUTPUT);
pinMode(UniIOB, OUTPUT);
digitalWrite(UniIOA, LOW);
digitalWrite(UniIOB, HIGH);
cli(); //disable interrupts
TCNT1 = 0; // Lösche Counter Value
TIMSK1 |= (1 << OCIE1A); // Erlaube Timer compare interrupt TIMSK - Timer/Counter Interrupt Mask Register
sei(); //allow interrupts
oled.clear();
oled.setCursor(0, 0);
oled.print("Square Wave Generator");
oled.set2X();
oled.setCursor(0, 1);
float Result = float(Frequency) / 1000;
if (Result > 1)
{
oled.print(Result,3);
oled.print(" kHz ");
} else
{
oled.print(float(Frequency),2);
oled.print(" Hz ");
}
oled.set1X();
oled.setCursor(0, 3);
oled.print("Step: ");
oled.print(IntFreqStep);
oled.print(" Prsc: ");
oled.println(Prescaler);
}
Modeswitched = false;
if (IntFreqStep != IntFreqStepOld)
{
IntFreqStepOld = IntFreqStep;
cli();//stop interrupts
OCR1A = IntFreqStep;
if ( TCNT1 >= OCR1A )
{
TCNT1 = OCR1A - 1; //initialize counter value to 0
}
sei();//allow interrupts
Frequency = (CrystalFreq / Prescaler) / (IntFreqStep + 1) / 2;
oled.set2X();
oled.setCursor(0, 1);
double Result = float(Frequency) / 1000;
Serial.println(Result);
if (Result > 1)
{
oled.print(Result,3);
oled.print(" kHz ");
} else
{
oled.print(Frequency);
oled.print(" Hz ");
}
oled.set1X();
oled.setCursor(0, 3);
oled.print("Step: ");
oled.print(IntFreqStep);
oled.print(" Prsc: ");
oled.println(Prescaler);
// oled.print(" ");
}
}
CheckMode();
if (SelectedMode == 2) // 2 Channel Voltmeter 0- 5 Volt 2 stellen genau. Vref ist mit 5 Volt angegeben.
{
if (Modeswitched)
{
cli(); //disable interrupts
TIMSK1 &= (0 << OCIE1A); // Verbiete Timer compare interrupt TIMSK - Timer/Counter Interrupt Mask Register
TCNT1 = 0; // Lösche Counter Value
sei(); //allow interrupts
digitalWrite(POTAConfig, LOW);
digitalWrite(POTBConfig, LOW);
pinMode(POTAConfig, OUTPUT);
pinMode(POTBConfig, OUTPUT);
digitalWrite(UniIOB, LOW);
digitalWrite(UniIOA, LOW);
pinMode(UniIOB, INPUT);
pinMode(UniIOA, INPUT);
oled.clear();
oled.setCursor(0, 0);
oled.print("2 Ch. Voltage Sensor");
oled.setCursor(0, 1);
oled.print("Channel 1 Channel 2 ");
}
Modeswitched = false;
if (millis() - previousMillis1 > interval1)
{
previousMillis1 = millis(); // aktuelle Zeit abspeichern
oled.set1X();
int VS0 = analogRead(VSensor0); // read the input pin
int VS1 = analogRead(VSensor1); // read the input pin
bool VSB0 = digitalRead(VSensor0); // read the input pin
bool VSB1 = digitalRead(VSensor1); // read the input pin
float Voltage1 = float(VS0) * VoltageMultiplicator;
float Voltage2 = float(VS1) * VoltageMultiplicator;
oled.setCursor(0, 1);
oled.print("Ch1 Ch2");
oled.setCursor(0, 10);
oled.print(VSB0);
oled.setCursor(0, 80);
oled.print(VSB1);
oled.set2X();
oled.setCursor(0, 2);
oled.print(Voltage1,2);
oled.print("V");
oled.setCursor(66, 2);
oled.print(Voltage2,2);
oled.print("V");
oled.set1X();
}
}
// END MainLoop
}