operational with OS5

Committer:
Tyari21
Date:
Thu Aug 23 14:02:08 2018 +0000
Revision:
67:18117455f5cb
Parent:
66:0a43d8aeeb76
operational with OS 5

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Jonathan Austin 0:2757d7abb7d9 1 #include "mbed.h"
Jonathan Austin 0:2757d7abb7d9 2
Tyari21 67:18117455f5cb 3 #define MCP9808_ADDR (0x30) // MCP9808 base address 0x18<<1
Tyari21 67:18117455f5cb 4 #define MCP9808_REG_TEMP (0x05) // Temperature Register
Tyari21 67:18117455f5cb 5 #define MCP9808_REG_CONF (0x01) // Configuration Register
Tyari21 67:18117455f5cb 6 #define MCP9808_REG_CRIT (0x04) // Critical Temperature Register
Tyari21 67:18117455f5cb 7
Tyari21 67:18117455f5cb 8 // Uncomment to use file save
Tyari21 67:18117455f5cb 9 // LocalFileSystem local("local"); // Create the local filesystem under the name "local"
Jonathan Austin 0:2757d7abb7d9 10
Tyari21 67:18117455f5cb 11 Serial pc(USBTX, USBRX);
Tyari21 67:18117455f5cb 12 I2C i2c(p9, p10); // SDA, SCL on MCP9808
Tyari21 67:18117455f5cb 13 // Note MCP9808 also needs VCC (3.3 V) and GND
Tyari21 67:18117455f5cb 14
Tyari21 67:18117455f5cb 15 float TempValue;
Tyari21 67:18117455f5cb 16 float data[20];
Tyari21 67:18117455f5cb 17 char data_write[3], data_read[2];
Tyari21 67:18117455f5cb 18 int ii, status;
Jonathan Austin 0:2757d7abb7d9 19 int main() {
Tyari21 67:18117455f5cb 20 // Wake up
Tyari21 67:18117455f5cb 21 data_write[0] = MCP9808_REG_CONF; // Register address
Tyari21 67:18117455f5cb 22 data_write[1] = 0x00; // Data MSB, b11-15 not used, b9-10 =>T_HYS=0, b8 => continuous conversions
Tyari21 67:18117455f5cb 23 data_write[2] = 0x04; // Data LSB, Alert Off, Alert Disabled, Alert T>T_Crit, Alert active low, Alert interrupt
Tyari21 67:18117455f5cb 24 status = i2c.write(MCP9808_ADDR, data_write, 3, 0); // address,data,length,repeat
Tyari21 67:18117455f5cb 25 // address is chip addess
Tyari21 67:18117455f5cb 26 // First byte of data has register address
Tyari21 67:18117455f5cb 27 // repeat true for multiple write when the chip autoincrements the address
Tyari21 67:18117455f5cb 28 // MCP9808 does not do this, so we leave it at 0
Tyari21 67:18117455f5cb 29
Tyari21 67:18117455f5cb 30 while(1){
Tyari21 67:18117455f5cb 31 int i=0;
Tyari21 67:18117455f5cb 32 // this loops takes 20 data points and then
Tyari21 67:18117455f5cb 33 // appends those data points to a file
Tyari21 67:18117455f5cb 34 // the file write will be mostly closed, so the mbed
Tyari21 67:18117455f5cb 35 // file system will be available to a host computer
Tyari21 67:18117455f5cb 36 // See warning below
Tyari21 67:18117455f5cb 37 while(i<20){
Tyari21 67:18117455f5cb 38 // Read temperature
Tyari21 67:18117455f5cb 39 data_write[0] = MCP9808_REG_TEMP;
Tyari21 67:18117455f5cb 40 i2c.write(MCP9808_ADDR, data_write, 1, 1); // no stop
Tyari21 67:18117455f5cb 41 i2c.read(MCP9808_ADDR, data_read, 2, 0);
Tyari21 67:18117455f5cb 42 status = data_read[0] >> 5; // Shift flags to b0-b2
Tyari21 67:18117455f5cb 43 // 1 in flag means: T>T_crit(b2), T>T_Upper(b1), T<T_Lower(b0)
Tyari21 67:18117455f5cb 44 // pc.printf("the status is %n",status);
Tyari21 67:18117455f5cb 45 data_read[0] = data_read[0] & 0x1F; // clear flag bits for temperature
Tyari21 67:18117455f5cb 46
Tyari21 67:18117455f5cb 47 data[i] = (data_read[0] * 16) + (data_read[1] / 16.0); // combine two bytes
Tyari21 67:18117455f5cb 48 // Display the flags, if you wish
Tyari21 67:18117455f5cb 49 // pc.printf("Flags are %i \n\r",status);
Tyari21 67:18117455f5cb 50 // This displays to the user
Tyari21 67:18117455f5cb 51 pc.printf("%i Temperature is %3.1f\n\r",i,data[i]);
Tyari21 67:18117455f5cb 52 i=i+1;
Tyari21 67:18117455f5cb 53 wait(1);
Tyari21 67:18117455f5cb 54 }
Tyari21 67:18117455f5cb 55 /*
Tyari21 67:18117455f5cb 56 If the microcontroller program opens a file on the local drive, it will be marked as “removed” on the Host computer.
Tyari21 67:18117455f5cb 57 This means the PC will often display a message such as "insert a disk into drive" if you try to access it at this time;
Tyari21 67:18117455f5cb 58 this is normal, and stops both the mbed and the PC trying to access the disk at the same time.
Tyari21 67:18117455f5cb 59
Tyari21 67:18117455f5cb 60 The USB drive will only re-appear when all file handles are closed in your program, or the microcontroller program exits.
Tyari21 67:18117455f5cb 61 If a running program on the mbed does not exit or does not release it's open file handles, you will no longer be able to see the USB drive
Tyari21 67:18117455f5cb 62 when you plug the mbed into your PC.
Tyari21 67:18117455f5cb 63
Tyari21 67:18117455f5cb 64 To allow you to see the drive again (and load a new program), use the following procedure:
Tyari21 67:18117455f5cb 65
Tyari21 67:18117455f5cb 66 Unplug the microcontroller
Tyari21 67:18117455f5cb 67 Hold the reset button down
Tyari21 67:18117455f5cb 68 While still holding the button, plug in the microcontroller. The mbed USB drive should appear.
Tyari21 67:18117455f5cb 69 Keep holding the button until the new program is saved onto the USB drive.
Tyari21 67:18117455f5cb 70
Tyari21 67:18117455f5cb 71 */
Tyari21 67:18117455f5cb 72 // This section does the file ave
Tyari21 67:18117455f5cb 73 /*
Tyari21 67:18117455f5cb 74 FILE *fp = fopen("/local/out.txt", "a"); // Open "out.txt" on the local file system for writing
Tyari21 67:18117455f5cb 75 for( ii = 0; ii < 20; ii++ ){
Tyari21 67:18117455f5cb 76 fprintf(fp, "Temperature is %3.1f\r\n",data[ii]);
Tyari21 67:18117455f5cb 77 }
Tyari21 67:18117455f5cb 78 fclose(fp);
Tyari21 67:18117455f5cb 79 */
Tyari21 67:18117455f5cb 80
Jonathan Austin 0:2757d7abb7d9 81 }
Jonathan Austin 0:2757d7abb7d9 82 }