Byoungsul Lee
/
Temperature_I2C
i2c temp sensor
Fork of Nucleo_i2c_mcp9808 by
main.cpp@1:404c34e19f93, 2015-03-02 (annotated)
- Committer:
- rkadrmas
- Date:
- Mon Mar 02 20:12:10 2015 +0000
- Revision:
- 1:404c34e19f93
- Parent:
- 0:41f53df528da
- Child:
- 2:43ecb155d581
Nucleo F103 testing Adafruit MCP9808 on i2c at address 0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bcostm | 0:41f53df528da | 1 | #include "mbed.h" |
rkadrmas | 1:404c34e19f93 | 2 | #include "I2C.h" |
rkadrmas | 1:404c34e19f93 | 3 | |
rkadrmas | 1:404c34e19f93 | 4 | /* MCP9808 high accuracy temp sensor from adafruit (no address pins pulled up) */ |
rkadrmas | 1:404c34e19f93 | 5 | #define MCP9808_REG_TEMP (0x05) // Temperature Register |
rkadrmas | 1:404c34e19f93 | 6 | #define MCP9808_REG_CONF (0x01) // Configuration Register |
rkadrmas | 1:404c34e19f93 | 7 | #define MCP9808_ADDR (0x30) // MCP9808 base address 0x18<<1 |
bcostm | 0:41f53df528da | 8 | |
bcostm | 0:41f53df528da | 9 | I2C i2c(I2C_SDA, I2C_SCL); |
bcostm | 0:41f53df528da | 10 | |
bcostm | 0:41f53df528da | 11 | DigitalOut myled(LED1); |
bcostm | 0:41f53df528da | 12 | |
bcostm | 0:41f53df528da | 13 | Serial pc(SERIAL_TX, SERIAL_RX); |
bcostm | 0:41f53df528da | 14 | |
rkadrmas | 1:404c34e19f93 | 15 | volatile char TempCelsiusDisplay[] = "+abc.dd C"; |
bcostm | 0:41f53df528da | 16 | |
bcostm | 0:41f53df528da | 17 | int main() |
bcostm | 0:41f53df528da | 18 | { |
bcostm | 0:41f53df528da | 19 | |
rkadrmas | 1:404c34e19f93 | 20 | char data_write[3]; |
bcostm | 0:41f53df528da | 21 | char data_read[2]; |
rkadrmas | 1:404c34e19f93 | 22 | int tempval; |
rkadrmas | 1:404c34e19f93 | 23 | |
rkadrmas | 1:404c34e19f93 | 24 | wait(3); |
rkadrmas | 1:404c34e19f93 | 25 | pc.printf("PumpHouse mcp9808 test! March 2, 2015 1450\n\r"); |
rkadrmas | 1:404c34e19f93 | 26 | i2c.frequency(10000); // default is 100000 |
rkadrmas | 1:404c34e19f93 | 27 | |
rkadrmas | 1:404c34e19f93 | 28 | /* Configure the Temperature sensor device MCP9808: |
rkadrmas | 1:404c34e19f93 | 29 | - Thermostat mode Interrupt not used |
bcostm | 0:41f53df528da | 30 | - Fault tolerance: 0 |
bcostm | 0:41f53df528da | 31 | */ |
rkadrmas | 1:404c34e19f93 | 32 | data_write[0] = MCP9808_REG_CONF; |
rkadrmas | 1:404c34e19f93 | 33 | data_write[1] = 0x00; // config msb |
rkadrmas | 1:404c34e19f93 | 34 | data_write[2] = 0x00; // config lsb |
rkadrmas | 1:404c34e19f93 | 35 | int status = i2c.write(MCP9808_ADDR, data_write, 3, 0); |
bcostm | 0:41f53df528da | 36 | if (status != 0) { // Error |
rkadrmas | 1:404c34e19f93 | 37 | pc.printf(" error status = 0x%08x\r\n", status); |
bcostm | 0:41f53df528da | 38 | while (1) { |
bcostm | 0:41f53df528da | 39 | myled = !myled; |
bcostm | 0:41f53df528da | 40 | wait(0.2); |
bcostm | 0:41f53df528da | 41 | } |
bcostm | 0:41f53df528da | 42 | } |
bcostm | 0:41f53df528da | 43 | |
rkadrmas | 1:404c34e19f93 | 44 | pc.printf("enter forever loop\r\n"); |
bcostm | 0:41f53df528da | 45 | while (1) { |
bcostm | 0:41f53df528da | 46 | // Read temperature register |
rkadrmas | 1:404c34e19f93 | 47 | data_write[0] = MCP9808_REG_TEMP; |
rkadrmas | 1:404c34e19f93 | 48 | i2c.write(MCP9808_ADDR, data_write, 1, 1); // no stop |
rkadrmas | 1:404c34e19f93 | 49 | i2c.read(MCP9808_ADDR, data_read, 2, 0); |
bcostm | 0:41f53df528da | 50 | |
rkadrmas | 1:404c34e19f93 | 51 | // check Ta vs Tcrit |
rkadrmas | 1:404c34e19f93 | 52 | if((data_read[0] & 0x80) == 0x80) { |
rkadrmas | 1:404c34e19f93 | 53 | pc.printf(" temp >= critical "); |
rkadrmas | 1:404c34e19f93 | 54 | } |
rkadrmas | 1:404c34e19f93 | 55 | if((data_read[0] & 0x40) == 0x40) { |
rkadrmas | 1:404c34e19f93 | 56 | pc.printf(" temp > upper limit "); |
rkadrmas | 1:404c34e19f93 | 57 | } |
rkadrmas | 1:404c34e19f93 | 58 | if((data_read[0] & 0x20) == 0x20) { |
rkadrmas | 1:404c34e19f93 | 59 | pc.printf(" temp < lower limit "); |
rkadrmas | 1:404c34e19f93 | 60 | } |
rkadrmas | 1:404c34e19f93 | 61 | if(data_read[0] & 0xE0) { |
rkadrmas | 1:404c34e19f93 | 62 | pc.printf("\r\n"); |
rkadrmas | 1:404c34e19f93 | 63 | data_read[0] = data_read[0] & 0x1F; // clear flag bits |
rkadrmas | 1:404c34e19f93 | 64 | } |
rkadrmas | 1:404c34e19f93 | 65 | if((data_read[0] & 0x10) == 0x10) { |
rkadrmas | 1:404c34e19f93 | 66 | data_read[0] = data_read[0] & 0x0F; |
rkadrmas | 1:404c34e19f93 | 67 | TempCelsiusDisplay[0] = '-'; |
rkadrmas | 1:404c34e19f93 | 68 | tempval = 256 - (data_read[0] << 4) + (data_read[1] >> 4); |
rkadrmas | 1:404c34e19f93 | 69 | } else { |
bcostm | 0:41f53df528da | 70 | TempCelsiusDisplay[0] = '+'; |
rkadrmas | 1:404c34e19f93 | 71 | tempval = (data_read[0] << 4) + (data_read[1] >> 4); |
bcostm | 0:41f53df528da | 72 | } |
bcostm | 0:41f53df528da | 73 | |
rkadrmas | 1:404c34e19f93 | 74 | // fractional part (0.25°C precision) |
rkadrmas | 1:404c34e19f93 | 75 | if (data_read[1] & 0x08) { |
rkadrmas | 1:404c34e19f93 | 76 | if(data_read[1] & 0x04) { |
rkadrmas | 1:404c34e19f93 | 77 | TempCelsiusDisplay[5] = '7'; |
rkadrmas | 1:404c34e19f93 | 78 | TempCelsiusDisplay[6] = '5'; |
rkadrmas | 1:404c34e19f93 | 79 | } else { |
rkadrmas | 1:404c34e19f93 | 80 | TempCelsiusDisplay[5] = '5'; |
rkadrmas | 1:404c34e19f93 | 81 | TempCelsiusDisplay[6] = '0'; |
rkadrmas | 1:404c34e19f93 | 82 | } |
bcostm | 0:41f53df528da | 83 | } else { |
rkadrmas | 1:404c34e19f93 | 84 | if(data_read[1] & 0x04) { |
rkadrmas | 1:404c34e19f93 | 85 | TempCelsiusDisplay[5] = '2'; |
rkadrmas | 1:404c34e19f93 | 86 | TempCelsiusDisplay[6] = '5'; |
rkadrmas | 1:404c34e19f93 | 87 | }else{ |
rkadrmas | 1:404c34e19f93 | 88 | TempCelsiusDisplay[5] = '0'; |
rkadrmas | 1:404c34e19f93 | 89 | TempCelsiusDisplay[6] = '0'; |
rkadrmas | 1:404c34e19f93 | 90 | } |
bcostm | 0:41f53df528da | 91 | } |
bcostm | 0:41f53df528da | 92 | |
bcostm | 0:41f53df528da | 93 | // Integer part |
bcostm | 0:41f53df528da | 94 | TempCelsiusDisplay[1] = (tempval / 100) + 0x30; |
bcostm | 0:41f53df528da | 95 | TempCelsiusDisplay[2] = ((tempval % 100) / 10) + 0x30; |
bcostm | 0:41f53df528da | 96 | TempCelsiusDisplay[3] = ((tempval % 100) % 10) + 0x30; |
bcostm | 0:41f53df528da | 97 | |
bcostm | 0:41f53df528da | 98 | // Display result |
rkadrmas | 1:404c34e19f93 | 99 | pc.printf("temp = %s\r\n", TempCelsiusDisplay); |
bcostm | 0:41f53df528da | 100 | myled = !myled; |
bcostm | 0:41f53df528da | 101 | wait(1.0); |
bcostm | 0:41f53df528da | 102 | } |
bcostm | 0:41f53df528da | 103 | |
bcostm | 0:41f53df528da | 104 | } |
bcostm | 0:41f53df528da | 105 |