benjo

Dependencies:   mbed

Fork of I2C_MCP9808_V2 by Advanced_Instrumentation_2019

Committer:
csoulen
Date:
Mon Nov 05 14:23:50 2018 +0000
Revision:
1:4255a3b6fae8
Parent:
0:80fb3c4621be
ben

Who changed what in which revision?

UserRevisionLine numberNew contents of line
billcooke51 0:80fb3c4621be 1 #include "mbed.h"
billcooke51 0:80fb3c4621be 2
billcooke51 0:80fb3c4621be 3 #define MCP9808_ADDR (0x30) // MCP9808 base address 0x18<<1
billcooke51 0:80fb3c4621be 4 #define MCP9808_REG_TEMP (0x05) // Temperature Register
billcooke51 0:80fb3c4621be 5 #define MCP9808_REG_CONF (0x01) // Configuration Register
billcooke51 0:80fb3c4621be 6 #define MCP9808_REG_CRIT (0x04) // Critical Temperature Register
billcooke51 0:80fb3c4621be 7
billcooke51 0:80fb3c4621be 8
billcooke51 0:80fb3c4621be 9 LocalFileSystem local("local"); // Create the local filesystem under the name "local"
billcooke51 0:80fb3c4621be 10 // create an empty file pointer.
billcooke51 0:80fb3c4621be 11 FILE *myLogFile = NULL;
billcooke51 0:80fb3c4621be 12
billcooke51 0:80fb3c4621be 13 Serial pc(USBTX, USBRX);
billcooke51 0:80fb3c4621be 14 I2C i2c(p9, p10); // SDA, SCL on MCP9808
billcooke51 0:80fb3c4621be 15 // Note MCP9808 also needs VCC (3.3 V) and GND
billcooke51 0:80fb3c4621be 16
billcooke51 0:80fb3c4621be 17 float TempValue;
csoulen 1:4255a3b6fae8 18 float data[20],data_f[20];
billcooke51 0:80fb3c4621be 19 char data_write[3], data_read[2];
billcooke51 0:80fb3c4621be 20 int ii, status;
billcooke51 0:80fb3c4621be 21 int main() {
billcooke51 0:80fb3c4621be 22 char text[128];
billcooke51 0:80fb3c4621be 23 pc.printf("Program will start when you enter a number\r\n");
billcooke51 0:80fb3c4621be 24 pc.scanf("%s", text);
billcooke51 0:80fb3c4621be 25 pc.printf("Printing all filenames:\r\n");
billcooke51 0:80fb3c4621be 26 char fname[128];
billcooke51 0:80fb3c4621be 27 DIR* dir = opendir("/local/");
billcooke51 0:80fb3c4621be 28 struct dirent* de;
billcooke51 0:80fb3c4621be 29 while((de = readdir(dir)) != NULL){
billcooke51 0:80fb3c4621be 30 pc.printf(" %s\r\n", &(de->d_name)[0]);
billcooke51 0:80fb3c4621be 31 }
billcooke51 0:80fb3c4621be 32 // Wake up
billcooke51 0:80fb3c4621be 33 data_write[0] = MCP9808_REG_CONF; // Register address
billcooke51 0:80fb3c4621be 34 data_write[1] = 0x00; // Data MSB, b11-15 not used, b9-10 =>T_HYS=0, b8 => continuous conversions
billcooke51 0:80fb3c4621be 35 data_write[2] = 0x04; // Data LSB, Alert Off, Alert Disabled, Alert T>T_Crit, Alert active low, Alert interrupt
billcooke51 0:80fb3c4621be 36 status = i2c.write(MCP9808_ADDR, data_write, 3, 0); // address,data,length,repeat
billcooke51 0:80fb3c4621be 37 // address is chip addess
billcooke51 0:80fb3c4621be 38 // First byte of data has register address
billcooke51 0:80fb3c4621be 39 // repeat true for multiple write when the chip autoincrements the address
billcooke51 0:80fb3c4621be 40 // MCP9808 does not do this, so we leave it at 0
billcooke51 0:80fb3c4621be 41
billcooke51 0:80fb3c4621be 42 while(1){
billcooke51 0:80fb3c4621be 43 int i=0;
billcooke51 0:80fb3c4621be 44 // this loops takes 10 data points and then
billcooke51 0:80fb3c4621be 45 // appends those data points to a file
billcooke51 0:80fb3c4621be 46 while(i<10){
billcooke51 0:80fb3c4621be 47 // Read temperature
billcooke51 0:80fb3c4621be 48 data_write[0] = MCP9808_REG_TEMP;
billcooke51 0:80fb3c4621be 49 i2c.write(MCP9808_ADDR, data_write, 1, 1); // no stop
billcooke51 0:80fb3c4621be 50 i2c.read(MCP9808_ADDR, data_read, 2, 0);
billcooke51 0:80fb3c4621be 51 status = data_read[0] >> 5; // Shift flags to b0-b2
billcooke51 0:80fb3c4621be 52 // 1 in flag means: T>T_crit(b2), T>T_Upper(b1), T<T_Lower(b0)
billcooke51 0:80fb3c4621be 53 // pc.printf("the status is %n",status);
billcooke51 0:80fb3c4621be 54 data_read[0] = data_read[0] & 0x1F; // clear flag bits for temperature
billcooke51 0:80fb3c4621be 55
billcooke51 0:80fb3c4621be 56 data[i] = (data_read[0] * 16) + (data_read[1] / 16.0); // combine two bytes
csoulen 1:4255a3b6fae8 57 data_f[i] = (data[i]+40)*9/5-40;
billcooke51 0:80fb3c4621be 58 // Display the flags, if you wish
billcooke51 0:80fb3c4621be 59 // pc.printf("Flags are %i \n\r",status);
billcooke51 0:80fb3c4621be 60 // This displays to the user
csoulen 1:4255a3b6fae8 61 pc.printf("%i Temperature is %3.1f C and %3.1f F\n\r",i,data[i],data_f[i]);
billcooke51 0:80fb3c4621be 62 i=i+1;
billcooke51 0:80fb3c4621be 63 wait(1);
billcooke51 0:80fb3c4621be 64 }
billcooke51 0:80fb3c4621be 65
billcooke51 0:80fb3c4621be 66 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
billcooke51 0:80fb3c4621be 67 if (myLogFile == NULL) { // check the file is open.
billcooke51 0:80fb3c4621be 68 pc.printf("Couldn't open the log file.");
billcooke51 0:80fb3c4621be 69 while (1) {
billcooke51 0:80fb3c4621be 70 wait(10);
billcooke51 0:80fb3c4621be 71 }
billcooke51 0:80fb3c4621be 72 }
billcooke51 0:80fb3c4621be 73 else{
billcooke51 0:80fb3c4621be 74 for( ii = 0; ii < 10; ii++ ){
billcooke51 0:80fb3c4621be 75 fprintf(myLogFile, "Index %i Temperature is %3.1f\r\n",ii,data[ii]);
billcooke51 0:80fb3c4621be 76 }
billcooke51 0:80fb3c4621be 77 fclose(myLogFile);
billcooke51 0:80fb3c4621be 78 }
billcooke51 0:80fb3c4621be 79 }
billcooke51 0:80fb3c4621be 80 }