1
1
//
2
2
// FILE: MultiSpeedI2CScanner.ino
3
3
// AUTHOR: Rob Tillaart
4
- // VERSION: 0.1.04
4
+ // VERSION: 0.1.05
5
5
// PURPOSE: I2C scanner @different speeds
6
6
// DATE: 2013-11-05
7
7
// URL: http://forum.arduino.cc/index.php?topic=197360
12
12
#include < Wire.h>
13
13
#include < Arduino.h>
14
14
15
+ const char version[] = " 0.1.05" ;
16
+
15
17
// scans devices from 50 to 800KHz I2C speeds.
16
18
// lower than 50 is not possible
17
19
// DS3231 RTC works on 800 KHz. TWBR = 2; (?)
18
- long speed [] = {
20
+ const long allSpeed [] = {
19
21
50 , 100 , 200 , 250 , 400 , 500 , 800 };
20
- const int speeds = sizeof (speed)/sizeof (speed[0 ]);
22
+ long speed[sizeof (allSpeed)/sizeof (allSpeed[0 ])];
23
+ int speeds;
24
+
25
+ int addressStart = 0 ;
26
+ int addressEnd = 127 ;
21
27
22
28
// DELAY BETWEEN TESTS
23
29
#define RESTORE_LATENCY 5 // for delay between tests of found devices.
@@ -39,13 +45,15 @@ void setup()
39
45
{
40
46
Serial.begin (115200 );
41
47
Wire.begin ();
48
+ setSpeed (' 0' );
42
49
displayHelp ();
43
50
}
44
51
45
52
46
53
void loop ()
47
54
{
48
- switch (getCommand ())
55
+ char command = getCommand ();
56
+ switch (command)
49
57
{
50
58
case ' s' :
51
59
state = ONCE;
@@ -58,25 +66,38 @@ void loop()
58
66
Serial.print (F (" <delay=" ));
59
67
Serial.println (delayFlag?F (" 5>" ):F (" 0>" ));
60
68
break ;
69
+
61
70
case ' e' :
62
71
// eeprom test TODO
63
72
break ;
73
+
64
74
case ' h' :
65
75
header = !header;
66
76
Serial.print (F (" <header=" ));
67
77
Serial.println (header?F (" yes>" ):F (" no>" ));
68
78
break ;
69
- case ' ?' :
70
- state = HELP;
71
- break ;
72
79
case ' p' :
73
80
printAll = !printAll;
74
81
Serial.print (F (" <print=" ));
75
82
Serial.println (printAll?F (" all>" ):F (" found>" ));
76
83
break ;
84
+
85
+ case ' 0' :
86
+ case ' 1' :
87
+ case ' 2' :
88
+ case ' 4' :
89
+ case ' 8' :
90
+ setSpeed (command);
91
+ break ;
92
+
93
+ case ' a' :
94
+ setAddress ();
95
+ break ;
96
+
77
97
case ' q' :
98
+ case ' ?' :
78
99
state = HELP;
79
- break ;
100
+ break ;
80
101
default :
81
102
break ;
82
103
}
@@ -102,6 +123,57 @@ void loop()
102
123
}
103
124
}
104
125
126
+
127
+ void setAddress ()
128
+ {
129
+ if (addressStart == 0 )
130
+ {
131
+ addressStart = 8 ;
132
+ addressEnd = 120 ;
133
+ }
134
+ else
135
+ {
136
+ addressStart = 0 ;
137
+ addressEnd = 127 ;
138
+ }
139
+ Serial.print (F (" <address Range = " ));
140
+ Serial.print (addressStart);
141
+ Serial.print (F (" .." ));
142
+ Serial.print (addressEnd);
143
+ Serial.println (F (" >" ));
144
+
145
+ }
146
+
147
+ void setSpeed (char sp)
148
+ {
149
+ switch (sp)
150
+ {
151
+ case ' 1' :
152
+ speed[0 ] = 100 ;
153
+ speeds = 1 ;
154
+ break ;
155
+ case ' 2' :
156
+ speed[0 ] = 200 ;
157
+ speeds = 1 ;
158
+ break ;
159
+ case ' 4' :
160
+ speed[0 ] = 400 ;
161
+ speeds = 1 ;
162
+ break ;
163
+ case ' 8' :
164
+ speed[0 ] = 800 ;
165
+ speeds = 1 ;
166
+ break ;
167
+ case ' 0' :
168
+ speeds = sizeof (allSpeed)/sizeof (allSpeed[0 ]);
169
+ for (int i=0 ; i< speeds; i++)
170
+ {
171
+ speed[i] = allSpeed[i];
172
+ }
173
+ break ;
174
+ }
175
+ }
176
+
105
177
char getCommand ()
106
178
{
107
179
char c = ' \0 ' ;
@@ -114,14 +186,25 @@ char getCommand()
114
186
115
187
void displayHelp ()
116
188
{
117
- Serial.println (F (" \n Arduino I2C Scanner - 0.1.03\n " ));
189
+ Serial.print (F (" \n Arduino I2C Scanner - " ));
190
+ Serial.println (version);
191
+ Serial.println ();
192
+ Serial.println (F (" Scanmode:" ));
118
193
Serial.println (F (" \t s = single scan" ));
119
194
Serial.println (F (" \t c = continuous scan - 1 second delay" ));
120
195
Serial.println (F (" \t q = quit continuous scan" ));
121
- Serial.println (F (" \t d = toggle latency delay between successful tests." ));
196
+ Serial.println (F (" \t d = toggle latency delay between successful tests. 0 - 5 ms" ));
197
+ Serial.println (F (" Output:" ));
122
198
Serial.println (F (" \t p = toggle printAll - printFound." ));
123
199
Serial.println (F (" \t h = toggle header - noHeader." ));
124
- Serial.println (F (" \t ? = help - this page" ));
200
+ Serial.println (F (" \t a = toggle address range, 0..127 - 8..120" ));
201
+ Serial.println (F (" Speeds:" ));
202
+ Serial.println (F (" \t 0 = 50 - 800 Khz" ));
203
+ Serial.println (F (" \t 1 = 100 KHz only" ));
204
+ Serial.println (F (" \t 2 = 200 KHz only" ));
205
+ Serial.println (F (" \t 4 = 400 KHz only" ));
206
+ Serial.println (F (" \t 8 = 800 KHz only" ));
207
+ Serial.println (F (" \n\t ? = help - this page" ));
125
208
Serial.println ();
126
209
}
127
210
@@ -159,7 +242,7 @@ void I2Cscan()
159
242
// 0000 1XX X High Speed master code
160
243
// 1111 1XX X reserved - future purposes
161
244
// 1111 0XX X 10-bit slave addressing
162
- for (uint8_t address = 8 ; address < 120 ; address++)
245
+ for (uint8_t address = addressStart ; address <= addressEnd ; address++)
163
246
{
164
247
bool printLine = printAll;
165
248
bool found[speeds];
@@ -184,6 +267,7 @@ void I2Cscan()
184
267
Serial.print (F (" \t " ));
185
268
Serial.print (address, DEC);
186
269
Serial.print (F (" \t 0x" ));
270
+ if (address < 0x10 ) Serial.print (0 , HEX);
187
271
Serial.print (address, HEX);
188
272
Serial.print (F (" \t " ));
189
273
@@ -207,3 +291,4 @@ void I2Cscan()
207
291
}
208
292
}
209
293
294
+
0 commit comments