Lab 10/24/18

Dependencies:   mbed

Fork of I2C_MCP9808_V2 by Advanced_Instrumentation_2019

Committer:
acabate
Date:
Tue Oct 30 22:12:34 2018 +0000
Revision:
1:568629da46dd
Parent:
0:80fb3c4621be
Lab 10/24/18;

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;
billcooke51 0:80fb3c4621be 18 float data[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
acabate 1:568629da46dd 35 data_write[2] = 0x1C; // 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
acabate 1:568629da46dd 41
acabate 1:568629da46dd 42 data_write[0] = MCP9808_REG_CRIT; // Register address
acabate 1:568629da46dd 43 data_write[1] = 0x01;
acabate 1:568629da46dd 44 data_write[2] = 0x70;
acabate 1:568629da46dd 45 status = i2c.write(MCP9808_ADDR, data_write, 3, 0); // address,data,length,repeat
acabate 1:568629da46dd 46
billcooke51 0:80fb3c4621be 47 while(1){
billcooke51 0:80fb3c4621be 48 int i=0;
billcooke51 0:80fb3c4621be 49 // this loops takes 10 data points and then
billcooke51 0:80fb3c4621be 50 // appends those data points to a file
billcooke51 0:80fb3c4621be 51 while(i<10){
billcooke51 0:80fb3c4621be 52 // Read temperature
billcooke51 0:80fb3c4621be 53 data_write[0] = MCP9808_REG_TEMP;
billcooke51 0:80fb3c4621be 54 i2c.write(MCP9808_ADDR, data_write, 1, 1); // no stop
billcooke51 0:80fb3c4621be 55 i2c.read(MCP9808_ADDR, data_read, 2, 0);
billcooke51 0:80fb3c4621be 56 status = data_read[0] >> 5; // Shift flags to b0-b2
billcooke51 0:80fb3c4621be 57 // 1 in flag means: T>T_crit(b2), T>T_Upper(b1), T<T_Lower(b0)
acabate 1:568629da46dd 58 pc.printf("the status is %d\n\r",status);
acabate 1:568629da46dd 59
acabate 1:568629da46dd 60
billcooke51 0:80fb3c4621be 61 data_read[0] = data_read[0] & 0x1F; // clear flag bits for temperature
billcooke51 0:80fb3c4621be 62
billcooke51 0:80fb3c4621be 63 data[i] = (data_read[0] * 16) + (data_read[1] / 16.0); // combine two bytes
billcooke51 0:80fb3c4621be 64 // Display the flags, if you wish
billcooke51 0:80fb3c4621be 65 // pc.printf("Flags are %i \n\r",status);
billcooke51 0:80fb3c4621be 66 // This displays to the user
acabate 1:568629da46dd 67 float temperature_fahr = data[i]*(9.0/5)+32;
acabate 1:568629da46dd 68 pc.printf("%i Celsius temperature is %3.1f\n\r",i,data[i]);
acabate 1:568629da46dd 69 pc.printf("%i Fahrenheit temperature is %3.1f\n\r",i,temperature_fahr);
billcooke51 0:80fb3c4621be 70 i=i+1;
billcooke51 0:80fb3c4621be 71 wait(1);
acabate 1:568629da46dd 72
acabate 1:568629da46dd 73 if (data_read[0] > 24){
acabate 1:568629da46dd 74
acabate 1:568629da46dd 75 }
billcooke51 0:80fb3c4621be 76 }
billcooke51 0:80fb3c4621be 77
billcooke51 0:80fb3c4621be 78 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 79 if (myLogFile == NULL) { // check the file is open.
billcooke51 0:80fb3c4621be 80 pc.printf("Couldn't open the log file.");
billcooke51 0:80fb3c4621be 81 while (1) {
billcooke51 0:80fb3c4621be 82 wait(10);
billcooke51 0:80fb3c4621be 83 }
billcooke51 0:80fb3c4621be 84 }
billcooke51 0:80fb3c4621be 85 else{
billcooke51 0:80fb3c4621be 86 for( ii = 0; ii < 10; ii++ ){
billcooke51 0:80fb3c4621be 87 fprintf(myLogFile, "Index %i Temperature is %3.1f\r\n",ii,data[ii]);
billcooke51 0:80fb3c4621be 88 }
billcooke51 0:80fb3c4621be 89 fclose(myLogFile);
billcooke51 0:80fb3c4621be 90 }
billcooke51 0:80fb3c4621be 91 }
billcooke51 0:80fb3c4621be 92 }