Chuck Swiger
/
FTHRChirpChangeAddr
Change i2c address in the Chirp! sensor
main.cpp@0:0b271f6ca9be, 2018-07-31 (annotated)
- Committer:
- cswiger
- Date:
- Tue Jul 31 18:17:05 2018 +0000
- Revision:
- 0:0b271f6ca9be
Change Chirp! i2c address
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cswiger | 0:0b271f6ca9be | 1 | #include "mbed.h" |
cswiger | 0:0b271f6ca9be | 2 | |
cswiger | 0:0b271f6ca9be | 3 | // Change Chirp! Address - default starts with 0x20 |
cswiger | 0:0b271f6ca9be | 4 | |
cswiger | 0:0b271f6ca9be | 5 | I2C i2c(P3_4, P3_5); |
cswiger | 0:0b271f6ca9be | 6 | |
cswiger | 0:0b271f6ca9be | 7 | int addr = 0x20; |
cswiger | 0:0b271f6ca9be | 8 | int addr8 = addr << 1; |
cswiger | 0:0b271f6ca9be | 9 | |
cswiger | 0:0b271f6ca9be | 10 | int main() { |
cswiger | 0:0b271f6ca9be | 11 | char cmd[2]; |
cswiger | 0:0b271f6ca9be | 12 | wait(30.0); // give user time to connect to serial port |
cswiger | 0:0b271f6ca9be | 13 | |
cswiger | 0:0b271f6ca9be | 14 | cmd[0] = 0x01; // change address command |
cswiger | 0:0b271f6ca9be | 15 | cmd[1] = 0x21; // new address |
cswiger | 0:0b271f6ca9be | 16 | i2c.write(addr8, cmd, 2); |
cswiger | 0:0b271f6ca9be | 17 | |
cswiger | 0:0b271f6ca9be | 18 | // update address |
cswiger | 0:0b271f6ca9be | 19 | // The above does successfully change the address, but 0x21 won't read until the device is reset |
cswiger | 0:0b271f6ca9be | 20 | addr = 0x20; // try 0x20 here, the device is still 0x20 and should read 0x21 from eeprom |
cswiger | 0:0b271f6ca9be | 21 | addr8 = addr << 1; |
cswiger | 0:0b271f6ca9be | 22 | |
cswiger | 0:0b271f6ca9be | 23 | wait(2.0); |
cswiger | 0:0b271f6ca9be | 24 | |
cswiger | 0:0b271f6ca9be | 25 | cmd[0] = 0x02; // read address from eeprom |
cswiger | 0:0b271f6ca9be | 26 | i2c.write(addr8, cmd, 1); |
cswiger | 0:0b271f6ca9be | 27 | i2c.read(addr8, cmd, 1); |
cswiger | 0:0b271f6ca9be | 28 | |
cswiger | 0:0b271f6ca9be | 29 | printf("New address read: %d",cmd[0]); |
cswiger | 0:0b271f6ca9be | 30 | |
cswiger | 0:0b271f6ca9be | 31 | } |
cswiger | 0:0b271f6ca9be | 32 |