I have spent so much time looking into how to change the address of a MLX90614 sensor for use of multiple sensors, so I think it might be helpful to somebody.
Fork of MLX90614 IR_Temp_Sensor (change address) by
main.cpp@1:117cc19bbd83, 2016-03-05 (annotated)
- Committer:
- ronixas
- Date:
- Sat Mar 05 18:36:10 2016 +0000
- Revision:
- 1:117cc19bbd83
- Parent:
- 0:039f25b3de3e
x
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ronixas | 0:039f25b3de3e | 1 | #include "mbed.h" |
ronixas | 0:039f25b3de3e | 2 | |
ronixas | 0:039f25b3de3e | 3 | |
ronixas | 0:039f25b3de3e | 4 | DigitalOut myled(LED1); //displays I2C wait |
ronixas | 0:039f25b3de3e | 5 | |
ronixas | 0:039f25b3de3e | 6 | I2C i2c(p28,p27); //sda,scl |
ronixas | 0:039f25b3de3e | 7 | Serial pc(USBTX,USBRX); //serial usb config |
ronixas | 0:039f25b3de3e | 8 | |
ronixas | 0:039f25b3de3e | 9 | bool ch,ch2,ch3; |
ronixas | 0:039f25b3de3e | 10 | float temp; //temperature in degrees C |
ronixas | 0:039f25b3de3e | 11 | char p1,p2; |
ronixas | 0:039f25b3de3e | 12 | |
ronixas | 0:039f25b3de3e | 13 | /* Change MLX90614 address |
ronixas | 0:039f25b3de3e | 14 | |
ronixas | 0:039f25b3de3e | 15 | 1. clear old address |
ronixas | 0:039f25b3de3e | 16 | 2. set new address |
ronixas | 0:039f25b3de3e | 17 | 3. check if the change happenned |
ronixas | 0:039f25b3de3e | 18 | 4. power cycle sensor (reconnect power) |
ronixas | 0:039f25b3de3e | 19 | |
ronixas | 0:039f25b3de3e | 20 | */ |
ronixas | 0:039f25b3de3e | 21 | |
ronixas | 0:039f25b3de3e | 22 | int main() { |
ronixas | 0:039f25b3de3e | 23 | while (1) { |
ronixas | 0:039f25b3de3e | 24 | |
ronixas | 0:039f25b3de3e | 25 | //----clear old address------------------ |
ronixas | 0:039f25b3de3e | 26 | |
ronixas | 0:039f25b3de3e | 27 | // make sure i2c is inactive |
ronixas | 0:039f25b3de3e | 28 | i2c.stop(); |
ronixas | 0:039f25b3de3e | 29 | wait(0.1); |
ronixas | 0:039f25b3de3e | 30 | //send start bit |
ronixas | 0:039f25b3de3e | 31 | i2c.start(); |
ronixas | 0:039f25b3de3e | 32 | // address the sensor using universal address 0x00 not its default 0x5A |
ronixas | 0:039f25b3de3e | 33 | ch = i2c.write(0x00); // last bit 0 indicates write |
ronixas | 0:039f25b3de3e | 34 | // prints if sensor acknowledges |
ronixas | 0:039f25b3de3e | 35 | if (!ch){ pc.printf("no ack\n"); } |
ronixas | 0:039f25b3de3e | 36 | else pc.printf("ack 0x00\n"); |
ronixas | 0:039f25b3de3e | 37 | // write command |
ronixas | 0:039f25b3de3e | 38 | // EEPROM address. Datasheet says 0x0E BUT!!! accessing EEPROM requires ading 0x20. Hence, 0x2E |
ronixas | 0:039f25b3de3e | 39 | ch2 = i2c.write(0x2E); |
ronixas | 0:039f25b3de3e | 40 | // prints if sensor acknowledges |
ronixas | 0:039f25b3de3e | 41 | if (!ch2) pc.printf("no ack address location\n"); |
ronixas | 0:039f25b3de3e | 42 | pc.printf("ack 0x0E\n"); |
ronixas | 0:039f25b3de3e | 43 | // delete old address |
ronixas | 0:039f25b3de3e | 44 | // LSB |
ronixas | 0:039f25b3de3e | 45 | i2c.write(0x00); |
ronixas | 0:039f25b3de3e | 46 | // MSB |
ronixas | 0:039f25b3de3e | 47 | i2c.write(0x00); |
ronixas | 0:039f25b3de3e | 48 | // use CRC-8 calculator to obtain PEC bit http://www.sunshine2k.de/coding/javascript/crc/crc_js.html |
ronixas | 0:039f25b3de3e | 49 | // I send 2E0000 --> PEC 0x6F |
ronixas | 0:039f25b3de3e | 50 | ch3 = i2c.write(0x6F); |
ronixas | 0:039f25b3de3e | 51 | // prints if sensor acknowledges |
ronixas | 0:039f25b3de3e | 52 | if(!ch3) pc.printf("no PEC\n"); |
ronixas | 0:039f25b3de3e | 53 | else pc.printf("PEC success\n"); |
ronixas | 0:039f25b3de3e | 54 | // release the bus |
ronixas | 0:039f25b3de3e | 55 | i2c.stop(); |
ronixas | 0:039f25b3de3e | 56 | wait(1); |
ronixas | 0:039f25b3de3e | 57 | |
ronixas | 0:039f25b3de3e | 58 | //-----set new address---------------- |
ronixas | 0:039f25b3de3e | 59 | |
ronixas | 0:039f25b3de3e | 60 | // send start bit |
ronixas | 0:039f25b3de3e | 61 | i2c.start(); |
ronixas | 0:039f25b3de3e | 62 | // send universal device address 0x00 |
ronixas | 0:039f25b3de3e | 63 | i2c.write(0x00); |
ronixas | 0:039f25b3de3e | 64 | // prints if sensor acknowledges |
ronixas | 0:039f25b3de3e | 65 | if (!ch) pc.printf("no ack\n"); |
ronixas | 0:039f25b3de3e | 66 | else pc.printf("ack\n"); |
ronixas | 0:039f25b3de3e | 67 | // send EEPROM location for slave address |
ronixas | 0:039f25b3de3e | 68 | ch2 = i2c.write(0x2E); |
ronixas | 0:039f25b3de3e | 69 | // prints if sensor acknowledges |
ronixas | 0:039f25b3de3e | 70 | if (!ch2) pc.printf("no ack slave address location\n"); |
ronixas | 0:039f25b3de3e | 71 | else pc.printf("ack 2\n"); |
ronixas | 0:039f25b3de3e | 72 | //write new address anything between 0x0000 and 0x007F (default 0x5A, PEC 0xE1) |
ronixas | 0:039f25b3de3e | 73 | // LSB |
ronixas | 0:039f25b3de3e | 74 | i2c.write(0x04); |
ronixas | 0:039f25b3de3e | 75 | // MSB |
ronixas | 0:039f25b3de3e | 76 | i2c.write(0x00); |
ronixas | 0:039f25b3de3e | 77 | // send PEC calculated from CRC-8 |
ronixas | 0:039f25b3de3e | 78 | // I send 2E0400 -> PEC 0x3B |
ronixas | 0:039f25b3de3e | 79 | i2c.write(0x3B); |
ronixas | 0:039f25b3de3e | 80 | // release the bus |
ronixas | 0:039f25b3de3e | 81 | i2c.stop(); |
ronixas | 0:039f25b3de3e | 82 | wait(1); |
ronixas | 0:039f25b3de3e | 83 | |
ronixas | 0:039f25b3de3e | 84 | //----read address to make sure it worked------------ |
ronixas | 0:039f25b3de3e | 85 | |
ronixas | 0:039f25b3de3e | 86 | // send start bit |
ronixas | 0:039f25b3de3e | 87 | i2c.start(); |
ronixas | 0:039f25b3de3e | 88 | // send universal address |
ronixas | 0:039f25b3de3e | 89 | ch = i2c.write(0x00); |
ronixas | 0:039f25b3de3e | 90 | // prints if sensor acknowledges |
ronixas | 0:039f25b3de3e | 91 | if (!ch) pc.printf("no ack\n"); |
ronixas | 0:039f25b3de3e | 92 | else pc.printf("ack\n"); |
ronixas | 0:039f25b3de3e | 93 | // send EEPROM slave location address |
ronixas | 0:039f25b3de3e | 94 | ch2 = i2c.write(0x2E); |
ronixas | 0:039f25b3de3e | 95 | // send repeated start |
ronixas | 0:039f25b3de3e | 96 | i2c.start(); |
ronixas | 0:039f25b3de3e | 97 | // send universal address 0x00 (seven bit) and use 1 as the eight bit -> 0x01 |
ronixas | 0:039f25b3de3e | 98 | i2c.write(0x01); // last bit 1 indicates read request |
ronixas | 0:039f25b3de3e | 99 | // read LSB |
ronixas | 0:039f25b3de3e | 100 | p1 = i2c.read(1); |
ronixas | 0:039f25b3de3e | 101 | // read MSB |
ronixas | 0:039f25b3de3e | 102 | p2 = i2c.read(1); |
ronixas | 0:039f25b3de3e | 103 | // read PEC |
ronixas | 0:039f25b3de3e | 104 | i2c.read(0); |
ronixas | 0:039f25b3de3e | 105 | // release the bus |
ronixas | 0:039f25b3de3e | 106 | i2c.stop(); |
ronixas | 0:039f25b3de3e | 107 | wait(0.1); |
ronixas | 0:039f25b3de3e | 108 | // print hex |
ronixas | 0:039f25b3de3e | 109 | pc.printf("0x%x%x\nMSB 0x%x, LSB 0x%x\n", p2, p1, p2, p1); |
ronixas | 0:039f25b3de3e | 110 | wait(10); |
ronixas | 0:039f25b3de3e | 111 | |
ronixas | 0:039f25b3de3e | 112 | //-----now turn of the sensor for the change to take effect----do not access it without turning it off first!!!!------- |
ronixas | 0:039f25b3de3e | 113 | } |
ronixas | 0:039f25b3de3e | 114 | } |