AA

Fork of max31865 by lzbp li

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MAX31865.h Source File

MAX31865.h

00001 /**************************************************************************
00002  * Arduino driver library for the MAX31865.
00003  *
00004  * Copyright (C) 2015 Ole Wolf <wolf@blazingangles.com>
00005  *
00006  *
00007  * Wire the circuit as follows, assuming that level converters have been
00008  * added for the 3.3V signals:
00009  *
00010  *    Arduino Uno            -->  MAX31865
00011  *    ------------------------------------
00012  *    CS: any available pin  -->  CS
00013  *    MOSI: pin 11           -->  SDI (mandatory for hardware SPI)
00014  *    MISO: pin 12           -->  SDO (mandatory for hardware SPI)
00015  *    SCK: pin 13            -->  SCLK (mandatory for hardware SPI)
00016  *
00017  *
00018  * This program is free software: you can redistribute it and/or modify
00019  * it under the terms of the GNU General Public License as published by
00020  * the Free Software Foundation, either version 3 of the License, or
00021  * (at your option) any later version.
00022  *
00023  * This program is distributed in the hope that it will be useful,
00024  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00025  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00026  * GNU General Public License for more details.
00027  *
00028  * You should have received a copy of the GNU General Public License
00029  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00030  **************************************************************************/
00031 
00032 
00033 #ifndef _MAX31865_H
00034 #define _MAX31865_H
00035 
00036 #include <stdint.h>
00037 #include "mbed.h"
00038 
00039 #define MAX31865_FAULT_HIGH_THRESHOLD  ( 1 << 7 )
00040 #define MAX31865_FAULT_LOW_THRESHOLD   ( 1 << 6 )
00041 #define MAX31865_FAULT_REFIN           ( 1 << 5 )
00042 #define MAX31865_FAULT_REFIN_FORCE     ( 1 << 4 )
00043 #define MAX31865_FAULT_RTDIN_FORCE     ( 1 << 3 )
00044 #define MAX31865_FAULT_VOLTAGE         ( 1 << 2 )
00045 
00046 #define MAX31865_FAULT_DETECTION_NONE      ( 0x00 << 2 )
00047 #define MAX31865_FAULT_DETECTION_AUTO      ( 0x01 << 2 )
00048 #define MAX31865_FAULT_DETECTION_MANUAL_1  ( 0x02 << 2 )
00049 #define MAX31865_FAULT_DETECTION_MANUAL_2  ( 0x03 << 2 )
00050 
00051 
00052 
00053 /* RTD data, RTD current, and measurement reference
00054    voltage. The ITS-90 standard is used; other RTDs
00055    may have coefficients defined by the DIN 43760 or
00056    the U.S. Industrial (American) standard. */
00057 #define RTD_A_ITS90         3.9080e-3
00058 #define RTD_A_USINDUSTRIAL  3.9692e-3
00059 #define RTD_A_DIN43760      3.9848e-3
00060 #define RTD_B_ITS90         -5.870e-7
00061 #define RTD_B_USINDUSTRIAL  -5.8495e-7
00062 #define RTD_B_DIN43760      -5.8019e-7
00063 /* RTD coefficient C is required only for temperatures
00064    below 0 deg. C.  The selected RTD coefficient set
00065    is specified below. */
00066 #define SELECT_RTD_HELPER(x) x
00067 #define SELECT_RTD(x) SELECT_RTD_HELPER(x)
00068 #define RTD_A         SELECT_RTD(RTD_A_ITS90)
00069 #define RTD_B         SELECT_RTD(RTD_B_ITS90)
00070 /*
00071  * The reference resistor on the hardware; see the MAX31865 datasheet
00072  * for details.  The values 400 and 4000 Ohm are recommended values for
00073  * the PT100 and PT1000.
00074  */
00075 #define RTD_RREF_PT100         400 /* Ohm */
00076 #define RTD_RREF_PT1000       4000 /* Ohm */
00077 /*
00078  * The RTD resistance at 0 degrees Celcius.  For the PT100, this is 100 Ohm;
00079  * for the PT1000, it is 1000 Ohm.
00080  */
00081 #define RTD_RESISTANCE_PT100   100 /* Ohm */
00082 #define RTD_RESISTANCE_PT1000 1000 /* Ohm */
00083 
00084 #define RTD_ADC_RESOLUTION  ( 1u << 15 ) /* 15 bits */
00085 
00086 
00087 /* See the main (MAX31865.cpp) file for documentation of the class methods. */
00088 class MAX31865_RTD
00089 {
00090 public:
00091   enum ptd_type { RTD_PT100, RTD_PT1000 };
00092 
00093   MAX31865_RTD( ptd_type type,PinName mosi, PinName miso, PinName sclk, PinName nss);
00094   void configure( bool v_bias, bool conversion_mode, bool one_shot, bool three_wire,
00095                   uint8_t fault_cycle, bool fault_clear, bool filter_50hz,
00096                   uint16_t low_threshold, uint16_t high_threshold );
00097   uint8_t read_all( );
00098   double temperature( ) const;
00099   uint8_t configuration( ) const { return( (measured_configuration == configuration_control_bits)? configuration_control_bits:measured_configuration); }
00100   uint8_t status( ) const { return( measured_status ); }
00101   uint16_t low_threshold( ) const { return( measured_low_threshold ); }
00102   uint16_t high_threshold( ) const  { return( measured_high_threshold ); }
00103   uint16_t raw_resistance( ) const { return( measured_resistance ); }
00104   double resistance( ) const
00105   {
00106     const double rtd_rref =
00107       ( this->type == RTD_PT100 ) ? (double)RTD_RREF_PT100 : (double)RTD_RREF_PT1000;
00108     return( (double)raw_resistance( ) * rtd_rref / (double)RTD_ADC_RESOLUTION );
00109   }
00110 
00111 private:
00112 
00113         /*!
00114     * SPI Interface
00115     */
00116     SPI spi; // mosi, miso, sclk
00117     DigitalOut nss;
00118   /* Our configuration. */
00119  // uint8_t  cs_pin;
00120   ptd_type type;
00121   uint8_t  configuration_control_bits;
00122   uint16_t configuration_low_threshold;
00123   uint16_t configuration_high_threshold;
00124   void reconfigure( );
00125 
00126   /* Values read from the device. */
00127   uint8_t  measured_configuration;
00128   uint16_t measured_resistance;
00129   uint16_t measured_high_threshold;
00130   uint16_t measured_low_threshold;
00131   uint8_t  measured_status;
00132 };
00133 
00134 #endif /* _MAX31865_H */
00135