MCP alert
Fork of I2C_MCP9808_V2 by
Embed:
(wiki syntax)
Show/hide line numbers
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],data_f[20]; 00019 char data_write[3], data_read[2],data_write_crit_temp[3]; 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] = 0x1E; // 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 // Now we set the critical temperature 00043 data_write_crit_temp[0] = MCP9808_REG_CRIT; 00044 data_write_crit_temp[1] = 0x01; 00045 data_write_crit_temp[2] = 0xB0; 00046 status = i2c.write(MCP9808_ADDR, data_write_crit_temp, 3, 0); 00047 00048 00049 00050 00051 while(1){ 00052 int i=0; 00053 // this loops takes 10 data points and then 00054 // appends those data points to a file 00055 while(i<10){ 00056 // Read temperature 00057 data_write[0] = MCP9808_REG_TEMP; 00058 i2c.write(MCP9808_ADDR, data_write, 1, 1); // no stop 00059 i2c.read(MCP9808_ADDR, data_read, 2, 0); 00060 status = data_read[0] >> 5; // Shift flags to b0-b2 00061 // 1 in flag means: T>T_crit(b2), T>T_Upper(b1), T<T_Lower(b0) 00062 pc.printf("the status is %X\n\r",status); 00063 data_read[0] = data_read[0] & 0x1F; // clear flag bits for temperature 00064 00065 data[i] = (data_read[0] * 16) + (data_read[1] / 16.0); // combine two bytes 00066 data_f[i] = (data[i]+40)*9/5-40; 00067 // Display the flags, if you wish 00068 // pc.printf("Flags are %i \n\r",status); 00069 // This displays to the user 00070 pc.printf("%i Temperature is %3.1f C and %3.1f F\n\r",i,data[i],data_f[i]); 00071 i=i+1; 00072 wait(1); 00073 } 00074 00075 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 00076 if (myLogFile == NULL) { // check the file is open. 00077 pc.printf("Couldn't open the log file."); 00078 while (1) { 00079 wait(10); 00080 } 00081 } 00082 else{ 00083 for( ii = 0; ii < 10; ii++ ){ 00084 fprintf(myLogFile, "Index %i Temperature is %3.1f\r\n",ii,data[ii]); 00085 } 00086 fclose(myLogFile); 00087 } 00088 } 00089 }
Generated on Fri Aug 12 2022 22:37:12 by
1.7.2
