epsilonrt / ad7124

AD7124 ADC Library
Other
21 stars 16 forks source link

problems communicating AD7124 with CN0391 guideline Calibration TC and RTC #10

Open BastiaanMDC opened 4 years ago

BastiaanMDC commented 4 years ago

Hello WizardTim,

I hope you can help me out: what I would like to do is a cold junction measurement with the RTD for the CN0391. I have seen the CN0391 but the library is so big and chaotic that I cant communicate with my TFT featherwing over SPI. So i started with the AD7124 example

allthough the code works and spits something out like 569.805,319.375 I am getting wrong values, this is based on the false input pins and configuration.

what I would like to do is to measure all RTDS use this as a cold junction then measure all the Thermocouple values substract those values (which I dont know yet how to do). so bare with me.

I wrote the procedure how to set the config channels and filter etc. //measuring RTC A typical procedure for reading the RTD is as follows: 1.Reset the ADC. 2.Select the power mode. 3.Set the CHANNEL_0 register analog input to AIN1/AIN2. Assign Setup 0 to this channel. Configure Setup 0 to have a gain of 16 and select the reference source REFIN2(±). Select the filter type and set the output data rate. 4.Program the excitation currents to 500 μA and output thecurrents on the AIN0 and AIN3 pins.
5.Wait until RDY goes low. Read the conversion value. 6.Repeat Step 4.

//measuring Thermocouple 1.Reset the ADC. 2.Select the power mode. 3.Set the CHANNEL_0 register analog input to AIN0/AIN1. Assign Setup 0 to this channel. Configure Setup 0 to have a gain of 128 and select the reference source to REFIN1(±). Select the filter type and set the output data rate. 4.Set the CHANNEL_1 register analog input to AIN2/AIN3. Assign Setup 0 to this channel (both channels use the same setup). 5.Set the CHANNEL_2 register analog input to AIN4/AIN5. Assign Setup 1 to this channel. Configure Setup 1 to have a gain of 1 and select the reference source REFIN2(±). Select the filter type and set the output data rate. 6.Program the excitation current and output the current on the AIN4 pin.
7.Enable both CHANNEL_0 and CHANNEL_1. Enable the DATA_STATUS bit to identify the channel from which the conversion originated. The ADC automatically sequences through these channels. 8.Wait until RDY goes low. Read the conversion value. 9.Repeat Step 8 until the temperature is to be read (every 10 conversions of the pressure sensor readings, for example). 10.Disable CHANNEL_0 and CHANNEL_1. Enable CHANNEL_2. 11.Wait until RDY goes low. Read the conversion.12.Repeat Step 6 to Step 10.

so i require to make 4 functions which read RTD, TC1,TC1 and TC2. and give them the channel parameters, voltage references how do I enable both channels 0 and 1 at the same time?. how do I program the excitation current? what I dont understand is the until ready ?

`/* CN-0381 Thermometer (AD7124) Completely Integrated 4-Wire RTD Measurement System Using a AD7124 See http://www.analog.com/media/en/reference-design-documentation/reference-designs/CN0381.pdf Prints out the RTD temperature and resistor measured at pins AIN2(+)/AIN3(-) for schematic see Figure 1., p.2 of CN-0381, e.g. : CN-0381 RTD Thermometer 23.143,108.910 23.143,108.910 For more on AD7124, see http://www.analog.com/media/en/technical-documentation/data-sheets/AD7124-4.pdf The circuit:

using namespace Ad7124;

/ constants ================================================================ / const int ledPin = LED_BUILTIN; const int ssPin = 10;

include

include

include "Adafruit_GFX.h"

include "HX8357_t3n.h"

include // library to draw w/o flicker

include

include

include

include

/* INPUTS and OUTPUTS

const int DIRmagnetLeft=0; const int DIRmagnetRight=1; const int DIR_TEC_Cool=2; const int DIR_TEC_HOT=3;

const int PWM_DRV_TEC1 = 4; const int PWM_DRV_TEC2 =5; const int PWM_PUMP =6; const int PWM_EMAGNET =7; const int PWM_FAN =36; const int FLOW_Sensor =25; const int FLT_Peltier1 =29; const int FLT_Peltier2 =30; const int FLT_PMP =31; const int FLT_EMAG =32;

const int CSTEC1=A9; const int CSTEC2=A8; const int CSPUMP=A7; const int CSEMAG=A6;

const int BTNPIN=19;

//SPI defines

define TFT_DC 9

define TFT_CS 10

define TFT_RST -1

define AD7124_CS 8

define AD7124_SPEED 5000000 // 2mhz?

//TFT size

define TS_MINX 3800

define TS_MAXX 100

define TS_MINY 100

define TS_MAXY 3750

//TFT defined colors

define C_BLACK 0x0000

define C_BLUE 0x001F

define C_RED 0xF800

define C_GREEN 0x07E0

define C_CYAN 0x07FF

define C_MAGENTA 0xF81F

define C_YELLOW 0xFFE0

define C_WHITE 0xFFFF

//constructors for TFT without flickering HX8357_t3n tft = HX8357_t3n(TFT_CS, TFT_DC, TFT_RST); FlickerFreePrint Data1(&tft, C_WHITE, C_BLACK); FlickerFreePrint Data2(&tft, C_WHITE, C_BLACK);

unsigned long endmicros; //speed check variables unsigned long currentmicros;

//menu Check values to check consistency of the menu loop. byte menuCount = 1; byte IntCount= 1; byte dir = 0; bool runState = false; bool runState2 = false; unsigned int count = 0; // how many times has it changed to low unsigned long countAt = 0; // when count changed unsigned int countPrinted = 0; unsigned int icount = 0; // how many times has it changed to low unsigned long icountAt = 0; // when count changed unsigned int icountPrinted = 0;

long oldPosition = -100; long newPosition; long Position;

long oldPWMPosition=-100; long PWMPosition=1;

//---------- values for the ad7124 const double Gain = 16; const double Rf = 5.11E3; const long Zero = 1L << 23; const long FullScale = 1L << 24;

/ public variables ========================================================= / Ad7124Chip adc;

/ internal public functions ================================================ /

// ----------------------------------------------------------------------------- void setup() { int ret; long value;

pinMode (ledPin, OUTPUT); digitalWrite (ledPin, 1); // clear the LED

//Initialize serial and wait for port to open: Serial.begin (38400); while (!Serial) { ; // wait for serial port to connect. Needed for native USB port only }

// prints title with ending line break Serial.println ("RTD Thermometer");

// Initializes the AD7124 device, the pin /CS is pin 10 (/SS) adc.begin (ssPin);

//RTD Check // Setting the configuration 0: // - use of the REFIN1(+)/REFIN1(-) reference // - gain of 16 for a bipolar measurement // - digital filter Sync4 FS=384 adc.setConfig (0, RefIn2, Pga16, true); adc.setConfigFilter (0, Sinc4Filter, 384); adc.setChannel (0, 0, AIN1Input, AIN2Input); // Configuring ADC in Full Power Mode (Fastest) ret = adc.setAdcControl (StandbyMode, FullPower, true);

//TC Channel 1 // Setting the configuration 0: // - use of the REFIN1(+)/REFIN1(-) reference // - gain of 128 for a bipolar measurement // - digital filter Sync4 FS=384 adc.setConfig (0, RefIn1, Pga128, true); adc.setConfigFilter (0, Sinc4Filter, 384); adc.setChannel (0, 0, AIN2Input, AIN3Input); //TC Channel 2 adc.setConfig (1, RefIn2, Pga1, true); adc.setConfigFilter (1, Sinc4Filter, 384); adc.setChannel (1, 0, AIN4Input, AIN5Input); // Configuring ADC in Full Power Mode (Fastest) ret = adc.setAdcControl (StandbyMode, FullPower, true);

if (ret < 0) {

Serial.println ("Unable to setting up ADC");

} }

// ----------------------------------------------------------------------------- void loop() { channelTC0(); channelTC1(); channelTC2(); } / ========================================================================== / int channelRTC0() { Serial.println("Channel TC1"); long RTCvalue;

// detect an open wire circuit (no RTD connected) or a short-circuit adc.setConfig (0, RefInternal, Pga1, true, Burnout4uA); RTCvalue = adc.read (0);

if (RTCvalue >= (FullScale - 10)) {

// A near full-scale reading can mean that the front-end sensor is open circuit.
adc.setConfig (0, RefInternal, Pga1, true, BurnoutOff); //config 0, internal reference, PGA,bipolar(true),burnoutcurrent

Serial.println ("OPENED");

} else {

// Setting the configuration 0 for measuring
adc.setConfig (0, RefIn1, Pga16, true);
// Program the excitation currents to 750 μA and output the currents on the AIN0
adc.setCurrentSource (0, IoutCh0, Current500uA);
adc.setBiasPins(0);
// Measuring on Channel 0 in Single Conversion Mode
digitalWrite (ledPin, 0);
RTCvalue = adc.read (0);
digitalWrite (ledPin, 1);

// Program the excitation currents to Off
adc.setCurrentSource (0, IoutCh0, CurrentOff);

if (RTCvalue >= 0) {
  double RTCr, RTCt = 0;

  // See Equation (1), p.4 of CN-0381
  RTCr = ((RTCvalue - Zero) * Rf) / (Zero * Gain);

  // See Equation (2), p.4 of CN-0381
  RTCt = (RTCr - 100.0) / 0.385;

  // Print results
  Serial.print("RTC values ");
  Serial.print (RTCt, 3);
  Serial.print (",");
  Serial.println (RTCr, 3);
}
else {

  Serial.println ("FAIL");
}

} }

int channelTC0() { Serial.println("Channel TC1"); long value;

// detect an open wire circuit (no RTD connected) or a short-circuit adc.setConfig (0, RefInternal, Pga1, true, Burnout4uA); value = adc.read (0);

if (value >= (FullScale - 10)) {

// A near full-scale reading can mean that the front-end sensor is open circuit.
adc.setConfig (0, RefInternal, Pga1, true, BurnoutOff); //config 0, internal reference, PGA,bipolar(true),burnoutcurrent

Serial.println ("OPENED");

} else {

// Setting the configuration 0 for measuring
adc.setConfig (0, RefIn1, Pga16, true);
// Program the excitation currents to 750 μA and output the currents on the AIN0
adc.setCurrentSource (0, IoutCh0, Current500uA);
adc.setBiasPins(0);
// Measuring on Channel 0 in Single Conversion Mode
digitalWrite (ledPin, 0);
value = adc.read (0);
digitalWrite (ledPin, 1);

// Program the excitation currents to Off
adc.setCurrentSource (0, IoutCh0, CurrentOff);

if (value >= 0) {
  double r, t = 0;

  // See Equation (1), p.4 of CN-0381
  r = ((value - Zero) * Rf) / (Zero * Gain);

  // See Equation (2), p.4 of CN-0381
  t = (r - 100.0) / 0.385;

  // Print results
  Serial.print (t, 3);
  Serial.print (",");
  Serial.println (r, 3);
}
else {

  Serial.println ("FAIL");
}

} }

int channelTC1() { Serial.println("Channel TC1"); long value1;

// detect an open wire circuit (no RTD connected) or a short-circuit adc.setConfig (0, RefInternal, Pga1, true, Burnout4uA); value1 = adc.read (0);

if (value1 >= (FullScale - 10)) {

// A near full-scale reading can mean that the front-end sensor is open circuit.
adc.setConfig (0, RefInternal, Pga1, true, BurnoutOff); //config 0, internal reference, PGA,bipolar(true),burnoutcurrent

Serial.println ("OPENED");

} else {

// Setting the configuration 0 for measuring
adc.setConfig (0, RefIn1, Pga16, true);
// Program the excitation currents to 750 μA and output the currents on the AIN0
adc.setCurrentSource (0, IoutCh0, Current500uA);
adc.setBiasPins(0);
// Measuring on Channel 0 in Single Conversion Mode
digitalWrite (ledPin, 0);
value = adc.read (0);
digitalWrite (ledPin, 1);

// Program the excitation currents to Off
adc.setCurrentSource (0, IoutCh0, CurrentOff);

if (value1 >= 0) {
  double r1, t1 = 0;

  // See Equation (1), p.4 of CN-0381
  r1 = ((value1 - Zero) * Rf) / (Zero * Gain);

  // See Equation (2), p.4 of CN-0381
  t1 = (r1 - 100.0) / 0.385;

  // Print results
   Serial.print ("TC1 ");
  Serial.print (t1, 3);
  Serial.print (",");
  Serial.println (r1, 3);
}
else {

  Serial.println ("FAIL");
}

} }

int channelTC2() { long value2; Serial.print("Channel TC2"); // detect an open wire circuit (no RTD connected) or a short-circuit adc.setConfig (0, RefInternal, Pga1, true, Burnout4uA); value2 = adc.read (0);

if (value2 >= (FullScale - 10)) {

// A near full-scale reading can mean that the front-end sensor is open circuit.
adc.setConfig (0, RefInternal, Pga1, true, BurnoutOff); //config 0, internal reference, PGA,bipolar(true),burnoutcurrent

Serial.println ("OPENED");

} else {

// Setting the configuration 0 for measuring
adc.setConfig (0, RefIn1, Pga16, true);
// Program the excitation currents to 750 μA and output the currents on the AIN0
adc.setCurrentSource (0, IoutCh0, Current500uA);
adc.setBiasPins(0);
// Measuring on Channel 0 in Single Conversion Mode
digitalWrite (ledPin, 0);
value2 = adc.read (0);
digitalWrite (ledPin, 1);

// Program the excitation currents to Off
adc.setCurrentSource (0, IoutCh0, CurrentOff);

if (value >= 0) {
  double r2, t2 = 0;

  // See Equation (1), p.4 of CN-0381
  r2 = ((value2 - Zero) * Rf) / (Zero * Gain);

  // See Equation (2), p.4 of CN-0381
  t2 = (r2 - 100.0) / 0.385;

  // Print results
  Serial.print ("TC2 ");
  Serial.print (t2, 3);
  Serial.print (",");
  Serial.println (r2, 3);
}
else {

  Serial.println ("FAIL");
}

} }

int channelRTC() { long value;

// detect an open wire circuit (no RTD connected) or a short-circuit adc.setConfig (0, RefInternal, Pga128, true, Burnout4uA); value = adc.read (0);

if (value >= (FullScale - 10)) {

// A near full-scale reading can mean that the front-end sensor is open circuit.
adc.setConfig (0, RefInternal, Pga1, true, BurnoutOff); //config 0, internal reference, PGA,bipolar(true),burnoutcurrent

Serial.println ("OPENED");

} else {

// Setting the configuration 0 for measuring
adc.setConfig (0, RefIn1, Pga16, true);
// Program the excitation currents to 750 μA and output the currents on the AIN0
adc.setCurrentSource (0, IoutCh0, Current500uA);
adc.setBiasPins(0);
// Measuring on Channel 0 in Single Conversion Mode
digitalWrite (ledPin, 0);
value = adc.read (0);
digitalWrite (ledPin, 1);

// Program the excitation currents to Off
adc.setCurrentSource (0, IoutCh0, CurrentOff);

if (value >= 0) {
  double r, t = 0;

  // See Equation (1), p.4 of CN-0381
  r = ((value - Zero) * Rf) / (Zero * Gain);

  // See Equation (2), p.4 of CN-0381
  t = (r - 100.0) / 0.385;

  // Print results
  Serial.print (t, 3);
  Serial.print (",");
  Serial.println (r, 3);
}
else {

  Serial.println ("FAIL");
}

} }

`