MAX31855 Cold-Junction Compensated Thermocouple-to-Digital Converter

Files at this revision

API Documentation at this revision

Comitter:
mederic
Date:
Tue Sep 09 08:17:42 2014 +0000
Child:
1:aa96d283eead
Commit message:
first release

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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MAX31855.cpp	Tue Sep 09 08:17:42 2014 +0000
@@ -0,0 +1,74 @@
+#include "MAX31855.h"
+
+//***********************************/************************************
+//                         Constructors                                 //
+//***********************************/************************************
+MAX31855::MAX31855(PinName mosi, PinName miso, PinName sck, PinName ncs) :_spi(mosi,miso,sck), _ncs(ncs)
+{
+    _ncs = 1;   //CS high
+    _spi.format(16,0);
+    _spi.frequency(1000000);
+}
+
+//***********************************/************************************
+//                                Get Set                               //
+//***********************************/************************************
+bool MAX31855::opened(void)
+{
+    read();
+    return _oc;
+}
+
+bool MAX31855::fault(void)
+{
+    read();
+    return _fault;
+}
+
+bool MAX31855::scToVcc(void)
+{
+    read();
+    return _scv;
+}
+
+bool MAX31855::scToGnd(void)
+{
+    read();
+    return _scg;
+}
+
+
+
+//***********************************/************************************
+//                             Public Methods                           //
+//***********************************/************************************
+float MAX31855::thermocouple(void)
+{
+    read();
+    return _t;
+}
+
+float MAX31855::chip(void)
+{
+    read();
+    return _chip_t;
+}
+    
+//***********************************/************************************
+//                             Protected Methods                        //
+//***********************************/************************************
+void MAX31855::read(void)
+{
+    _ncs = 0;
+    unsigned short high = _spi.write(0);
+    unsigned short  low = _spi.write(0);
+    _ncs = 1;
+    
+    _t = (high>>2)/4.0;
+    _chip_t = (low>>4)/16.0;
+    _fault = high&0x01;
+    _scv = low&0x04;
+    _scg = low&0x02;
+    _oc = low&0x01;
+    
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MAX31855.h	Tue Sep 09 08:17:42 2014 +0000
@@ -0,0 +1,93 @@
+#ifndef MAX31855_H
+#define MAX31855_H
+
+#include "mbed.h"
+/** MAX31855 class.
+ *  Used for read MAX31855 Cold-Junction Compensated Thermocouple-to-Digital Converter
+ *
+ * Example:
+ * @code
+ * DigitalOut led(LED2);
+ * Serial pc(USBTX,USBRX);
+ * MAX31855 therm(p5,p6,p7,p8);
+ * 
+ * int main() 
+ * {
+ *     while(1) 
+ *     {
+ *         pc.printf("T=%f;Chip=%f\r\n",therm.thermocouple(),therm.chip());
+ *         led  = therm.fault();
+ *         wait(0.5);
+ *     }
+ * }
+ * @endcode
+ */  
+class MAX31855
+{
+public:
+    /** Create MAX31855 instance connected to spi & ncs
+    * @param mosi SPI master out slave in pin (MAX31855 is only read device)
+    * @param miso SPI master in slave out pin
+    * @param sck SPI clock pin
+    * @param ncs pin to connect at CS input
+    */
+    MAX31855(PinName mosi, PinName miso, PinName sck, PinName ncs);
+    
+    /**Get Thermocouple temperature
+    * @returns temperature [°C]
+    */    
+    float thermocouple(void);
+    
+    /**Get Chip temperature
+    * @returns temperature [°C]
+    */       
+    float chip(void);
+    
+    /**Check if thermocouple disconnected
+    */
+    bool opened(void);
+    
+    /**Check if an error
+    */
+    bool fault(void);
+    
+    /**Check if thermocouple short-circuited to Vcc
+    */
+    bool scToVcc(void);
+    
+    /**Check if thermocouple shorted-circuited to GND
+    */
+    bool scToGnd(void);
+
+#ifdef MBED_OPERATORS
+    /** An operator shorthand for thermocouple()
+     *
+     * The float() operator can be used as a shorthand for thermocouple() to simplify common code sequences
+     *
+     * Example:
+     * @code
+     * float x = temp.thermocouple();
+     * float x = temp;
+     *
+     * if(temp.thermocouple() > 20.25) { ... }
+     * if(temp > 20.25) { ... }
+     * @endcode
+     */
+    operator float(){return thermocouple();}
+#endif
+    
+protected:
+    void read(void);
+      
+private:
+    SPI         _spi;
+    DigitalOut  _ncs;
+    float       _t;
+    float       _chip_t;
+    bool        _fault;
+    bool        _scv;
+    bool        _scg;
+    bool        _oc;
+};
+
+#endif
\ No newline at end of file