A collection of Analog Devices drivers for the mbed platform

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers CN0391.cpp Source File

CN0391.cpp

00001 #include "CN0391.h"
00002 #include "Thermocouple.h"
00003 #include "AD7124.h"
00004 
00005 //#define DEBUG_MODE
00006 
00007 extern Serial pc;
00008 
00009 
00010 #define ms_delay (1)
00011 #define R2 (1600.0)
00012 #define RTD_SLOPE (15/39.0)
00013 #define RTD_CONSTANT (1039.0)
00014 #define RTD_CONVERSION(R1) RTD_SLOPE * (R1-RTD_CONSTANT)
00015 #define CAL_CHANNEL (8)
00016 Thermocouple_Channel::Thermocouple_Channel()
00017 {
00018     this->t = NULL;
00019 }
00020 
00021 Thermocouple* Thermocouple_Channel::get_thermocouple_type()
00022 {
00023     return t;
00024 }
00025 void Thermocouple_Channel::set_thermocouple_type(Thermocouple* new_t)
00026 {
00027     t = new_t;
00028 }
00029 
00030 void Thermocouple_Channel::setup_channel(Thermocouple* new_t, uint16_t thermocouple_channel, uint16_t rtd_channel, uint16_t calibration_channel)
00031 {
00032 
00033     this->t = new_t;
00034     this->thermocouple_channel = thermocouple_channel;
00035     this->rtd_channel = rtd_channel;
00036     this->calibration_channel = calibration_channel;
00037     this->calibration_current = 0.0005;
00038 }
00039 
00040 CN0391::CN0391(PinName cs) : ad7124(cs)
00041 {
00042 
00043 }
00044 
00045 void CN0391::set_thermocouple_type(channel_t ch, Thermocouple* new_t)
00046 {
00047     tc[ch].setup_channel(new_t, ch * 2, (ch * 2) + 1, CAL_CHANNEL);
00048 }
00049 
00050 
00051 float CN0391::read_channel(channel_t ch)
00052 {
00053     int32_t data;
00054 
00055     enable_current_source(tc[ch].rtd_channel);
00056     enable_channel(tc[ch].rtd_channel);
00057     start_single_conversion();
00058 
00059     if (ad7124.WaitForConvReady (10000) == -3) {
00060         pc.printf("TIMEOUT");
00061         return 0;
00062     }
00063 
00064     ad7124.ReadData (&data);
00065     disable_channel(tc[ch].rtd_channel);
00066     float volt = data_to_voltage(data >> 8);
00067     float R1 = (volt / tc[ch].calibration_current) - R2;
00068     float temp = RTD_CONVERSION(R1);
00069     float mv_cold_junction = tc[ch].t->convert_inv(temp);
00070     //disable_current_source(tc[ch].rtd_channel);
00071 #ifdef DEBUG_MODE
00072     pc.printf("Resistance of RTD on channel %d is: %f\r\n", ch , R1);
00073     pc.printf("Temperature of RTD on channel %d is: %f\r\n", ch, temp);
00074     pc.printf("mV equivalent(poly method) of thermocouple is %f \r\n", mv_cold_junction);
00075 #endif
00076     // read thermocouple
00077     enable_channel(tc[ch].thermocouple_channel);
00078     start_single_conversion();
00079 
00080     if (ad7124.WaitForConvReady (10000) == -3) {
00081         pc.printf("TIMEOUT");
00082         return 0;
00083     }
00084 
00085     ad7124.ReadData (&data);
00086     disable_channel(tc[ch].thermocouple_channel);
00087 
00088 
00089     volt = data_to_voltage(data >> 8);
00090     float mv = volt * 1000 + mv_cold_junction;
00091     temp = tc[ch].t->convert(mv);
00092 #ifdef DEBUG_MODE
00093     pc.printf("mV of thermocouple on channel %d is: %f\r\n", ch, volt * 1000.0);
00094     pc.printf("mV(compensated) of thermocouple on channel %d is: %f\r\n", ch, mv);
00095     pc.printf("Temperature on channel %d is: %f\r\n", ch, temp);
00096 #endif
00097     return temp;
00098 }
00099 float CN0391::calibrate(channel_t ch)
00100 {
00101     int32_t data;
00102     enable_current_source(tc[ch].rtd_channel);
00103     enable_channel(tc[ch].calibration_channel); // calibration channel
00104     start_single_conversion();
00105     if (ad7124.WaitForConvReady (10000) == -3) {
00106         pc.printf("TIMEOUT");
00107         return 0;
00108     }
00109 
00110     ad7124.ReadData (&data);
00111     disable_channel(tc[ch].calibration_channel);
00112     //disable_current_source(tc[ch].rtd_channel);
00113     float volt = data_to_voltage(data >> 8);
00114     tc[ch].calibration_current = volt / R2;
00115 
00116 #ifdef DEBUG_MODE
00117     pc.printf("Calibration current for channel %d is: %f \r\n", ch, tc[ch].calibration_current);
00118 #endif
00119     return tc[ch].calibration_current;
00120 
00121 }
00122 
00123 
00124 float CN0391::data_to_voltage(uint32_t data)
00125 {
00126     data = data & 0xFFFFFF;
00127     return ((data / static_cast<float>(0xFFFFFF / 2)) - 1) * (2.5 / 1);
00128 }
00129 
00130 void CN0391::enable_channel(int channel)
00131 {
00132     AD7124::ad7124_registers regNr = static_cast<AD7124::ad7124_registers> (AD7124::AD7124_Channel_0 + channel); //Select ADC_Control register
00133     uint32_t setValue = ad7124.ReadDeviceRegister(regNr);
00134     setValue |= (uint32_t) AD7124_CH_MAP_REG_CH_ENABLE;  //Enable channel
00135     setValue &= 0xFFFF;
00136     ad7124.WriteDeviceRegister(regNr, setValue);    // Write data to ADC
00137     wait_ms(ms_delay);
00138 }
00139 
00140 void CN0391::disable_channel(int channel)
00141 {
00142     AD7124::ad7124_registers regNr = static_cast<AD7124::ad7124_registers> (AD7124::AD7124_Channel_0 + channel); //Select ADC_Control register
00143     uint32_t setValue = ad7124.ReadDeviceRegister(regNr);
00144     setValue &= (~(uint32_t) AD7124_CH_MAP_REG_CH_ENABLE);  //Enable channel
00145     setValue &= 0xFFFF;
00146     ad7124.WriteDeviceRegister(regNr, setValue);    // Write data to ADC
00147     wait_ms(ms_delay);
00148 }
00149 
00150 void CN0391::enable_current_source(int current_source_channel)
00151 {
00152     AD7124::ad7124_registers regNr = AD7124::AD7124_IOCon1; //Select ADC_Control register
00153     uint32_t setValue = ad7124.ReadDeviceRegister(regNr);
00154     setValue &= ~(AD7124_IO_CTRL1_REG_IOUT_CH0(0xF));
00155     setValue |= AD7124_IO_CTRL1_REG_IOUT_CH0(current_source_channel);// set IOUT0 current to 500uA
00156     setValue &= 0xFFFFFF;
00157     ad7124.WriteDeviceRegister(regNr, setValue);    // Write data to ADC
00158     wait_ms(ms_delay);
00159 }
00160 
00161 void CN0391::start_single_conversion()
00162 {
00163     AD7124::ad7124_registers regNr = AD7124::AD7124_ADC_Control; //Select ADC_Control register
00164     uint32_t setValue = ad7124.ReadDeviceRegister(regNr);
00165     setValue &= 0xFFC3;
00166     setValue |= 0x04;               //single conversion;
00167     setValue |= 0x1600;
00168     setValue &= 0xFFFF;
00169     ad7124.WriteDeviceRegister(regNr, setValue);    // Write data to ADC*/
00170     wait_ms(ms_delay);
00171 }
00172 
00173 void CN0391::reset()
00174 {
00175     ad7124.frequency(500000);
00176     ad7124.Reset();
00177     pc.printf("Reseted AD7124\r\n");
00178 }
00179 
00180 void CN0391::setup()
00181 {
00182     ad7124.frequency(500000);
00183     ad7124.Setup ();
00184 }
00185 
00186 void CN0391::init()
00187 {
00188     uint32_t setValue;
00189     enum AD7124::ad7124_registers regNr;
00190     setup();
00191     wait_ms(ms_delay);
00192 
00193     /* Set Config_0 0x19*/
00194     regNr = AD7124::AD7124_Config_0;               //Select Config_0 register
00195     setValue = ad7124.ReadDeviceRegister(regNr);
00196     setValue |= AD7124_CFG_REG_BIPOLAR;     //Select bipolar operation
00197     setValue |= AD7124_CFG_REG_BURNOUT(0);  //Burnout current source off
00198     setValue |= AD7124_CFG_REG_REF_BUFP;
00199     setValue |= AD7124_CFG_REG_REF_BUFM;
00200     setValue |= AD7124_CFG_REG_AIN_BUFP;    //Buffer AIN5
00201     setValue |= AD7124_CFG_REG_AINN_BUFM;   //Buffer AIN4
00202     setValue |= AD7124_CFG_REG_REF_SEL(2); //Select REFIN1(+)/REFIN1(-)  internal reference
00203     setValue |= AD7124_CFG_REG_PGA(0);
00204     setValue &= 0xFFFF;
00205     ad7124.WriteDeviceRegister(regNr, setValue);   // Write data to ADC
00206 
00207     /* Set Channel_0 register 0x09*/
00208     regNr = AD7124::AD7124_Channel_0;
00209     setValue = ad7124.ReadDeviceRegister(regNr);
00210     //setValue |= (uint32_t) AD7124_CH_MAP_REG_CH_ENABLE;  //Enable channel0
00211     setValue &= (~(uint32_t)AD7124_CH_MAP_REG_CH_ENABLE);
00212     setValue |= AD7124_CH_MAP_REG_SETUP(0);             // Select setup0
00213     setValue |= AD7124_CH_MAP_REG_AINP(0);         // Set AIN4 as positive input
00214     setValue |= AD7124_CH_MAP_REG_AINM(15);         // Set AIN5 as negative input
00215     setValue &= 0xFFFF;
00216     ad7124.WriteDeviceRegister(regNr, setValue);   // Write data to ADC
00217 
00218     regNr = AD7124::AD7124_Channel_1;
00219     setValue = ad7124.ReadDeviceRegister(regNr);
00220     //setValue |= (uint32_t) AD7124_CH_MAP_REG_CH_ENABLE;  //Enable channel0
00221     setValue &= (~(uint32_t)AD7124_CH_MAP_REG_CH_ENABLE);
00222     setValue |= AD7124_CH_MAP_REG_SETUP(0);             // Select setup0
00223     setValue |= AD7124_CH_MAP_REG_AINP(1);         // Set AIN4 as positive input
00224     setValue |= AD7124_CH_MAP_REG_AINM(15);         // Set AIN5 as negative input
00225     setValue &= 0xFFFF;
00226     ad7124.WriteDeviceRegister(regNr, setValue);   // Write data to ADC
00227 
00228     regNr = AD7124::AD7124_Channel_2;
00229     setValue = ad7124.ReadDeviceRegister(regNr);
00230     //setValue |= (uint32_t) AD7124_CH_MAP_REG_CH_ENABLE;  //Enable channel0
00231     setValue &= (~(uint32_t)AD7124_CH_MAP_REG_CH_ENABLE);
00232     setValue |= AD7124_CH_MAP_REG_SETUP(0);             // Select setup0
00233     setValue |= AD7124_CH_MAP_REG_AINP(2);         // Set AIN4 as positive input
00234     setValue |= AD7124_CH_MAP_REG_AINM(15);         // Set AIN5 as negative input
00235     setValue &= 0xFFFF;
00236     ad7124.WriteDeviceRegister(regNr, setValue);   // Write data to ADC
00237 
00238     regNr = AD7124::AD7124_Channel_3;
00239     setValue = ad7124.ReadDeviceRegister(regNr);
00240     //setValue |= (uint32_t) AD7124_CH_MAP_REG_CH_ENABLE;  //Enable channel0
00241     setValue &= (~(uint32_t)AD7124_CH_MAP_REG_CH_ENABLE);
00242     setValue |= AD7124_CH_MAP_REG_SETUP(0);             // Select setup0
00243     setValue |= AD7124_CH_MAP_REG_AINP(3);         // Set AIN4 as positive input
00244     setValue |= AD7124_CH_MAP_REG_AINM(15);         // Set AIN5 as negative input
00245     setValue &= 0xFFFF;
00246     ad7124.WriteDeviceRegister(regNr, setValue);   // Write data to ADC
00247 
00248     regNr = AD7124::AD7124_Channel_4;
00249     setValue = ad7124.ReadDeviceRegister(regNr);
00250     //setValue |= (uint32_t) AD7124_CH_MAP_REG_CH_ENABLE;  //Enable channel0
00251     setValue &= (~(uint32_t)AD7124_CH_MAP_REG_CH_ENABLE);
00252     setValue |= AD7124_CH_MAP_REG_SETUP(0);             // Select setup0
00253     setValue |= AD7124_CH_MAP_REG_AINP(4);         // Set AIN4 as positive input
00254     setValue |= AD7124_CH_MAP_REG_AINM(15);         // Set AIN5 as negative input
00255     setValue &= 0xFFFF;
00256     ad7124.WriteDeviceRegister(regNr, setValue);   // Write data to ADC
00257 
00258 
00259     regNr = AD7124::AD7124_Channel_5;
00260     setValue = ad7124.ReadDeviceRegister(regNr);
00261     //setValue |= (uint32_t) AD7124_CH_MAP_REG_CH_ENABLE;  //Enable channel0
00262     setValue &= (~(uint32_t)AD7124_CH_MAP_REG_CH_ENABLE);
00263     setValue |= AD7124_CH_MAP_REG_SETUP(0);             // Select setup0
00264     setValue |= AD7124_CH_MAP_REG_AINP(5);         // Set AIN4 as positive input
00265     setValue |= AD7124_CH_MAP_REG_AINM(15);         // Set AIN5 as negative input
00266     setValue &= 0xFFFF;
00267     ad7124.WriteDeviceRegister(regNr, setValue);   // Write data to ADC
00268 
00269 
00270     regNr = AD7124::AD7124_Channel_6;
00271     setValue = ad7124.ReadDeviceRegister(regNr);
00272     //setValue |= (uint32_t) AD7124_CH_MAP_REG_CH_ENABLE;  //Enable channel0
00273     setValue &= (~(uint32_t)AD7124_CH_MAP_REG_CH_ENABLE);
00274     setValue |= AD7124_CH_MAP_REG_SETUP(0);             // Select setup0
00275     setValue |= AD7124_CH_MAP_REG_AINP(6);         // Set AIN4 as positive input
00276     setValue |= AD7124_CH_MAP_REG_AINM(15);         // Set AIN5 as negative input
00277     setValue &= 0xFFFF;
00278     ad7124.WriteDeviceRegister(regNr, setValue);   // Write data to ADC
00279 
00280     regNr = AD7124::AD7124_Channel_7;
00281     setValue = ad7124.ReadDeviceRegister(regNr);
00282     //setValue |= (uint32_t) AD7124_CH_MAP_REG_CH_ENABLE;  //Enable channel0
00283     setValue &= (~(uint32_t)AD7124_CH_MAP_REG_CH_ENABLE);
00284     setValue |= AD7124_CH_MAP_REG_SETUP(0);             // Select setup0
00285     setValue |= AD7124_CH_MAP_REG_AINP(7);         // Set AIN4 as positive input
00286     setValue |= AD7124_CH_MAP_REG_AINM(15);         // Set AIN5 as negative input
00287     setValue &= 0xFFFF;
00288     ad7124.WriteDeviceRegister(regNr, setValue);   // Write data to ADC
00289 
00290     regNr = AD7124::AD7124_Channel_8;
00291     setValue = ad7124.ReadDeviceRegister(regNr);
00292     //setValue |= (uint32_t) AD7124_CH_MAP_REG_CH_ENABLE;  //Enable channel0
00293     setValue &= (~(uint32_t)AD7124_CH_MAP_REG_CH_ENABLE);
00294     setValue |= AD7124_CH_MAP_REG_SETUP(0);             // Select setup0
00295     setValue |= AD7124_CH_MAP_REG_AINP(14);         // Set AIN4 as positive input
00296     setValue |= AD7124_CH_MAP_REG_AINM(15);         // Set AIN5 as negative input
00297     setValue &= 0xFFFF;
00298     ad7124.WriteDeviceRegister(regNr, setValue);   // Write data to ADC
00299 
00300 
00301     /* Set Config_0 0x19*/
00302 
00303 #ifdef  CALIBRATION
00304     // start calibration
00305     regNr = AD7124::AD7124_Offset_0;
00306     setValue = 0x800000;
00307     ad7124.WriteDeviceRegister(regNr, setValue);// Write data to ADC
00308 
00309 // internal fullscale before zero scale
00310     pc.printf("\r\n Gain before cali :%x", ad7124.ReadDeviceRegister(AD7124::AD7124_Gain_0));
00311     regNr = AD7124::AD7124_ADC_Control;//Select ADC_Control register
00312     setValue = AD7124_ADC_CTRL_REG_MODE(6);
00313     setValue |= AD7124_ADC_CTRL_REG_REF_EN;
00314     setValue &= 0xFFFF;
00315     ad7124.WriteDeviceRegister(regNr, setValue);// Write data to ADC
00316     //dut.WaitForConvReady(10000);
00317     wait_ms(2000);
00318 
00319     pc.printf("\r\n Gain:%x", ad7124.ReadDeviceRegister(AD7124::AD7124_Gain_0));
00320 
00321     pc.printf("\r\n Offset before cali:%x", ad7124.ReadDeviceRegister(AD7124::AD7124_Offset_0));
00322 // internal zeroscale
00323     regNr = AD7124::AD7124_ADC_Control;//Select ADC_Control register
00324     setValue = AD7124_ADC_CTRL_REG_MODE(5);
00325     setValue |= AD7124_ADC_CTRL_REG_REF_EN;
00326     setValue &= 0xFFFF;
00327     ad7124.WriteDeviceRegister(regNr, setValue);// Write data to ADC
00328     wait_ms(2000);
00329     pc.printf("\r\n Offset:%x\r\n", ad7124.ReadDeviceRegister(AD7124::AD7124_Offset_0));
00330 
00331     // end of calibration
00332 
00333 #endif
00334 
00335     /* Set IO_Control_1 0x03 */
00336     regNr = AD7124::AD7124_IOCon1;               //Select IO_Control_1 register
00337     setValue = ad7124.ReadDeviceRegister(regNr);
00338     setValue |= AD7124_IO_CTRL1_REG_IOUT0(0x4);// set IOUT0 current to 500uA
00339     setValue |= AD7124_IO_CTRL1_REG_IOUT_CH0(0x1);
00340     setValue &= 0xFFFFFF;
00341     ad7124.WriteDeviceRegister(regNr, setValue);// Write data to ADC
00342 
00343     /* Set ADC_Control 0x01 */
00344     regNr = AD7124::AD7124_ADC_Control;            //Select ADC_Control register
00345     setValue = ad7124.ReadDeviceRegister(regNr);
00346     setValue |= AD7124_ADC_CTRL_REG_DATA_STATUS; // set data status bit in order to check on which channel the conversion is
00347     setValue |= AD7124_ADC_CTRL_REG_REF_EN;
00348     setValue &= 0xFFC3;
00349     setValue |= AD7124_ADC_CTRL_REG_MODE(1);
00350     setValue &= 0xFFFF;
00351     ad7124.WriteDeviceRegister(regNr, setValue);    // Write data to ADC
00352     wait_ms(ms_delay);
00353 }
00354 
00355 
00356 
00357 
00358