|
| 1 | +// |
| 2 | +// FILE: MultiSpeedI2CScanner.ino |
| 3 | +// AUTHOR: Rob Tillaart |
| 4 | +// VERSION: 0.1.03 |
| 5 | +// PURPOSE: I2C scanner @different speeds |
| 6 | +// DATE: 2013-11-05 |
| 7 | +// URL: |
| 8 | +// |
| 9 | +// Released to the public domain |
| 10 | +// |
| 11 | + |
| 12 | +#include <Wire.h> |
| 13 | +#include <Arduino.h> |
| 14 | + |
| 15 | +// scans devices from 50 to 800KHz I2C speeds. |
| 16 | +// lower than 50 is not possible |
| 17 | +// DS3231 RTC works on 800 KHz. TWBR = 2; (?) |
| 18 | +long speed[] = { |
| 19 | + 50, 100, 200, 250, 400, 500, 800 }; |
| 20 | +const int speeds = sizeof(speed)/sizeof(speed[0]); |
| 21 | + |
| 22 | +// DELAY BETWEEN TESTS |
| 23 | +#define RESTORE_LATENCY 5 // for delay between tests of found devices. |
| 24 | +bool delayFlag = false; |
| 25 | + |
| 26 | +// MINIMIZE OUTPUT |
| 27 | +bool printAll = true; |
| 28 | +bool header = true; |
| 29 | + |
| 30 | +// STATE MACHINE |
| 31 | +enum states { |
| 32 | + STOP, ONCE, CONT, HELP }; |
| 33 | +states state = STOP; |
| 34 | + |
| 35 | +uint32_t startScan; |
| 36 | +uint32_t stopScan; |
| 37 | + |
| 38 | +void setup() |
| 39 | +{ |
| 40 | + Serial.begin(115200); |
| 41 | + Wire.begin(); |
| 42 | + displayHelp(); |
| 43 | +} |
| 44 | + |
| 45 | + |
| 46 | +void loop() |
| 47 | +{ |
| 48 | + switch (getCommand()) |
| 49 | + { |
| 50 | + case 's': |
| 51 | + state = ONCE; |
| 52 | + break; |
| 53 | + case 'c': |
| 54 | + state = CONT; |
| 55 | + break; |
| 56 | + case 'd': |
| 57 | + delayFlag = !delayFlag; |
| 58 | + Serial.print(F("<delay=")); |
| 59 | + Serial.println(delayFlag?F("5>"):F("0>")); |
| 60 | + break; |
| 61 | + case 'e': |
| 62 | + // eeprom test TODO |
| 63 | + break; |
| 64 | + case 'h': |
| 65 | + header = !header; |
| 66 | + Serial.print(F("<header=")); |
| 67 | + Serial.println(header?F("yes>"):F("no>")); |
| 68 | + break; |
| 69 | + case '?': |
| 70 | + state = HELP; |
| 71 | + break; |
| 72 | + case 'p': |
| 73 | + printAll = !printAll; |
| 74 | + Serial.print(F("<print=")); |
| 75 | + Serial.println(printAll?F("all>"):F("found>")); |
| 76 | + break; |
| 77 | + case 'q': |
| 78 | + state = HELP; |
| 79 | + break; |
| 80 | + default: |
| 81 | + break; |
| 82 | + } |
| 83 | + |
| 84 | + switch(state) |
| 85 | + { |
| 86 | + case ONCE: |
| 87 | + I2Cscan(); |
| 88 | + state = HELP; |
| 89 | + break; |
| 90 | + case CONT: |
| 91 | + I2Cscan(); |
| 92 | + delay(1000); |
| 93 | + break; |
| 94 | + case HELP: |
| 95 | + displayHelp(); |
| 96 | + state = STOP; |
| 97 | + break; |
| 98 | + case STOP: |
| 99 | + break; |
| 100 | + default: // ignore all non commands |
| 101 | + break; |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +char getCommand() |
| 106 | +{ |
| 107 | + char c = '\0'; |
| 108 | + if (Serial.available()) |
| 109 | + { |
| 110 | + c = Serial.read(); |
| 111 | + } |
| 112 | + return c; |
| 113 | +} |
| 114 | + |
| 115 | +void displayHelp() |
| 116 | +{ |
| 117 | + Serial.println(F("\nArduino I2C Scanner - 0.1.03\n")); |
| 118 | + Serial.println(F("\ts = single scan")); |
| 119 | + Serial.println(F("\tc = continuous scan - 1 second delay")); |
| 120 | + Serial.println(F("\tq = quit continuous scan")); |
| 121 | + Serial.println(F("\td = toggle latency delay between successful tests.")); |
| 122 | + Serial.println(F("\tp = toggle printAll - printFound.")); |
| 123 | + Serial.println(F("\th = toggle header - noHeader.")); |
| 124 | + Serial.println(F("\t? = help - this page")); |
| 125 | + Serial.println(); |
| 126 | +} |
| 127 | + |
| 128 | + |
| 129 | +void I2Cscan() |
| 130 | +{ |
| 131 | + startScan = millis(); |
| 132 | + uint8_t count = 0; |
| 133 | + |
| 134 | + if (header) |
| 135 | + { |
| 136 | + Serial.print(F("TIME\tDEC\tHEX\t")); |
| 137 | + for (uint8_t s = 0; s < speeds; s++) |
| 138 | + { |
| 139 | + Serial.print(F("\t")); |
| 140 | + Serial.print(speed[s]); |
| 141 | + } |
| 142 | + Serial.println(F("\t[KHz]")); |
| 143 | + for (uint8_t s = 0; s < speeds + 5; s++) |
| 144 | + { |
| 145 | + Serial.print(F("--------")); |
| 146 | + } |
| 147 | + Serial.println(); |
| 148 | + } |
| 149 | + |
| 150 | + // TEST |
| 151 | + for (uint8_t address = 0; address < 128; address++) |
| 152 | + { |
| 153 | + bool printLine = printAll; |
| 154 | + bool found[speeds]; |
| 155 | + bool fnd = false; |
| 156 | + |
| 157 | + for (uint8_t s = 0; s < speeds ; s++) |
| 158 | + { |
| 159 | + TWBR = (F_CPU/(speed[s]*1000) - 16)/2; |
| 160 | + Wire.beginTransmission (address); |
| 161 | + found[s] = (Wire.endTransmission () == 0); |
| 162 | + fnd |= found[s]; |
| 163 | + // give device 5 millis |
| 164 | + if (fnd && delayFlag) delay(RESTORE_LATENCY); |
| 165 | + } |
| 166 | + |
| 167 | + if (fnd) count++; |
| 168 | + printLine |= fnd; |
| 169 | + |
| 170 | + if (printLine) |
| 171 | + { |
| 172 | + Serial.print(millis()); |
| 173 | + Serial.print(F("\t")); |
| 174 | + Serial.print(address, DEC); |
| 175 | + Serial.print(F("\t0x")); |
| 176 | + Serial.print(address, HEX); |
| 177 | + Serial.print(F("\t")); |
| 178 | + |
| 179 | + for (uint8_t s = 0; s < speeds ; s++) |
| 180 | + { |
| 181 | + Serial.print(F("\t")); |
| 182 | + Serial.print(found[s]? F("V"):F(".")); |
| 183 | + } |
| 184 | + Serial.println(); |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + stopScan = millis(); |
| 189 | + if (header) |
| 190 | + { |
| 191 | + Serial.println(); |
| 192 | + Serial.print(count); |
| 193 | + Serial.print(F(" devices found in ")); |
| 194 | + Serial.print(stopScan - startScan); |
| 195 | + Serial.println(F(" milliseconds.")); |
| 196 | + } |
| 197 | +} |
| 198 | + |
0 commit comments