starting integration with XBee
Dependencies: SDFileSystem mbed
temp_sensor.cpp@3:c611fff05072, 2016-03-15 (annotated)
- Committer:
- jphbergeson
- Date:
- Tue Mar 15 17:52:05 2016 +0000
- Revision:
- 3:c611fff05072
Working with temp sensor
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jphbergeson | 3:c611fff05072 | 1 | #include "temp_sensor.h" |
jphbergeson | 3:c611fff05072 | 2 | |
jphbergeson | 3:c611fff05072 | 3 | /* MCP9808 high accuracy temp sensor from adafruit (no address pins pulled up) */ |
jphbergeson | 3:c611fff05072 | 4 | #define MCP9808_REG_TEMP (0x05) // Temperature Register |
jphbergeson | 3:c611fff05072 | 5 | #define MCP9808_REG_CONF (0x01) // Configuration Register |
jphbergeson | 3:c611fff05072 | 6 | #define MCP9808_ADDR (0x30) // MCP9808 base address 0x18<<1 |
jphbergeson | 3:c611fff05072 | 7 | |
jphbergeson | 3:c611fff05072 | 8 | I2C i2c(PTE25, PTE24); |
jphbergeson | 3:c611fff05072 | 9 | |
jphbergeson | 3:c611fff05072 | 10 | char data_write[3]; |
jphbergeson | 3:c611fff05072 | 11 | char data_read[2]; |
jphbergeson | 3:c611fff05072 | 12 | int tempval; |
jphbergeson | 3:c611fff05072 | 13 | |
jphbergeson | 3:c611fff05072 | 14 | volatile char TempCelsiusDisplay[] = "+abc.dd C"; |
jphbergeson | 3:c611fff05072 | 15 | |
jphbergeson | 3:c611fff05072 | 16 | void init_temp_sensor() { |
jphbergeson | 3:c611fff05072 | 17 | |
jphbergeson | 3:c611fff05072 | 18 | wait(3); |
jphbergeson | 3:c611fff05072 | 19 | i2c.frequency(10000); // default is 100000 |
jphbergeson | 3:c611fff05072 | 20 | |
jphbergeson | 3:c611fff05072 | 21 | /* Configure the Temperature sensor device MCP9808: |
jphbergeson | 3:c611fff05072 | 22 | - Thermostat mode Interrupt not used |
jphbergeson | 3:c611fff05072 | 23 | - Fault tolerance: 0 |
jphbergeson | 3:c611fff05072 | 24 | */ |
jphbergeson | 3:c611fff05072 | 25 | data_write[0] = MCP9808_REG_CONF; |
jphbergeson | 3:c611fff05072 | 26 | data_write[1] = 0x00; // config msb |
jphbergeson | 3:c611fff05072 | 27 | data_write[2] = 0x00; // config lsb |
jphbergeson | 3:c611fff05072 | 28 | int status = i2c.write(MCP9808_ADDR, data_write, 3, 0); |
jphbergeson | 3:c611fff05072 | 29 | if (status != 0) { // Error |
jphbergeson | 3:c611fff05072 | 30 | // pc.printf(" error status = 0x%08x\r\n", status); |
jphbergeson | 3:c611fff05072 | 31 | // while (1) { |
jphbergeson | 3:c611fff05072 | 32 | // myled = !myled; |
jphbergeson | 3:c611fff05072 | 33 | // wait(0.2); |
jphbergeson | 3:c611fff05072 | 34 | // } |
jphbergeson | 3:c611fff05072 | 35 | } |
jphbergeson | 3:c611fff05072 | 36 | |
jphbergeson | 3:c611fff05072 | 37 | } |
jphbergeson | 3:c611fff05072 | 38 | |
jphbergeson | 3:c611fff05072 | 39 | void check_temp() { |
jphbergeson | 3:c611fff05072 | 40 | // Read temperature register |
jphbergeson | 3:c611fff05072 | 41 | data_write[0] = MCP9808_REG_TEMP; |
jphbergeson | 3:c611fff05072 | 42 | i2c.write(MCP9808_ADDR, data_write, 1, 1); // no stop |
jphbergeson | 3:c611fff05072 | 43 | i2c.read(MCP9808_ADDR, data_read, 2, 0); |
jphbergeson | 3:c611fff05072 | 44 | |
jphbergeson | 3:c611fff05072 | 45 | if(data_read[0] & 0xE0) { |
jphbergeson | 3:c611fff05072 | 46 | data_read[0] = data_read[0] & 0x1F; // clear flag bits |
jphbergeson | 3:c611fff05072 | 47 | } |
jphbergeson | 3:c611fff05072 | 48 | if((data_read[0] & 0x10) == 0x10) { |
jphbergeson | 3:c611fff05072 | 49 | data_read[0] = data_read[0] & 0x0F; |
jphbergeson | 3:c611fff05072 | 50 | TempCelsiusDisplay[0] = '-'; |
jphbergeson | 3:c611fff05072 | 51 | tempval = 256 - (data_read[0] << 4) + (data_read[1] >> 4); |
jphbergeson | 3:c611fff05072 | 52 | } else { |
jphbergeson | 3:c611fff05072 | 53 | TempCelsiusDisplay[0] = '+'; |
jphbergeson | 3:c611fff05072 | 54 | tempval = (data_read[0] << 4) + (data_read[1] >> 4); |
jphbergeson | 3:c611fff05072 | 55 | } |
jphbergeson | 3:c611fff05072 | 56 | |
jphbergeson | 3:c611fff05072 | 57 | // fractional part (0.25°C precision) |
jphbergeson | 3:c611fff05072 | 58 | if (data_read[1] & 0x08) { |
jphbergeson | 3:c611fff05072 | 59 | if(data_read[1] & 0x04) { |
jphbergeson | 3:c611fff05072 | 60 | TempCelsiusDisplay[5] = '7'; |
jphbergeson | 3:c611fff05072 | 61 | TempCelsiusDisplay[6] = '5'; |
jphbergeson | 3:c611fff05072 | 62 | } else { |
jphbergeson | 3:c611fff05072 | 63 | TempCelsiusDisplay[5] = '5'; |
jphbergeson | 3:c611fff05072 | 64 | TempCelsiusDisplay[6] = '0'; |
jphbergeson | 3:c611fff05072 | 65 | } |
jphbergeson | 3:c611fff05072 | 66 | } else { |
jphbergeson | 3:c611fff05072 | 67 | if(data_read[1] & 0x04) { |
jphbergeson | 3:c611fff05072 | 68 | TempCelsiusDisplay[5] = '2'; |
jphbergeson | 3:c611fff05072 | 69 | TempCelsiusDisplay[6] = '5'; |
jphbergeson | 3:c611fff05072 | 70 | }else{ |
jphbergeson | 3:c611fff05072 | 71 | TempCelsiusDisplay[5] = '0'; |
jphbergeson | 3:c611fff05072 | 72 | TempCelsiusDisplay[6] = '0'; |
jphbergeson | 3:c611fff05072 | 73 | } |
jphbergeson | 3:c611fff05072 | 74 | } |
jphbergeson | 3:c611fff05072 | 75 | |
jphbergeson | 3:c611fff05072 | 76 | // Integer part |
jphbergeson | 3:c611fff05072 | 77 | TempCelsiusDisplay[1] = (tempval / 100) + 0x30; |
jphbergeson | 3:c611fff05072 | 78 | TempCelsiusDisplay[2] = ((tempval % 100) / 10) + 0x30; |
jphbergeson | 3:c611fff05072 | 79 | TempCelsiusDisplay[3] = ((tempval % 100) % 10) + 0x30; |
jphbergeson | 3:c611fff05072 | 80 | } |
jphbergeson | 3:c611fff05072 | 81 | |
jphbergeson | 3:c611fff05072 | 82 | |
jphbergeson | 3:c611fff05072 | 83 |