Maxim Integrated / Mbed 2 deprecated MAX31856_example_program

Dependencies:   MAX31856 mbed

Fork of MAX31856_example_program by Central Applications - Mbed Code repo

Revision:
7:2e45068189b1
Parent:
6:e1200ae7d6a3
Child:
8:8723d0006097
--- a/MAX31856.cpp	Mon Jul 31 18:10:24 2017 +0000
+++ b/MAX31856.cpp	Mon Jul 31 18:46:49 2017 +0000
@@ -1,17 +1,18 @@
 #include <mbed.h>
-#include <string>
 #include "MAX31856.h"
 
 #define LOG(args...)    printf(args)
 
-MAX31856::MAX31856(SPI& _spi, PinName _ncs, uint8_t _type, uint8_t _fltr, uint8_t _samples, uint8_t _conversion_mode) : spi(_spi), ncs(_ncs), samples(_samples) {  //, setThermocoupleType(_type), setEmiFilterFreq(_fltr)
-    spi.format(8,3);
+MAX31856::MAX31856(SPI& _spi, PinName _ncs, uint8_t _type, uint8_t _fltr, uint8_t _samples, uint8_t _conversion_mode) : spi(_spi), ncs(_ncs), samples(_samples) {  
+    spi.format(8,3); //configure the correct SPI mode to beable to program the registers intially correctly
     setThermocoupleType(_type);
     setEmiFilterFreq(_fltr);
     setNumSamplesAvg(_samples);
     setConversionMode(_conversion_mode);
 }
 
+
+//*****************************************************************************
 float MAX31856::readTC()
 {
     int32_t temp;
@@ -26,25 +27,18 @@
             buf_read[i]=spi.write(buf_write[i]);
             spiDisable();
         }   
-
-    
-//    spiEnable();
-//    buf_read[0]=spi.write(0x0C);
-//    buf_read[0]=spi.write(0x0C);
-//    buf_read[1]=spi.write(0x0C);
-//    buf_read[2]=spi.write(0x0C);
-//    spiDisable();
-
         
         //Convert the registers contents into the correct value
         temp =((buf_read[0] & 0xFF) << 11);       //Shift Byte 2 into place
         temp|=((buf_read[1] & 0xFF) << 3);        //Shift Byte 1 into place
         temp|=((buf_read[2] & 0xFF) >> 5);        //Shift Byte 0 into place
-        float val=(temp/128.0f);            //Divide the binary string by 2 to the 7th power
+        float val=(temp/128.0f);                  //Divide the binary string by 2 to the 7th power
         return val;
 //  }
 }
 
+
+//*****************************************************************************
 float MAX31856::readCJ()
 {
     int32_t temp;
@@ -65,8 +59,11 @@
     return val;
 }
 
+
 //Register:CR0    Bits: 7
-bool MAX31856::setConversionMode(uint8_t val) {
+//*****************************************************************************
+bool MAX31856::setConversionMode(uint8_t val) 
+{
     if      (val==CR0_CONV_MODE_NORMALLY_OFF) {
         return_val=registerReadWriteByte(ADDRESS_CR0_READ, ADDRESS_CR0_WRITE, CR0_CLEAR_BITS_7, val);
         conversion_mode=0;
@@ -84,8 +81,11 @@
     return return_val;
 }
 
+
 //Register:CR0    Bits: 6
-bool MAX31856::setOneShotMode(uint8_t val) {
+//*****************************************************************************
+bool MAX31856::setOneShotMode(uint8_t val) 
+{
     if      (val==CR0_1_SHOT_MODE_NO_CONVERSION) {
         return_val=registerReadWriteByte(ADDRESS_CR0_READ, ADDRESS_CR0_WRITE, CR0_CLEAR_BITS_6, val);
         LOG("Register containing\t\tsetOneShotMode\t\twas programmed with the parameter\t\tCR0_1_SHOT_MODE_NO_CONVERSIONS\r\n");
@@ -101,8 +101,11 @@
     return return_val;
 }
 
+
 //Register:CR0    Bits: 5:4
-bool MAX31856::setOpenCircuitFaultDetection(uint8_t val) {
+//*****************************************************************************
+bool MAX31856::setOpenCircuitFaultDetection(uint8_t val) 
+{
     if      (val==CR0_OC_DETECT_DISABLED) { 
         return_val=registerReadWriteByte(ADDRESS_CR0_READ, ADDRESS_CR0_WRITE, CR0_CLEAR_BITS_5_4, val);
         LOG("Register containing\t\tsetOpenCircuitFaultDetection\t\twas programmed with the parameter\t\tCR0_OC_DETECT_DISABLED\r\n");
@@ -126,8 +129,11 @@
     return return_val;
 }
 
+
 //Register:CR0    Bits: 3
-bool MAX31856::setColdJunctionDisable(uint8_t val) {
+//*****************************************************************************
+bool MAX31856::setColdJunctionDisable(uint8_t val) 
+{
     if (val==CR0_COLD_JUNC_ENABLE) { 
         return_val=registerReadWriteByte(ADDRESS_CR0_READ, ADDRESS_CR0_WRITE, CR0_CLEAR_BITS_3, val);
         cold_junction_enabled=1;
@@ -145,8 +151,11 @@
     return return_val;
 }
 
+
 //Register:CR0    Bits: 2
-bool MAX31856::setFaultMode(uint8_t val) {
+//*****************************************************************************
+bool MAX31856::setFaultMode(uint8_t val) 
+{
     if      (val==CR0_FAULT_MODE_COMPARATOR) {
         return_val=registerReadWriteByte(ADDRESS_CR0_READ, ADDRESS_CR0_WRITE, CR0_CLEAR_BITS_2, val);
         LOG("Register containing\t\tsetFaultMode\t\twas programmed with the parameter\t\tCR0_FAULT_MODE_COMPARATOR\r\n");
@@ -162,8 +171,11 @@
     return return_val;
 }
 
+
 //Register:CR0    Bits: 1
-bool MAX31856::setFaultStatusClear(uint8_t val) {
+//*****************************************************************************
+bool MAX31856::setFaultStatusClear(uint8_t val) 
+{
     if      (val==CR0_FAULTCLR_DEFAULT_VAL) { 
         return_val=registerReadWriteByte(ADDRESS_CR0_READ, ADDRESS_CR0_WRITE, CR0_CLEAR_BITS_1, val);
         LOG("Register containing\t\tsetFaultStatusClear\t\twas programmed with the parameter\t\tCR0_FAULTCLR_DEFAULT_VAL\r\n");
@@ -179,8 +191,11 @@
     return return_val;
 }
 
+
 //Register:CR0    Bits: 0
-bool MAX31856::setEmiFilterFreq(uint8_t val) {
+//*****************************************************************************
+bool MAX31856::setEmiFilterFreq(uint8_t val) 
+{
     if      (val==CR0_FILTER_OUT_60Hz) {
         return_val=registerReadWriteByte(ADDRESS_CR0_READ, ADDRESS_CR0_WRITE, CR0_CLEAR_BITS_0, val);
         filter_mode=0;
@@ -198,8 +213,11 @@
     return return_val;
 }
 
+
 //Register:CR1    Bits: 6:4
-bool MAX31856::setNumSamplesAvg(uint8_t val) {   
+//*****************************************************************************
+bool MAX31856::setNumSamplesAvg(uint8_t val) 
+{   
     if      (val==CR1_AVG_TC_SAMPLES_1) {
         return_val=registerReadWriteByte(ADDRESS_CR1_READ, ADDRESS_CR1_WRITE, CR1_CLEAR_BITS_6_4, val);
         samples=1;
@@ -232,8 +250,11 @@
     return return_val;
 }
 
+
 //Register:CR1    Bits: 3:0
-bool MAX31856::setThermocoupleType(uint8_t val) {   
+//*****************************************************************************
+bool MAX31856::setThermocoupleType(uint8_t val) 
+{   
     if      (val==CR1_TC_TYPE_B) {
         return_val=registerReadWriteByte(ADDRESS_CR1_READ, ADDRESS_CR1_WRITE, CR1_CLEAR_BITS_3_0, val);
         voltage_mode=false;
@@ -291,8 +312,11 @@
     return return_val;
 }
 
+
 //Register:MASK    Bits: 5:0
-bool MAX31856::setFaultMasks(uint8_t val, bool enable) {
+//*****************************************************************************
+bool MAX31856::setFaultMasks(uint8_t val, bool enable) 
+{
     if(enable)
         val=0;
     ;
@@ -327,9 +351,12 @@
     return return_val;
 }
 
+
 //Register:MASK    Bits: 5:0
-float MAX31856::setFaultThresholds(uint8_t val, bool enable_mask, float temperature) {
-    float return_val;///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////ghjkcghjcfczseraweraenaet aert e
+//******************************************************************************
+float MAX31856::setFaultThresholds(uint8_t val, bool enable_mask, float temperature) 
+{
+    float return_val;
     uint8_t temp_val;
     if(enable_mask) {
         temp_val=0;
@@ -411,16 +438,25 @@
 
 
 //The following functions are for internal library use only
-void MAX31856::spiEnable() {
-    ncs=0; //Set CS high to start transmission (interrupts conversion)
-    return;
-}
-void MAX31856::spiDisable() {
-    ncs=1; //Set CS low to stop transmission (restarts conversion)
+//******************************************************************************
+void MAX31856::spiEnable() 
+{
+    ncs=0; //Set CS low to start transmission (interrupts conversion)
     return;
 }
 
-bool MAX31856::registerReadWriteByte(uint8_t read_address, uint8_t write_address, uint8_t clear_bits, uint8_t val) {   
+
+//******************************************************************************
+void MAX31856::spiDisable() 
+{
+    ncs=1; //Set CS high to stop transmission (restarts conversion)
+    return;
+}
+
+
+//******************************************************************************
+bool MAX31856::registerReadWriteByte(uint8_t read_address, uint8_t write_address, uint8_t clear_bits, uint8_t val) 
+{   
     uint8_t buf_read[2];
     
     //Read the current contents of a register
@@ -443,7 +479,10 @@
     return true;
 }
 
-bool MAX31856::registerWriteByte(uint8_t write_address, uint8_t val) {   
+
+//******************************************************************************
+bool MAX31856::registerWriteByte(uint8_t write_address, uint8_t val) 
+{   
     //Write the updated byte to the register 
     spiEnable();
     spi.write(write_address);
@@ -452,20 +491,30 @@
     return true;
 }
 
-int8_t MAX31856::twosComplimentToSigned8(int8_t temp){
+
+//******************************************************************************
+int8_t MAX31856::twosComplimentToSigned8(int8_t temp)
+{
     temp=(~(temp)+1);               //Take two's complement of the negative number
     temp|=(int8_t)(0x80UL);                 //And convert it into 7-bit val with msb as sign bit
     return temp;
 }
-int16_t MAX31856::twosComplimentToSigned16(int16_t temp){
+
+//******************************************************************************
+int16_t MAX31856::twosComplimentToSigned16(int16_t temp)
+{
     temp=(~(temp)+1);               //Take two's complement of the negative number
     temp|=(int16_t)(0x8000UL);               //And convert it into 15-bit val with msb as sign bit
     return temp;
 }
 
-//void MAX31856::printSetting(string register_bits, string register_info) {
-//    printf("Register containing\t\t",register_bits, "\t\twas programmed with the parameter\t\t",register_info);
-//}
+
+//******************************************************************************
+MAX31856::~MAX31856(void) 
+{
+  //empty block
+}
+
 
 //bool MAX31856::checkForFaults() {
 //