frederic blanc / OneWireFB

Dependents:   MAX31850_HelloWorld

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MAX31850.cpp Source File

MAX31850.cpp

Go to the documentation of this file.
00001 /**
00002 * @file MAX31850.cpp
00003 * @brief library of MAX31850  1-Wire Thermocouple-to-Digital Converters,Cold-Junction Compensated  (http://www.maximintegrated.com/datasheet/index.mvp/id/7953)
00004 * @author Frederic BLANC
00005 * @date 2014_01_15
00006 */
00007 #include "mbed.h"
00008 #include "onewire.h"
00009 #include "MAX31850.h"
00010 #include "crc8.h"
00011 #include "Horner_ITS90.h"
00012 int MAX31850_Read_Scratch(uint8_t id[],uint8_t sp[],uint8_t n)
00013 {
00014     if (id[0] == MAX31850_ID) {
00015         if (ow_reset(n))                 //reset OW
00016             return OW_SHORT_CIRCUIT;
00017         sp[0]=MAX31850_READ;   // command
00018         ow_command(n,sp[0], &id[0]);
00019         for ( int i=0 ; i< MAX31850_SP_SIZE; i++ ) { //read 8xdata + CRC16
00020             sp[i]=ow_byte_rd(n);
00021         }
00022         if(sp[8]!=crc8(sp,8))
00023             return OW_ERROR_CRC;
00024         return OW_OK;
00025     }
00026     return OW_ERROR_BAD_ID;
00027 }
00028 
00029 int MAX31850_Temp_TRUE(uint8_t *sp,double *temp)
00030 {
00031 
00032 
00033     float temp_cj,temp_tc;
00034     double vtc;
00035     int err; 
00036     
00037     err = MAX31850_Temp_TC(sp,&temp_tc);
00038     if(err) {
00039         return err;
00040     }
00041     MAX31850_Temp_CJ(sp,&temp_cj);
00042 
00043     vtc = MAX31850_K * temp_tc ;
00044     *temp=0;
00045     if(temp_tc < 0.0) {
00046         for(int i=0;i<10;++i)
00047         {
00048         
00049         *temp+=ITS90_TCK_subzero[i]*pow(vtc,i);
00050         }
00051     } else if(temp_tc < 500.0) {
00052         for(int i=0;i<10;++i)
00053         {
00054         
00055         *temp+=ITS90_TCK_sub500[i]*pow(vtc,i);
00056         }
00057     } else {
00058         for(int i=0;i<10;++i)
00059         {
00060         
00061         *temp+=ITS90_TCK_sub1372[i]*pow(vtc,i);
00062         }
00063     }
00064     return OW_OK;
00065 }
00066 
00067 
00068 int MAX31850_Temp_TC(uint8_t *sp,float *temp)
00069 {
00070 
00071     uint8_t *ptr_meas;
00072     uint16_t meas;
00073 
00074     if(sp[0]& 1)
00075         return MAX31850_ERR_FAULT;
00076     if(sp[2]& (1<<1))
00077         return MAX31850_ERR_OPEN_CIRCUIT;
00078     if(sp[2]& (1<<2))
00079         return MAX31850_ERR_SHORT2GND;
00080     if(sp[2]& (1<<3))
00081         return MAX31850_ERR_SHORT2VDD;
00082     ptr_meas=(uint8_t*) &meas;
00083     *ptr_meas = sp[0]  ;  // LSB
00084     *++ptr_meas = sp[1]; // MSB
00085     if ( meas & 0x8000 )  {
00086 
00087         meas ^= 0xffff;  // convert to positive => (twos complement)++
00088         meas++;
00089         *temp= -1.0 * MAX31850_ADC_TEMP_TC_UNIT * (float)(meas>>2);
00090     } else {
00091         *temp=  MAX31850_ADC_TEMP_TC_UNIT * (float)(meas>>2);
00092     }
00093 
00094     return OW_OK;
00095 }
00096 
00097 int MAX31850_Temp_CJ(uint8_t *sp,float *temp)
00098 {
00099     uint8_t *ptr_meas;
00100     uint16_t meas;
00101 
00102     ptr_meas=(uint8_t*) &meas;
00103     *ptr_meas = sp[2]  ;  // LSB
00104     *++ptr_meas = sp[3]; // MSB
00105     if ( meas & 0x8000 )  {
00106 
00107         meas ^= 0xffff;  // convert to positive => (twos complement)++
00108         meas++;
00109         *temp= -1.0 * MAX31850_ADC_TEMP_CJ_UNIT * (float)(meas>>4);
00110     } else {
00111         *temp=  MAX31850_ADC_TEMP_CJ_UNIT * (float)(meas>>4);
00112     }
00113     return OW_OK;
00114 }
00115 
00116 int MAX31850_Start_meas(uint8_t id[],uint8_t n=0)
00117 {
00118     if (id[0] == MAX31850_ID) {
00119         if (ow_reset(n))                 //reset OW
00120             return OW_SHORT_CIRCUIT;
00121         ow_command(n,MAX31850_CONVERT_T, &id[0]);
00122         return OW_OK;
00123     }
00124     return OW_ERROR_BAD_ID;
00125 }