Library to interface with the MAX31855 Cold Junction Compensated Thermocouple-to-Digital Converter
Fork of MAX31855 by
max31855.cpp
00001 00002 #include <mbed.h> 00003 #include "max31855.h" 00004 00005 max31855::max31855(SPI& _spi, PinName _ncs) : spi(_spi), ncs(_ncs) { 00006 00007 } 00008 00009 float max31855::read_temp() { 00010 short value = 0; 00011 float temp = 0; 00012 00013 //Variables to hold probe temperature 00014 uint8_t tempProbeHigh=0; 00015 uint8_t tempProbeLow=0; 00016 00017 //Variables to hold chip temperature and device status 00018 uint8_t tempChipHigh=0; 00019 uint8_t tempChipLow=0; 00020 00021 if (pollTimer.read_ms() > 250){ 00022 //Set CS to initiate transfer and stop conversion 00023 select(); 00024 00025 //Read in Probe tempeature 00026 tempProbeHigh = spi.write(0); 00027 tempProbeLow = spi.write(0); 00028 00029 //Get the chip temperature and the fault data 00030 tempChipHigh = spi.write(0); 00031 tempChipLow = spi.write(0); 00032 00033 //Set the chip temperature 00034 chipTemp = (tempChipHigh<<4 | tempChipLow>>4)*0.0625; 00035 00036 //Set CS to stop transfer and restart conversion 00037 deselect(); 00038 00039 //Check for a fault (last bit of transfer is fault bit) 00040 if ((tempProbeLow & 1)==1){ 00041 //Chip reports a fault, extract fault from Chip Temperature data 00042 int faultType = (tempChipLow & 7); 00043 00044 faultCode=faultType; 00045 00046 return 2000+faultType; 00047 /*if (faultType==1){ 00048 //Open circuit (no TC) 00049 return 2000 + faultType; 00050 }else if (faultType==2){ 00051 //Short to GND 00052 return 2000 + faultType; 00053 }else if (faultType==4){ 00054 //Short to VCC 00055 return 0.4; 00056 }else{ 00057 return 0.5; 00058 }*/ 00059 }else{ 00060 //Integer value of temperature 00061 value = (tempProbeHigh<< 6 | tempProbeLow>>2); 00062 00063 //Get actual temperature (last 2 bits of integer are decimal 0.5 and 0.25) 00064 temp = (value*0.25); // Multiply the value by 0.25 to get temp in C or 00065 // * (9.0/5.0)) + 32.0; // Convert value to F (ensure proper floats!) 00066 00067 return temp; 00068 } 00069 }else{ 00070 //Chip not ready for reading 00071 return -1; 00072 } 00073 } 00074 00075 void max31855::select() { 00076 //Set CS low to start transmission (interrupts conversion) 00077 ncs = 0; 00078 } 00079 00080 void max31855::deselect() { 00081 //Set CS high to stop transmission (restarts conversion) 00082 ncs = 1; 00083 //Reset conversion timer 00084 pollTimer.reset(); 00085 } 00086 00087 void max31855::initialise(int setType) { 00088 //Start the conversion timer 00089 pollTimer.start(); 00090 faultCode=0; 00091 } 00092 00093 int max31855::ready() { 00094 //Check to see if conversion is complete 00095 if (pollTimer.read_ms() > 250) { 00096 //Conversion complete 00097 return 1; 00098 }else{ 00099 //Conversion incomplete 00100 return 0; 00101 } 00102 }
Generated on Mon Jul 25 2022 05:55:29 by 1.7.2