benjo
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]; 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] = 0x04; // 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 while(1){ 00043 int i=0; 00044 // this loops takes 10 data points and then 00045 // appends those data points to a file 00046 while(i<10){ 00047 // Read temperature 00048 data_write[0] = MCP9808_REG_TEMP; 00049 i2c.write(MCP9808_ADDR, data_write, 1, 1); // no stop 00050 i2c.read(MCP9808_ADDR, data_read, 2, 0); 00051 status = data_read[0] >> 5; // Shift flags to b0-b2 00052 // 1 in flag means: T>T_crit(b2), T>T_Upper(b1), T<T_Lower(b0) 00053 // pc.printf("the status is %n",status); 00054 data_read[0] = data_read[0] & 0x1F; // clear flag bits for temperature 00055 00056 data[i] = (data_read[0] * 16) + (data_read[1] / 16.0); // combine two bytes 00057 data_f[i] = (data[i]+40)*9/5-40; 00058 // Display the flags, if you wish 00059 // pc.printf("Flags are %i \n\r",status); 00060 // This displays to the user 00061 pc.printf("%i Temperature is %3.1f C and %3.1f F\n\r",i,data[i],data_f[i]); 00062 i=i+1; 00063 wait(1); 00064 } 00065 00066 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 00067 if (myLogFile == NULL) { // check the file is open. 00068 pc.printf("Couldn't open the log file."); 00069 while (1) { 00070 wait(10); 00071 } 00072 } 00073 else{ 00074 for( ii = 0; ii < 10; ii++ ){ 00075 fprintf(myLogFile, "Index %i Temperature is %3.1f\r\n",ii,data[ii]); 00076 } 00077 fclose(myLogFile); 00078 } 00079 } 00080 }
Generated on Mon Jul 18 2022 09:18:23 by
1.7.2
