Library to interface with the MAX31855 Cold Junction Compensated Thermocouple-to-Digital Converter

Dependents:   max31855Sample Reflow_Oven_Controller test_draft Soldering_Tips_Thermometer

Interface library for the MAX31855 Cold Junction Compendated Thermocouple-to-Digital Converter. This part is the reaplacement for the now discontinued MAX6675. The library is based on the original MAX6675 library found on this site altered to fit the serial specification of the new chip as well as adding a few new features.

The main new feature of note is that the library itself deals with the 0.25 second delay required to complete a conversion. Polling the 'ready()' function will return a '1' when the chip is read to submit a new reading.

Sample Program

#include "mbed.h"
#include "max31855.h"

DigitalOut myled(LED1);

//----------------------------------------------------------
//SPI Interfaces
SPI testSPI(p11,p12,p13);
//----------------------------------------------------------

//----------------------------------------------------------
//Thermocouples
max31855 max1(testSPI,p21);
//----------------------------------------------------------

int main() {
    //Initialise chip (starts internal timer)
    max1.initialise();
    
    //Float value to hold temperature returned
    float fvalue = 0;
    
    while(1) {
        //Check if the chip is ready for a reading to be taken
        if (max1.ready()==1){
            //Get the reading
            fvalue = max1.read_temp();
            
            if (fvalue > 2000){
                if(fvalue==2001){
                    printf("No TC");
                }else if(fvalue==2002){
                    printf("Short to GND");
                }else if(fvalue==2004){
                    printf("Short to VCC");
                }
            }else{
                printf("Temperature is: %f\n\r", fvalue);
            }
     }
        
        //Heartbeat signal (not necessary)
        myled = !myled;
        
        //Delay is not required, here simply for test program
        wait(0.25);
    }
}

Fault Codes:

  • 1 (return value of 2001) - No Thermocouple detected
  • 2 (return value of 2002) - Thermocouple short to GND
  • 4 (return value of 2004) - Thermocouple short to VCC
Committer:
Stavlin
Date:
Tue Oct 23 10:51:21 2012 +0000
Revision:
1:5eeee89cb281
Parent:
0:656c522152d4
Fixed fault reporting. Returns a value of 2000 + fault code on error.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Stavlin 1:5eeee89cb281 1
Stavlin 1:5eeee89cb281 2 #include <mbed.h>
Stavlin 1:5eeee89cb281 3 #include "max31855.h"
Stavlin 1:5eeee89cb281 4
Stavlin 1:5eeee89cb281 5 max31855::max31855(SPI& _spi, PinName _ncs) : spi(_spi), ncs(_ncs) {
Stavlin 1:5eeee89cb281 6
Stavlin 1:5eeee89cb281 7 }
Stavlin 1:5eeee89cb281 8
Stavlin 1:5eeee89cb281 9 float max31855::read_temp() {
Stavlin 1:5eeee89cb281 10 short value = 0;
Stavlin 1:5eeee89cb281 11 float temp = 0;
Stavlin 1:5eeee89cb281 12
Stavlin 1:5eeee89cb281 13 //Variables to hold probe temperature
Stavlin 1:5eeee89cb281 14 uint8_t tempProbeHigh=0;
Stavlin 1:5eeee89cb281 15 uint8_t tempProbeLow=0;
Stavlin 1:5eeee89cb281 16
Stavlin 1:5eeee89cb281 17 //Variables to hold chip temperature and device status
Stavlin 1:5eeee89cb281 18 uint8_t tempChipHigh=0;
Stavlin 1:5eeee89cb281 19 uint8_t tempChipLow=0;
Stavlin 1:5eeee89cb281 20
Stavlin 1:5eeee89cb281 21 if (pollTimer.read_ms() > 250){
Stavlin 1:5eeee89cb281 22 //Set CS to initiate transfer and stop conversion
Stavlin 1:5eeee89cb281 23 select();
Stavlin 1:5eeee89cb281 24
Stavlin 1:5eeee89cb281 25 //Read in Probe tempeature
Stavlin 1:5eeee89cb281 26 tempProbeHigh = spi.write(0);
Stavlin 1:5eeee89cb281 27 tempProbeLow = spi.write(0);
Stavlin 1:5eeee89cb281 28
Stavlin 1:5eeee89cb281 29 //Get the chip temperature and the fault data
Stavlin 1:5eeee89cb281 30 tempChipHigh = spi.write(0);
Stavlin 1:5eeee89cb281 31 tempChipLow = spi.write(0);
Stavlin 1:5eeee89cb281 32
Stavlin 1:5eeee89cb281 33 //Set the chip temperature
Stavlin 1:5eeee89cb281 34 chipTemp = (tempChipHigh<<4 | tempChipLow>>4)*0.25;
Stavlin 1:5eeee89cb281 35
Stavlin 1:5eeee89cb281 36 //Set CS to stop transfer and restart conversion
Stavlin 1:5eeee89cb281 37 deselect();
Stavlin 1:5eeee89cb281 38
Stavlin 1:5eeee89cb281 39 //Check for a fault (last bit of transfer is fault bit)
Stavlin 1:5eeee89cb281 40 if ((tempProbeLow & 1)==1){
Stavlin 1:5eeee89cb281 41 //Chip reports a fault, extract fault from Chip Temperature data
Stavlin 1:5eeee89cb281 42 int faultType = (tempChipLow & 7);
Stavlin 1:5eeee89cb281 43
Stavlin 1:5eeee89cb281 44 faultCode=faultType;
Stavlin 1:5eeee89cb281 45
Stavlin 1:5eeee89cb281 46 return 2000+faultType;
Stavlin 1:5eeee89cb281 47 /*if (faultType==1){
Stavlin 1:5eeee89cb281 48 //Open circuit (no TC)
Stavlin 1:5eeee89cb281 49 return 2000 + faultType;
Stavlin 1:5eeee89cb281 50 }else if (faultType==2){
Stavlin 1:5eeee89cb281 51 //Short to GND
Stavlin 1:5eeee89cb281 52 return 2000 + faultType;
Stavlin 1:5eeee89cb281 53 }else if (faultType==4){
Stavlin 1:5eeee89cb281 54 //Short to VCC
Stavlin 1:5eeee89cb281 55 return 0.4;
Stavlin 1:5eeee89cb281 56 }else{
Stavlin 1:5eeee89cb281 57 return 0.5;
Stavlin 1:5eeee89cb281 58 }*/
Stavlin 1:5eeee89cb281 59 }else{
Stavlin 1:5eeee89cb281 60 //Integer value of temperature
Stavlin 1:5eeee89cb281 61 value = (tempProbeHigh<< 6 | tempProbeLow>>2);
Stavlin 1:5eeee89cb281 62
Stavlin 1:5eeee89cb281 63 //Get actual temperature (last 2 bits of integer are decimal 0.5 and 0.25)
Stavlin 1:5eeee89cb281 64 temp = (value*0.25); // Multiply the value by 0.25 to get temp in C or
Stavlin 1:5eeee89cb281 65 // * (9.0/5.0)) + 32.0; // Convert value to F (ensure proper floats!)
Stavlin 1:5eeee89cb281 66
Stavlin 1:5eeee89cb281 67 return temp;
Stavlin 1:5eeee89cb281 68 }
Stavlin 1:5eeee89cb281 69 }else{
Stavlin 1:5eeee89cb281 70 //Chip not ready for reading
Stavlin 1:5eeee89cb281 71 return -1;
Stavlin 1:5eeee89cb281 72 }
Stavlin 1:5eeee89cb281 73 }
Stavlin 1:5eeee89cb281 74
Stavlin 1:5eeee89cb281 75 void max31855::select() {
Stavlin 1:5eeee89cb281 76 //Set CS low to start transmission (interrupts conversion)
Stavlin 1:5eeee89cb281 77 ncs = 0;
Stavlin 1:5eeee89cb281 78 }
Stavlin 1:5eeee89cb281 79
Stavlin 1:5eeee89cb281 80 void max31855::deselect() {
Stavlin 1:5eeee89cb281 81 //Set CS high to stop transmission (restarts conversion)
Stavlin 1:5eeee89cb281 82 ncs = 1;
Stavlin 1:5eeee89cb281 83 //Reset conversion timer
Stavlin 1:5eeee89cb281 84 pollTimer.reset();
Stavlin 1:5eeee89cb281 85 }
Stavlin 1:5eeee89cb281 86
Stavlin 1:5eeee89cb281 87 void max31855::initialise(int setType) {
Stavlin 1:5eeee89cb281 88 //Start the conversion timer
Stavlin 1:5eeee89cb281 89 pollTimer.start();
Stavlin 1:5eeee89cb281 90 faultCode=0;
Stavlin 1:5eeee89cb281 91 }
Stavlin 1:5eeee89cb281 92
Stavlin 1:5eeee89cb281 93 int max31855::ready() {
Stavlin 1:5eeee89cb281 94 //Check to see if conversion is complete
Stavlin 1:5eeee89cb281 95 if (pollTimer.read_ms() > 250) {
Stavlin 1:5eeee89cb281 96 //Conversion complete
Stavlin 1:5eeee89cb281 97 return 1;
Stavlin 1:5eeee89cb281 98 }else{
Stavlin 1:5eeee89cb281 99 //Conversion incomplete
Stavlin 1:5eeee89cb281 100 return 0;
Stavlin 1:5eeee89cb281 101 }
Stavlin 0:656c522152d4 102 }