![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Q
main.cpp@0:d66d4aaa919f, 2019-03-14 (annotated)
- Committer:
- nemanja1994
- Date:
- Thu Mar 14 18:10:31 2019 +0000
- Revision:
- 0:d66d4aaa919f
Gg
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nemanja1994 | 0:d66d4aaa919f | 1 | #include "mbed.h" |
nemanja1994 | 0:d66d4aaa919f | 2 | |
nemanja1994 | 0:d66d4aaa919f | 3 | #define IrThermo_Addr (0x5A<<1) |
nemanja1994 | 0:d66d4aaa919f | 4 | #define Tamb 0x06 |
nemanja1994 | 0:d66d4aaa919f | 5 | #define Tobj 0x07 |
nemanja1994 | 0:d66d4aaa919f | 6 | |
nemanja1994 | 0:d66d4aaa919f | 7 | I2C i2c(D14, D15); |
nemanja1994 | 0:d66d4aaa919f | 8 | DigitalOut myled(LED1); |
nemanja1994 | 0:d66d4aaa919f | 9 | Serial console(SERIAL_TX, SERIAL_RX); |
nemanja1994 | 0:d66d4aaa919f | 10 | |
nemanja1994 | 0:d66d4aaa919f | 11 | int temperature=0; |
nemanja1994 | 0:d66d4aaa919f | 12 | char data_read[2]; |
nemanja1994 | 0:d66d4aaa919f | 13 | char data_write[1]= {Tobj}; |
nemanja1994 | 0:d66d4aaa919f | 14 | char data_add[1]= {(0x2E)}; |
nemanja1994 | 0:d66d4aaa919f | 15 | |
nemanja1994 | 0:d66d4aaa919f | 16 | void changeAddress() |
nemanja1994 | 0:d66d4aaa919f | 17 | { |
nemanja1994 | 0:d66d4aaa919f | 18 | //char newAddr[2]= {0x5E,0x00}; |
nemanja1994 | 0:d66d4aaa919f | 19 | char p1,p2; |
nemanja1994 | 0:d66d4aaa919f | 20 | bool ch,ch2,ch3; |
nemanja1994 | 0:d66d4aaa919f | 21 | |
nemanja1994 | 0:d66d4aaa919f | 22 | //----read address to make sure it worked------------ |
nemanja1994 | 0:d66d4aaa919f | 23 | |
nemanja1994 | 0:d66d4aaa919f | 24 | // send start bit |
nemanja1994 | 0:d66d4aaa919f | 25 | i2c.start(); |
nemanja1994 | 0:d66d4aaa919f | 26 | // send universal address |
nemanja1994 | 0:d66d4aaa919f | 27 | ch = i2c.write(0x00); |
nemanja1994 | 0:d66d4aaa919f | 28 | // prints if sensor acknowledges |
nemanja1994 | 0:d66d4aaa919f | 29 | if (!ch) console.printf("no ack\n"); |
nemanja1994 | 0:d66d4aaa919f | 30 | else console.printf("ack\n"); |
nemanja1994 | 0:d66d4aaa919f | 31 | // send EEPROM slave location address |
nemanja1994 | 0:d66d4aaa919f | 32 | ch2 = i2c.write(0x2E); |
nemanja1994 | 0:d66d4aaa919f | 33 | // send repeated start |
nemanja1994 | 0:d66d4aaa919f | 34 | i2c.start(); |
nemanja1994 | 0:d66d4aaa919f | 35 | // send universal address 0x00 (seven bit) and use 1 as the eight bit -> 0x01 |
nemanja1994 | 0:d66d4aaa919f | 36 | i2c.write(0x01); // last bit 1 indicates read request |
nemanja1994 | 0:d66d4aaa919f | 37 | // read LSB |
nemanja1994 | 0:d66d4aaa919f | 38 | p1 = i2c.read(1); |
nemanja1994 | 0:d66d4aaa919f | 39 | // read MSB |
nemanja1994 | 0:d66d4aaa919f | 40 | p2 = i2c.read(1); |
nemanja1994 | 0:d66d4aaa919f | 41 | // read PEC |
nemanja1994 | 0:d66d4aaa919f | 42 | i2c.read(0); |
nemanja1994 | 0:d66d4aaa919f | 43 | // release the bus |
nemanja1994 | 0:d66d4aaa919f | 44 | i2c.stop(); |
nemanja1994 | 0:d66d4aaa919f | 45 | wait(0.1); |
nemanja1994 | 0:d66d4aaa919f | 46 | // print hex |
nemanja1994 | 0:d66d4aaa919f | 47 | console.printf("0x%x%x\nMSB 0x%x, LSB 0x%x\n", p2, p1, p2, p1); |
nemanja1994 | 0:d66d4aaa919f | 48 | wait(10); |
nemanja1994 | 0:d66d4aaa919f | 49 | |
nemanja1994 | 0:d66d4aaa919f | 50 | |
nemanja1994 | 0:d66d4aaa919f | 51 | // make sure i2c is inactive |
nemanja1994 | 0:d66d4aaa919f | 52 | i2c.stop(); |
nemanja1994 | 0:d66d4aaa919f | 53 | wait(0.1); |
nemanja1994 | 0:d66d4aaa919f | 54 | //send start bit |
nemanja1994 | 0:d66d4aaa919f | 55 | i2c.start(); |
nemanja1994 | 0:d66d4aaa919f | 56 | // address the sensor using universal address 0x00 not its default 0x5A |
nemanja1994 | 0:d66d4aaa919f | 57 | ch = i2c.write(0x00); // last bit 0 indicates write |
nemanja1994 | 0:d66d4aaa919f | 58 | // prints if sensor acknowledges |
nemanja1994 | 0:d66d4aaa919f | 59 | if (!ch) { |
nemanja1994 | 0:d66d4aaa919f | 60 | console.printf("no ack\n"); |
nemanja1994 | 0:d66d4aaa919f | 61 | } else console.printf("ack 0x00\n"); |
nemanja1994 | 0:d66d4aaa919f | 62 | // write command |
nemanja1994 | 0:d66d4aaa919f | 63 | // EEPROM address. Datasheet says 0x0E BUT!!! accessing EEPROM requires ading 0x20. Hence, 0x2E |
nemanja1994 | 0:d66d4aaa919f | 64 | ch2 = i2c.write(0x2E); |
nemanja1994 | 0:d66d4aaa919f | 65 | // prints if sensor acknowledges |
nemanja1994 | 0:d66d4aaa919f | 66 | if (!ch2) console.printf("no ack address location\n"); |
nemanja1994 | 0:d66d4aaa919f | 67 | console.printf("ack 0x0E\n"); |
nemanja1994 | 0:d66d4aaa919f | 68 | // delete old address |
nemanja1994 | 0:d66d4aaa919f | 69 | // LSB |
nemanja1994 | 0:d66d4aaa919f | 70 | i2c.write(0x00); |
nemanja1994 | 0:d66d4aaa919f | 71 | // MSB |
nemanja1994 | 0:d66d4aaa919f | 72 | i2c.write(0x00); |
nemanja1994 | 0:d66d4aaa919f | 73 | // use CRC-8 calculator to obtain PEC bit http://www.sunshine2k.de/coding/javascript/crc/crc_js.html |
nemanja1994 | 0:d66d4aaa919f | 74 | // I send 2E0000 --> PEC 0x6F |
nemanja1994 | 0:d66d4aaa919f | 75 | ch3 = i2c.write(0x6F); |
nemanja1994 | 0:d66d4aaa919f | 76 | // prints if sensor acknowledges |
nemanja1994 | 0:d66d4aaa919f | 77 | if(!ch3) console.printf("no PEC\n"); |
nemanja1994 | 0:d66d4aaa919f | 78 | else console.printf("PEC success\n"); |
nemanja1994 | 0:d66d4aaa919f | 79 | // release the bus |
nemanja1994 | 0:d66d4aaa919f | 80 | i2c.stop(); |
nemanja1994 | 0:d66d4aaa919f | 81 | wait(1); |
nemanja1994 | 0:d66d4aaa919f | 82 | |
nemanja1994 | 0:d66d4aaa919f | 83 | //-----set new address---------------- |
nemanja1994 | 0:d66d4aaa919f | 84 | |
nemanja1994 | 0:d66d4aaa919f | 85 | // send start bit |
nemanja1994 | 0:d66d4aaa919f | 86 | i2c.start(); |
nemanja1994 | 0:d66d4aaa919f | 87 | // send universal device address 0x00 |
nemanja1994 | 0:d66d4aaa919f | 88 | i2c.write(0x00); |
nemanja1994 | 0:d66d4aaa919f | 89 | // prints if sensor acknowledges |
nemanja1994 | 0:d66d4aaa919f | 90 | if (!ch) console.printf("no ack\n"); |
nemanja1994 | 0:d66d4aaa919f | 91 | else console.printf("ack\n"); |
nemanja1994 | 0:d66d4aaa919f | 92 | // send EEPROM location for slave address |
nemanja1994 | 0:d66d4aaa919f | 93 | ch2 = i2c.write(0x2E); |
nemanja1994 | 0:d66d4aaa919f | 94 | // prints if sensor acknowledges |
nemanja1994 | 0:d66d4aaa919f | 95 | if (!ch2) console.printf("no ack slave address location\n"); |
nemanja1994 | 0:d66d4aaa919f | 96 | else console.printf("ack 2\n"); |
nemanja1994 | 0:d66d4aaa919f | 97 | //write new address anything between 0x0000 and 0x007F (default 0x5A, PEC 0xE1) |
nemanja1994 | 0:d66d4aaa919f | 98 | // LSB |
nemanja1994 | 0:d66d4aaa919f | 99 | i2c.write(0x04); |
nemanja1994 | 0:d66d4aaa919f | 100 | // MSB |
nemanja1994 | 0:d66d4aaa919f | 101 | i2c.write(0x00); |
nemanja1994 | 0:d66d4aaa919f | 102 | // send PEC calculated from CRC-8 |
nemanja1994 | 0:d66d4aaa919f | 103 | // I send 2E0400 -> PEC 0x3B |
nemanja1994 | 0:d66d4aaa919f | 104 | i2c.write(0x3B); |
nemanja1994 | 0:d66d4aaa919f | 105 | // release the bus |
nemanja1994 | 0:d66d4aaa919f | 106 | i2c.stop(); |
nemanja1994 | 0:d66d4aaa919f | 107 | wait(1); |
nemanja1994 | 0:d66d4aaa919f | 108 | |
nemanja1994 | 0:d66d4aaa919f | 109 | //----read address to make sure it worked------------ |
nemanja1994 | 0:d66d4aaa919f | 110 | |
nemanja1994 | 0:d66d4aaa919f | 111 | // send start bit |
nemanja1994 | 0:d66d4aaa919f | 112 | i2c.start(); |
nemanja1994 | 0:d66d4aaa919f | 113 | // send universal address |
nemanja1994 | 0:d66d4aaa919f | 114 | ch = i2c.write(0x00); |
nemanja1994 | 0:d66d4aaa919f | 115 | // prints if sensor acknowledges |
nemanja1994 | 0:d66d4aaa919f | 116 | if (!ch) console.printf("no ack\n"); |
nemanja1994 | 0:d66d4aaa919f | 117 | else console.printf("ack\n"); |
nemanja1994 | 0:d66d4aaa919f | 118 | // send EEPROM slave location address |
nemanja1994 | 0:d66d4aaa919f | 119 | ch2 = i2c.write(0x2E); |
nemanja1994 | 0:d66d4aaa919f | 120 | // send repeated start |
nemanja1994 | 0:d66d4aaa919f | 121 | i2c.start(); |
nemanja1994 | 0:d66d4aaa919f | 122 | // send universal address 0x00 (seven bit) and use 1 as the eight bit -> 0x01 |
nemanja1994 | 0:d66d4aaa919f | 123 | i2c.write(0x01); // last bit 1 indicates read request |
nemanja1994 | 0:d66d4aaa919f | 124 | // read LSB |
nemanja1994 | 0:d66d4aaa919f | 125 | p1 = i2c.read(1); |
nemanja1994 | 0:d66d4aaa919f | 126 | // read MSB |
nemanja1994 | 0:d66d4aaa919f | 127 | p2 = i2c.read(1); |
nemanja1994 | 0:d66d4aaa919f | 128 | // read PEC |
nemanja1994 | 0:d66d4aaa919f | 129 | i2c.read(0); |
nemanja1994 | 0:d66d4aaa919f | 130 | // release the bus |
nemanja1994 | 0:d66d4aaa919f | 131 | i2c.stop(); |
nemanja1994 | 0:d66d4aaa919f | 132 | wait(0.1); |
nemanja1994 | 0:d66d4aaa919f | 133 | // print hex |
nemanja1994 | 0:d66d4aaa919f | 134 | console.printf("0x%x%x\nMSB 0x%x, LSB 0x%x\n", p2, p1, p2, p1); |
nemanja1994 | 0:d66d4aaa919f | 135 | wait(10); |
nemanja1994 | 0:d66d4aaa919f | 136 | } |
nemanja1994 | 0:d66d4aaa919f | 137 | |
nemanja1994 | 0:d66d4aaa919f | 138 | int main() |
nemanja1994 | 0:d66d4aaa919f | 139 | { |
nemanja1994 | 0:d66d4aaa919f | 140 | console.printf("IrThermo Test\n"); |
nemanja1994 | 0:d66d4aaa919f | 141 | i2c.write(IrThermo_Addr, data_add, 1, 1); |
nemanja1994 | 0:d66d4aaa919f | 142 | i2c.read(IrThermo_Addr, data_read, 2, 0); |
nemanja1994 | 0:d66d4aaa919f | 143 | console.printf("Sensor Address: %x\n",data_read[0]); |
nemanja1994 | 0:d66d4aaa919f | 144 | //changeAddress(); |
nemanja1994 | 0:d66d4aaa919f | 145 | |
nemanja1994 | 0:d66d4aaa919f | 146 | while(1) { |
nemanja1994 | 0:d66d4aaa919f | 147 | i2c.write(IrThermo_Addr, data_write, 1, 1); |
nemanja1994 | 0:d66d4aaa919f | 148 | i2c.read(IrThermo_Addr, data_read, 2, 0); |
nemanja1994 | 0:d66d4aaa919f | 149 | |
nemanja1994 | 0:d66d4aaa919f | 150 | console.printf("Raw Data: %x %x\n",(data_read[0]), (data_read[1])); |
nemanja1994 | 0:d66d4aaa919f | 151 | temperature = (int) ((int)data_read[1] << 8) | data_read[0]; |
nemanja1994 | 0:d66d4aaa919f | 152 | temperature = (temperature * 0.02 - 273.15); |
nemanja1994 | 0:d66d4aaa919f | 153 | if (data_write[0]==Tamb) { |
nemanja1994 | 0:d66d4aaa919f | 154 | console.printf("Ambient temperature= %d\n", temperature); |
nemanja1994 | 0:d66d4aaa919f | 155 | } else { |
nemanja1994 | 0:d66d4aaa919f | 156 | console.printf("Object temperature= %d\n", temperature); |
nemanja1994 | 0:d66d4aaa919f | 157 | } |
nemanja1994 | 0:d66d4aaa919f | 158 | myled = !myled; |
nemanja1994 | 0:d66d4aaa919f | 159 | wait(1.0); |
nemanja1994 | 0:d66d4aaa919f | 160 | } |
nemanja1994 | 0:d66d4aaa919f | 161 | |
nemanja1994 | 0:d66d4aaa919f | 162 | } |