|
| 1 | +#include <Wire.h> |
| 2 | +#include <i2cEncoderMiniLib.h> |
| 3 | + |
| 4 | +/* This example is used for change the address of the I2C Encoder Mini |
| 5 | + Connections with Arduino UNO: |
| 6 | + - -> GND |
| 7 | + + -> 5V |
| 8 | + SDA -> A4 |
| 9 | + SCL -> A5 |
| 10 | +
|
| 11 | + The COM boudrate is 115200. |
| 12 | + You can use the command 'S' for find the I2C encoder mini on the I2C bus. |
| 13 | + Or you can write directly the address in decimal or hexadecimal. |
| 14 | + After after you need to write the new address in the same way. |
| 15 | + Example: |
| 16 | +
|
| 17 | + **** I2C Encoder Address changer! **** |
| 18 | + All the devices must have different address! |
| 19 | + Avaiable commands: |
| 20 | + S: for searching the I2C encoder mini |
| 21 | + 0xXX or XXX: Address of the target device in hex or decimal |
| 22 | +
|
| 23 | + S |
| 24 | + |
| 25 | + Scanning.... |
| 26 | + I2C Encoder mini found at 33 ( 0x21 ) !! |
| 27 | + |
| 28 | + |
| 29 | + All the devices must have different address! |
| 30 | + Avaiable commands: |
| 31 | + S: for searching the I2C encoder mini |
| 32 | + 0xXX or XXX: Address of the target device in hex or decimal |
| 33 | +
|
| 34 | + 0x21 |
| 35 | + |
| 36 | + Insert the desired address in hex or decimal: |
| 37 | +
|
| 38 | + 24 |
| 39 | + |
| 40 | + Changing address from 33 ( 0x21 ) to 24 ( 0x18 ) |
| 41 | + Address Changed! |
| 42 | +
|
| 43 | +*/ |
| 44 | + |
| 45 | +void ChangeAddress(uint8_t add); |
| 46 | +int8_t NewAdd(void); |
| 47 | +int8_t CommadParser(void); |
| 48 | +int8_t NumberParser(char buff[]); |
| 49 | +bool CommandRead(void); |
| 50 | +void Search(void); |
| 51 | +void PrintHEX(uint8_t i); |
| 52 | + |
| 53 | +i2cEncoderMiniLib Encoder(0x20); |
| 54 | + |
| 55 | + |
| 56 | +char c_buff[5] = {0}; |
| 57 | +uint8_t c_buff_cnt = 0; |
| 58 | + |
| 59 | +int8_t t_add; |
| 60 | + |
| 61 | + |
| 62 | +void setup(void) { |
| 63 | + Wire.begin(); |
| 64 | + Serial.begin(115200); |
| 65 | + Serial.println("\n\n**** I2C Encoder Address changer! ****"); |
| 66 | +} |
| 67 | + |
| 68 | +void loop() { |
| 69 | + Serial.println("All the devices must have different address!"); |
| 70 | + Serial.println("Avaiable commands:"); |
| 71 | + Serial.println(" S: for searching the I2C encoder mini"); |
| 72 | + Serial.println(" 0xXX or XXX: Address of the target device in hex or decimal\n"); |
| 73 | + |
| 74 | + t_add = CommadParser(); |
| 75 | + if (t_add <= 0) { |
| 76 | + switch (t_add) { |
| 77 | + case -1: |
| 78 | + Serial.println("Wrong address range!"); |
| 79 | + break; |
| 80 | + |
| 81 | + case -2: |
| 82 | + Serial.println("Wrong command!"); |
| 83 | + break; |
| 84 | + |
| 85 | + case -3: |
| 86 | + Search(); |
| 87 | + break; |
| 88 | + } |
| 89 | + } else { |
| 90 | + Encoder.address = t_add; |
| 91 | + if (Encoder.readIDCode() == 0x39) { |
| 92 | + Serial.println("Insert the desired address in hex or decimal: "); |
| 93 | + t_add = NewAdd(); |
| 94 | + if (t_add <= 0) { |
| 95 | + Serial.println("Incorrect Address!"); |
| 96 | + } else { |
| 97 | + ChangeAddress(t_add); |
| 98 | + } |
| 99 | + } else { |
| 100 | + Serial.println("No device at that address!"); |
| 101 | + } |
| 102 | + } |
| 103 | + Serial.print("\n\n"); |
| 104 | +} |
| 105 | + |
| 106 | + |
| 107 | +void ChangeAddress(uint8_t add) { |
| 108 | + Serial.print("Changing address from "); |
| 109 | + Serial.print(Encoder.address); |
| 110 | + Serial.print(" ( "); |
| 111 | + PrintHEX(Encoder.address); |
| 112 | + Serial.print(" ) to "); |
| 113 | + Serial.print(add); |
| 114 | + Serial.print(" ( "); |
| 115 | + PrintHEX(add); |
| 116 | + Serial.println(" )"); |
| 117 | + Encoder.ChangeI2CAddress(add); |
| 118 | + // delay(1000); |
| 119 | + Encoder.reset(); |
| 120 | + Encoder.address = add; |
| 121 | + if (Encoder.readIDCode() == 0x39) { |
| 122 | + Serial.println("Address Changed!"); |
| 123 | + } else { |
| 124 | + Serial.println("Error in changing address!"); |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | + |
| 129 | +int8_t NewAdd(void) { |
| 130 | + |
| 131 | + if (CommandRead() == true) { |
| 132 | + return (NumberParser(c_buff)); |
| 133 | + } else { |
| 134 | + return -1; |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | + |
| 139 | +int8_t CommadParser(void) { |
| 140 | + |
| 141 | + if (CommandRead() == true) { |
| 142 | + if ( c_buff[0] == 'S' && c_buff[1] == '\n') { |
| 143 | + return -3; |
| 144 | + } |
| 145 | + return (NumberParser(c_buff)); |
| 146 | + } else { |
| 147 | + return -2; |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | + |
| 152 | +int8_t NumberParser(char buff[]) { |
| 153 | + int8_t temp; |
| 154 | + if ( buff[0] == '0' && buff[1] == 'x') { |
| 155 | + temp = (int8_t)strtol(buff, NULL, 16); |
| 156 | + } else { |
| 157 | + temp = (int8_t)strtol(buff, NULL, 10); |
| 158 | + } |
| 159 | + |
| 160 | + if (temp <= 0) { |
| 161 | + return -1; |
| 162 | + } else { |
| 163 | + return temp; |
| 164 | + } |
| 165 | + |
| 166 | +} |
| 167 | + |
| 168 | + |
| 169 | +bool CommandRead(void) { |
| 170 | + c_buff_cnt = 0; |
| 171 | + while(Serial. read() >= 0); |
| 172 | + while (c_buff_cnt < 5) { |
| 173 | + if (Serial.available()) { |
| 174 | + char inChar = (char)Serial.read(); |
| 175 | + c_buff[c_buff_cnt] = inChar; |
| 176 | + c_buff_cnt++; |
| 177 | + if (inChar == '\n') |
| 178 | + return true; |
| 179 | + } |
| 180 | + } |
| 181 | + return false; |
| 182 | +} |
| 183 | + |
| 184 | + |
| 185 | +void Search(void) { |
| 186 | + uint8_t i, error; |
| 187 | + Serial.println("Scanning...."); |
| 188 | + for (i = 0; i < 0x80; i++ ) |
| 189 | + { |
| 190 | + Wire.beginTransmission(i); |
| 191 | + error = Wire.endTransmission(); |
| 192 | + |
| 193 | + if (error == 0) |
| 194 | + { |
| 195 | + Encoder.address = i; |
| 196 | + if (Encoder.readIDCode() == 0x39) { |
| 197 | + Serial.print("I2C Encoder mini found at "); |
| 198 | + Serial.print(Encoder.address); |
| 199 | + Serial.print(" ( "); |
| 200 | + PrintHEX(Encoder.address); |
| 201 | + Serial.print(" )"); |
| 202 | + Serial.println(" !!"); |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + } |
| 207 | +} |
| 208 | + |
| 209 | + |
| 210 | +void PrintHEX(uint8_t i) { |
| 211 | + Serial.print("0x"); |
| 212 | + if (i < 16) |
| 213 | + Serial.print("0"); |
| 214 | + Serial.print(i, HEX); |
| 215 | +} |
0 commit comments