Lab 10/24/18

Dependencies:   mbed

Fork of I2C_MCP9808_V2 by Advanced_Instrumentation_2019

Committer:
billcooke51
Date:
Tue Oct 23 19:39:34 2018 +0000
Revision:
0:80fb3c4621be
Child:
1:568629da46dd
Uses I2C to read temperature from an MCP9808

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
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
billcooke51 0:80fb3c4621be 57 // Display the flags, if you wish
billcooke51 0:80fb3c4621be 58 // pc.printf("Flags are %i \n\r",status);
billcooke51 0:80fb3c4621be 59 // This displays to the user
billcooke51 0:80fb3c4621be 60 pc.printf("%i Temperature is %3.1f\n\r",i,data[i]);
billcooke51 0:80fb3c4621be 61 i=i+1;
billcooke51 0:80fb3c4621be 62 wait(1);
billcooke51 0:80fb3c4621be 63 }
billcooke51 0:80fb3c4621be 64
billcooke51 0:80fb3c4621be 65 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 66 if (myLogFile == NULL) { // check the file is open.
billcooke51 0:80fb3c4621be 67 pc.printf("Couldn't open the log file.");
billcooke51 0:80fb3c4621be 68 while (1) {
billcooke51 0:80fb3c4621be 69 wait(10);
billcooke51 0:80fb3c4621be 70 }
billcooke51 0:80fb3c4621be 71 }
billcooke51 0:80fb3c4621be 72 else{
billcooke51 0:80fb3c4621be 73 for( ii = 0; ii < 10; ii++ ){
billcooke51 0:80fb3c4621be 74 fprintf(myLogFile, "Index %i Temperature is %3.1f\r\n",ii,data[ii]);
billcooke51 0:80fb3c4621be 75 }
billcooke51 0:80fb3c4621be 76 fclose(myLogFile);
billcooke51 0:80fb3c4621be 77 }
billcooke51 0:80fb3c4621be 78 }
billcooke51 0:80fb3c4621be 79 }