AA

Fork of max31865 by lzbp li

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MAX31865.cpp Source File

MAX31865.cpp

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 //#include <Arduino.h>
00033 //#include <SPI.h>
00034 #include <MAX31865.h>
00035 #include "mbed.h"
00036 
00037 /**
00038  * The constructor for the MAX31865_RTD class registers the CS pin and
00039  * configures it as an output.
00040  *
00041  * @param [in] cs_pin Arduino pin selected for the CS signal.
00042  */
00043 MAX31865_RTD::MAX31865_RTD( ptd_type type,PinName mosi, PinName miso, PinName sclk, PinName nss)
00044     :spi( mosi, miso, sclk ),
00045      nss( nss )
00046      
00047 
00048 {
00049   /* Set the type of PTD. */
00050       this->type = type;
00051       
00052       spi.format(8,3);
00053       spi.frequency(1000000); 
00054     
00055     //nss = 1;
00056   /* CS pin for the SPI device. */
00057   //this->cs_pin = cs_pin;
00058   //pinMode( this->cs_pin, OUTPUT );
00059 
00060   /* Pull the CS pin high to avoid conflicts on SPI bus. */
00061   //nss = 1;
00062   //nss = type;
00063 }
00064 
00065 
00066 
00067 /**
00068  * Configure the MAX31865.  The parameters correspond to Table 2 in the MAX31865
00069  * datasheet.  The parameters are combined into a control bit-field that is stored
00070  * internally in the class for later reconfiguration, as are the fault threshold values.
00071  *
00072  * @param [in] v_bias Vbias enabled (@a true) or disabled (@a false).
00073  * @param [in] conversion_mode Conversion mode auto (@a true) or off (@a false).
00074  * @param [in] one_shot 1-shot measurement enabled (@a true) or disabled (@a false).
00075  * @param [in] three_wire 3-wire enabled (@a true) or 2-wire/4-wire (@a false).
00076  * @param [in] fault_detection Fault detection cycle control (see Table 3 in the MAX31865
00077  *             datasheet).
00078  * @param [in] fault_clear Fault status auto-clear (@a true) or manual clear (@a false).
00079  * @param [in] filter_50hz 50 Hz filter enabled (@a true) or 60 Hz filter enabled
00080  *             (@a false).
00081  * @param [in] low_threshold Low fault threshold.
00082  * @param [in] high_threshold High fault threshold.
00083 */
00084 void MAX31865_RTD::configure( bool v_bias, bool conversion_mode, bool one_shot,
00085                               bool three_wire, uint8_t fault_cycle, bool fault_clear,
00086                               bool filter_50hz, uint16_t low_threshold,
00087                               uint16_t high_threshold )
00088 {
00089   uint8_t control_bits = 0;
00090    nss = 1;
00091   /* Assemble the control bit mask. */
00092   control_bits |= ( v_bias ? 0x80 : 0 );
00093   control_bits |= ( conversion_mode ? 0x40 : 0 );
00094   control_bits |= ( one_shot ? 0x20 : 0 );
00095   control_bits |= ( three_wire ? 0x10 : 0 );
00096   //control_bits |= (fault_cycle & 0b00001100);
00097   control_bits |= ( fault_clear ? 0x02 : 0 );
00098   control_bits |= ( filter_50hz ? 0x01 : 0 );
00099 
00100   /* Store the control bits and the fault threshold limits for reconfiguration
00101      purposes. */
00102   this->configuration_control_bits   = control_bits;
00103   this->configuration_low_threshold  = low_threshold;
00104   this->configuration_high_threshold = high_threshold;
00105 
00106   /* Perform an initial "reconfiguration." */
00107   reconfigure( );
00108 }
00109 
00110 
00111 
00112 /**
00113  * Reconfigure the MAX31865 by writing the stored control bits and the stored fault
00114  * threshold values back to the chip.
00115  */ 
00116 void MAX31865_RTD::reconfigure( )
00117 {
00118   /* Write the configuration to the MAX31865. */
00119   nss = 0;
00120  // wait_us(100);
00121   spi.write( 0x80 );
00122   spi.write( this->configuration_control_bits );
00123   nss = 1;
00124 
00125   /* Write the threshold values. */
00126   nss = 0;
00127  // wait_us(100);
00128   spi.write( 0x83 );
00129   spi.write( ( this->configuration_high_threshold >> 8 ) & 0x00ff );
00130   spi.write(   this->configuration_high_threshold        & 0x00ff );
00131   spi.write( ( this->configuration_low_threshold >> 8 ) & 0x00ff );
00132   spi.write(   this->configuration_low_threshold        & 0x00ff );
00133   nss = 1;
00134   
00135 }
00136 
00137 
00138 
00139 /**
00140  * Apply the Callendar-Van Dusen equation to convert the RTD resistance
00141  * to temperature:
00142  *
00143  *   \f[
00144  *   t=\frac{-A\pm \sqrt{A^2-4B\left(1-\frac{R_t}{R_0}\right)}}{2B}
00145  *   \f],
00146  *
00147  * where
00148  *
00149  * \f$A\f$ and \f$B\f$ are the RTD coefficients, \f$R_t\f$ is the current
00150  * resistance of the RTD, and \f$R_0\f$ is the resistance of the RTD at 0
00151  * degrees Celcius.
00152  *
00153  * For more information on measuring with an RTD, see:
00154  * <http://newton.ex.ac.uk/teaching/CDHW/Sensors/an046.pdf>.
00155  *
00156  * @param [in] resistance The measured RTD resistance.
00157  * @return Temperature in degrees Celcius.
00158  */
00159 double MAX31865_RTD::temperature( ) const
00160 {
00161   static const double a2   = 2.0 * RTD_B;
00162   static const double b_sq = RTD_A * RTD_A;
00163 
00164   const double rtd_resistance =
00165     ( this->type == RTD_PT100 ) ? RTD_RESISTANCE_PT100 : RTD_RESISTANCE_PT1000;
00166 
00167   double c = 1.0 - resistance( ) / rtd_resistance;
00168   double D = b_sq - 2.0 * a2 * c;
00169   double temperature_deg_C = ( -RTD_A + sqrt( D ) ) / a2;
00170 
00171   return( temperature_deg_C );
00172 }
00173 
00174 
00175 
00176 /**
00177  * Read all settings and measurements from the MAX31865 and store them
00178  * internally in the class.
00179  *
00180  * @return Fault status byte
00181  */
00182 uint8_t MAX31865_RTD::read_all( )
00183 {
00184   uint16_t combined_bytes;
00185     reconfigure( );
00186   /* Start the read operation. */
00187   nss = 0;
00188   /* Tell the MAX31865 that we want to read, starting at register 0. */
00189   spi.write( 0x00 );
00190 
00191   /* Read the MAX31865 registers in the following order:
00192        Configuration
00193        RTD
00194        High Fault Threshold
00195        Low Fault Threshold
00196        Fault Status */
00197 
00198   this->measured_configuration = spi.write( 0x00 );
00199 
00200   combined_bytes  = spi.write( 0x00 ) << 8;
00201   combined_bytes |= spi.write( 0x00 );
00202   this->measured_resistance = combined_bytes >> 1;
00203 
00204   combined_bytes  = spi.write( 0x00 ) << 8;
00205   combined_bytes |= spi.write( 0x00 );
00206   this->measured_high_threshold = combined_bytes >> 1;
00207 
00208   combined_bytes  = spi.write( 0x00 ) << 8;
00209   combined_bytes |= spi.write( 0x00 );
00210   this->measured_low_threshold = combined_bytes >> 1;
00211 
00212   this->measured_status = spi.write( 0x00 );
00213 
00214   nss = 1;
00215 
00216   /* Reset the configuration if the measured resistance is
00217      zero or a fault occurred. */
00218   if(    ( this->measured_resistance == 0 )
00219       || ( this->measured_status != 0 ) )
00220   {
00221     reconfigure( );
00222   }
00223 
00224   return( status( ) );
00225 }
00226 
00227