improved MAX31855 Library with errorchecking
Fork of MAX31855 by
Diff: max31855.cpp
- Revision:
- 2:2613cf295ccb
- Parent:
- 1:5eeee89cb281
- Child:
- 3:e2731f1e595b
--- a/max31855.cpp Tue Oct 23 10:51:21 2012 +0000 +++ b/max31855.cpp Wed Jan 06 15:49:29 2016 +0000 @@ -1,102 +1,80 @@ - #include <mbed.h> #include "max31855.h" -max31855::max31855(SPI& _spi, PinName _ncs) : spi(_spi), ncs(_ncs) { +max31855::max31855(SPI& _spi, PinName ncs1, PinName ncs2) : _spi(_spi), _CS_1(ncs1), _CS_2(ncs2) +{ } -float max31855::read_temp() { +float max31855::read_temp(max31855::THERMOCOUPLES thermo) +{ short value = 0; float temp = 0; - //Variables to hold probe temperature uint8_t tempProbeHigh=0; uint8_t tempProbeLow=0; - //Variables to hold chip temperature and device status uint8_t tempChipHigh=0; uint8_t tempChipLow=0; - - if (pollTimer.read_ms() > 250){ + if (_pollTimer.read_ms() > 250) { //Set CS to initiate transfer and stop conversion - select(); - + select(thermo); //Read in Probe tempeature - tempProbeHigh = spi.write(0); - tempProbeLow = spi.write(0); - + tempProbeHigh = _spi.write(0); + tempProbeLow = _spi.write(0); //Get the chip temperature and the fault data - tempChipHigh = spi.write(0); - tempChipLow = spi.write(0); - - //Set the chip temperature - chipTemp = (tempChipHigh<<4 | tempChipLow>>4)*0.25; - + tempChipHigh = _spi.write(0); + tempChipLow = _spi.write(0); + //Set the chip temperature + _chipTemp = (tempChipHigh<<4 | tempChipLow>>4)*0.0625; //Set CS to stop transfer and restart conversion - deselect(); - - //Check for a fault (last bit of transfer is fault bit) - if ((tempProbeLow & 1)==1){ - //Chip reports a fault, extract fault from Chip Temperature data - int faultType = (tempChipLow & 7); - - faultCode=faultType; - - return 2000+faultType; - /*if (faultType==1){ - //Open circuit (no TC) - return 2000 + faultType; - }else if (faultType==2){ - //Short to GND - return 2000 + faultType; - }else if (faultType==4){ - //Short to VCC - return 0.4; - }else{ - return 0.5; - }*/ - }else{ + deselect(thermo); + if (CHECK_BIT(tempProbeLow, 0)) { + //not connected error + if(CHECK_BIT(tempChipLow, 0))return -2; + //short to ground + else if(CHECK_BIT(tempChipLow, 1))return -3; + //short to VCC + else if(CHECK_BIT(tempChipLow, 2))return -4; + else return -5; + } else { //Integer value of temperature value = (tempProbeHigh<< 6 | tempProbeLow>>2); - //Get actual temperature (last 2 bits of integer are decimal 0.5 and 0.25) temp = (value*0.25); // Multiply the value by 0.25 to get temp in C or - // * (9.0/5.0)) + 32.0; // Convert value to F (ensure proper floats!) - + // * (9.0/5.0)) + 32.0; // Convert value to F (ensure proper floats!) return temp; } - }else{ - //Chip not ready for reading - return -1; + } else { + return 8; } } -void max31855::select() { +void max31855::select(THERMOCOUPLES thermo) +{ //Set CS low to start transmission (interrupts conversion) - ncs = 0; -} - -void max31855::deselect() { - //Set CS high to stop transmission (restarts conversion) - ncs = 1; - //Reset conversion timer - pollTimer.reset(); + if(thermo == THERMO_1) _CS_1 = 0; + else _CS_2 = 0; } -void max31855::initialise(int setType) { - //Start the conversion timer - pollTimer.start(); - faultCode=0; +void max31855::deselect(THERMOCOUPLES thermo) +{ + //Set CS high to stop transmission (restarts conversion) + if(thermo == THERMO_1) _CS_1 = 1; + else _CS_2 = 1; + _pollTimer.reset(); } -int max31855::ready() { +void max31855::initialise(int setType) +{ + //Start the conversion timer + _pollTimer.start(); + _faultCode=0; +} + +int max31855::ready() +{ //Check to see if conversion is complete - if (pollTimer.read_ms() > 250) { - //Conversion complete - return 1; - }else{ - //Conversion incomplete - return 0; - } + if(_pollTimer.read_ms() > 500 ) return 1; + else return 0; } \ No newline at end of file