Byoungsul Lee / Mbed 2 deprecated Temperature_I2C

Dependencies:   mbed

Dependents:   Home-Automation

Fork of Nucleo_i2c_mcp9808 by Richard Kadrmas

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "I2C.h"
00003 
00004 /* MCP9808 high accuracy temp sensor from adafruit (no address pins pulled up) */ 
00005 #define MCP9808_REG_TEMP (0x05) // Temperature Register
00006 #define MCP9808_REG_CONF (0x01) // Configuration Register
00007 #define MCP9808_ADDR     (0x30) // MCP9808 base address 0x18<<1
00008  
00009 I2C i2c(I2C_SDA, I2C_SCL);
00010  
00011 DigitalOut myled(LED1);
00012  
00013 //Serial pc(SERIAL_TX, SERIAL_RX);
00014  
00015 volatile char TempCelsiusDisplay[] = "+abc.dd C";
00016  
00017 int main()
00018 {
00019  
00020     char data_write[3];
00021     char data_read[2];
00022     int tempval;
00023     
00024     wait(3);
00025     printf("PumpHouse mcp9808 test! March 2, 2015  1450\n\r");
00026     i2c.frequency(10000); // default is 100000
00027     
00028     /* Configure the Temperature sensor device MCP9808:
00029     - Thermostat mode Interrupt not used
00030     - Fault tolerance: 0
00031     */
00032     data_write[0] = MCP9808_REG_CONF;
00033     data_write[1] = 0x00;  // config msb
00034     data_write[2] = 0x00;  // config lsb
00035     int status = i2c.write(MCP9808_ADDR, data_write, 3, 0);
00036     if (status != 0) { // Error
00037         printf("  error status = 0x%08x\r\n", status);
00038         while (1) {
00039             myled = !myled;
00040             wait(0.2);
00041         }
00042     }
00043  
00044     printf("enter forever loop\r\n");
00045     while (1) {
00046         // Read temperature register
00047         data_write[0] = MCP9808_REG_TEMP;
00048         i2c.write(MCP9808_ADDR, data_write, 1, 1); // no stop
00049         i2c.read(MCP9808_ADDR, data_read, 2, 0);
00050  
00051  // check Ta vs Tcrit
00052         if((data_read[0] & 0x80)  == 0x80) {
00053             printf(" temp >= critical ");
00054         }
00055         if((data_read[0] & 0x40) == 0x40) {
00056             printf("   temp > upper limit ");
00057         }
00058         if((data_read[0] & 0x20) == 0x20) {
00059             printf(" temp < lower limit  ");
00060         }
00061         if(data_read[0] & 0xE0) {
00062             printf("\r\n");   
00063             data_read[0] = data_read[0] & 0x1F;  // clear flag bits
00064         }
00065         if((data_read[0] & 0x10) == 0x10) { 
00066             data_read[0] = data_read[0] & 0x0F;
00067             TempCelsiusDisplay[0] = '-';
00068             tempval = 256 - (data_read[0] << 4) + (data_read[1] >> 4);
00069         } else {
00070             TempCelsiusDisplay[0] = '+';
00071             tempval = (data_read[0] << 4) + (data_read[1] >> 4);
00072         }
00073  
00074         // fractional part (0.25°C precision)
00075         if (data_read[1] & 0x08) {
00076             if(data_read[1] & 0x04) {
00077                 TempCelsiusDisplay[5] = '7';
00078                 TempCelsiusDisplay[6] = '5';
00079             } else {
00080                 TempCelsiusDisplay[5] = '5';
00081                 TempCelsiusDisplay[6] = '0';
00082             }
00083         } else {
00084             if(data_read[1] & 0x04) {
00085                 TempCelsiusDisplay[5] = '2';
00086                 TempCelsiusDisplay[6] = '5';
00087             }else{
00088                 TempCelsiusDisplay[5] = '0';
00089                 TempCelsiusDisplay[6] = '0';
00090             }
00091         }
00092  
00093         // Integer part
00094         TempCelsiusDisplay[1] = (tempval / 100) + 0x30;
00095         TempCelsiusDisplay[2] = ((tempval % 100) / 10) + 0x30;
00096         TempCelsiusDisplay[3] = ((tempval % 100) % 10) + 0x30;
00097  
00098         // Display result
00099         printf("temp = %s\r\n", TempCelsiusDisplay);
00100         myled = !myled;
00101         wait(1.0);
00102     }
00103  
00104 }
00105