'Automated Test Software for 7Bxx-xx Voltage,
'Current, TC and RTD input models.
'** MAIN PROGRAM (part 2 of 4) **
'******************************************************************
'AUTHOR:        Tim Knowlton
'DATE:          08/26/96
'******************************************************************
'       REVISION RECORD
'******************************************************************
'
'       See REVISION RECORD in 7BMAIN4.BAS for revision notes.
'
DECLARE FUNCTION LOOPVOLT$ (TTYPE%)
DECLARE FUNCTION OPENLOOPDETECT$ ()
DECLARE SUB THRESET ()
DECLARE SUB INITPS (ADDR%, OVERI!, OVERV!)
DECLARE SUB CALDAC (TE$, DACFLAG%)
DECLARE SUB INIT488 (DEVADDR%)
DECLARE SUB IO488 (DDATA$, ADDR%, SEL%)
DECLARE SUB DMM2010 (FUNC$, DDATA$, VALUE!, RATE!, DELAY!, HOLD!, HCOUNT%, FILTER%, FCOUNT%, COMP%, BW!)
DECLARE SUB LOADMUX (CH%, CHON%)
DECLARE SUB SETTH (DOUT%, CB%)
DECLARE SUB PAUSE (TIME!)
DECLARE SUB OHMSET (OHM!, BIT!)
DECLARE SUB SETTEST (TEST%)
DECLARE SUB SETDAC (VOLTAGE!, DACACCV!, CH%, VSENATTEN!)

'THIS MAIN MODULE ROUTINES...
DECLARE SUB SETAC (ONOFF%)
DECLARE SUB COMPTEST (STATUS$(), CAL%, ERRORPCT!(), STAT$, TEST%)  'Complete set of tests
DECLARE SUB CONTINUE ()                         'Waits for a key press
DECLARE SUB FOOTER (STATUS$())                  'Sends footer to printer if all tests passed
'DECLARE SUB FUNCTEST (STATUS$(), CAL%, TEST%)   'Routines for functional test
DECLARE SUB FUNCTEST (STATUS$(), CAL%, TEST%, TSPEC$())   'Routines for functional test
DECLARE SUB GENSET ()                           'Displays message to set signal generator
DECLARE SUB GETADD ()                           'Gets parallel port/Mux/Testhead address
DECLARE SUB GETSN (SN$)                         'Gets serial number
DECLARE SUB GETSPECS ()                         'Gets module type and specifications
DECLARE SUB HEADERA (SN$)                       'Prints test sheet header
DECLARE SUB HEADERB (TESTTITLE$)                'Prints test screen header
DECLARE SUB LOGIT (STATUS$(), SN$, ERRORPCT!(), TEST%) 'Logs test results to disk
DECLARE SUB MEASRES (OHM!, RESNUM%, EMF!)      'Measure test head resistors
DECLARE SUB NOTES ()                            'Notes on ATE operation
DECLARE SUB REPORT (STATUS$(), TSPEC$())        'Prints test data on screen
DECLARE SUB RTDTHCAL ()
DECLARE SUB SETOUTLOAD (LOAD!)                  'Set output load
DECLARE SUB THRESET ()
DECLARE SUB TSPECS (TSPEC$())                   'Creates a string array of test specifications
DECLARE SUB VTHCAL (TE$)

DECLARE FUNCTION BESTFIT! (SLOPE!, OFFSETS!, TSI!(), NUMPOINTS%, ERRO!()) ' Calculates bestfit line and max error
DECLARE FUNCTION CJCACC$ ()                     'Test CJC gain accuracy
DECLARE FUNCTION COMPLIANCE$ ()                 'Test output current source compliance
DECLARE FUNCTION FAILS% (STATUS$())             'Tests for failed tests
DECLARE FUNCTION FREQRESP$ ()                   'Performs Frequency response Test
DECLARE FUNCTION GAINCAL$ (CAL%, CONTRES!, PFSMEAS!, INTERACT!, OSERR!, PCB$) 'Performs Gain Calibration
DECLARE FUNCTION GENATTENR! ()                   'Calculate fn gen attenuator
DECLARE FUNCTION LEADREFF$ (CONTRES!)            'Measure lead resistance effects
DECLARE FUNCTION LINTEST$ (ACC$, CONTRES!, ERRORPCT!())     'Performs Accuracy and Linearity Tests
DECLARE FUNCTION MENU1% ()                       'Gets the Test Group selection
DECLARE FUNCTION MENU2% ()                       'Gets the individual test #
DECLARE FUNCTION MENU3% ()                       'Gets the module family
DECLARE FUNCTION OFFSETCAL$ (CAL%, CONTRES!, PCB$) 'Performs Offset Calibration
DECLARE FUNCTION OPENLOOPDETECT$ ()              'Test 39-1257 open loop detect function
DECLARE FUNCTION OPENTC$ ()                      'Measure open thermocouple response
DECLARE FUNCTION OUTNOISERMS$ ()                 'Performs RMS 300KHZ Output Noise Test
DECLARE FUNCTION PARALLEL (X, Y)
DECLARE FUNCTION POWERIO$ (ADDR%, CMD$)          'Library fn. Kepco DPS power supply I/O
DECLARE FUNCTION REPEAT$ ()                      'Ask if you would like to repeat test
DECLARE FUNCTION RTDOHMS! (TEMP!, SENTYPE$)      'Library fn. Convert RTD in temp to ohms
DECLARE FUNCTION RTDTEMP! (OHMS!, SENTYPE$)      'Library fn. Convert RTD ohms to temp
DECLARE FUNCTION SENSORNUM% ()                   'Assign a number to each sensor type
DECLARE FUNCTION SETATTEN% (VOLTAGE!)            'Library fn. Sets test head R/2R ladder
DECLARE FUNCTION SETCURRENTIN! (MODIN!, DACACCV!)
DECLARE FUNCTION SETILIM$ (MAXI!)
DECLARE FUNCTION SETSUPPLYV$ (ADDR%, SUPPLYV!, SUPPLYON$) 'Sets programmable supply voltage
DECLARE FUNCTION SUPPLYI$ (EXCL%)                'Measure module supply current
DECLARE FUNCTION SUPPLYVSENS$ ()
DECLARE FUNCTION IEXCHI$ ()
DECLARE FUNCTION IEXCLO$ ()
DECLARE FUNCTION IFINDEXC$ (ILO$, IHI$)
DECLARE FUNCTION SENSORNUM% ()                   'Assign a number to each sensor type
DECLARE FUNCTION SENSOROUT! (SENSORIN!)          'Calculates module input
DECLARE FUNCTION TCTEMP (TCV!, SENTYPE$)        'Library fn. Convert TC volts to deg. C
DECLARE FUNCTION TCVOLTS (TEMP!, TCTYPE$)       'Library fn. Convert TC in temp to V
DECLARE FUNCTION THERM! (OHMS!)                   'Calculate thermistor temp from ohms
DECLARE FUNCTION UPSN$ (SN$)                     'Increments dash# of serial#
DECLARE FUNCTION VCJC! ()                         'Test CJC gain accuracy
DECLARE FUNCTION VOUT! (SENOUT!, PEDOS$)                'Calculates module output for a given input
DECLARE FUNCTION SEEBECK (I$)
'Database Record definition for the specifications
		TYPE DBASE
				MODNAME         AS STRING * 17  '7B47X-XX-XXXX   01/18/06
				SENTYPE         AS STRING * 7   'mV, V, mA, xTC, RTD
				MINVS           AS SINGLE       'Lowest supply voltage (V)
				NOMVS           AS SINGLE       'Nominal supply voltage (V)
				MAXVS           AS SINGLE       'Highest supply voltage (V)
				VLIM            AS SINGLE       'Voltage output limit (V)
				ILIM            AS SINGLE       'Current output limit (mA)
				PE              AS SINGLE       'Effective available power out (mW)
				ISMAXNEXCL      AS SINGLE       'Supply current, no out/ load (mA)
				MININ           AS SINGLE       'Minus F.S. input
				MAXIN           AS SINGLE       'Plus F.S. input
				MINOUT          AS SINGLE       'Minus F.S. output
				MAXOUT          AS SINGLE       'Plus F.S. output
				IEXC            AS SINGLE       'RTD stimulus current (uA)
				EXCIMIN         AS SINGLE       'Lowest RTD stimulus current (uA)
				EXCIMAX         AS SINGLE       'Highest RTD stimulus current (uA)
				LEADRERR        AS SINGLE       'RTD lead resistance effects (Deg/Ohm)
				RCONV           AS SINGLE       '7B32,35 internal I-V resistor (ohms)
				OSCALPT         AS SINGLE       'LIN TC offset cal point (mV)
				GNCALPT         AS SINGLE       'LIN TC gain cal point (mV)
				CALTOL          AS SINGLE       'Calibration tolerance (% SPAN)
				ISMAXFEXCL      AS SINGLE       'Supply current, full out/ load (mA)
				VEXC            AS SINGLE       '35 nominal loop voltage (V)
				VEXCLO          AS SINGLE       '35 loop voltage with 20mA load (V)
				VEXCHI          AS SINGLE       '35 loop voltage with 4mA load (V)
				LOOPIMAX        AS SINGLE       '35 max loop current (mA)
				LINEAR          AS SINGLE       'Module Linearity/conformity (% SPAN)
				ACCURACY        AS SINGLE       'Module accuracy (% SPAN)
				PSS             AS SINGLE       'Power supply sensitivity (%/% Delta Vs)
				TESTFREQ        AS INTEGER      'Bandwidth test frequency (Hz)
				ATTEN           AS INTEGER      'Filter Attenuation at test frequency (dB)
				ATTENTOL        AS INTEGER      'Filter attenuation tolerance (dB)
				STEPRESP        AS SINGLE       'Step response in 200ms (%)
				STEPTOL         AS SINGLE       'Step response tolerance (%)
				OUTNOISERMS     AS SINGLE       'Output noise @ 100kHz, in (Vrms)
				OUTNOISEVPK     AS SINGLE       'Output noise @ 5Mhz, in (Vpk)
				INPUTRES        AS SINGLE       'Module input resistance (kohms)
				VOPENTC         AS SINGLE       'TC model open input voltage response (V)
				CJCACC          AS SINGLE       'TC CJC gain accuracy (Deg C)
				IBIAS           AS SINGLE       'Input bias current (nA)
		END TYPE

COMMON SHARED /SAMPLE/ SPECS AS DBASE
COMMON SHARED PON%, SN$, GENAMPL!, TA!, TA1!, VTCTA!, NUMPOINTS%, TE$, OINSPEC!, COND$
COMMON SHARED SYSTYPE%, FAILFLAG%, LOGDAT%, FINAL%, DMMSERIAL$, PSPORT$
'COMMON SHARED DATASHEET%, VERSION$, VAC!, IOUTLOAD2.MEAS!, COMPTOL!
COMMON SHARED DATASHEET%, VAC!, IOUTLOAD2.MEAS!, COMPTOL!
COMMON SHARED IINSEN.MEAS!, IOUTSEN.MEAS!, TIME2!, ELAP2!
COMMON SHARED CB0VAL%, CB1VAL%, CB2VAL%, CB3VAL%
COMMON SHARED MAINMAX!, MAINMIN!, VERNMAX!, VERNMIN!
COMMON SHARED BADDRS%, LPTADDR%, MUXADDR%, THADDR%, VSADDR%, VINADDR%, DMMADDR%, PSPORT%

CONST CON% = 1, COFF% = 0
CONST CB0% = 0, CB1% = 1, CB2% = 2, CB3% = 3

										'assign constants to Kepco DPS power
										'supply commands:
CONST VOLT$ = "STV="                    'Set terminal voltage
CONST SUPPLYON$ = "SOP=ON"              'Set output to ON
CONST SUPPLYOFF$ = "SOP=OFF"            'Set output to OFF
CONST ISTAT$ = "RCS"                    'Read Current protection status
CONST SETIMAX$ = "SOC="                 'Set overcurrent limit...4mA resol.
CONST IREAD$ = "RTC"                    'Read output current
CONST VREAD$ = "RTV"                    'Read output voltage
CONST PSID$ = "ID"                      'Power supply ID

'********************************************************************
'Assign constants to Keithley 2010 functions:
CONST VODC$ = "volt:dc"
CONST VOAC$ = "volt:ac"
CONST OHM2$ = "res"
CONST OHM4$ = "fres"
CONST FREQ$ = "freq"
CONST PER$ = "per"
CONST TEMP$ = "temp"
CONST DIODE$ = "diod"
CONST CONT$ = "cont"
'********************************************************************

'assign specifications which are constant for all modules
CONST RFNGN! = 6040                     'Function generator attenuator series R, 6.04Kohm
CONST IINSEN! = 192                     'Input current source sense resistor, 250ohm
CONST IOUTSEN! = 192                    'Output current sense resistor.
CONST IOUTLOAD2! = 702                  'Output current source max load.
CONST VOUTLOAD1! = 4750                 'Voltage output max load.

DIM STATUS(31) As String, TSPEC(31) As String

KEY(10) ON                              'Activates F10 key
ON KEY(10) GOSUB FINISH                 'Traps for F10 key Exits if pressed
 
FINISH: END

FUNCTION BESTFIT! (SLOPE!, OFFSETS!, TSI!(), NUMP%, ERRO!())
'Calculates Max error of data from bestfit line
			  
		MTERR! = 0
		MIN! = TSI!(1)
		MAX! = TSI!(NUMP%)

		FOR INC% = 1 TO NUMP%          'Increments thru test points

				BVEC! = TSI!(INC%) * SLOPE! + OFFSETS!       'Calculates point on best fit line
				AVEC! = ERRO!(INC%)                     'Gets corresponding data point
			   
				TERR! = AVEC! - BVEC!                   'Calculates difference
			  
				IF ABS(TERR!) > ABS(MTERR!) THEN        'Checks if bigger than last Max
						MTERR! = TERR!                  'Assigns max error
				END IF
		NEXT

		BESTFIT! = MTERR!                               'Passes Max error back

END FUNCTION

FUNCTION CJCACC$
	
	'Measure gain of the CJC input (Seebeck coefficient)
	'Two methods of measuring CJC gain have been derived.  Reference
	'TC theory notes on 12/20/96 for details.
	'Method 2 is based on the ideal Seebeck coeff. and the measured module
	'f.s. output voltage vs. f.s input temperature transfer function.  This
	'method is used below because it required fewer measurements.
	'It has been verified that both methods yield the same result for SCM5Bs.

	 TCTYPE$ = UCASE$(LEFT$(SPECS.SENTYPE, 1))
	 MINTIN! = SPECS.MININ               'C
	 MAXTIN! = SPECS.MAXIN               'C
	 MINOUT! = SPECS.MINOUT             'V
	 MAXOUT! = SPECS.MAXOUT             'V
	 TAMB1! = TA!                       '25C +/-0.1C, Measured upon test head startup
	 TAMB2! = TA1!                      '35C +/-0.1C, Measured upon test head startup
	 CJCMAXACCERR! = SPECS.CJCACC       'C
	
	 ORANGE! = MAXOUT! - MINOUT!
	 INRANGE! = MAXTIN! - MINTIN!            'Input range (deg. C)
	 SNM% = SENSORNUM%
	 DACACC! = .0001                         '.01% DAC setting accuracy
											 'Accurate setting eliminates the
											 'need to re-measure
	 CLS
	 TESTTITLE$ = "Cold Junction Compensation Gain Measurement"
	 CALL HEADERB(TESTTITLE$)
	 LOCATE 10, 10
	
	 IF SNM% <> 3 THEN     'Check for TC in
		  PRINT "This test is not performed on model "; SPECS.MODNAME
		  CALL CONTINUE
		  EXIT FUNCTION
	 ELSE
		IF TCTYPE$ = "B" THEN
			PRINT "This test is not performed on model "; SPECS.MODNAME
			CALL CONTINUE
			EXIT FUNCTION
		ELSE
			PRINT "Testing, please wait..."
		END IF
	 END IF

	MININ1! = SENSOROUT!(MINTIN)            'Calc. minin
	CALL SETDAC(MININ1!, DACACC! * INRANGE!, 1, 0) 'Set -f.s. input
												'Measure Vout with approx 0.5100V
												'(25C) input to CJC port
	LOCATE 10, 10
	PRINT "Measuring Vout @ Tin = -f.s and Ta = 25C..."
	CALL LOADMUX(2, CON%)
	CALL DMM2010(VODC$, DDATA$, 2000!, 1, 3, 99, 99, 99, 99, 99, 99)
	VOUT1! = VAL(DDATA$): DDATA$ = ""
   
	CB0VAL% = CB0VAL% AND &HDF      '1 1 0 1  1 1 1 1
									'set resistor corresponding to 35C
	CALL SETTH(CB0VAL%, CB0%)

	PRINT TAB(10); "Measuring Vout @ Tin = -f.s and Ta = 35C..."
	CALL LOADMUX(2, CON%)
	CALL DMM2010(VODC$, DDATA$, 2000!, 1, 1, 99, 99, 99, 99, 99, 99)
	VOUT3! = VAL(DDATA$): DDATA$ = ""
	
	CB0VAL% = CB0VAL% OR &H20       '0 0 1 0  0 0 0 0
									'set resistor corresponding to 25C
	CALL SETTH(CB0VAL%, CB0%)
   
									'Determine gain at -f.s. before any
									'breakpoints turn on.  Choose 2% span
	MININ2! = SENSOROUT!(MINTIN + .02 * INRANGE!)  'Calc. minin
	CALL SETDAC(MININ2!, DACACC! * INRANGE!, 1, 0) 'Set -f.s. input
												'Measure Vout with approx 0.5100V
												'(25C) input to CJC port
	PRINT TAB(10); "Measuring Vout @ Tin = 2% f.s and Ta = 25C..."
	CALL LOADMUX(2, CON%)
	CALL DMM2010(VODC$, DDATA$, 2000!, 1, 1, 99, 99, 99, 99, 99, 99)
	VOUT2! = VAL(DDATA$): DDATA$ = ""
	
	MEASGAINMFS! = (VOUT2! - VOUT1!) / (MININ2! - MININ1!)

	GCJCMEAS! = (VOUT3! - VOUT1!) / MEASGAINMFS! / (TAMB2! - TAMB1!)
   
	SB! = SEEBECK(TCTYPE$)
	MEASCJCACCERR! = (GCJCMEAS! - SB!) * (TAMB2! - TAMB1!) / SB!

'LOCATE 10
'PRINT "SEEBECK = "; SB!, "VOUT2CALC = "; VOUT2CALC!
'PRINT "VOUT1 = "; VOUT1!, "VOUT2 = "; VOUT2!
'PRINT "TA1 = "; TAMB1!, "TA2 = "; TAMB2!, "TIN2 = "; TIN2SIM!
'PRINT "XFERFN = "; MEASXFERFN!, "TCV1 = "; TCV1!, "TCV2 = "; TCV2!
'CALL CONTINUE

	IF ABS(MEASCJCACCERR!) <= CJCMAXACCERR! THEN
		CJC$ = "PASS"
	ELSE
		CJC$ = "FAIL"
		SOUND 1000, .5
	END IF

	LOCATE 15, 10
	PRINT "Status: ";
	IF CJC$ = "FAIL" THEN
		COLOR 12, 1, 1
	ELSE
		COLOR 10, 1, 1
	END IF
	PRINT CJC$
	PRINT SPC(12);
	COLOR 7, 1, 1
	PRINT
   
	PRINT TAB(10); "Measured CJC gain is";
	PRINT TAB(43); USING "###.## uV/C"; GCJCMEAS! * 1000000!
	PRINT TAB(10); "Resulting measured CJC error is";
	PRINT TAB(45); USING "#.## C"; MEASCJCACCERR!
	PRINT TAB(10); "Maximum CJC error is";
	PRINT TAB(45); USING "#.## C"; CJCMAXACCERR!

	CJCACC$ = CJC$ + STR$(GCJCMEAS! * 1000000!) + "1"

	IF FINAL% <> 1 THEN CALL PAUSE(1)

END FUNCTION

FUNCTION COMPLIANCE$

	'Test output current source compliance.
	'Max load resistance for 7B39-xx standard product is 800 ohms
	'Max load resistance for 7B39-1256 custom is 700 ohms
	'Use 700 ohms to test.

	MAXOUT! = SPECS.MAXOUT
	MINOUT! = SPECS.MINOUT
	SNM% = SENSORNUM%
	PLUSFSIN! = SENSOROUT!(SPECS.MAXIN)
	INRANGE! = PLUSFSIN! - SENSOROUT!(SPECS.MININ)
	DACACC! = .01                           '1% DAC setting accuracy
	INITTOL! = .1                           '10% initial tolerance
	COMPTOL! = SPECS.ACCURACY / 100 + 5 / IOUTLOAD2! 'Compliance tolerance
											'Assume 5 ohms lead resistance
						'(similar to all other product lines)
											'error in 700 ohm measurement.
	ORANGE! = MAXOUT! - MINOUT!
  
	CLS
	TESTTITLE$ = "Compliance Test"
	CALL HEADERB(TESTTITLE$)
	LOCATE 10, 10
	IF SNM% <> 8 AND SNM% <> 10 THEN            'Check if 39
		PRINT "This test is not performed on model "; SPECS.MODNAME
		EXIT FUNCTION
	ELSE
		PRINT "Testing, please wait..."
	END IF
 
	CALL SETOUTLOAD(IOUTSEN!)                   'Connect output load
	IF SNM% = 8 THEN                            'Voltage input
		CALL SETDAC(PLUSFSIN!, DACACC! * INRANGE!, 1, 0)'Sets input voltage
	ELSE    'SNM% = 10
		MAXINMEAS! = SETCURRENTIN!(PLUSFSIN!, DACACC! * INRANGE! / 1000! * IINSEN!)
	END IF
		 

	CALL LOADMUX(2, CON%)                       'Measure Iin
	CALL DMM2010(VODC$, DDATA$, 5, 99, 99, 99, 99, 99, 99, 99, 99)
	IOUT1! = VAL(DDATA$) / IOUTSEN.MEAS! * 1000!       'mA
	'PRINT IOUT1!: CALL CONTINUE
	DDATA$ = ""
	CALL LOADMUX(2, COFF%)
   
	CALL SETOUTLOAD(IOUTLOAD2!)                 'Connect output load
	CALL LOADMUX(2, CON%)                       'Measure Iin

	CALL DMM2010(VODC$, DDATA$, 99, 99, 99, 99, 99, 99, 99, 99, 99)
	IOUT2! = VAL(DDATA$) / IOUTLOAD2.MEAS! * 1000!       'mA
	'PRINT IOUT2!: CALL CONTINUE
	DDATA$ = ""
	CALL LOADMUX(2, COFF%)

	CERROR! = (IOUT2! - IOUT1!) / ORANGE! * 100         '%

	LOCATE 10, 5
	PRINT "Status: ";

	IF ABS(IOUT1! - MAXOUT!) < MAXOUT! * INITTOL! AND ABS(CERROR! / 100!) < COMPTOL! THEN
		COMPL$ = "PASS"
		COLOR 10, 1, 1
	ELSE
		COMPL$ = "FAIL"
		SOUND 1000, .5
		COLOR 12, 1, 1
	END IF
 
	PRINT TAB(18); COMPL$;
	PRINT SPC(17);
	COLOR 7, 1, 1
	PRINT SPC(31);
  
	PRINT TAB(5); USING "Output current with ### ohm load is"; IOUTSEN!;
	PRINT TAB(52); USING "##.# mA"; IOUT1!
	PRINT TAB(5); "Required output current is";
	PRINT TAB(52); USING "##.# mA to ##.# mA"; MAXOUT! * (1 - INITTOL!); MAXOUT! * (1 + INITTOL!)
	PRINT
	PRINT TAB(5); USING "Change in output current with ### ohm load is"; IOUTLOAD2!;
	PRINT TAB(52); USING "+###.## %"; CERROR!
	PRINT TAB(5); "Required output current is";
	PRINT TAB(52); USING "  +#.## % to +#.##%"; -COMPTOL! * 100; COMPTOL! * 100

	COMPLIANCE$ = COMPL$ + STR$(CERROR!) + "2"
 
	COLOR 7, 1, 1
 
	IF FINAL% <> 1 THEN
		CALL PAUSE(1)
	ELSE
		CALL PAUSE(.5)
	END IF

END FUNCTION

FUNCTION FREQRESP$

	'CALL THRESET
   
	MININ! = SENSOROUT!(SPECS.MININ) 'Determine min in
	MAXIN! = SENSOROUT!(SPECS.MAXIN) 'Determine max in
	IEXC! = SPECS.IEXC / 1000000
	INPUTRES! = SPECS.INPUTRES
	INRANGE! = MAXIN! - MININ!
	SPEC! = SPECS.ATTEN
	SPECTOL! = SPECS.ATTENTOL
	SNM% = SENSORNUM%

	IF SNM% = 8 THEN
		ORANGE! = ((SPECS.MAXOUT - SPECS.MINOUT) / 1000) * IOUTSEN.MEAS!
	ELSE
		ORANGE! = SPECS.MAXOUT - SPECS.MINOUT
	END IF

	CLS
	TESTTITLE$ = "Frequency Response Test"
	CALL HEADERB(TESTTITLE$)
	LOCATE 10, 10
	PRINT "Testing, please wait..."
   
	IF SNM% = 2 OR SNM% = 9 THEN
		CALL SETAC(1)
		THATTEN! = 1
		VGAIN! = ORANGE! / (INRANGE! / 1000! * IINSEN.MEAS!)
	ELSEIF SNM% = 8 THEN
		CALL SETAC(1)
		CALL SETOUTLOAD(IOUTSEN!)               'Connect output load
		THATTEN! = SETATTEN%(MAXIN!)
		VGAIN! = ORANGE! / INRANGE!             'Gain in V/V
	ELSEIF SNM% = 10 THEN
		CALL SETAC(1)
		CALL SETOUTLOAD(IOUTSEN!)               'Connect output load
		THATTEN! = 1
		VGAIN! = 1
	ELSEIF SNM% = 4 OR SNM% = 5 THEN
		'Simulated RTD input is the bottom leg in a voltage divider
		'from the function generator.  The series resistor is 6.04Kohms.
		B$ = SETSUPPLYV$(VSADDR%, 0, SUPPLYOFF$)
		R! = GENATTENR!     'Find attenuator shunt R
		CALL OHMSET(R!, 0)
		OHM! = R!
		CALL MEASRES(OHM!, 3, EMF!)
		RSIM! = OHM!          'Measure simulated resistor
		CALL SETTEST(6)
		THATTEN! = (RFNGN! + RSIM!) / RSIM!
		VGAIN! = ORANGE! / (INRANGE! * IEXC!) 'Gain in V/V
		B$ = SETSUPPLYV$(VSADDR%, SPECS.NOMVS, SUPPLYON$) 'Set supply voltage and apply
	ELSE
		CALL SETAC(1)
		IF SNM% = 7 THEN
			CALL SETOUTLOAD(VOUTLOAD1!)
		END IF
		THATTEN! = SETATTEN%(MAXIN!) 'Set test head attenuator
		VGAIN! = ORANGE! / INRANGE!             'Gain in V/V
	END IF
   
	CALL LOADMUX(2, CON%)                       'Measure Vout
	   
	CALL DMM2010(VOAC$, DDATA$, 2000!, 1, .5, 99, 99, 99, 99, 99, 99)
	VOUTMEAS! = VAL(DDATA$) * SQR(2)
	DDATA$ = ""
	ATTEN! = -20 * (LOG(ABS(VOUTMEAS! / (VAC! / THATTEN! * VGAIN!))) / LOG(10))
	CALL LOADMUX(2, COFF%)                      'Turns off mux
   
	IF ABS(ATTEN! - SPEC!) <= SPECTOL! THEN
		ATTEN$ = "PASS"
	ELSE
		ATTEN$ = "FAIL"
		SOUND 1000, .5
	END IF
			   
	LOCATE 10, 10
	PRINT ; USING "&"; "Status: ";
	IF ATTEN$ = "FAIL" THEN
		COLOR 12, 1, 1
	ELSE
		COLOR 10, 1, 1
	END IF
			   
	PRINT TAB(18); ATTEN$;
	PRINT SPC(12);
	COLOR 7, 1, 1
	PRINT SPC(20);
	PRINT TAB(10); USING "Attenuation at ##### Hz is"; SPECS.TESTFREQ;
	PRINT TAB(50); USING "###.# dB"; ATTEN!
	PRINT TAB(10); "Required attenuation is";
	PRINT TAB(50); USING "###.# +/- ##.# dB"; SPEC!; SPECTOL!
   
	FREQRESP$ = ATTEN$ + STR$(ABS(ATTEN!)) + "0"
	COLOR 7, 1, 1
	   
	IF FINAL% <> 1 THEN CALL PAUSE(1)
	
	IF SNM% = 4 OR SNM% = 5 THEN
		B$ = SETSUPPLYV$(VSADDR%, 0, SUPPLYOFF$)
	END IF

	CALL SETAC(0)

END FUNCTION

FUNCTION GENATTENR!

		'This routine is used for RTD or pot. input modules only.
		'It determines the simulated RTD input to the module which
		'serves as a voltage divider for the function generator.
		'Series resistor from the function generator is 6.8Kohms.

	   
		IEXC! = SPECS.IEXC / 1000000!                   'Convert to A
		MININ! = SENSOROUT!(SPECS.MININ)                'Determine min in
		MAXIN! = SENSOROUT!(SPECS.MAXIN)                'Determine max in
		RMID! = (MAXIN! - MININ!) / 2 + MININ!          'Determine mid in
		VOS! = 5
		VIN! = RMID! * IEXC!
		RTIM! = (-1 * RFNGN! * VIN!) / (VIN - IEXC! * RFNGN! - VOS!)
		GENATTENR! = RTIM!

END FUNCTION

SUB GENSET
	  
	MININ! = SENSOROUT!(SPECS.MININ)        'Determine min in
	MAXIN! = SENSOROUT!(SPECS.MAXIN)        'Determine max in
	FREQUENCY = SPECS.TESTFREQ
	SNM% = SENSORNUM%

	IF SNM% = 4 OR SNM% = 5 THEN            'Check if resistance in
		IEXC! = SPECS.IEXC / 1000000!           'Convert to A
		R! = GENATTENR!                         'Find attenuator shunt R
		VIN! = IEXC! * ((MAXIN! - MININ!) / 2 + MININ!) / 2
		GENAMPL! = VIN! / (R! / (RFNGN! + R!))
		GENOS! = GENAMPL!
		IF UCASE$(SPECS.MODNAME) = "7B36-1246" THEN GENOS! = 2 * GENAMPL!
	ELSEIF SNM% = 2 OR SNM% = 9 THEN
		GENAMPL! = 2
		GENOS! = 2
		'IF LEFT$(SPECS.MODNAME, 8) = "7B32-01D" THEN GENOS! = .5
	ELSEIF SNM% = 8 OR SNM% = 10 THEN
		IF LEFT$(SPECS.MODNAME, 9) = "7B39-1257" OR LEFT$(SPECS.MODNAME, 7) = "7B39-04" THEN
			GENOS! = 5
			GENAMPL! = 2.5
		ELSEIF LEFT$(SPECS.MODNAME, 7) = "7B39-01" THEN
			GENOS! = 2
			GENAMPL! = 2
		'ELSEIF LEFT$(SPECS.MODNAME, 7) = "7B39-02" OR LEFT$(SPECS.MODNAME, 7) = "7B39-03" THEN
		ELSEIF LEFT$(SPECS.MODNAME, 7) = "7B39-02" OR LEFT$(SPECS.MODNAME, 7) = "7B39-03" OR LEFT$(SPECS.MODNAME, 9) = "7B39-1808" THEN
			GENOS! = 5
			GENAMPL! = 5
		END IF
		CALL SETOUTLOAD(IOUTSEN!)               'Connect output load
	ELSE
		IF SNM% = 7 THEN
			CALL SETOUTLOAD(VOUTLOAD1!)
		END IF
		INRANGE! = SPECS.MAXIN! - SPECS.MININ!
		GENAMPL! = 5
		GATTEN! = INRANGE! / GENAMPL!           'Attenuator gain
		GENOS! = (SPECS.MININ! + INRANGE! / 2!) / GATTEN!
		'TSO 12/1/09 added the next line of code due to Agilent Function Generator Limitations
		IF (GENAMPL! + ABS(GENOS!)) * 2 > 20 THEN GENAMPL! = (20 - ABS(GENOS!) * 2) / 2
	END IF

	IF SNM% <> 5 OR SNM% <> 4 THEN CALL SETTH(&HFF, 3) 'Prevents shorting out the +15V supply.
	  
	IF SNM% = 5 OR SNM% = 4 THEN                'Check if resistance in
		CALL OHMSET(R!, 0)
		CALL SETTEST(6)                         'Connect function generator
	ELSEIF SNM% = 2 OR SNM% = 9 THEN
		IF SNM% = 2 THEN                            ' mA in
			IF TE$ = "TE1040OLDCS" THEN
				'CB0VAL% = &HF9                          'CMD to set relays 21 & 22
				'CB3VAL% = &HF5                          'CMD to set relays 21 & 22
				CB0VAL% = &HFD                  '1 1 1 1  1 1 0 1 CMD to set relays 21 & 22
				CB3VAL% = &HF5                  '1 1 1 1  0 1 0 1 CMD to set relays 21 & 22
			ELSE
				CB0VAL% = &HE9                  '1 1 1 0  1 0 0 1 CMD to set relays 21 & 22
				CB3VAL% = &HDF                  '1 1 0 1  1 1 1 1 CMD to set relays 21 & 22
			END IF
			CALL SETTH(CB0VAL%, CB0%)               'do it
			CALL SETTH(CB3VAL%, CB3%)               'do it
		ELSE
			IF TE$ = "TE1040OLDCS" THEN
				'CB3VAL% = &HFC                          'CMD to set relay 22 only
				'CB0VAL% = &HE9                          'CMD to set relay 22 only
				CB0VAL% = &HED                  '1 1 1 0  1 1 0 1 CMD to set relay 22 only
				CB3VAL% = &HFD                  '1 1 1 1  1 1 0 1 CMD to set relay 22 only
			ELSE
				CB0VAL% = &HED                  '1 1 1 0  1 1 0 1
				CB3VAL% = &HC7                  '1 1 0 0  0 1 1 1
			END IF
			CALL SETTH(CB3VAL%, CB3%)
			CALL SETTH(CB0VAL%, CB0%)
		END IF
	ELSEIF SNM% = 10 THEN                           'mA input module
		CB3VAL% = (CB3VAL% OR &H10) AND &HDF        '0 0 0 1  0 0 0 0
													'D.U.T. input = current source
													'1 1 0 1  1 1 1 1
		CALL SETTH(CB3VAL%, CB3%)                   'Input current sourced from +15V
		CB0VAL% = (CB0VAL% AND &HE9) OR &H8         '1 1 1 0  1 0 0 1
		CALL SETTH(CB0VAL%, CB0%)                   'ground inputs, short series R
													'remove short on inputs

	ELSE
		THATTEN! = SETATTEN%(MAXIN!) 'Set test head attenuator
		CB0VAL% = (CB0VAL% AND &HE9) OR &H8         '1 1 1 0  1 0 0 1
		CALL SETTH(CB0VAL%, CB0%)                   'ground inputs, short series R
													'remove short on inputs
	END IF
	   
	IF SNM% = 5 OR SNM% = 4 THEN
		CH% = 3
	ELSEIF SNM% = 7 OR SNM% = 8 THEN
		CH% = 1
	ELSEIF SNM% = 10 THEN
		A$ = SETILIM$(SPECS.ISMAXNEXCL)                 'Set current limit
		B$ = SETSUPPLYV$(VSADDR%, SPECS.NOMVS, SUPPLYON$)        'Set supply voltage and apply
		CH% = 6
	ELSE
		CH% = 8
	END IF

	CALL LOADMUX(CH%, CON%)
	   
	CLS
	FAILFLAG% = 1
	   
	CALL DMM2010(VODC$, "99", 10!, .1, .5, 99, 99, 99, 99, 99, 99)
	   
	LOCATE 5, 10
	PRINT "Function Generator Settings"
	PRINT TAB(10); "------------------------------------------------------------"
	PRINT TAB(10); USING "Required Frequency is: ######.# Hz "; FREQUENCY
			 
	DO
		CALL DMM2010(FREQ$, DDATA$, 2000!, .01, 99, 99, 99, 99, 99, 99, 30)
		FREMEAS! = VAL(DDATA$): DDATA$ = ""
		IF ABS(FREMEAS! - FREQUENCY) > .05 * FREQUENCY THEN
			COLOR 12, 0, 0
			FREQFLAG% = 0
		ELSE
			COLOR 10, 0, 0
			FREQFLAG% = 1
		END IF
		LOCATE 7, 50
		PRINT ; USING "#####.# &"; FREMEAS!; " Hz"
		A$ = INKEY$
	LOOP UNTIL FREQFLAG% = 1 OR A$ <> ""
			   
	COLOR 7, 1, 1
	PRINT TAB(10); USING "Required DC Offset is:     +#.# V  "; GENOS!

	DO
		IF FREQUENCY <= 50 THEN
			PLC! = 10
		ELSE
			PLC! = 99
		END IF
		CALL DMM2010(VODC$, DDATA$, 10!, PLC!, 99, 99, 99, 99, 99, 99, 99)
		VDCMEAS! = VAL(DDATA$): DDATA$ = ""
		IF SNM% = 10 THEN
			'Read current input, but display voltage to match that of the func. gen
			VDCDISP! = (VDCMEAS! + (VDCMEAS! / IINSEN.MEAS! * SPECS.INPUTRES * 1000))*1.1 	'Display reads 10% low
		ELSE
			VDCDISP! = VDCMEAS!
		END IF
		IF GENOS! <> 0 THEN
			IF ABS(VDCDISP! - GENOS!) > .1 * GENOS THEN
				COLOR 12, 0, 0
				VDCFLAG% = 0
			ELSE
				COLOR 10, 0, 0
				VDCFLAG% = 1
			END IF
		ELSE
			IF ABS(VDCDISP!) > .1 THEN
				COLOR 12, 0, 0
				VDCFLAG% = 0
			ELSE
				COLOR 10, 0, 0
				VDCFLAG% = 1
			END IF
		END IF
		LOCATE 8, 50
		PRINT ; USING "+##.# &"; VDCDISP!; "Vdc"
			  
		IF VDCFLAG% = 1 AND VACFLAG% = 1 AND FREQFLAG% = 1 THEN
			FAILFLAG% = 1
		ELSE
			FAILFLAG% = 0
		END IF

		A$ = INKEY$
			  
	LOOP UNTIL VDCFLAG% = 1 OR A$ <> ""
   
	COLOR 7, 1, 1
	PRINT TAB(10); USING "Required Amplitude is:     ##.# Vpk"; GENAMPL!

	DO
		CALL DMM2010(VOAC$, DDATA$, 2000!, .01, 99, 99, 99, 99, 99, 99, 99)
		VACMEAS! = VAL(DDATA$) * SQR(2): DDATA$ = ""
		IF SNM% = 10 THEN
			'Read current input, but display voltage to match that of the func. gen
			VACDISP! = (VACMEAS! + (VACMEAS! / IINSEN.MEAS! * SPECS.INPUTRES * 1000))*1.1 'Display reads 10% low
		ELSE
			VACDISP! = VACMEAS!
		END IF
		IF ABS(VACDISP! - GENAMPL!) > .05 * GENAMPL! THEN
			COLOR 12, 0, 0
			VACFLAG% = 0
		ELSE
			COLOR 10, 0, 0
			VACFLAG% = 1
		END IF
		LOCATE 9, 50
		PRINT ; USING "##.# &"; VACDISP!; "Vpk"
		A$ = INKEY$
	LOOP UNTIL VACFLAG% = 1 OR A$ <> ""

	COLOR 7, 1, 1
	VAC! = VACMEAS!

	'CALL THRESET                            'Prevents shorting out the +15V supply.
	CALL LOADMUX(CH%, COFF%)
	CALL PAUSE(1)

END SUB

FUNCTION LEADREFF$ (CONTRES!)
	  
	SENSOR$ = SPECS.SENTYPE                 'Plat or Nickel or ?
	   
	LOWOHM! = SENSOROUT!(SPECS.MININ)
	HIGHOHM! = SENSOROUT!(SPECS.MAXIN)
	MIDOHM! = (LOWOHM! + HIGHOHM!) / 2      'Use midpoint ohms

	MAXOUT! = SPECS.MAXOUT
	MINOUT! = SPECS.MINOUT
	ORANGE! = (SPECS.MAXOUT - SPECS.MINOUT) 'volts
	INRANGE! = SPECS.MAXIN - SPECS.MININ    'temperature or ohms
	   
	VGAIN! = ORANGE! / INRANGE!             'V/C or V/ohms (POT)
	TGAIN! = INRANGE! / ORANGE!             'C/V or Ohms/V (POT)
	SNM% = SENSORNUM%
	   
	RLEAD! = 20                             'hard-wired in fixture
	SPEC! = SPECS.LEADRERR
	set! = 0
	   
	CLS
	TESTTITLE$ = "Lead Resistance Effects Test"
	CALL HEADERB(TESTTITLE$)
	LOCATE 10, 10
		
	CALL OHMSET(MIDOHM! + set!, 0)              'set the rdac
	CALL SETTEST(2)                             'measure the rdac and add series R in (-) leg
   
	IF SNM% < 4 AND SNM% > 5 THEN   'Check if resistance in
		PRINT "This test is not performed on model "; SPECS.MODNAME
		EXIT FUNCTION
	ELSE
		B$ = SETSUPPLYV$(VSADDR%, 0!, SUPPLYOFF$)   'Set supply voltage and apply
'1/8/10 TSO        PRINT "Make sure the module under test is in the 7BXX socket location..."
'1/8/10 TSO        BEEP
'1/8/10 TSO        CALL CONTINUE
	END IF
	B$ = SETSUPPLYV$(VSADDR%, SPECS.NOMVS, SUPPLYON$)   'Set supply voltage and apply

	'RDAC likes to set approx one ohm less...
	set! = .5
	
	CALL LOADMUX(2, CON%)                       'connect output of module to dvm
	IF INRANGE! > 1000 THEN
		CALL PAUSE(3)                           'set up dvm
	END IF
	CALL DMM2010(VODC$, DDATA$, 2000!, 5, .5, 99, 99, 99, 99, 99, 99)
	V1! = VAL(DDATA$): DDATA$ = ""
	   
	'B. PUT IN 20 OHMS (0.1%) AND DETERMINE EFFECT ON OUTPUT
	'Instruct test operator to move module to leader test sockets...
	B$ = SETSUPPLYV$(VSADDR%, 0!, SUPPLYOFF$)   'Set supply voltage and apply
	LOCATE 10, 10
	PRINT "Install the module under test in the Lead Comp socket location..."
	BEEP
	CALL CONTINUE
	B$ = SETSUPPLYV$(VSADDR%, SPECS.NOMVS, SUPPLYON$)   'Set supply voltage and apply
	CLS
	TESTTITLE$ = "Lead Resistance Effects Test"
	CALL HEADERB(TESTTITLE$)
	LOCATE 10, 10
	
	IF INRANGE! >= 1000 THEN
		CALL PAUSE(3)                           'read module output
	END IF
	CALL DMM2010(VODC$, DDATA$, 2000!, 5, .5, 99, 99, 99, 99, 99, 99)
	V2! = VAL(DDATA$): DDATA$ = ""
	OHM! = MIDOHM!
	CALL MEASRES(OHM!, 3, EMF!)
	RSIM! = OHM! + CONTRES!
   
	'calculate temperature based on the rdac value alone
	IF SNM% = 5 THEN
		TCALC! = RTDTEMP!(RSIM!, SENSOR$)
	ELSE
		TCALC! = RSIM!
	END IF
   
	CALL LOADMUX(2, COFF%)                  'Deselect MUX channel
	VERR! = V2! - V1!
   
	IF SNM% = 5 THEN
		SPECRTO! = SPEC! * VGAIN! * RLEAD!  'øC/OHM*(V/Cø)*OHM = V
	ELSE
		SPECRTO! = SPEC! * VGAIN! * RLEAD!  'OHM/OHM*(V/OHM)*OHM = V
	END IF
   
	IF ABS(VERR!) <= SPECRTO! THEN
		LRE$ = "PASS"
	ELSE
		LRE$ = "FAIL"
		SOUND 1000, .5
	END IF
	  
	LOCATE 10, 10
	PRINT ; USING "&"; "Status: ";
	IF LRE$ = "FAIL" THEN
		COLOR 12, 1, 1
	ELSE
		COLOR 10, 1, 1
	END IF
	PRINT TAB(18); LRE$;
	PRINT SPC(58);
	COLOR 7, 1, 1
	PRINT SPC(20);

	IF SNM% = 5 THEN
		UNITS$ = "C/ohm"
	ELSE
		UNITS$ = "ohm/ohm"
	END IF

	PRINT TAB(10); "Measured lead resistance effect is";
	PRINT TAB(50); USING "  +#.### &"; VERR! / VGAIN! / RLEAD!; UNITS$
	PRINT TAB(10); "Maximum test lead resistance effect is";
	PRINT TAB(50); USING "+/-#.### &"; SPEC!; UNITS$
	   
	LEADREFF$ = LRE$ + STR$(VERR! / VGAIN! / RLEAD!) + "3"

	COLOR 7, 1, 1
   
	PRINT
	PRINT TAB(10); "If you want to continue testing this module"
	PRINT TAB(10); "return the module to the 7BXX socket location..."
	BEEP
   
	IF FINAL% <> 1 THEN CALL PAUSE(1)

END FUNCTION

FUNCTION LOOPVOLT$ (TTYPE%)
	
	'Function that measures the loop voltage under the specified load
	'and supply conditions defined by the input flag (TTYPE!)
	'
	'TTYPE%=0, min input load, minimum loop supply
	'TTYPE%=1, max input load, minimum loop supply

	MAXLOAD! = SPECS.MAXIN                      'Max input value
	MINLOAD! = SPECS.MININ
	TOL! = .15                                  'Maximum tolerance allowed
												'Changed from 5% to 15% for SCM7B35-1256 oscillator op-amp IC2005-002 to IC2089-002. Vcompl old 21V@4mA 17.7V@20mA, new 22.3V@4mA 19.8V@20mA.
	SNM% = SENSORNUM%
 
	IF TTYPE% = 0 THEN
		LOAD$ = " (Min Load, Minimum Supply Voltage)"
		INLOAD! = MINLOAD!
		VSUPPLY! = SPECS.MINVS                      'Minimum supply voltage
		VLOOPEXP! = SPECS.VEXCLO                    'Expected loop voltage
	ELSE
		LOAD$ = " (Full Load, Minimum Supply Voltage)"
		INLOAD! = MAXLOAD!
		VSUPPLY! = SPECS.MINVS                      'Minimum supply voltage
		VLOOPEXP! = SPECS.VEXCHI                    'Expected loop voltage
	END IF
  
	CLS
	TESTTITLE$ = "Loop Voltage Test" + LOAD$
	CALL HEADERB(TESTTITLE$)
	LOCATE 10, 10
	 
	IF SNM% <> 9 THEN
		PRINT "This test is not performed on model "; SPECS.MODNAME
		EXIT FUNCTION
	ELSE
		PRINT "Testing, please wait..."
	END IF
  
	IINMEAS! = SETCURRENTIN!(INLOAD!, .001)

	B$ = SETSUPPLYV$(VSADDR%, VSUPPLY!, SUPPLYON$)  'Set supply voltage and apply
  
	CALL DMM2010(VODC$, "99", 2000!, 5, 99, 99, 99, 99, 99, 99, 99)
	CALL LOADMUX(1, CON%)
	CALL LOADMUX(2, COFF%)                  'Turns ON randomly shorting the input
	CALL LOADMUX(4, COFF%)                  'Turns ON randomly shorting the input
	CALL DMM2010(VODC$, DDATA$, VLOOPEXP! * (1 + 1.5 * TOL!), 5, 99, 99, 99, 99, 99, 99, 99)
	VLOOPMEAS! = VAL(DDATA$): DDATA$ = ""
	CALL LOADMUX(1, COFF%)

	LOCATE 10, 10
	PRINT "Status: ";
 
	IF ABS(VLOOPMEAS! - VLOOPEXP!) < (TOL! * VLOOPEXP!) THEN
		LOOPV$ = "PASS"
		COLOR 10, 1, 1
	ELSE
		LOOPV$ = "FAIL"
		SOUND 1000, .5
		COLOR 12, 1, 1
	END IF
  
	PRINT LOOPV$;
	COLOR 7, 1, 1
	PRINT SPC(10);
	PRINT TAB(10); "Measured loop voltage is";
	PRINT TAB(50); USING "###.## V"; VLOOPMEAS!
	PRINT TAB(10); "Expected Value is";
	PRINT TAB(50); STR$(VLOOPEXP!) + " +/-" + STR$(VLOOPEXP! * TOL!) + " V"

	B$ = SETSUPPLYV$(VSADDR%, SPECS.NOMVS, SUPPLYON$)  'Set supply voltage and apply
  
	LOOPVOLT$ = LOOPV$ + STR$(VLOOPMEAS!) + "2"
	 
	IF FINAL% <> 1 THEN CALL PAUSE(1)

END FUNCTION

SUB MEASRES (OHM!, RESNUM%, EMF!)

	'Measure specified resistance
	'Legend:
	'RESNUM%        Resistor
	'-------        --------
	'   1           7B39 Output current conversion resistor
	'   2           Simulated thermistors, 25C & 35C
	'   3           Simulated sensor input, RTD or pot, 8-20Mohm
	'   4           Contact resistance, RTD or pot input, milli-ohm
	'   5           IDAC turnover resistor
	'   6           7B39 & 7B22 load resistors

	SNM% = SENSORNUM%

	EMF! = 0
	EMFFAIL% = 0
   
	SELECT CASE RESNUM%                     'Branches to the specific measurement
		CASE 1
			'Measure 39 output turnover resistor
			CALL LOADMUX(2, CON%)
			CALL LOADMUX(8, CON%)
			OHM$ = OHM4$
			RATE! = 3
			DELAY! = 99
			COMP% = 1
			INITTOL! = .05                          '5% initial tolerance
		CASE 2
			'Measure simulated thermistors - voltage test head
			CALL LOADMUX(4, CON%)                   'Sets mux channel
			CALL PAUSE(.5)
			OHM$ = OHM2$
			RATE! = 99
			DELAY! = 99
			COMP% = 99
			INITTOL! = .05                          '5% initial tolerance
		CASE 3
			'Measure RTD testhead RSIM
			CALL SETTEST(7)  '120ê 3-WIRE INPUT
			CALL LOADMUX(1, CON%)
			CALL LOADMUX(8, CON%)

			IF OHM! > 1000 THEN
				OHM$ = OHM2$
			ELSE
				OHM$ = OHM4$
			END IF
	   
			RATE! = 3
			DELAY! = 99 '.5
			COMP% = 99 '1
			INITTOL! = .15                          '15% initial tolerance
		CASE 4
			CALL OHMSET(1E+10, 0)                   'Open all relays
			CALL SETTEST(3)                         'Measure contact resistance
			CALL LOADMUX(1, CON%)                   'Sets mux channel
			CALL DMM2010(VODC$, DDATA$, .001, 3, 2, 99, 99, 99, 99, 99, 99)
			EMF! = VAL(DDATA$)
			CALL LOADMUX(6, CON%)                   'set sense channel
			OHM$ = OHM4$
			RATE! = 3
			DELAY! = 99 '.5
			COMP% = 99 '1
			INITTOL! = .15                          '15% initial tolerance
		CASE 5
			'Measure voltage test head IDAC turnover resistor
			CALL LOADMUX(0, CON%)
			CB0VAL% = (CB0VAL% OR &H4)              'Open K15 to iso R-2R
			CALL SETTH(CB0VAL%, CB0%)               '0 0 0 0  0 1 0 0
			CB3VAL% = &HFF                          '1 1 1 1 0 1 0 1
			CALL SETTH(CB3VAL%, CB3%)               'CMD to set relays 21 and 22
			CALL LOADMUX(6, CON%)
			CALL LOADMUX(7, CON%)
			OHM$ = OHM4$
			RATE! = 3
			DELAY! = 99
			COMP% = 1
			INITTOL! = .05                          '5% initial tolerance
		CASE 6
			'Measure 39 or 22 load resistor
			CALL LOADMUX(2, CON%)
			OHM$ = OHM2$
			RATE! = 99
			DELAY! = 99
			COMP% = 99
			INITTOL! = .15                          '15% initial tolerance
	END SELECT

	CALL DMM2010(OHM$, DDATA$, OHM!, RATE!, DELAY!, 99, 99, 99, 99, COMP%, 99)
	TEMPR! = VAL(DDATA$): DDATA$ = ""

	IF OHM! > 1! AND ABS(TEMPR! - OHM!) > (OHM! * INITTOL!) THEN   'Test for deviation
		CLS
		LOCATE 10, 10
		PRINT "Measured resistance = "; TEMPR!; " ohms."
		PRINT TAB(10); USING "Required resistance = #######.## ohms to #######.## ohms"; OHM! * (1 - INITTOL!); OHM! * (1 + INITTOL!)
		PRINT TAB(10); "Check all cables and try again."
		PRINT TAB(10); "If the failure continues, notify engineering."
		END
	END IF

	CALL LOADMUX(0, CON%)                           'Clears mux

	IF RESNUM% = 3 THEN
		CALL SETTEST(2)                         'Reset to standard operation
		IF OHM! < 17000 THEN
			IF EMF! > .0002 THEN
				EMFFAIL% = 1
			END IF
		ELSE
			IF EMF! > .005 THEN
				EMFFAIL% = 1
			END IF
		END IF
	ELSEIF RESNUM% = 4 THEN
		CALL SETTEST(2)
		IF TEMPR! > .3 THEN                     'Contact res. out of spec
			PRINT
			PRINT TAB(10); "Contact resistance = "; TEMPR!; "ohms."
			PRINT TAB(10); "This test head requires repair."
			PRINT TAB(10); "Please notify engineering."
			CALL CONTINUE
			END
		END IF
		IF OHM! < 17000 THEN
			IF EMF! > .0002 THEN
				EMFFAIL% = 1
			END IF
		ELSE
			IF EMF! > .005 THEN
				EMFFAIL% = 1
			END IF
		END IF
	END IF

	IF EMFFAIL% = 1 THEN                      'EMF out of spec
		PRINT
		PRINT TAB(10); "Relay EMF = "; EMF! * 1000000!; "uV."
		PRINT TAB(10); "This test head requires repair."
		PRINT TAB(10); "Please notify engineering."
		CALL CONTINUE
		END
	END IF

	OHM! = TEMPR!                                'Passes resistor value back

END SUB

FUNCTION OPENLOOPDETECT$

	'Test the open loop detect function for the SCM7B39-1257.
	'If the output current loop is open, the input impedance of the
	'module must go to > 20Kohm in <= 5ms.  Since the input is also a
	'current loop, open the output loop and measure the input current.

	MAXOUT! = SPECS.MAXOUT
	SNM% = SENSORNUM%
	PLUSFSIN! = SENSOROUT!(SPECS.MAXIN)
	INRANGE! = PLUSFSIN! - SENSOROUT!(SPECS.MININ)
	DACACC! = .01                           '1% DAC setting accuracy
	ITOL! = .1                              '10% input current tolerance
	OLISPEC! = 0                            'input current for open output loop
	OLITOL! = 1                             '+/-1mA measurement tolerance
 											'Changed from 0.1mA to 1mA due to SCM7B39-1257 circuit revision JL MF 10/11/2024
 
	CLS
	TESTTITLE$ = "Open Loop Detect Test"
	CALL HEADERB(TESTTITLE$)
	LOCATE 10, 10
	IF SNM% <> 10 AND LEFT$(MN$, 9) <> "7B39-1257" THEN  'Check if 39-1257
		PRINT "This test is not performed on model "; SPECS.MODNAME
		EXIT FUNCTION
	ELSE
		PRINT "Testing, please wait..."
	END IF
 
	CALL SETOUTLOAD(IOUTSEN!)                   'Connect output load

	IIN1! = SETCURRENTIN!(PLUSFSIN!, DACACC! * INRANGE! / 1000! * IINSEN!)
		  
	CALL SETOUTLOAD(100000!)                    'Open output loop
   
	CALL LOADMUX(6, CON%)                       'Measure Iin
  
	CALL DMM2010(VODC$, DDATA$, 5, 99, 99, 99, 99, 99, 99, 99, 99)
	IIN2! = VAL(DDATA$) / IINSEN! * 1000!       'mA
	DDATA$ = ""
	CALL LOADMUX(6, COFF%)

	LOCATE 10, 10
	PRINT "Status: ";
 
	IF ABS(IIN1! - PLUSFSIN!) < PLUSFSIN! * ITOL! AND ABS(IIN2! - OLISPEC!) < OLITOL! THEN
		OLDET$ = "PASS"
		COLOR 10, 1, 1
	ELSE
		OLDET$ = "FAIL"
		SOUND 1000, .5
		COLOR 12, 1, 1
	END IF
  
	PRINT TAB(18); OLDET$;
	PRINT SPC(12);
	COLOR 7, 1, 1
	PRINT SPC(31);
   
	PRINT TAB(10); "Input current with output loop intact is";
	PRINT TAB(52); USING "##.# mA"; IIN1!
	PRINT TAB(10); "Required input current is";
	PRINT TAB(52); USING "##.# mA to ##.# mA"; PLUSFSIN! * (1 - ITOL!); PLUSFSIN! * (1 + ITOL!)
	PRINT
	PRINT TAB(10); "Input current with output loop open is";
	PRINT TAB(52); USING "##.# mA"; IIN2!
	PRINT TAB(10); "Required input current is";
	PRINT TAB(52); USING "##.# mA to ##.# mA"; OLISPEC! - OLITOL!; OLISPEC! + OLITOL!

	IF SNM% = 10 THEN	'SCM7B39-04, -1257 are showing +f.s. shift. Suspect test head issue. Modules stable on bench.
		OPENLOOPDETECT$ = OLDET$ + STR$(IIN2!) + "1"
	ELSE
		OPENLOOPDETECT$ = OLDET$ + STR$(IIN2!) + "0"
	END IF

  	COLOR 7, 1, 1
  
	IF FINAL% <> 1 THEN
		CALL PAUSE(1)
	ELSE
		CALL PAUSE(.5)
	END IF

END FUNCTION

FUNCTION OPENTC$
	 
	MINOUT! = SPECS.MINOUT          'Output voltage (V)
	MAXOUT! = SPECS.MAXOUT

	MN$ = SPECS.MODNAME
	SNM% = SENSORNUM%

	INRANGE! = SENSOROUT!(SPECS.MAXIN!) - SENSOROUT!(SPECS.MININ!)
	ORANGE! = MAXOUT! - MINOUT!
	DACACC! = .01                   '1% DAC setting accuracy

	IF SNM% = 4 THEN
		OINSPEC! = .5
	ELSEIF LEFT$(MN$, 10) = "7B47K-1254" OR LEFT$(MN$, 10) = "7B47K-1288" OR LEFT$(MN$, 9) = "7B36-1246" THEN
	   OINSPEC! = -.3
	ELSE
		OINSPEC! = MAXOUT! + .15
	END IF
	CLS
	TESTTITLE$ = "Open Sensor Detect Test"
	CALL HEADERB(TESTTITLE$)
	LOCATE 10, 10
	 
	IF SNM% < 3 OR SNM% > 5 THEN   'Check for TC/POT in
		PRINT "This test is not performed on model "; SPECS.MODNAME
		EXIT FUNCTION
	ELSE
		PRINT "Testing, please wait..."
	END IF

	'inputs should be shorted and attenuator cleared upon entering test

	IF SNM% <> 4 THEN                               'Check if pot in
		MININ! = SPECS.MININ
		VOLTAGE! = SENSOROUT!(MININ!)
		CALL SETDAC(VOLTAGE!, DACACC! * INRANGE!, 1, 0) 'Sets input voltage
		CALL PAUSE(.2)
													'Remove short on inputs  06/05/07 MR
		CALL SETTH(CB0VAL% OR &H8, CB0%)            '0 0 0 0  1 0 0 0        06/05/07 MR
		CALL SETTH((CB1VAL% OR &HC0), CB1%)         'Clear R/2R ladder       06/05/07 MR
		CALL SETTH((CB2VAL% OR &HFF), CB2%)
		CALL LOADMUX(2, CON%)                      'Measure Vout
		CALL PAUSE(1.2)                            'Wait 2.5s (spec is 10s)
		CALL DMM2010(VODC$, DDATA$, 2000!, 99, 99, 99, 99, 99, 99, 99, 99)
		VO! = VAL(DDATA$): DDATA$ = ""
		CALL LOADMUX(2, COFF%)
	ELSE
		MININ! = SPECS.MININ
		CALL LOADMUX(0, CON%)                      'Disable DUT output channel
		RIN! = SENSOROUT!(MININ!) + set!            'øC->ê + Add set (ê)
		CALL OHMSET(RIN!, 0)                       'Set RDAC
		CALL SETTEST(2)                             'Normal operation
		CALL LOADMUX(2, CON%)                       'Enable DUT output channel
		CALL OHMSET(1E+10, 0)                      'Set RDAC
		CALL PAUSE(1)
		CALL DMM2010(VODC$, DDATA$, 2000!, 99, .8, 99, 99, 99, 99, 99, 99)
		VO! = VAL(DDATA$): DDATA$ = ""
		CALL LOADMUX(2, COFF%)
	END IF
	   
	IF OINSPEC! >= MAXOUT! THEN                   'Check for upscale open TC detect
		COND$ = ">"
		IF VO! >= OINSPEC! THEN
			OTC$ = "PASS"
		ELSE
			OTC$ = "FAIL"
			SOUND 1000, .5
		END IF
	ELSE                                        'else downscale open TC detect
		COND$ = "<"
		IF VO! <= OINSPEC! THEN
			OTC$ = "PASS"
		ELSE
			OTC$ = "FAIL"
			SOUND 1000, .5
		END IF
	END IF

	LOCATE 10, 10
	PRINT ; USING "&"; "Status: ";
	IF OTC$ = "FAIL" THEN
		COLOR 12, 1, 1
	ELSE
		COLOR 10, 1, 1
	END IF
	PRINT TAB(18); OTC$;
	PRINT SPC(12);
	COLOR 7, 1, 1
	PRINT SPC(20);
	PRINT TAB(10); "Open sensor response is";
	PRINT TAB(50); USING "  ##.### V"; VO!
	PRINT TAB(10); "Required response is";
	PRINT TAB(50); USING "& ##.## V"; COND$; OINSPEC!
	   
	OPENTC$ = OTC$ + STR$(VO!) + "1"
	
	COLOR 7, 1, 1
	IF FINAL% <> 1 THEN CALL PAUSE(1)

END FUNCTION

FUNCTION OUTNOISERMS$

	MININ! = SENSOROUT!(SPECS.MININ)                'Calculate module input
	MAXIN! = SENSOROUT!(SPECS.MAXIN)
	MINVS! = SPECS.MINVS
	SPEC! = SPECS.OUTNOISERMS      'uVrms or uArms
	SNM% = SENSORNUM%

	INRANGE! = MAXIN! - MININ!
	DACACC! = .01                                   '1% DAC setting accuracy

	IF SNM% = 8 OR SNM% = 10 THEN
		MINOUT! = (SPECS.MINOUT / 1000) * IOUTSEN.MEAS!
		MAXOUT! = (SPECS.MAXOUT / 1000) * IOUTSEN.MEAS!
		CALL SETOUTLOAD(IOUTSEN!)               'Connect output load
	ELSE
		IF SNM% = 7 THEN
			CALL SETOUTLOAD(VOUTLOAD1!)
		END IF
		MINOUT! = SENSOROUT!(SPECS.MINOUT)              'Calculate module input
		MAXOUT! = SENSOROUT!(SPECS.MAXOUT)
	END IF
	  
	XFRFN! = (MAXOUT! - MINOUT!) / INRANGE!
	YINT! = MINOUT! - XFRFN! * MININ!

	IF ABS(MININ!) = ABS(MAXIN!) AND (YINT! = 0) THEN
		ONMININ! = 0!
	ELSE
		ONMININ! = MININ!           'Calc. -f.s. input
	END IF
	  
	CLS
	TESTTITLE$ = "Output Noise Test"
	CALL HEADERB(TESTTITLE$)
	LOCATE 10, 10
	PRINT "Testing, please wait..."

	IF SNM% = 4 OR SNM% = 5 THEN     'Check if resistance in
		CALL SETTEST(2)
		CALL OHMSET(ONMININ!, 0)               'Set -f.s. input
	ELSEIF SNM% = 8 THEN
		IF MINOUT! = 0 THEN
			VOLTAGE! = MAXIN! / 2
		ELSE
			VOLTAGE! = MAXIN! 'ONMININ!  Changed  JL 08/30/00
		END IF
		CALL SETDAC(VOLTAGE!, DACACC! * INRANGE!, 1, 0)
	ELSEIF SNM% = 2 OR SNM% = 9 OR SNM% = 10 THEN
		MODIN! = SETCURRENTIN!(ONMININ!, DACACC! * INRANGE! / 1000! * IINSEN!)
	ELSE
		VOLTAGE! = ONMININ!
		CALL SETDAC(VOLTAGE!, DACACC! * INRANGE!, 1, 0)'Set -f.s. input
	END IF

	CALL LOADMUX(2, CON%)                           'Measure Vout
		
	CALL DMM2010(VOAC$, DDATA$, 2000!, 99, 99, 99, 99, 99, 99, 99, 30)
	   
	VRMS! = ABS(VAL(DDATA$) * 1000000!)
	DDATA$ = ""
	CALL LOADMUX(2, COFF%)
	   
	IF SNM% = 8 OR SNM% = 10 THEN
		VRMS! = VRMS! / IOUTSEN.MEAS!
	END IF

	IF VRMS! <= SPEC! THEN
		VNOISE$ = "PASS"
	ELSE
		VNOISE$ = "FAIL"
		SOUND 1000, .5
	END IF
	   
	LOCATE 10, 10
	PRINT ; USING "&"; "Status: ";
	IF VNOISE$ = "FAIL" THEN
		COLOR 12, 1, 1
	ELSE
		COLOR 10, 1, 1
	END IF
	PRINT TAB(18); VNOISE$;
	PRINT SPC(12);
	COLOR 7, 1, 1
	PRINT SPC(20);
	PRINT TAB(10); "Measured output noise is";
	IF SNM% <> 8 AND SNM% <> 10 THEN
		PRINT TAB(50); USING "##### uV rms"; VRMS!
	ELSE
		PRINT TAB(50); USING "####.# uA rms"; VRMS!
	END IF
	PRINT TAB(10); "Maximum output noise is";
	IF SNM% <> 8 AND SNM% <> 10 THEN
		PRINT TAB(50); USING "##### uV rms"; SPEC!
		OUTNOISERMS$ = VNOISE$ + STR$(VRMS!) + "0"
	ELSE
		PRINT TAB(50); USING "####.# uA rms"; SPEC!
		OUTNOISERMS$ = VNOISE$ + STR$(VRMS!) + "1"
	END IF
   
	COLOR 7, 1, 1
   
	IF FINAL% <> 1 THEN CALL PAUSE(1)

END FUNCTION

SUB SETAC (ONOFF%)

	SNM% = SENSORNUM%

	IF SNM% <> 5 OR SNM% <> 4 THEN CALL SETTH(&HFF, 3) 'Prevents shorting out the +15V supply.
	  
	IF ONOFF% = 1 THEN
		IF SNM% = 5 OR SNM% = 4 THEN    'Check if resistance in
			CALL SETTEST(6)                         'Connect function generator
		ELSEIF SNM% = 2 THEN
			IF TE$ = "TE1040OLDCS" THEN
				CB0VAL% = &HFD                  '1 1 1 1  1 1 0 1 CMD to set relays 21 & 22
				CB3VAL% = &HF5                  '1 1 1 1  0 1 0 1 CMD to set relays 21 & 22
			ELSE
				CB0VAL% = &HE9                  '1 1 1 0  1 0 0 1 CMD to set relays 21 & 22
				CB3VAL% = &HDF                  '1 1 0 1  1 1 1 1 CMD to set relays 21 & 22
			END IF
			CALL SETTH(CB0VAL%, CB0%)
			CALL SETTH(CB3VAL%, CB3%)
		ELSEIF SNM% = 9 THEN
			IF TE$ = "TE1040OLDCS" THEN
				CB0VAL% = &HED                  '1 1 1 0  1 1 0 1 CMD to set relay 22 only
				CB3VAL% = &HFD                  '1 1 1 1  1 1 0 1 CMD to set relay 22 only
			ELSE
				CB0VAL% = &HED                  '1 1 1 0  1 1 0 1
				CB3VAL% = &HC7                  '1 1 0 0  0 1 1 1
			END IF
			CALL SETTH(CB3VAL%, CB3%)
			CALL SETTH(CB0VAL%, CB0%)
		ELSEIF SNM% = 10 THEN
			CB3VAL% = (CB3VAL% OR &H10) AND &HDF'0 0 0 1  0 0 0 0
													'D.U.T. input = current source
													'1 1 0 1  1 1 1 1
			CALL SETTH(CB3VAL%, CB3%)           'Input current sourced from +15V
			CB0VAL% = (CB0VAL% AND &HE9) OR &H8 '1 1 1 0  1 0 0 1
			CALL SETTH(CB0VAL%, CB0%)           'ground inputs, short series R
												'remove short on inputs
		ELSE
			CB0VAL% = (CB0VAL% AND &HE9) OR &H8 '1 1 1 0  1 0 0 1
			CALL SETTH(CB0VAL%, CB0%)           'ground inputs, short series R
												'remove short on inputs
		END IF

	ELSE
		IF SNM% = 4 OR SNM% = 5 THEN
			CALL THRESET
		ELSE
			CALL SETTH(&HFF, 3) 'Prevents shorting out the +15V supply.
			CB0VAL% = CB0VAL% OR &H2                '0 0 0 0  0 0 1 0
			CALL SETTH(CB0VAL%, CB0%)               'Select DAC input
		END IF
	END IF

END SUB

FUNCTION SETCURRENTIN! (MODIN!, DACACCV!)

	'Sets the input current source to the D.U.T.
	'Inputs;    Desired input current in mA.
	'Outputs;   Measured input current in mA.
	'
	'Schematic; Current Input, New Current Source (PCB1197).
	'Used on TE1062 and TE1040NEWCS, post 05/2007
	'* Short +IN and -IN using test head relay for startup test.
	'             -------
	'   Vdac-----|       |----- +15V
	'            |       |  --> Iout = Vdac / (IINSEN.MEAS + RCONV)
	'            |       |---
	'            |       |  |
	'             -------   IINSEN.MEAS
	'                       |
	'                       RCONV (DUT)
	'                       |
	'                       GA1
	'
	'
	'Schematic; Two-Wire Transmitter Input, New Current Source (PCB1197).
	'Used on TE1062 and TE1040NEWCS, post 05/2007
	'             -------
	'   Vdac-----|       |----- +EXC
	'            |       |  --> Iout = Vdac / IINSEN.MEAS
	'            |       |---
	'            |       |  |
	'             -------   IINSEN.MEAS
	'                       |---------------
	'                       |               |
	'                       GA1             RCONV (DUT)
	'                                       |
	'                                      FGND
	'
	'Schematic; Current Input, Old Current Source.
	'Used on TE1040OLDCS, pre 05/2007
	'* Short +IN and -IN using test head relay for startup test.
	'
	'                      +15V
	'                       |
	'                       RCONV (DUT)
	'                       |
	'             -------   |
	'   Vdac-----|       |---
	'            |       |  --> Iout = Vdac / IINSEN.MEAS
	'            |       |---
	'            |       |  |
	'             -------   IINSEN.MEAS
	'                       |
	'                       GA1
	'
	'Schematic; Two-Wire Transmitter Input, Old Current Source.
	'Used on TE1040OLDCS, pre 05/2007
	'             -------
	'   Vdac-----|       |----- +EXC
	'            |       |  --> Iout = Vdac / IINSEN.MEAS
	'            |       |---
	'            |       |  |
	'             -------   IINSEN.MEAS
	'                       |---------------
	'                       |               |
	'                       GA1             RCONV (DUT)
	'                                       |
	'                                      FGND

	SNM% = SENSORNUM%
 
	IF TE$ = "TE1062" THEN 'New Current Source
		IF SNM% = 0 THEN                    'SNM% = 0 is case upon system startup
			RCONV! = 0
		ELSE                                'SNM% = 10, mA input
			RCONV! = 250                    'Module current conversion resistor
		END IF
		CB3VAL% = CB3VAL% OR &H10           '0 0 0 1  0 0 0 0
		CALL SETTH(CB3VAL%, CB3%)           'Input current sourced from +15V
		VSENATTEN! = (IINSEN.MEAS! + RCONV!) / IINSEN.MEAS!
	ELSE                                    'TE1040, Old Current Source
   
		IF SNM% = 0 THEN                    'test head startup test
			IF SPECS.INPUTRES! <> 0 AND TE$ = "TE1040NEWCS" THEN
											'Pass thru modules w/ conversion R.
				VSENATTEN! = -2
			ELSE
				VSENATTEN! = -1             'flag used in SETDAC
			END IF
		ELSEIF SNM% = 2 THEN                'mA input
			VSENATTEN! = -2                 'flag used in SETDAC
		ELSE                                'SNM% = 9, 2WTX input
			VSENATTEN! = -3                 'flag used in SETDAC
		END IF
	END IF

	VSENSE! = MODIN! / 1000! * IINSEN.MEAS! 'Iin, desired * current source sense R
	CH% = 6
	CALL SETDAC(VSENSE!, DACACCV!, CH%, VSENATTEN!)    'Sets DAC
	CALL LOADMUX(CH%, CON%)
	CALL DMM2010(VODC$, DDATA$, 99, 99, 99, 99, 99, 99, 99, 99, 99)
	VSENSE! = VAL(DDATA$): DDATA$ = ""
	CALL LOADMUX(CH%, COFF%)
	SETCURRENTIN! = VSENSE! / IINSEN.MEAS! * 1000!

END FUNCTION

SUB SETOUTLOAD (LOAD!)

	'Set the specified output load.
  
	IF LOAD! >= 100000 THEN                 'load = open
		MASK% = &HFF                        '1 1 1 1  1 1 1 1
	ELSEIF LOAD! > 4700 THEN                'load = 4.75K ohm
		MASK% = &HFB                        '1 1 1 1  1 0 1 1
	ELSEIF LOAD! > 699 THEN                 'load = 700 ohm
		MASK% = &HFD                        '1 1 1 1  1 1 0 1
	ELSE                                    'load = 192 ohm
		MASK% = &HFE                        '1 1 1 1  1 1 1 0
	END IF

	CB3VAL% = (CB3VAL% OR &H7) AND MASK%    '0 0 0 0  0 1 1 1
	CALL SETTH(CB3VAL%, CB3%)

END SUB

FUNCTION SUPPLYVSENS$
   
	MAXIN! = SPECS.MAXIN        'Calc. maxin
	MININ! = SPECS.MININ        'Calc. minin
	MAXOUT! = SPECS.MAXOUT
	MINOUT! = SPECS.MINOUT
	MINVS! = SPECS.MINVS                   'low supply test voltage
	NOMVS! = SPECS.NOMVS
	MAXVS! = SPECS.MAXVS                   'high supply test voltage
	SPEC! = SPECS.PSS
	INRANGE! = MAXIN! - MININ!
	ORANGE! = MAXOUT! - MINOUT!

	CLS
	TESTTITLE$ = "Supply Voltage Sensitivity Test"
	CALL HEADERB(TESTTITLE$)
   
	LOCATE 10, 10
	PRINT "Testing, please wait..."
  
	SNM% = SENSORNUM%
  
	IF SNM% = 0 OR SNM% = 1 OR SNM% = 3 OR SNM% = 7 OR SNM% = 8 THEN  'VOLTAGE IN
		IF SNM% = 7 THEN
			CALL SETOUTLOAD(VOUTLOAD1!)
		ELSEIF SNM% = 8 THEN
			CALL SETOUTLOAD(IOUTSEN!)               'Connect output load
		END IF
	  
		VOLTAGE! = SENSOROUT!(MAXIN!)
		IF SNM% = 3 THEN                        'TC IN
			TCTYPE$ = UCASE$(LEFT$(SENSOR$, 1))
			IF TCTYPE$ = "B" THEN
				IF VOLTAGE! < .00024 THEN
					VOLTAGE! = 6.3201E-04
				END IF
			END IF
		END IF
		CALL SETDAC(VOLTAGE!, .01 * INRANGE!, 1, 0) 'Sets input voltage
		 
		IF MID$(SPECS.MODNAME, 3, 2) = "47" THEN
			TSIM! = TCTEMP(TSIM! + VTCTA!, SENSOR$)
		ELSE
			TSIM! = MEASUREVIN!
		END IF
		
	ELSEIF SNM% = 2 OR SNM% = 9 OR SNM% = 10 THEN 'CURRENT IN
		IF SNM% = 10 THEN
			CALL SETOUTLOAD(IOUTSEN!)               'Connect output load
		END IF
		TSIM! = SETCURRENTIN!(MAXIN!, .01 * INRANGE! / 1000! * IINSEN!)
  
	ELSEIF SNM% = 5 OR SNM% = 4 THEN 'RESISTANCE IN
		RINPUT! = SENSOROUT!(MAXIN!)
		CALL OHMSET(RINPUT!, 0)                      'Set input
		OHM! = RINPUT!
		CALL MEASRES(OHM!, 3, EMF!)    'Measure simulated resistor
		RSIM! = OHM!
		IF SNM% = 5 THEN
			TSIM! = RTDTEMP!(RSIM!, SENSOR$)      'Calculate actual simulated temp.
		ELSE
			TSIM! = RSIM!
		END IF
		CALL PAUSE(2)
	END IF
  
	CALL LOADMUX(2, CON%)                       'Measure Vout
	CALL DMM2010(VODC$, "99", 2000!, 2, 99, 99, 99, 99, 99, 99, 99)
	CALL PAUSE(1)
	CALL DMM2010(VODC$, DDATA$, 99, 2, 99, 99, 99, 99, 99, 99, 99)
	VOUT1! = VAL(DDATA$): DDATA$ = ""
	SL$ = SETSUPPLYV$(VSADDR%, MINVS!, SUPPLYON$)  'Set supply voltage and apply
	CALL PAUSE(2)
	CALL DMM2010(VODC$, DDATA$, 99, 2, 99, 99, 99, 99, 99, 99, 99)
	VOUT2! = VAL(DDATA$): DDATA$ = ""
	CALL LOADMUX(2, COFF%)                          'Clear mux lines
	SL$ = SETSUPPLYV$(VSADDR%, 0!, SUPPLYOFF$)              'Set supply OFF
	 
	MAXCHG! = ABS(SPEC! * ((MINVS! - NOMVS!) / (MAXVS! - MINVS!)) * MAXOUT!)  'volts change allowed
  
	IF (ABS(VOUT2! - VOUT1!) <= MAXCHG!) THEN
		PSS$ = "PASS"
	ELSE
		PSS$ = "FAIL"
		SOUND 1000, .5
	END IF

	LOCATE 10, 10
	PRINT ; USING "&"; "Status: ";
	IF PSS$ = "FAIL" THEN
		COLOR 12, 1, 1
	ELSE
		COLOR 10, 1, 1
	END IF
	PRINT TAB(18); PSS$
	COLOR 7, 1, 1
	PRINT SPC(20);
	PRINT TAB(10); "Measured change in output (+F.S. input) is";
	PRINT TAB(55); USING "#####.### mV"; (VOUT2! - VOUT1!) * 1000!
	PRINT
	PRINT TAB(10); "Maximum change in output voltage allowed is";
	PRINT TAB(55); USING "+/- ###.### mV"; MAXCHG! * 1000!
	 
	'CALL THRESET
	
'    SUPPLYVSENS$ = PSS$ + STR$((100 * MAXCHG! / VOUT4!) / (100 * (1 - (MINVS! / MAXVS!)))) + "4"
	COLOR 7, 1, 1

END FUNCTION

SUB THRESET
  
	SNM% = SENSORNUM%
	'reset the voltage or RTD test head to a known, desired condition.
	IF TE$ = "TE1039" THEN              'Check if resistance in
		CALL OHMSET(100!, 0)            'Nominal resistance value
		CALL SETTEST(2)                 'Standard operation
	ELSE
		IF (SNM% = 1 OR SNM% = 7 OR SNM% = 8) AND (SPECS.MAXIN > 10! OR SPECS.MININ < -10!) THEN
			SL$ = POWERIO$(VINADDR%, SUPPLYOFF$)  'Disable power supply
												'Remove high voltage from +/-IN
		END IF
		CB3VAL% = &HFF
		CALL SETTH(CB3VAL%, CB3%)
		CB2VAL% = &HFF
		CALL SETTH(CB2VAL%, CB2%)
		CB1VAL% = &HFF
		CALL SETTH(CB1VAL%, CB1%)
		IF SNM% <> 9 THEN
			CB0VAL% = &HE3                  'short inputs and ground
			CALL SETTH(CB0VAL%, CB0%)       '1 1 1 0  0 0 1 1
		ELSE
			CB0VAL% = &HFB                  'open inputs and do not ground them
			CALL SETTH(CB0VAL%, CB0%)       '1 1 1 1  1 0 1 1
		END IF
	END IF
  
	CALL LOADMUX(0, CON%)                   'Resets MUX

END SUB

