Charles Soulen
/
Lab_6_I2C_MCP9808_ALERT
MCP alert
Fork of I2C_MCP9808_V2 by
main.cpp@1:3baecb064743, 2018-11-05 (annotated)
- Committer:
- csoulen
- Date:
- Mon Nov 05 14:17:14 2018 +0000
- Revision:
- 1:3baecb064743
- Parent:
- 0:80fb3c4621be
To Ben
Who changed what in which revision?
User | Revision | Line number | New 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:3baecb064743 | 18 | float data[20],data_f[20]; |
csoulen | 1:3baecb064743 | 19 | char data_write[3], data_read[2],data_write_crit_temp[3]; |
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 |
csoulen | 1:3baecb064743 | 35 | data_write[2] = 0x1E; // 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 |
csoulen | 1:3baecb064743 | 41 | |
csoulen | 1:3baecb064743 | 42 | // Now we set the critical temperature |
csoulen | 1:3baecb064743 | 43 | data_write_crit_temp[0] = MCP9808_REG_CRIT; |
csoulen | 1:3baecb064743 | 44 | data_write_crit_temp[1] = 0x01; |
csoulen | 1:3baecb064743 | 45 | data_write_crit_temp[2] = 0xB0; |
csoulen | 1:3baecb064743 | 46 | status = i2c.write(MCP9808_ADDR, data_write_crit_temp, 3, 0); |
csoulen | 1:3baecb064743 | 47 | |
csoulen | 1:3baecb064743 | 48 | |
csoulen | 1:3baecb064743 | 49 | |
billcooke51 | 0:80fb3c4621be | 50 | |
billcooke51 | 0:80fb3c4621be | 51 | while(1){ |
billcooke51 | 0:80fb3c4621be | 52 | int i=0; |
billcooke51 | 0:80fb3c4621be | 53 | // this loops takes 10 data points and then |
billcooke51 | 0:80fb3c4621be | 54 | // appends those data points to a file |
billcooke51 | 0:80fb3c4621be | 55 | while(i<10){ |
billcooke51 | 0:80fb3c4621be | 56 | // Read temperature |
billcooke51 | 0:80fb3c4621be | 57 | data_write[0] = MCP9808_REG_TEMP; |
billcooke51 | 0:80fb3c4621be | 58 | i2c.write(MCP9808_ADDR, data_write, 1, 1); // no stop |
billcooke51 | 0:80fb3c4621be | 59 | i2c.read(MCP9808_ADDR, data_read, 2, 0); |
billcooke51 | 0:80fb3c4621be | 60 | status = data_read[0] >> 5; // Shift flags to b0-b2 |
billcooke51 | 0:80fb3c4621be | 61 | // 1 in flag means: T>T_crit(b2), T>T_Upper(b1), T<T_Lower(b0) |
csoulen | 1:3baecb064743 | 62 | pc.printf("the status is %X\n\r",status); |
billcooke51 | 0:80fb3c4621be | 63 | data_read[0] = data_read[0] & 0x1F; // clear flag bits for temperature |
billcooke51 | 0:80fb3c4621be | 64 | |
billcooke51 | 0:80fb3c4621be | 65 | data[i] = (data_read[0] * 16) + (data_read[1] / 16.0); // combine two bytes |
csoulen | 1:3baecb064743 | 66 | data_f[i] = (data[i]+40)*9/5-40; |
billcooke51 | 0:80fb3c4621be | 67 | // Display the flags, if you wish |
billcooke51 | 0:80fb3c4621be | 68 | // pc.printf("Flags are %i \n\r",status); |
billcooke51 | 0:80fb3c4621be | 69 | // This displays to the user |
csoulen | 1:3baecb064743 | 70 | pc.printf("%i Temperature is %3.1f C and %3.1f F\n\r",i,data[i],data_f[i]); |
billcooke51 | 0:80fb3c4621be | 71 | i=i+1; |
billcooke51 | 0:80fb3c4621be | 72 | wait(1); |
billcooke51 | 0:80fb3c4621be | 73 | } |
billcooke51 | 0:80fb3c4621be | 74 | |
billcooke51 | 0:80fb3c4621be | 75 | 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 | 76 | if (myLogFile == NULL) { // check the file is open. |
billcooke51 | 0:80fb3c4621be | 77 | pc.printf("Couldn't open the log file."); |
billcooke51 | 0:80fb3c4621be | 78 | while (1) { |
billcooke51 | 0:80fb3c4621be | 79 | wait(10); |
billcooke51 | 0:80fb3c4621be | 80 | } |
billcooke51 | 0:80fb3c4621be | 81 | } |
billcooke51 | 0:80fb3c4621be | 82 | else{ |
billcooke51 | 0:80fb3c4621be | 83 | for( ii = 0; ii < 10; ii++ ){ |
billcooke51 | 0:80fb3c4621be | 84 | fprintf(myLogFile, "Index %i Temperature is %3.1f\r\n",ii,data[ii]); |
billcooke51 | 0:80fb3c4621be | 85 | } |
billcooke51 | 0:80fb3c4621be | 86 | fclose(myLogFile); |
billcooke51 | 0:80fb3c4621be | 87 | } |
billcooke51 | 0:80fb3c4621be | 88 | } |
billcooke51 | 0:80fb3c4621be | 89 | } |