Test program that talkes to a I2C Microchip MCP9801 and logs results to USB serial and to mbeb local file system
main.cpp
00001 00002 // Read from I2C slave at address 0x62 00003 00004 #include "mbed.h" 00005 00006 I2C i2c(p28, p27); 00007 Serial pc(USBTX, USBRX); 00008 DigitalOut myled(LED2); 00009 LocalFileSystem local("local"); // Create the local filesystem under the name "local" 00010 00011 00012 int main() 00013 { 00014 unsigned int loop = 1; 00015 unsigned loopCount = 1; 00016 int address = 0x90; 00017 char RXdata[2]; 00018 char TXdata[2]; 00019 float temperature = 0; 00020 time_t seconds = 0; 00021 00022 //Hello splash screen 00023 printf("\r\n\nmbeb Temperature logger V0.1\r\nBy Paul J clarke MIET\r\n13th Oct 2010\r\n\n"); 00024 00025 //clear serial buffer 00026 while( pc.readable() ); 00027 00028 //Configure sensor at 0.0625'C steps 00029 TXdata[0] = 0x01; 00030 TXdata[1] = 0x60; 00031 i2c.write(address, TXdata, 2); 00032 //Set dat for reading temperature 00033 TXdata[0] = 0x00; 00034 myled = 0; 00035 00036 // Opens the root directory of the local file system 00037 printf("Files on mbed\r\n"); 00038 DIR *d = opendir("/local"); 00039 struct dirent *p; 00040 while((p = readdir(d)) != NULL) 00041 { // Print the names of the files in the local file system 00042 printf("%s\n\r", p->d_name); // to stdout (USB Serial). 00043 } 00044 closedir(d); 00045 printf("\n"); 00046 00047 // Removes the file "out.txt" from the local file system 00048 remove("/local/log.csv"); 00049 // Open "log.csv" on the local file system for writing 00050 FILE *fp = fopen("/local/log.csv", "w"); 00051 00052 //reset time to zero 00053 set_time(0); 00054 00055 //Send headers to csv file 00056 fprintf(fp, "Time_Seconds,Temperature_C\r"); 00057 00058 //main loop 00059 while(loop) 00060 { 00061 //toggle LED to show we are running 00062 myled = !myled; 00063 //set I2C device to point at temperature registor 00064 i2c.write(address, TXdata, 1); 00065 //read two bytes of raw data back 00066 i2c.read(address, RXdata, 2); 00067 //generate floating point number from raw data recived 00068 temperature = (((float)RXdata[1]) / 256) + RXdata[0]; 00069 //stream temperature to USB serial port 00070 printf("Temperature = %.4f'C Data Point = %d \r", temperature, loopCount); 00071 //get time in seconds 00072 seconds = time(NULL); 00073 //and save to log file 00074 fprintf(fp, "%d,%.4f\n", seconds, temperature); 00075 //wait 250ms - time for ADC in temp sensor to re calculate new value 00076 wait(1); 00077 00078 //inc loop counter 00079 loopCount++; 00080 00081 if( pc.readable() || loopCount==1200 ) //1200 = 5 min's of loggin time 00082 { 00083 loop = 0; 00084 } 00085 } 00086 printf("\r\n\n Finished...\r\nTotal data points = %d\r\n", (loopCount-1)); 00087 fclose(fp); 00088 00089 while(1); //HALT 00090 00091 }
Generated on Mon Jul 18 2022 01:25:45 by 1.7.2