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

Files at this revision

API Documentation at this revision

Comitter:
Stavlin
Date:
Tue Oct 23 10:51:21 2012 +0000
Parent:
0:656c522152d4
Commit message:
Fixed fault reporting. Returns a value of 2000 + fault code on error.

Changed in this revision

max31855.cpp Show annotated file Show diff for this revision Revisions of this file
max31855.h Show annotated file Show diff for this revision Revisions of this file
diff -r 656c522152d4 -r 5eeee89cb281 max31855.cpp
--- a/max31855.cpp	Thu Aug 16 12:46:52 2012 +0000
+++ b/max31855.cpp	Tue Oct 23 10:51:21 2012 +0000
@@ -1,98 +1,102 @@
-
-#include <mbed.h>
-#include "max31855.h"
-
-max31855::max31855(SPI& _spi, PinName _ncs) : spi(_spi), ncs(_ncs) {
-
-}
-
-float max31855::read_temp() {
-    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){
-        //Set CS to initiate transfer and stop conversion
-        select();
-    
-        //Read in Probe tempeature
-        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;
-        
-        //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);
-            
-            if (faultType==1){
-                //Open circuit (no TC)
-                return 0.1;
-            }else if (faultType==2){
-                //Short to GND
-                return 0.2;
-            }else if (faultType==4){
-                //Short to VCC               
-                return 0.4;
-            }else{
-                return 0.5;
-            }
-        }else{
-            //Set CS to stop transfer and restart conversion
-            deselect();    
-    
-            //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!)
-                             
-            return temp;
-        }
-    }else{
-        //Chip not ready for reading
-        return -1;
-    }
-}
-
-void max31855::select() {
-    //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();
-}
-
-void max31855::initialise() {
-    //Start the conversion timer
-    pollTimer.start();
-}
-
-int max31855::ready() {
-    //Check to see if conversion is complete
-    if (pollTimer.read_ms() > 250) {
-        //Conversion complete
-        return 1;
-    }else{
-        //Conversion incomplete
-        return 0;
-    }
+
+#include <mbed.h>
+#include "max31855.h"
+
+max31855::max31855(SPI& _spi, PinName _ncs) : spi(_spi), ncs(_ncs) {
+
+}
+
+float max31855::read_temp() {
+    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){
+        //Set CS to initiate transfer and stop conversion
+        select();
+    
+        //Read in Probe tempeature
+        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;
+        
+        //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{
+            //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!)
+                             
+            return temp;
+        }
+    }else{
+        //Chip not ready for reading
+        return -1;
+    }
+}
+
+void max31855::select() {
+    //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();
+}
+
+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;
+    }
 }
\ No newline at end of file
diff -r 656c522152d4 -r 5eeee89cb281 max31855.h
--- a/max31855.h	Thu Aug 16 12:46:52 2012 +0000
+++ b/max31855.h	Tue Oct 23 10:51:21 2012 +0000
@@ -13,9 +13,10 @@
     max31855(SPI& _spi, PinName _ncs);
     void select();
     void deselect();
-    void initialise();
+    void initialise(int setType=0);
     
     int ready();
+    int faultCode;
     
     float chipTemp;
     float read_temp();