Lab 10/24/18

Dependencies:   mbed

Fork of I2C_MCP9808_V2 by Advanced_Instrumentation_2019

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 #define MCP9808_ADDR     (0x30) // MCP9808 base address 0x18<<1
00004 #define MCP9808_REG_TEMP (0x05) // Temperature Register
00005 #define MCP9808_REG_CONF (0x01) // Configuration Register
00006 #define MCP9808_REG_CRIT (0x04) // Critical Temperature Register
00007 
00008 
00009 LocalFileSystem local("local");               // Create the local filesystem under the name "local"
00010 // create an empty file pointer.
00011 FILE *myLogFile = NULL;
00012 
00013 Serial pc(USBTX, USBRX);
00014 I2C i2c(p9, p10);   // SDA, SCL on MCP9808
00015 // Note MCP9808 also needs VCC (3.3 V) and GND
00016 
00017 float TempValue;
00018 float data[20];
00019 char data_write[3], data_read[2];
00020 int ii, status;
00021 int main() {
00022     char text[128];
00023     pc.printf("Program will start when you enter a number\r\n");
00024     pc.scanf("%s", text);
00025     pc.printf("Printing all filenames:\r\n");
00026     char fname[128];
00027     DIR* dir = opendir("/local/");
00028     struct dirent* de;
00029     while((de = readdir(dir)) != NULL){
00030         pc.printf("  %s\r\n", &(de->d_name)[0]);
00031     }
00032     // Wake up
00033     data_write[0] = MCP9808_REG_CONF; // Register address
00034     data_write[1] = 0x00;  // Data MSB, b11-15 not used, b9-10 =>T_HYS=0, b8 => continuous conversions
00035     data_write[2] = 0x1C;  // Data LSB,  Alert Off, Alert Disabled, Alert T>T_Crit, Alert active low, Alert interrupt
00036     status = i2c.write(MCP9808_ADDR, data_write, 3, 0); // address,data,length,repeat
00037     // address is chip addess
00038     // First byte of data has register address 
00039     // repeat true for multiple write when the chip autoincrements the address
00040     // MCP9808 does not do this, so we leave it at 0
00041      
00042     data_write[0] = MCP9808_REG_CRIT; // Register address
00043     data_write[1] = 0x01;  
00044     data_write[2] = 0x70;  
00045     status = i2c.write(MCP9808_ADDR, data_write, 3, 0); // address,data,length,repeat
00046  
00047     while(1){
00048         int i=0;
00049         // this loops takes 10 data points and then
00050         // appends those data points to a file
00051         while(i<10){
00052         // Read temperature  
00053             data_write[0] = MCP9808_REG_TEMP;
00054             i2c.write(MCP9808_ADDR, data_write, 1, 1); // no stop
00055             i2c.read(MCP9808_ADDR, data_read, 2, 0);
00056             status = data_read[0] >> 5;  // Shift flags to b0-b2
00057             // 1 in flag means: T>T_crit(b2), T>T_Upper(b1), T<T_Lower(b0)
00058             pc.printf("the status is %d\n\r",status);
00059            
00060            
00061             data_read[0] = data_read[0] & 0x1F;  // clear flag bits for temperature
00062             
00063             data[i] = (data_read[0] * 16) + (data_read[1] / 16.0); // combine two bytes
00064             // Display the flags, if you wish
00065             // pc.printf("Flags are %i \n\r",status);
00066             // This displays to the user
00067             float temperature_fahr = data[i]*(9.0/5)+32;
00068             pc.printf("%i Celsius temperature is %3.1f\n\r",i,data[i]);
00069             pc.printf("%i Fahrenheit temperature is %3.1f\n\r",i,temperature_fahr);
00070             i=i+1;
00071             wait(1);
00072             
00073             if (data_read[0] > 24){
00074                 
00075             }
00076         }
00077                 
00078         myLogFile = fopen("/local/log.txt", "a"); // open log.txt as a write only file (any existing file will be overwritten). Replace "w" with "a" to append
00079         if (myLogFile == NULL) {  // check the file is open.
00080             pc.printf("Couldn't open the log file.");
00081             while (1) {
00082                 wait(10);
00083             }
00084         }
00085         else{
00086             for( ii = 0; ii < 10; ii++ ){
00087                 fprintf(myLogFile, "Index %i Temperature is %3.1f\r\n",ii,data[ii]);
00088             }
00089             fclose(myLogFile);
00090         }
00091     }
00092 }