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:
Thu Aug 16 12:46:52 2012 +0000
Revision:
0:656c522152d4
Child:
1:5eeee89cb281
Initial library release

Who changed what in which revision?

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