added spectrometer timeout

Dependencies:   SDFileSystem mbed

Fork of All_Combined_Real2 by Colin Maxfield

Committer:
colingm
Date:
Tue Apr 12 18:13:57 2016 +0000
Revision:
12:2df303e4aee2
Parent:
9:924d80730ac5
The const char wasn't working

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jphbergeson 3:c611fff05072 1 #include "temp_sensor.h"
jphbergeson 3:c611fff05072 2
jphbergeson 3:c611fff05072 3 /* MCP9808 high accuracy temp sensor from adafruit (no address pins pulled up) */
jphbergeson 3:c611fff05072 4 #define MCP9808_REG_TEMP (0x05) // Temperature Register
jphbergeson 3:c611fff05072 5 #define MCP9808_REG_CONF (0x01) // Configuration Register
jphbergeson 3:c611fff05072 6 #define MCP9808_ADDR (0x30) // MCP9808 base address 0x18<<1
jphbergeson 3:c611fff05072 7
jphbergeson 3:c611fff05072 8 I2C i2c(PTE25, PTE24);
jphbergeson 3:c611fff05072 9
jphbergeson 3:c611fff05072 10 char data_write[3];
jphbergeson 3:c611fff05072 11 char data_read[2];
jphbergeson 3:c611fff05072 12 int tempval;
jphbergeson 3:c611fff05072 13
jphbergeson 3:c611fff05072 14 volatile char TempCelsiusDisplay[] = "+abc.dd C";
jphbergeson 9:924d80730ac5 15 double tempCelsiusDouble;
jphbergeson 3:c611fff05072 16
jphbergeson 3:c611fff05072 17 void init_temp_sensor() {
jphbergeson 3:c611fff05072 18
jphbergeson 3:c611fff05072 19 wait(3);
jphbergeson 3:c611fff05072 20 i2c.frequency(10000); // default is 100000
jphbergeson 3:c611fff05072 21
jphbergeson 3:c611fff05072 22 /* Configure the Temperature sensor device MCP9808:
jphbergeson 3:c611fff05072 23 - Thermostat mode Interrupt not used
jphbergeson 3:c611fff05072 24 - Fault tolerance: 0
jphbergeson 3:c611fff05072 25 */
jphbergeson 3:c611fff05072 26 data_write[0] = MCP9808_REG_CONF;
jphbergeson 3:c611fff05072 27 data_write[1] = 0x00; // config msb
jphbergeson 3:c611fff05072 28 data_write[2] = 0x00; // config lsb
jphbergeson 3:c611fff05072 29 int status = i2c.write(MCP9808_ADDR, data_write, 3, 0);
jphbergeson 3:c611fff05072 30 if (status != 0) { // Error
jphbergeson 3:c611fff05072 31 // pc.printf(" error status = 0x%08x\r\n", status);
jphbergeson 3:c611fff05072 32 // while (1) {
jphbergeson 3:c611fff05072 33 // myled = !myled;
jphbergeson 3:c611fff05072 34 // wait(0.2);
jphbergeson 3:c611fff05072 35 // }
jphbergeson 3:c611fff05072 36 }
jphbergeson 3:c611fff05072 37
jphbergeson 3:c611fff05072 38 }
jphbergeson 3:c611fff05072 39
jphbergeson 3:c611fff05072 40 void check_temp() {
jphbergeson 3:c611fff05072 41 // Read temperature register
jphbergeson 3:c611fff05072 42 data_write[0] = MCP9808_REG_TEMP;
jphbergeson 3:c611fff05072 43 i2c.write(MCP9808_ADDR, data_write, 1, 1); // no stop
jphbergeson 3:c611fff05072 44 i2c.read(MCP9808_ADDR, data_read, 2, 0);
jphbergeson 3:c611fff05072 45
jphbergeson 3:c611fff05072 46 if(data_read[0] & 0xE0) {
jphbergeson 3:c611fff05072 47 data_read[0] = data_read[0] & 0x1F; // clear flag bits
jphbergeson 3:c611fff05072 48 }
jphbergeson 3:c611fff05072 49 if((data_read[0] & 0x10) == 0x10) {
jphbergeson 3:c611fff05072 50 data_read[0] = data_read[0] & 0x0F;
jphbergeson 3:c611fff05072 51 TempCelsiusDisplay[0] = '-';
jphbergeson 3:c611fff05072 52 tempval = 256 - (data_read[0] << 4) + (data_read[1] >> 4);
jphbergeson 3:c611fff05072 53 } else {
jphbergeson 3:c611fff05072 54 TempCelsiusDisplay[0] = '+';
jphbergeson 3:c611fff05072 55 tempval = (data_read[0] << 4) + (data_read[1] >> 4);
jphbergeson 3:c611fff05072 56 }
jphbergeson 3:c611fff05072 57
jphbergeson 3:c611fff05072 58 // fractional part (0.25°C precision)
jphbergeson 3:c611fff05072 59 if (data_read[1] & 0x08) {
jphbergeson 3:c611fff05072 60 if(data_read[1] & 0x04) {
jphbergeson 3:c611fff05072 61 TempCelsiusDisplay[5] = '7';
jphbergeson 3:c611fff05072 62 TempCelsiusDisplay[6] = '5';
jphbergeson 3:c611fff05072 63 } else {
jphbergeson 3:c611fff05072 64 TempCelsiusDisplay[5] = '5';
jphbergeson 3:c611fff05072 65 TempCelsiusDisplay[6] = '0';
jphbergeson 3:c611fff05072 66 }
jphbergeson 3:c611fff05072 67 } else {
jphbergeson 3:c611fff05072 68 if(data_read[1] & 0x04) {
jphbergeson 3:c611fff05072 69 TempCelsiusDisplay[5] = '2';
jphbergeson 3:c611fff05072 70 TempCelsiusDisplay[6] = '5';
jphbergeson 3:c611fff05072 71 }else{
jphbergeson 3:c611fff05072 72 TempCelsiusDisplay[5] = '0';
jphbergeson 3:c611fff05072 73 TempCelsiusDisplay[6] = '0';
jphbergeson 3:c611fff05072 74 }
jphbergeson 3:c611fff05072 75 }
jphbergeson 3:c611fff05072 76
jphbergeson 3:c611fff05072 77 // Integer part
jphbergeson 3:c611fff05072 78 TempCelsiusDisplay[1] = (tempval / 100) + 0x30;
jphbergeson 3:c611fff05072 79 TempCelsiusDisplay[2] = ((tempval % 100) / 10) + 0x30;
jphbergeson 3:c611fff05072 80 TempCelsiusDisplay[3] = ((tempval % 100) % 10) + 0x30;
jphbergeson 9:924d80730ac5 81
jphbergeson 9:924d80730ac5 82 if (TempCelsiusDisplay[0] == '+') {
colingm 12:2df303e4aee2 83 sscanf((const char *)TempCelsiusDisplay, "+%lf C", &tempCelsiusDouble);
jphbergeson 9:924d80730ac5 84 } else {
jphbergeson 9:924d80730ac5 85 // negative
colingm 12:2df303e4aee2 86 sscanf((const char *)TempCelsiusDisplay, "%lf C", &tempCelsiusDouble);
jphbergeson 9:924d80730ac5 87 }
jphbergeson 3:c611fff05072 88 }
jphbergeson 3:c611fff05072 89
jphbergeson 3:c611fff05072 90
jphbergeson 3:c611fff05072 91