PT100 RTD TEMPERATURE SENSOR AMPLIFIER - MAX31865
Embed:
(wiki syntax)
Show/hide line numbers
max31865.cpp
00001 #include "max31865.h" 00002 #include "mbed.h" 00003 00004 max31865::max31865(PinName MOSI, PinName MISO, PinName SCLK, PinName CS) : spi(MOSI, MISO, SCLK), cs(CS) // mosi, miso, sclk 00005 { 00006 //constructor 00007 cs = 1; //deselect chip 00008 spi.format(8,1); //set mode 1 and 8 bit 00009 spi.frequency(500000); //set frequency to 0.5mhz 00010 00011 00012 } 00013 00014 int max31865::ReadRTD() 00015 { 00016 ClearFault(); 00017 EnableBias(true); 00018 00019 int t = ReadRegistor8(MAX31856_CONFIG_REG); 00020 t |= MAX31856_CONFIG_1SHOT; 00021 WriteRegistor(MAX31856_CONFIG_REG, t); 00022 00023 int RTD = ReadRegistor16(MAX31856_RTDMSB_REG); 00024 00025 // remove fault 00026 RTD >>= 1; 00027 00028 return RTD; 00029 } 00030 00031 void max31865::Begin(max31865_numwires_t wires) 00032 { 00033 cs = 1; 00034 SetWires(wires); 00035 EnableBias(false); 00036 AutoConvert(false); 00037 ClearFault(); 00038 } 00039 00040 int max31865::ReadFault() 00041 { 00042 return ReadRegistor8(MAX31856_FAULTSTAT_REG); 00043 } 00044 00045 void max31865::ClearFault() 00046 { 00047 int t = ReadRegistor8(MAX31856_CONFIG_REG); 00048 t &= ~0x2C; 00049 t |= MAX31856_CONFIG_FAULTSTAT; 00050 WriteRegistor(MAX31856_CONFIG_REG, t); 00051 } 00052 00053 void max31865::EnableBias(bool b) 00054 { 00055 int t = ReadRegistor8(MAX31856_CONFIG_REG); 00056 if (b) 00057 { 00058 t |= MAX31856_CONFIG_BIAS; // enable bias 00059 } 00060 else 00061 { 00062 t &= ~MAX31856_CONFIG_BIAS; // disable bias 00063 } 00064 WriteRegistor(MAX31856_CONFIG_REG, t); 00065 } 00066 00067 void max31865::AutoConvert(bool b) 00068 { 00069 int t = ReadRegistor8(MAX31856_CONFIG_REG); 00070 if (b) 00071 { 00072 t |= MAX31856_CONFIG_MODEAUTO; // enable autoconvert 00073 } 00074 else 00075 { 00076 t &= ~MAX31856_CONFIG_MODEAUTO; // disable autoconvert 00077 } 00078 WriteRegistor(MAX31856_CONFIG_REG, t); 00079 } 00080 00081 void max31865::SetWires(max31865_numwires_t wires ) 00082 { 00083 int t = ReadRegistor8(MAX31856_CONFIG_REG); 00084 00085 if (wires == MAX31865_3WIRE) 00086 { 00087 t |= MAX31856_CONFIG_3WIRE; 00088 } 00089 else 00090 { 00091 // 2 or 4 wire 00092 t &= ~MAX31856_CONFIG_3WIRE; 00093 } 00094 WriteRegistor(MAX31856_CONFIG_REG, t); 00095 } 00096 00097 00098 int max31865::ReadRegistor8(int address) 00099 { 00100 int ret = 0; 00101 ReadRegistorN(address, &ret, 1); 00102 return ret; 00103 } 00104 00105 int max31865::ReadRegistor16(int address) 00106 { 00107 int buffer[2] = {0, 0}; 00108 ReadRegistorN(address, buffer, 2); 00109 00110 int ret = buffer[0]; 00111 ret <<= 8; 00112 ret |= buffer[1]; 00113 00114 return ret; 00115 } 00116 00117 void max31865::ReadRegistorN(int address, int buffer[], int n) 00118 { 00119 address &= 0x7F; // make sure top bit is not set 00120 00121 cs = 0; 00122 00123 spiXfer(address); 00124 00125 while(n--) 00126 { 00127 buffer[0] = spiXfer(0xFF); 00128 buffer++; 00129 } 00130 00131 cs = 1; 00132 } 00133 00134 void max31865::WriteRegistor(int address, int data) 00135 { 00136 00137 cs = 0; //select chip 00138 spiXfer(address | 0x80); // make sure top bit is set 00139 spiXfer(data); 00140 00141 cs = 1; 00142 } 00143 00144 int max31865::spiXfer(int x) 00145 { 00146 return spi.write(x); 00147 }
Generated on Wed Jul 13 2022 18:39:57 by
1.7.2