'Automated Test Software for DSCAxx-xx Voltage,
'Current, TC and RTD input models.

'AUTHOR:        John Lehman
'DATE:          1/18/99
'USES LIBRARY = NLIBATED.BAS

'       REVISION RECORD

'DATE     APPR    	DESCRIPTION
'----     ----    	-----------
'1/18/99	JL     	Initial Release.  Modified version of TST2DIN1.BAS w/ basis
'                 	from SCM5B program TEST49.BAS.
' ...				See "Revs.txt" file.
'11/15/13	PWR		Version for lot-status and extended serial numbers release. Added "LOGMODFILES"
'					sub to combine logging and file operations common to post-encap and sealed-module
'					tests. Added "DATASHEETWRITE", "DATASHEETPRINT", "WORKORDERHEADER", "WORKORDERLINE"
'					and "WORKORDERPRINT" subs in the "TSTDIN2C.BAS" file for datasheet and work order
'					status file generation and print. Datasheet file creation and data logging is
'					consolidated at the end of testing to prevent partial file generation if F10 is used
'					to abort testing. F10 abort is disabled in the file functions to prevent the
'					creation of defective datasheet or work order status files. The "FOOTER" sub was
'					also updated to not print to the screen.
'
CONST VERSION$ = "B.1 2013.11.15 PWR KDSCOUT1.EXE"				'Version (Revision Date) and initials of engr.
CONST SOURCEFILES$ = "KDSCOUT1.BAS KDSCOUT2.BAS NLIBATE.BAS"	'Source file names 

'******** Declare some useful constants *******
CONST MAXSTATUSINDEX = 18	'Maximum index of the Status array

'****************** Declare Subs *****************
DECLARE SUB CALDAC (TE$)
'
DECLARE SUB CHANGEDN (SN$)                      'Allow user to change dash number
DECLARE SUB COMPTEST (STATUS$(), ITERATION%, CAL%, GENAMPL!, TSIM!(), OUTCALC!(), OUTMEAS!(), ERROROUT!(), ACCSTAT$(), TOTLRESPEC!) 'Complete set of tests
DECLARE SUB CONTINUE ()
DECLARE SUB FOOTER (STATUS$())                  'Sends footer to printer if all tests passed
DECLARE SUB FUNCTEST (CAL%)                     'Routines for functional test
DECLARE SUB GENSET (GENAMPL!)                   'Displays message to set signal generator
DECLARE SUB GETADD (FILENAME$)                  '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 INDIVID (SEL%, ITERATION%)          'Performs the tests individually
DECLARE SUB INIT488 (DEVADDR%)
DECLARE SUB INITPS (ADDR%, OVERI!, OVERV!)
DECLARE SUB LOADMUX (CH%, CHON%)
DECLARE SUB LOGIT (STATUS$(), SN$, TSIM!(), OUTCALC!(), OUTMEAS!(), ERROROUT!(), ACCSTAT$(), TOTLRESPEC!)               'Logs test results to disk
DECLARE SUB MODOUTLINE ()
DECLARE SUB NOTES ()                            'Notes on ATE operation
DECLARE SUB OHMSET (OHM!)
DECLARE SUB ONESHOTTEST ()                      'Tests one-shot pulse width
DECLARE SUB PAUSE (TIME!)
DECLARE SUB REPORT (STATUS$(), TSPEC$())        'Prints test data on screen
DECLARE SUB RESETTH ()                          'Reset test head to known condition
'
DECLARE SUB SETDAC (VOLTAGE!, CH%, VSENATTEN!)
DECLARE SUB SETDAC2 (VOLTAGE!, CH%, VSENATTEN!)  'Sets DAC, output module test heads
DECLARE SUB SETDACFAST (VOLTAGE!)
DECLARE SUB SETOUTLOAD (LOAD!)                  'Set output load
'
DECLARE SUB SETTEST (TEST%)
DECLARE SUB SETTH (DOUT%, CB%)
DECLARE SUB SETTHFAST (DOUT%, CB%)
DECLARE SUB SORTDB (ENDFLAG%)
'
'
'
'
'
DECLARE SUB TSPECS (TSPEC$(), TOTLRESPEC!)  'Creates a string array of test specifications
DECLARE SUB WRITEDVM (FUNC$, VALUE!)
'
'
DECLARE SUB INTERLUDE () ' WAIT FOR USER
DECLARE SUB WORKORDERLINE(FAILSTATE%, SN$)
DECLARE SUB WORKORDERPRINT (SN$)
DECLARE SUB WORKORDERHEADER (SN$)
DECLARE SUB HARDCOPY ()                         ' cjt                       ' cjt
DECLARE SUB SNPARSE (SN$, WO$, DS$)			'Parses serial number into work order and dash numbers

DECLARE SUB DATASHEETWRITE (SN$, STATUS$(), TSIM!(), OUTCALC!(), OUTMEAS!(), ERROROUT!(), ACCSTAT$(), TSPEC$(), TX%, OUTLOAD! )

DECLARE SUB DATASHEETPRINT (SN$, STATUS$())
'Sub below writes data log, datasheet and work order status files
DECLARE SUB LOGMODFILES(STATUS$(), SN$, TSIM!(), OUTCALC!(), OUTMEAS!(), ERROROUT!(), ACCSTAT$(), TSPEC$(), TOTLRESPEC!, TX%)

'DECLARE FUNCTION GETDSFNAME$ (SN$)         	'Returns datasheet file name from serial number
DECLARE FUNCTION GETWOSFNAME$ (SN$)         'Returns work order status file name from serial number

'************** Declare Functions ****************
DECLARE FUNCTION BESTFIT! (SLOPE!, OFFSETS!, TSIM!(), NUMPTS%, ERRO!())
												'Library fn. Calculates besfit line and max error
'
'
'
'
'
'
DECLARE FUNCTION FAILS% (STATUS$())             'Tests for failed tests
DECLARE FUNCTION FREQRESP$ (GENAMPL!)           'Performs Frequency response Test
DECLARE FUNCTION GAINCAL$ (CAL%)                'Performs Gain Calibration
'
DECLARE FUNCTION GETTHID% ()                    'Library fn. Get test head id #
DECLARE FUNCTION GETPSID% ()
'
DECLARE FUNCTION ILIMIT$ (POSI%)                'Test current limit
'
DECLARE FUNCTION INPUTR$ ()                     'Performs Input Resistance Test
DECLARE FUNCTION IOUTMAXL$ (MINMAX%)            'Test compliance
DECLARE FUNCTION KEYBDIN$ ()                    'Get keyboard input
'
DECLARE FUNCTION LINTEST$ (ACC$, OUTLOAD!, TSIM!(), OUTCALC!(), OUTMEAS!(), ERROROUT!(), ACCSTAT$())       'Performs Accuracy and Linearity Tests
DECLARE FUNCTION MEASRES! (OHM!, RESNUM%, TE$)  'Measure loop current sense resistor
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%)              'Performs Offset Calibration
'
'
DECLARE FUNCTION OUTNOISE$ ()                   'Performs Output Noise Test
'
DECLARE FUNCTION POWERIO$ (ADDR%, CMD$)         'Library fn. Kepco DPS power supply I/O
DECLARE FUNCTION READDVM! (TOL!)                'Library fn. Reads Fluke meter to TOL!
'
DECLARE FUNCTION READGPIB$ (ADDR%, CMD$)
DECLARE FUNCTION REPEAT$ (N$)                   'Ask if you would like to repeat test
DECLARE FUNCTION REPEAT2$ (N$, SN$)
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 SETAC! (ONOFF%, GENAMPL!)      'Sets AC in Off or On
DECLARE FUNCTION SETATTEN% (VOLTAGE!)           'Library fn. Sets test head R/2R ladder
DECLARE FUNCTION SETCURRENTIN! (MODIN!)         'Set input current source
DECLARE FUNCTION SETPOWER$ (ADDR%, VSUPPLY!, FUNC$)    '** Controls Kepco DPS 125
DECLARE FUNCTION STEPRESP$ ()                   'Performs Step Response Test
DECLARE FUNCTION STRINGVAL% (A$)                'Library fn.
DECLARE FUNCTION SUPPLYI$ (MAXL%)               'Measure module supply current
DECLARE FUNCTION SUPPLYSEN$ ()                  'Performs Power Supply Sensitivity
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 UPSN$ (SN$)                    'Increments dash# of serial#
'
DECLARE FUNCTION MODULEOUT! (SENOUT!)           'Calculates module output for a given input
'
'************** End of Functions ********************

'Database Record definition for the specifications
	TYPE DBASE
		MODNAME         AS STRING * 13  'DSCA39-XXXXn
		SENTYPE         AS STRING * 7   'mV, V, mA
		ISMAXNL         AS SINGLE
		ISMAXFL         AS SINGLE
		MININ           AS SINGLE
		MAXIN           AS SINGLE
		RCONV           AS SINGLE
'
'
'
		MINOUT          AS SINGLE
		MAXOUT          AS SINGLE
		OUTSIGTYPE      AS STRING * 7 ''VOLTAGE' or 'CURRENT'
		OSCALIN         AS SINGLE   'input for offset calibration
		GNCALIN         AS SINGLE   'input for gain calibration
		CALTOL          AS SINGLE
		LOAD1           AS SINGLE   'Vout min load (mA), Iout nom load (ohms)
		LINEAR1         AS SINGLE   'Voltage or current output
		ACCURACY1       AS SINGLE   'Voltage or current output
		LOAD2           AS SINGLE   'Vout max load (mA), Iout min load (ohms)
		LINEAR2         AS SINGLE   'Voltage output
		ACCURACY2       AS SINGLE   'Voltage output
		LOAD3           AS SINGLE   'Vout max load (mA), Iout not used
		LINEAR3         AS SINGLE   'Voltage output
		ACCURACY3       AS SINGLE   'Voltage output
		BANDWIDTH       AS SINGLE   'filter -3dB point
		TESTFREQ        AS SINGLE
		ATTEN           AS SINGLE
		ATTENTOL        AS SINGLE
		STEPRMIN        AS SINGLE
		STEPRMAX        AS SINGLE
		PSS             AS SINGLE
		OUTNOISE        AS SINGLE
		INPUTRES        AS SINGLE
		COMPLIANCE      AS SINGLE   'Current output, open loop voltage
		MAXLOAD         AS SINGLE   'Current output, max load resistance
		ILIMIT          AS SINGLE   'Voltage output, current limit
		PERCOVER        AS SINGLE   'Voltage output, percent overrange
'
'
'
		MINVS           AS SINGLE
		MAXVS           AS SINGLE
	END TYPE
			   
	TYPE DBASE2
		RECNUM          AS INTEGER
		MODNAME         AS STRING * 13
	END TYPE

'define common variables
COMMON SHARED /SAMPLE/ WO$, STATE$
COMMON SHARED /SAMPLE/ SPECS AS DBASE, SORTDATA1 AS DBASE2, SORTDATA2 AS DBASE2
COMMON SHARED /SAMPLE/ PON%, TX%, IINSEN.MEAS!, IOUTSEN.MEAS!, SN$, LOGDAT%
COMMON SHARED /SAMPLE/ TE$
COMMON SHARED CB0VAL%, CB1VAL%, CB2VAL%, CB3VAL%
COMMON SHARED MAINMAX!, MAINMIN!, VERNMAX!, VERNMIN!
COMMON SHARED BADDRS%, LPTADDR%, MUXADDR%, THADDR%, PSADDR%, VINADDR%
COMMON SHARED PSPORT%, DVMADDR%, GENADDR%
COMMON SHARED DPSPSADDR%, DPSVINADDR%, ABCPSADDR%, ABCVINADDR%
COMMON SHARED PSMODEL$, VINMODEL$
'VINMODEL$ will be used if 2 different KEPCOs are used.

'COMMON SHARED /PRTSCR/ inreg AS RegType, outreg AS RegType

					'assign constants to Fluke meter commands
CONST OHM4$ = "F4", OHM2$ = "F3", VODC$ = "F1", VOAC$ = "F2", MADC$ = "F5"
					'assign constants to test head variables
CONST CB0% = 0, CB1% = 1, CB2% = 2, CB3% = 3  'control bank addresses
CONST CON% = 1, COFF% = 0               'channel 'on' defined as '1'
										'off' defined as '0'
'DVMADDR% = 1                           'GPIB address of Fluke 8842A meter
										'assign constants to Kepco DPS power
										'supply commands
CONST VOLT$ = "STV=", LOOPON$ = "SOP=ON", LOOPOFF$ = "SOP=OFF"
CONST CURRENT$ = "RTC", ISTAT$ = "RCS"
'CONST PSADDR% = 1                       'Power supply for module power
'CONST VINADDR% = 2                      'Power supply for high voltage signal input
CONST OVERV! = 35                       'Power supply over voltage limit
CONST PSILIMIT! = 500                     'Power supply over current limit
										'(max spec multiplier).
CONST VINOVERV! = 125
CONST VINILIMIT! = 32                    'High voltage input current limit

'assign specifications which are constant for all modules
CONST IINSEN! = 192                     'Input current source sense resistor, 250ohm
CONST IOUTSEN! = 192                    'Output current sense resistor.
'
CONST RSERIES! = 1E+07                  '10M ohm, test head series input R
'
'
'
'
'

DIM STATUS(MAXSTATUSINDEX) AS STRING, TSPEC(MAXSTATUSINDEX) AS STRING
DIM TSIM!(105), OUTCALC!(105), OUTMEAS!(105), ERROROUT!(105), ACCSTAT$(105)

' Variable intialization
BADDRS% = &H250                         'Base address of PC4311 '488 controller
CB0VAL% = &HFF                          'Set all lines off
CB1VAL% = &HFF
CB2VAL% = &HFF
CB3VAL% = &HFF

'TE1055 (pcb1133 A)
'(all control lines are active low)

'Use Relay/Mux TE1037 (see below)
'
'Control Bank    Line      Assignment
'------------   ------     ----------
'     0          7 MSB     0 = Connect DUT output to S/H input
'                6         1 = Step input to DUT, start S/H timer
'                5         0 = Bypass series input R
'                4         Vsupply control MSB
'                3         Vsupply control LSB
'                2         0 = AC signal input to DUT
'                1         0 = Connect step input to DUT
'                0 LSB     0 = DC signal input to DUT
'
'
'     1          7 MSB     NC
'                6         NC
'                5         NC
'                4         NC
'                3         NC
'                2         NC
'                1         NC
'                0 LSB     0 = Connect Iout to load
'
'     2          7 MSB     1 = Output filter & CH4 connected to ouput, 0 = CH2 connected to output
'                6         1 = Input buffer gain = 3, 0 = Input buffer gain = 1
'                5         0 = Disable charge offset timer
'                4         0 = Tie WR EN\ to +5V.
'                3         0 = Output load #4, 5 ohm
'                2         0 = Output load #3, 750 ohm
'                1         0 = Output load #2, 500 ohm
'                0 LSB     0 = Output load #1, 250 ohm
'
'
'
'
'
'
'
'
'
'
'

'TE1054 (pcb1133 A)
'(all control lines are active low)
'Use Relay/Mux TE1013 (see below)
'
'Control Bank    Line      Assignment
'------------   ------     ----------
'     0          7 MSB     0 = Connect DUT output to S/H input
'                6         Step input to DUT, start S/H timer
'                5         0 = Bypass series input R
'                4         Vsupply control MSB
'                3         Vsupply control LSB
'                2         0 = AC signal input to DUT
'                1         0 = Connect step input to DUT
'                0 LSB     0 = DC signal input to DUT
'
'     1          7 MSB     NC
'                6         NC
'                5         NC
'                4         NC
'                3         NC
'                2         NC
'                1         NC
'                0 LSB     NC
'
'     2          7 MSB     0 = Output filter & CH4 connected to ouput, 1 = CH2 connected to output
'                6         0 = Input buffer gain = 3, 1 = Input buffer gain = 1
'                5         0 = Enable charge offset timer
'                4         0 = Connect output to BNC
'                3         0 = Output load #4, 200 ohm
'                2         0 = Output load #3, 100 ohm
'                1         0 = Output load #2, 2K ohm
'                0 LSB     0 = Output load #1, 1K ohm
'

'TE1013 Relay/Mux channel assignments
'Mux Channel   Assignment
'-----------   ----------
'     1        DUT +/-IN terminals
'     2        Vout
'     3        Supply current sense resistor
'     4        Vout, post 10KHz filter
'     5        Sample/Hold output
'     6        NC
'     7        Supply voltage
'     8        NC

'
'
'
'
'
'
'
'
'
'
'
'
'
'
'
'
'
'
'
'
'
'
'
'
'
'TE1037 Relay/Mux channel assignments
'Mux Channel   Assignment
'-----------   ----------
'     1        DUT +/-IN terminals
'     2        Iout * Rload
'     3        Input current sense resistor
'     4        Output Noise, post 10KHz filter
'     5        Sample/Hold output
'     6        Sense lines, output load #4 (4-wire ohms)
'     7        Sense lines, input current sense resistor
'     8        Sense lines, output load #1 (4-wire ohms)

'****************** MAIN PROGRAM************************************
KEY(10) ON                              'Activates F10 key
ON KEY(10) GOSUB FINISH                 'Traps for F10 key Exits if pressed

CLS
TX% = 0                             ' cjt
FILENAME$ = "KDSCIOUT.ADR"           'Parallel port, mux & test head addresses
CALL GETADD(FILENAME$)              'Gets the Parallel port, Mux and Testhead address

CLS
LOCATE 5, 20: PRINT "Initializing test system.  Please wait."
LOCATE 6, 10: PRINT "VERSION:      "; VERSION$
LOCATE 7, 10: PRINT "SOURCE FILES: "; SOURCEFILES$
CALL CONTINUE
CLS
CALL INIT488(DVMADDR%)                  'Initialize the GPIB interface

'
'
'CALL CONTINUE

PSID% = GETPSID%
'



LOCATE 9, 20: PRINT "Initializing DVM..."
CALL WRITEDVM("*T2S1", 0)               'Initialize Fluke meter





LOCATE , 20: PRINT "Initializing relay mux..."
CALL LOADMUX(0, CON%)                   'Resets all mux's, independent of mux address

M% = MENU3%
SELECT CASE M%
	CASE 1
		TE$ = "TE1055"                  'Assign test head #, defines channel
		FILENAME$ = "KDSCIOUT.ADR"       'Parallel port, mux & test head addresses
										'used for DAC input.
		THIDSET% = 2
	CASE 2
		TE$ = "TE1054"                  'Assign test head #, defines channel
		FILENAME$ = "KDSCVOUT.ADR"       'Parallel port, mux & test head addresses
										'used for DAC input.
		THIDSET% = 3
	CASE 3
		GOTO MAINMENU
END SELECT
   
	PRINT
	PRINT TAB(20); "Initializing test head "; TE$; "..."
	CALL GETADD(FILENAME$)              'Gets the Parallel port, Mux and Testhead address

	THID% = GETTHID%                    'Get test head ID #
'    PRINT THID%
	IF THID% <> THIDSET% THEN
		'BEEP
		F% = F% + 1
		PRINT
		PRINT TAB(20); "SYSTEM STARTUP ERROR"
		PRINT TAB(20); "Test head "; TE$; " is not connected."
		PRINT TAB(20); "Check system setup."
		PRINT TAB(20); "Press space bar to continue or ESC to exit."
		DO
			I$ = KEYBDIN$       ' user response
			IF I$ = CHR$(27) THEN STOP
		LOOP UNTIL I$ = CHR$(27) OR I$ = " "
	ELSE
		IF THID% = 2 THEN                           'Don't know DUT input
			PRINT                                   'conversion resistor
			PRINT TAB(20); "Remove modules from test head."
			BEEP
			CALL CONTINUE
		END IF
		CB0VAL% = CB0VAL% AND &HDE                  '1 1 0 1  1 1 1 0
													'Sets relays K00, K05
		CALL SETTH(CB0VAL%, CB0%)
		CB1VAL% = &HFF
		CALL SETTH(CB1VAL%, CB1%)
		CB2VAL% = &H1F                              '0 0 0 1  1 1 1 1  K27..K20
		CALL SETTH(CB2VAL%, CB2%)
		PRINT TAB(20); "Initializing DAC..."
		CALL CALDAC(TE$)                    'Calibrate DAC
		IF TE$ = "ERROR" THEN
			'BEEP
			LOCATE , 20: PRINT " "
			LOCATE , 20: PRINT "SYSTEM STARTUP ERROR"
			LOCATE , 20: PRINT "DAC is not connected or is faulty."
			LOCATE , 20: PRINT "Check system setup and restart program."
			GOTO FINISH
		END IF
		CALL RESETTH
	END IF

IF F% = 1 THEN STOP                         'No test heads connected

PRINT TAB(20); "Initializing power supply..."
SL$ = SETPOWER$(PSADDR%, 999!, LOOPOFF$)       'Disable power supply

LOGDAT% = -1                            'Log linearity test data OFF
'
PON% = 0
DO                                      'Main program loop
	SN$ = ""                            'Reset serial number
	SEL% = MENU1%                       'Gets the # of the sub program
	SELECT CASE SEL%                    'Branches to the specific Tests
	   
'******************* Pre-Burn In Functional Test ***************************
		CASE 1
			TX% = 0				' cjt no print to file         
			CAL% = 2			'Measure o.s and gain, don't calibrate
			CALL GETSPECS		'Gets module test specs
			IF LEFT$(SPECS.MODNAME, 4) <> "EXIT" THEN
				CALL FUNCTEST(CAL%)
			END IF
			
'*********************** Pre-Encap Complete Test ******************************
		CASE 2
			TX% = 0					' cjt no print to file
			CAL% = 0				'Calibrate o.s. & gain.
			CALL GETSPECS				'Gets module test specs
			IF LEFT$(SPECS.MODNAME, 4) <> "EXIT" THEN

			CALL GENSET(GENAMPL!)	'Displays message to set signal gen.
				DO						'Complete test loop start
					ITERATION% = 1		'Sets # of times to repeat offset/gain cal
					CALL COMPTEST(STATUS$(), ITERATION%, CAL%, GENAMPL!, TSIM!(), OUTCALC!(), OUTMEAS!(), ERROROUT!(), ACCSTAT$(), TOTLRESPEC!) 'Performs complete set of tests
					CALL TSPECS(TSPEC$(), TOTLRESPEC!)	'Sets up a string with the test specifications
					WIDTH , 43                      	'Change screen lines to 43
					CALL REPORT(STATUS$(), TSPEC$())	'Prints data to screen
					CALL INTERLUDE
					WIDTH , 25	'Change screen lines to 25
				LOOP WHILE REPEAT$(SPECS.MODNAME) <> "N" 'End of Complete Test loop
				IF SENSORNUM% = 1 AND (SPECS.MAXIN > 10 OR SPECS.MININ < -10) THEN
					PRINT
					PRINT TAB(5); "Insert the yellow connector into the meter +/-SENSE terminals."
					CALL CONTINUE
				END IF
			END IF
			
'**************** Post-Encap Complete Test  ***********************************
		CASE 3
			CALL HARDCOPY                           'Ask for harcopy printout (PWR)
			TX% = 1									' cjt print to file
			CAL% = 0								'calibrate o.s & gain
			CALL GETSPECS							'Gets module test specs
			IF LEFT$(SPECS.MODNAME, 4) <> "EXIT" THEN
				CALL GENSET(GENAMPL!)				'Displays message to set signal generator
				CALL GETSN(SN$)						'Gets module serial #
				CALL WORKORDERHEADER (SN$)			'Write header to work order status file
				DO									'Start of Test loop
					IF SPECS.OUTSIGTYPE$ = "CURRENT" AND SPECS.MINOUT < .001 THEN
						ITERATION% = 2	'o.s. calibrated at +2.5% span results in interactive adjustments
					ELSE
						ITERATION% = 1	'Sets # of times to repeat offset/gain cal non-interactive adjustments
					END IF
					CALL HEADERA(SN$)	'Prints data sheet header on printer
					CALL COMPTEST(STATUS$(), ITERATION%, CAL%, GENAMPL!, TSIM!(), OUTCALC!(), OUTMEAS!(), ERROROUT!(), ACCSTAT$(), TOTLRESPEC!) 'Performs complete set of tests
					CALL TSPECS(TSPEC$(), TOTLRESPEC!)	'Creates a string of test parameters
					WIDTH , 43							'Change screen lines to 43
					CALL REPORT(STATUS$(), TSPEC$())	'Displays test results
					CALL FOOTER(STATUS$())            	'Prints footer if no fails
					'CALL INTERRUPT(5, inreg, outreg)	'Prints test data
					'Log test results to data log, datasheet and work order status files
					CALL LOGMODFILES(STATUS$(), SN$, TSIM!(), OUTCALC!(), OUTMEAS!(), ERROROUT!(), ACCSTAT$(), TSPEC$(), TOTLRESPEC!, TX%)
					'CALL INTERLUDE
					WIDTH , 25	'Change screen lines to 25
				LOOP WHILE REPEAT2$(SPECS.MODNAME, SN$) <> "N"  'End of test loop
				CLS
				PRINT TAB(10); "Printing work order status file: "; WO$ + ".TXT
				CALL WORKORDERPRINT (SN$)	'Print the work order status file after "N" (not testing more of same module type)
				IF SENSORNUM% = 1 AND (SPECS.MAXIN > 10 OR SPECS.MININ < -10) THEN
					PRINT
					PRINT TAB(5); "Insert the yellow connector into the meter +/-SENSE terminals."
					CALL CONTINUE
				END IF
			END IF
		   
'**************** Sealed Module Re-Test  ***********************************
		CASE 4
			CALL HARDCOPY                           'Ask for hardcopy printout (PWR)
			TX% = 1									' cjt print to file
			CAL% = 2								'Check o.s & gain
			'CAL% = 4                                'Check o.s & gain (4 or 2?: input modules used "4")
			CALL GETSPECS							'Gets module test specs
			IF LEFT$(SPECS.MODNAME, 4) <> "EXIT" THEN
				CALL GENSET(GENAMPL!)               'Displays message to set signal generator
				CALL GETSN(SN$)                     'Gets module serial #
				CALL WORKORDERHEADER (SN$)			'Write header to work order status file
				DO                                  'Start of Test loop
					ITERATION% = 0                  'Sets # of times to repeat offset/gain cal
					CALL HEADERA(SN$)               'Prints data sheet header on printer
					CALL COMPTEST(STATUS$(), ITERATION%, CAL%, GENAMPL!, TSIM!(), OUTCALC!(), OUTMEAS!(), ERROROUT!(), ACCSTAT$(), TOTLRESPEC!) 'Performs complete set of tests
					CALL TSPECS(TSPEC$(), TOTLRESPEC!) 'Creates a string of test parameters
					WIDTH , 43                      'Change screen lines to 43
					CALL REPORT(STATUS$(), TSPEC$()) 'Displays test results
					CALL FOOTER(STATUS$())            	'Prints footer if no fails
					'CALL INTERRUPT(5, inreg, outreg)  'Prints test data
					'Log test results to data log, datasheet and work order status files
					CALL LOGMODFILES(STATUS$(), SN$, TSIM!(), OUTCALC!(), OUTMEAS!(), ERROROUT!(), ACCSTAT$(), TSPEC$(), TOTLRESPEC!, TX%)
					'CALL INTERLUDE
					WIDTH , 25                      'Change screen lines to 25
				LOOP WHILE REPEAT2$(SPECS.MODNAME, SN$) <> "N"  'End of test loop
				CALL WORKORDERPRINT (SN$)	'Print the work order status file after "N" (not testing more of same module type)
				IF SENSORNUM% = 1 AND (SPECS.MAXIN > 10 OR SPECS.MININ < -10) THEN
					PRINT
					PRINT TAB(5); "Insert the yellow connector into the meter +/-SENSE terminals."
					CALL CONTINUE
				END IF
			END IF
		  
'***********************Individual Test Routines******************************
		CASE 5
			TX% = 0                                 ' cjt no print to file
			DO                                      'Start of loop
				ITERATION% = 1                      'Sets # of times to repeat offset/gain cal
				SEL% = MENU2%                       'Gets # of test
				IF SEL% <> 16 THEN                  'Checks for exit
					IF SEL% = 14 THEN               'Checks for printer toggle
						IF PON% = 1 THEN 
							PON% = 0
						ELSE
							PON% = 1                					'Toggles printer status
						END IF 
					ELSEIF SEL% = 13 THEN
						CALL NOTES                  'Print ATE notes
					ELSEIF SEL% = 15 THEN
						LOGDAT% = -LOGDAT%
					ELSEIF SEL% = 17 THEN
						CALL ONESHOTTEST
					ELSE
					   CALL GETSPECS                'Gets module specs
					   IF LEFT$(SPECS.MODNAME, 4) <> "EXIT" THEN
						   CALL INDIVID(SEL%, ITERATION%)'Performs individual test
					   END IF
					END IF
				END IF
			LOOP UNTIL SEL% = 16                    'End of loop
			
'*************************** End Program Selection *********************************

		CASE 6
			ENDPROG% = 1                            'Sets end program flag
   END SELECT
LOOP WHILE ENDPROG% <> 1                            'Loops until end program flag set


FINISH:
	'Exit program from F10 keypress (PWR)
	CLS ' CJT
	SL$ = SETPOWER$(PSADDR%, 999!, LOOPOFF$)               'Turn off loop power
	SL$ = SETPOWER$(PSADDR%, 999!, "LOC")                  'Return to local mode
	SL$ = SETPOWER$(PSADDR%, 999!, "ZER")                  'Clear the error queue
	   
	IF SENSORNUM% = 1 AND (SPECS.MAXIN > 10 OR SPECS.MININ < -10) THEN
		SL$ = SETPOWER$(VINADDR%, 999!, LOOPOFF$)          'Turn off loop power
		SL$ = SETPOWER$(VINADDR%, 999!, "LOC")             'Return to local mode
		SL$ = SETPOWER$(VINADDR%, 999!, "ZER")             'Clear the error queue
	   
	END IF
	CHAIN "C:\ATE\MENUX"
END                                                 'End of program
'*@*@*@*@*@*@*@*@*@*@*@**@*@*@*@*@*@*@*@*@*@*@*@*@*@*@*@*@*@*@*@**@*@*@*@*@*@

MAINMENU:
	CHAIN "C:\ATE\MENUX"
END
'*******************************

'*******************************
SUB COMPTEST (STATUS$(), ITERATION%, CAL%, GENAMPL!, TSIM!(), OUTCALC!(), OUTMEAS!(), ERROROUT!(), ACCSTAT$(), TOTLRESPEC!)
		   
	MINOUT! = SPECS.MINOUT
	OUTTYPE$ = SPECS.OUTSIGTYPE
	SNM% = SENSORNUM%
	
	IF SNM% = 2 THEN                                'Check if current input
		CALL WRITEDVM("S0", 0)                      'Slow reading rate
		IINSEN.MEAS! = MEASRES!(IINSEN!, 2, TE$)    'Measure current source sense R
		CALL WRITEDVM("S1", 0)                      'Medium reading rate
	
	
	
	ELSE
		IINSEN.MEAS! = 1
	END IF
	  
	FOR X = 1 TO 18                                 'Clear test result array
		STATUS$(X) = ""
	NEXT X

	IF SPECS.ISMAXFL <> 0 THEN
		MODILIM! = SPECS.ISMAXFL
	ELSE
		MODILIM! = SPECS.ISMAXNL
	END IF

	CALL INITPS(PSADDR%, MODILIM! * PSILIMIT!, OVERV!)
	SL$ = SETPOWER$(PSADDR%, 24!, FUNC$)                   'Set Vsupply to 24.0V
	STATUS$(1) = SUPPLYI$(0)                        'Supply current test
													'No excitation load
'
'
'
'
'
	IF LEFT$(STATUS$(1), 4) = "FAIL" THEN
		SL$ = SETPOWER$(PSADDR%, 999!, LOOPOFF$)           'Disable power supply
		EXIT SUB                                    'Exit on over current
	END IF
   
	IF OUTTYPE$ = "VOLTAGE" THEN                    'Check if voltage out
		STATUS$(2) = SUPPLYI$(1)                    'Supply current test
	ELSE                                            'Max output load
		STATUS$(2) = "PASS"
	END IF
														
	IF LEFT$(STATUS$(2), 4) = "FAIL" THEN EXIT SUB  'Exit on over current    
	POSI% = 1                                       'Test positive current limit
	IF OUTTYPE$ = "VOLTAGE" THEN

			STATUS$(9) = ILIMIT$(POSI%)
		ELSE
			STATUS$(9) = "PASS"
		END IF
	IF OUTTYPE$ = "VOLTAGE" AND MINOUT! <> 0 THEN
		POSI% = 0                                   'Test negative current limit
		STATUS$(10) = ILIMIT$(POSI%)
	ELSE
		STATUS$(10) = "PASS"
	END IF
	   
	STATUS$(11) = "PASS" 'OVERRANGE$                'Test module overrange
	STATUS$(12) = SUPPLYSEN$                        'Power supply sensitivity
	STATUS$(13) = "PASS"
	STATUS$(14) = FREQRESP$(GENAMPL!)               'Tests Frequency Response
	STATUS$(15) = "PASS"
	STATUS$(16) = OUTNOISE$                         'Test Output Noise
   
	IF OUTTYPE$ = "CURRENT" THEN                    'Max load resistance test
		STATUS$(17) = IOUTMAXL$(1)
		IF LEFT$(SPECS.MODNAME, 9) = "DSCA39-07" THEN
			STATUS$(18) = IOUTMAXL$(0)
		END IF
	ELSE
		STATUS$(17) = "PASS"
		STATUS$(18) = "PASS"
	END IF
		 
	WHILE ITERATION% > 0                            'Offset/Gain calibration loop
	
		A$ = OFFSETCAL$(CAL%)                       'Offset calibration
		A$ = GAINCAL$(CAL%)                         'Gain calibration
		ITERATION% = ITERATION% - 1

	WEND                                            'Tests for Offset/Gain calibration end

	FOR T% = 1 TO 3
		SELECT CASE T%
			CASE 1
				OUTLOAD! = SPECS.LOAD3
				STATUS$(3) = LINTEST$(ACC$, OUTLOAD!, TSIM!(), OUTCALC!(), OUTMEAS!(), ERROROUT!(), ACCSTAT$())           'Performs linearity and
													'accuracy tests
				STATUS$(4) = ACC$                   'Assigns results of accuracy test
			CASE 2
				IF OUTTYPE$ = "VOLTAGE" THEN
					OUTLOAD! = SPECS.LOAD2
					STATUS$(5) = LINTEST$(ACC$, OUTLOAD!, TSIM!(), OUTCALC!(), OUTMEAS!(), ERROROUT!(), ACCSTAT$())           'Performs linearity and
													'accuracy tests
					STATUS$(6) = ACC$               'Assigns results of accuracy test
				ELSE
					STATUS$(5) = "PASS"
					STATUS$(6) = "PASS"
				END IF
			CASE 3
				IF OUTTYPE$ = "VOLTAGE" THEN
					OUTLOAD! = SPECS.LOAD1
					STATUS$(7) = LINTEST$(ACC$, OUTLOAD!, TSIM!(), OUTCALC!(), OUTMEAS!(), ERROROUT!(), ACCSTAT$())           'Performs linearity and
													'accuracy tests
					STATUS$(8) = ACC$               'Assigns results of accuracy test
				ELSE
					STATUS$(7) = "PASS"
					STATUS$(8) = "PASS"
				END IF
		END SELECT
	NEXT
  
	SL$ = SETPOWER$(PSADDR%, 999!, LOOPOFF$)               'Disable power supply
	  
	CALL INTERLUDE  
END SUB

'*******************************
FUNCTION FAILS% (STATUS$())

	FAILS% = 0
	   
	'FOR X = 1 TO 18
	FOR X = 1 TO MAXSTATUSINDEX
		IF LEFT$(STATUS$(X), 1) = "F" THEN        'Tests for failed tests
			FAILS% = 1
		END IF
	NEXT

END FUNCTION

'*******************************
SUB FOOTER (STATUS$())
	'Sub to create footer (if there are no fails). Removed code to print footer to file
	'since that is now accomplished by the "DATASHEETWRITE" sub. The datasheet can still
	'be sent to two places (screen and printer), so when updating remember to make any
	'changes to both the "PRINT" and "LPRINT" sections.
	'
	'NOTE:	Currently, the footer is only printed to the printer, not the screen.
	'
	OUTTYPE$ = SPECS.OUTSIGTYPE
   
	'When updating remember to make the changes to all sections.
	IF FAILS%(STATUS$()) <> 1 THEN

'		'Send to screen.
'		PRINT TAB(5); "240VAC Withstand"; TAB(71); "PASS"
'		PRINT TAB(5); "Hi-Pot"; TAB(71); "PASS"
'		IF OUTTYPE$ = "CURRENT" THEN
'			PRINT
'			PRINT TAB(5); "Standard output load for test is 250 ohms."
'		END IF
'		
'		PRINT TAB(5);
'		FOR X = 5 TO 75
'			PRINT "_";
'		NEXT
'		PRINT
'		PRINT TAB(35); "Check List"
'		PRINT
'		PRINT TAB(5); "Tested by:    _______________"; TAB(45); "QC:             _______________"
'		PRINT
'		PRINT TAB(5); "It is hereby certified that the above product is in conformance with"
'		PRINT TAB(5); "all requirements to the extent specified.  This product is not"
'		PRINT TAB(5); "authorized or warranted for use in life support devices and/or systems."
'		PRINT
'		PRINT TAB(5); "* NIST traceable calibration certificates support Measured Value data."
'		PRINT TAB(5); "  Calibration services are available through ANSI/NCSL Z540-1 and"
'		PRINT TAB(5); "  ISO Guide 25 Certified Metrology Labs."
	 
		'Send to printer
		IF PON% = 1 THEN
			LPRINT TAB(5); "240VAC Withstand"; TAB(71); "PASS"
			LPRINT TAB(5); "Hi-Pot"; TAB(71); "PASS"
   
			IF OUTTYPE$ = "CURRENT" THEN
				LPRINT
				LPRINT TAB(5); "Standard output load for test is 250 ohms."
			END IF
		   
			LPRINT TAB(5);
			FOR X = 5 TO 75
				LPRINT "_";
			NEXT
			LPRINT
			LPRINT TAB(35); "Check List"
			LPRINT
			LPRINT TAB(5); "Tested by:    _______________"; TAB(45); "QC:             _______________"
			LPRINT
			LPRINT TAB(5); "It is hereby certified that the above product is in conformance with"
			LPRINT TAB(5); "all requirements to the extent specified.  This product is not"
			LPRINT TAB(5); "authorized or warranted for use in life support devices and/or systems."
			LPRINT
			LPRINT TAB(5); "* NIST traceable calibration certificates support Measured Value data."
			LPRINT TAB(5); "  Calibration services are available through ANSI/NCSL Z540-1 and"
			LPRINT TAB(5); "  ISO Guide 25 Certified Metrology Labs."
		END IF
	END IF
	IF PON% = 1 THEN LPRINT CHR$(12)   'Send form feed to printer (whether a passing or failing module)
END SUB

'*******************************
FUNCTION FREQRESP$ (GENAMPL!)

	MININ! = SPECS.MININ                        'Determine min in
	MAXIN! = SPECS.MAXIN                        'Determine max in
'
	INRANGE! = MAXIN! - MININ!
	ORANGE! = SPECS.MAXOUT - SPECS.MINOUT       'V or A
	SPEC! = SPECS.ATTEN
	SPECTOL! = SPECS.ATTENTOL
	SNM% = SENSORNUM%
	  
	CLS
	TESTTITLE$ = "Frequency Response Test"
	CALL HEADERB(TESTTITLE$)
	LOCATE 10, 10: PRINT "Testing, please wait."
	   
	IF SNM% = 2 THEN                            'mA in
		SCALE% = 1000
	ELSE
		SCALE% = 1
	END IF

	XFERFN! = ORANGE! / (INRANGE! / SCALE%)  'V/A or A/A

	VAC! = SETAC!(CON%, GENAMPL!)               'Turns on AC input
		
	CALL LOADMUX(2, CON%)                       'Turns on mux
	CALL WRITEDVM(VOAC$, (10 ^ (SPEC! / -20) * VAC! / IINSEN.MEAS! * XFERFN!) * IOUTSEN.MEAS! * 2)'Sets up Meter (Autorange)
   
	OUTMEAS! = READDVM!(.0002 * ORANGE!) / IOUTSEN.MEAS! 'Reads Meter to +/-0.02% span
   
	IF OUTMEAS! > .0002 THEN
		OUTMEAS! = SQR(OUTMEAS! ^ 2 - .0002 ^ 2)  'Account for meter zero-input error
	END IF                                        'See output noise test for details
	   
	CALL LOADMUX(2, COFF%)                      'Turns off mux
												'Calculate attenuation
												'
												'
												'
	ATTEN! = -20 * (LOG(OUTMEAS! / (VAC! / IINSEN.MEAS! * XFERFN!)) / LOG(10))
	   
	IF ABS(ATTEN! - SPEC!) <= SPECTOL! THEN
		ATTEN$ = "PASS"
	ELSE
		ATTEN$ = "FAIL"
		SOUND 1000, .5
	END IF

	LOCATE 10, 10: PRINT "Status: "; 
	
	IF  LEFT$(ATTEN$, 4) = "FAIL" THEN COLOR 12, 0
	PRINT ATTEN$;
	COLOR 11, 0
	
	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$(ATTEN!) + "1"
	VAC! = SETAC(COFF%, GENAMPL!)                   'Turns off AC input
	   
	CALL PAUSE(1!)
	   
END FUNCTION

'*******************************
SUB FUNCTEST (CAL%)
	   
	'Sequence of tests to determine if main module circuits are functional
	   
	MAXOUT! = SPECS.MAXOUT                          'V or A
	SNM% = SENSORNUM%

	IF SNM% = 2 THEN                                'Check if current input
		IINSEN.MEAS! = IINSEN!
	ELSE
		IINSEN.MEAS! = 1
	END IF                                          'sensor type.

	 IF SPECS.ISMAXFL <> 0 THEN
		MODILIM! = SPECS.ISMAXFL
	ELSE
		MODILIM! = SPECS.ISMAXNL
	END IF
'        CALL INITPS(PSADDR%, MODILIM! * PSILIMIT!, OVERV!)
	DO
		SL$ = SETPOWER$(PSADDR%, 24!, FUNC$)                       'Set Vsupply to 24.0V
		CONFLAG% = 0
		FOR FTEST% = 1 TO 2
			SELECT CASE FTEST%
				CASE 1
					IF LEFT$(SUPPLYI$(0), 4) = "FAIL" THEN  'Check supply current
						CALL CONTINUE
						CONFLAG% = 1                        'Sets exit flag
					END IF
				CASE 2
				
					IF LEFT$(OFFSETCAL$(CAL%), 4) = "FAIL" THEN
						CALL CONTINUE           'Checks for offset error < 8% span
						CONFLAG% = 1
					END IF
							  
					IF LEFT$(GAINCAL$(CAL%), 4) = "FAIL" THEN
						CALL CONTINUE           'Checks for Gain error < 8% span
						CONFLAG% = 1
					END IF

'               CASE 3

			END SELECT
			IF CONFLAG% <> 0 THEN EXIT FOR
		NEXT
		   
		SL$ = SETPOWER$(PSADDR%, 999!, LOOPOFF$)           'Disable power supply
'
		
	LOOP WHILE REPEAT$(SPECS.MODNAME) <> "N"        'Repeat for another module
	   
	CALL RESETTH                                    'Reset the test head

END SUB

'*******************************
FUNCTION GAINCAL$ (CAL%)        ' THIS SHOULD USE A UNIVERSAL FUNCTION GAINCAL$(FAMILY, CAL%, ETC OR PASS A PTR TO A STRUCT)
	   
	'Calibrate module gain
	'Inputs;        calibration flag
	'               measured contact resistance (used for resistance in only)
	 
	SENSOR$ = SPECS.SENTYPE
	MAXIN! = SPECS.MAXIN
	MININ! = SPECS.MININ
	MINOUT! = SPECS.MINOUT                  'V or A
	MAXOUT! = SPECS.MAXOUT
	INITTOL! = 8                            'Uncalibrated gain tolerance (%)
	CALTOL! = SPECS.CALTOL                  'Calibration tolerance (%)
	OSCALIN! = SPECS.OSCALIN                'Module input for offset calibration
	GNCALIN! = SPECS.GNCALIN                'Module input for gain calibration
'
'
	ORANGE! = MAXOUT! - MINOUT!             'Output range (V or A)
	CALSPAN! = MODULEOUT!(GNCALIN!) - MODULEOUT!(OSCALIN!) 'Adjustment range is +/- x% of +f.s.
	OUTTYPE$ = SPECS.OUTSIGTYPE
	SNM% = SENSORNUM%
   
	IF SNM% = 2 THEN
		SCALE% = 1000
	
	ELSE
		SCALE% = 1
	END IF

	INRANGE! = (MAXIN! - MININ!) / SCALE% * IINSEN.MEAS!
   
	IF OUTTYPE$ = "CURRENT" AND ABS(MINOUT!) = MAXOUT! THEN 'Bipolar current output
		CALGN! = .00025       'Calibrate @ +0.025% span to correct for nonlinearity
																'@ -f.s. output.
	END IF

	CLS
	TESTTITLE$ = "Gain Calibration"
	CALL HEADERB(TESTTITLE$)
	CALL MODOUTLINE
	LOCATE 7
	PRINT TAB(44); CHR$(24);
	PRINT TAB(35); "GAIN POTENTIOMETER";
   
	IF CAL% = 2 THEN
		GERROR! = INITTOL!                  'Gain tolerance for func. test (%)
		TOL! = .0001 * ORANGE!              'Measurement tolerance, 1 pass test
	ELSE
		GERROR! = CALTOL!                   'Gain calibration tolerance (%)
		TOL! = 0!                           'Measurement tolerance, continuous test
	END IF

	LOCATE 10, 10: PRINT "Observe that the LED between the potentiometers is ON."
	BEEP: BEEP: BEEP
	LOCATE 20, 5: PRINT "After you have verified the LED is ON, you may hit any key to continue."
	CALL CONTINUE
	
	DO
		TIME1! = TIMER                              'Start timer
		LOCATE 12, 10: PRINT TAB(10); "Measuring input.  Please wait..."
		MAXIN! = GNCALIN!                           'Calc. maxin volts
		IF SNM% = 2 THEN
			MAXINMEAS! = SETCURRENTIN!(MAXIN!)
		ELSE

		
				CALL SETDAC2(MAXIN!, 1, 0)              'Sets DAC
				IF CAL% = 0 OR MAXIN! > 10! THEN        'Always measure high voltage
														'input to get exact setting
					CALL WRITEDVM("S0", 0)              'Slow reading rate for max accuracy
					CALL LOADMUX(1, CON%)
					CALL WRITEDVM(VODC$, MAXIN!)        'Sets meter to read
					CALL PAUSE(2!)
					MAXINMEAS! = READDVM!(INRANGE! * MAXACCERR! / 10!) '10x better than accuracy

					CALL LOADMUX(1, COFF%)
				ELSE
					MAXINMEAS! = MAXIN!
'
			END IF
		END IF
		   
		CALL WRITEDVM("S1", 0)                          'Medium reading rate

		   OUTCALC! = MODULEOUT!(MAXINMEAS!) + CALGN! * CALSPAN!
			 
		CALL LOADMUX(2, CON%)                           'Measure Vout
'
		CALL WRITEDVM(VODC$, OUTCALC! * IOUTSEN.MEAS!)
	 
		LOCATE 12, 10: PRINT "Gain Error =  ";
		PRINT SPC(20);
		   
		L% = 0

		DO
			L% = L% + 1
			ERROROUT! = (READDVM!(TOL!) / IOUTSEN.MEAS! - OUTCALC!) / CALSPAN! * 100! 'Error (%)

			LOCATE 12, 24
			PRINT USING "+###.### %"; ERROROUT!
			 
			LOCATE 15, 10
			IF CAL% > 0 THEN
				CONREADS% = 5
			ELSEIF ABS(ERROROUT!) < GERROR! THEN
				SOUND 400, .5
				PRINT "Stop turning gain potentiometer          "
				CONREADS% = CONREADS% + 1
			ELSEIF ERROROUT! < 0 THEN
				PRINT "Turn gain potentiometer clockwise        "
				CONREADS% = 0
			ELSE
				PRINT "Turn gain potentiometer counter-clockwise"
				CONREADS% = 0
			END IF

			LOCATE 20, 10
			A$ = INKEY$
			IF UCASE$(A$) = "T" THEN
				PRINT TAB(10); "Trouble Shooting Mode"
				CALL WRITEDVM("*", OUTCALC! * IOUTSEN.MEAS!)
				CALL CONTINUE
'
				LOCATE 20, 10: PRINT SPC(22);
			ELSEIF UCASE$(A$) = "C" THEN
				CONREADS% = 5
			END IF
													're-measure input
													'once every 40 cycles
			CALL PAUSE(.2)                          'Slow down loop
		LOOP WHILE CONREADS% < 5 AND L% < 400
		CALL LOADMUX(2, COFF%)
		
	LOOP WHILE CONREADS% < 5
	   
	IF ABS(ERROROUT!) <= GERROR! THEN
		GN$ = "PASS"
	ELSE
		GN$ = "FAIL"
		SOUND 1000, .5
	END IF
			 
	LOCATE 15, 10
	PRINT "Status: "; 
	
	IF  LEFT$(GN$, 4) = "FAIL" THEN COLOR 12, 0
	PRINT GN$;
	COLOR 11, 0
	PRINT SPC(30);
	PRINT TAB(10); "Gain calibration error is";
	PRINT TAB(50); USING " +###.### %"; ERROROUT!
	PRINT TAB(10); "Max. gain calibration error is";
	PRINT TAB(50); USING "+/-##.### %"; GERROR!
	
	GAINCAL$ = GN$ + STR$(ERROROUT!)
   
	CALL RESETTH                                    'Reset the test head
	CALL PAUSE(1!)

END FUNCTION

SUB GENSET (GENAMPL!)

	MININ! = SPECS.MININ
	MAXIN! = SPECS.MAXIN                 'Convert to A

		
	RCONV! = SPECS.RCONV
	ATTEN! = SPECS.ATTEN
	SNM% = SENSORNUM%
	   
	IF SNM% = 2 THEN
		SCALE% = 1000
	ELSE
		SCALE% = 1
	END IF

	INRANGE! = (MAXIN! - MININ!) / SCALE% * IINSEN!
	ORANGE! = (SPECS.MAXOUT! - SPECS.MINOUT!) * IOUTSEN.MEAS! 'V
											   
	IF SNM% = 2 THEN
		SCALE% = 1000
		GENAMPL! = MAXIN! / SCALE% * (RCONV! + IINSEN!)
	ELSE
		SCALE% = 1
												'Set amplitude for > 5mV p-p signal out
		UNATTENOUT! = 10 ^ (ATTEN! / 20) * .005
		GENAMPL! = UNATTENOUT! / ORANGE! * INRANGE! * .9
		IF GENAMPL! < 5 THEN
			GENAMPL! = 4
		ELSE
			GENAMPL! = 8                            '10V and +5V offset can cause
													'clipping on 0-20mA output modules.
													'Leave some leeway in the setting.
		END IF
	END IF
												'Convert to round numbers
												'for ease of use
	GATTEN! = INRANGE! / GENAMPL!               'Attenuator gain
	GENOS! = (MININ! / SCALE% * IINSEN! + INRANGE! / 2!) / GATTEN!

		IF GENAMPL! < .5 * ABS(GENOS!) THEN         'GENAMPL! cannot be less
		   GENAMPL! = .5 * ABS(GENOS!)              'than 1/2 the offset.  Max
		END IF                                      'offset is 2x amplitude.
	IF GENAMPL! > 20 - 2 * ABS(GENOS!) THEN GENAMPL! = 20 - 2 * ABS(GENOS!)
 
 
	CLS
	LOCATE 5, 10: PRINT "Set the function generator as follows:"
	LOCATE 10, 27: PRINT "Function Generator Settings"
	LOCATE 11, 27: PRINT "---------------------------"
	LOCATE 12, 31: PRINT USING "Frequency: ##### Hz"; SPECS.TESTFREQ!
	LOCATE 13, 31: PRINT USING "Amplitude:  ##.# V p-p"; GENAMPL!
	LOCATE 14, 31: PRINT USING "DC Offset: +##.# V"; GENOS!
	CALL CONTINUE

END SUB

'*******************************
SUB GETADD (FILENAME$)
	   
	'LPT$ = parallel printer port used to control ATE
	'MUX$ = multiplexer box address
	'TESTHEAD$ = test head address
	'PS1ADDR$ = Power Supply #1 address (module power)
	'PS2ADDR$ = Power Supply #2 address (high voltage input to dut)
	'PSPORTADDR$ = Power Supply serial port address
	'DVM$ = DVM address

		OPEN "C:\ATE\ADDR\" + FILENAME$ FOR RANDOM AS #3 LEN = 19
		FIELD #3, 3 AS LPT$, 2 AS MUX$, 2 AS TESTHEAD$, 2 AS DPSPSADDR$, 2 AS DPSVINADDR$, 2 AS PSPORTADDR$, 2 AS DVM$, 2 AS ABCPSADDR$, 2 AS ABCVINADDR$
		GET #3
		LPTADDR% = VAL(LPT$)
		MUXADDR% = VAL(MUX$)
		THADDR% = VAL(TESTHEAD$)
		DPSPSADDR% = VAL(DPSPSADDR$)
		DPSVINADDR% = VAL(DPSVINADDR$)
		PSPORT% = VAL(PSPORTADDR$)
		DVMADDR% = VAL(DVM$)
		ABCPSADDR% = VAL(ABCPSADDR$)
		ABCVINADDR% = VAL(ABCVINADDR$)

		CLOSE #3
'PRINT LPTADDR%, MUXADDR%, THADDR%, DPSPSADDR%, DPSVINADDR%, PSPORT%, DVMADDR%, ABCPSADDR%, ABCVINADDR%
'CALL CONTINUE

END SUB

'*******************************
FUNCTION GETPSID%
CLS

LOCATE 3, 10: PRINT "Identifying power supplies connected to test system"

PRINT
'****** First, assume KEPCO DPS125 for PSMODEL$ string assignment.
	PSMODEL$ = "DPS125"

'****** KEPCO DPS VIN supply address for serial interface.
'        PSADDR% = 2

'****** Obtain Identification string from KEPCO DPS 125.

LOCATE 5, 20: PRINT "KEPCO DPS125 Power Supply: ";
	DPSPS$ = POWERIO$(DPSPSADDR%, "ID")

'****** Assign corresponding address obtained from address file dependent
'****** on if the Power supply is DPS125 or ABC125.
	IF DPSPS$ = "KEPCO DPS 125-0.5M" THEN
		PSADDR% = DPSPSADDR%    'this value should = 2
		
		LOCATE 5, 53: PRINT "Yes!"
		PSMODEL$ = "DPS125"
		CALL INITPS(DPSPSADDR%, PSILIMIT!, OVERV!)
		DPSPSFLAG! = 1!
	ELSE
		LOCATE 5, 53: PRINT "Not present"
		DPSPSFLAG! = 0!
		PSADDR% = 0!
	END IF

'****** Check for High Voltage Power Supply
	
	LOCATE 6, 20: PRINT "KEPCO DPS125 High-Voltage Input: ";
	DPSVIN$ = POWERIO$(DPSVINADDR%, "ID")

	IF DPSVIN$ = "KEPCO DPS 125-0.5M" THEN
		VINADDR% = DPSVINADDR%
		
		LOCATE 6, 53: PRINT "Yes!"
		PRINT
		CALL INITPS(DPSVINADDR%, VINILIMIT!, VINOVERV!)
		DPSVINFLAG! = 1!
	ELSE
		LOCATE 6, 53: PRINT "Not present"
		DPSVINFLAG! = 0!
		VINADDR% = 0!
	END IF

	IF DPSPSFLAG! = 0! AND DPSVINFLAG! = 0! THEN
		  
		PSMODEL$ = "ABC125"
		PRINT TAB(20); "KEPCO ABC-125 Power Supply: ";
		ABCPS$ = READGPIB$(ABCPSADDR%, "*IDN?")
			   IF LEFT$(ABCPS$, 14) = "KEPCO,ABC-1251" THEN
				PSADDR% = ABCPSADDR%
				CALL INITPS(ABCPSADDR%, PSILIMIT!, OVERV!)
				PRINT TAB(55); "Yes!"
			   ELSE
				PRINT TAB(55); "Not present"
			   END IF

		PRINT TAB(20); "KEPCO ABC-125 High-Voltage Input: ";
		ABCVIN$ = READGPIB$(ABCVINADDR%, "*IDN?")
			   IF LEFT$(ABCVIN$, 14) = "KEPCO,ABC-1251" THEN
				VINADDR% = ABCVINADDR%
				CALL INITPS(ABCVINADDR%, VINILIMIT!, VINOVERV!)
				PRINT TAB(55); "Yes!"
				PRINT
			   ELSE
				PRINT TAB(55); "Not present"
				PRINT
			   END IF
	END IF
			 
		IF PSADDR% = 0! AND VINADDR% = 0! THEN
			PRINT TAB(15); ">>>>>>>> No Power Supplies are connected <<<<<<<<"
			CALL CONTINUE
		END IF
	  
	GETPSID% = 99      'This has yet to be utilized.  At this time there is
			   'no requirement to pass anthing back to function call
CALL PAUSE(1)
END FUNCTION

'*****************************************
SUB GETSPECS            ' THIS ONE SHOULD BE CHANGED TO SUB GETSPECS CALL GETSPECS(TE$=FAMILY) END SUB
	  
	DIM POINTER%(1000)

	CALL SORTDB(ENDFLAG%)
	IF ENDFLAG% = 1 THEN
		SPECS.MODNAME = "EXIT"
		EXIT SUB
	END IF
	 
	CLS
	LOCATE 3, 30: PRINT " Model Selection Menu "
	LOCATE 4, 30: PRINT " -------------------- "
	LOCATE 5, 30: PRINT " "
 
	YINIT% = CSRLIN                 'Initialize starting rows
 
	OPEN "C:\ATE\DSCDATA\DBSORT2.DAT" FOR RANDOM AS #2 LEN = LEN(SORTDATA1)
  
	DO
		I% = I% + 1
		GET #2, I%, SORTDATA1
		IF SORTDATA1.RECNUM <> -1 THEN NUMRECORD! = NUMRECORD! + 1
	LOOP WHILE SORTDATA1.RECNUM <> -1

	SCRNCTR% = 40                   'display center
	NUMCHAR% = 19                   '13 char + 4 char for # + 2 spaces
	NUMLINES! = 16                  '# of lines for model display below header
	NUMCOLUMNS! = 1 + INT(NUMRECORD! / NUMLINES!)
									'Model # length = 20 characters max
									'-> 3 columns max per screen
									'3 columns x 26 char = 78 spaces
	I% = 1
 
	FOR C% = 0 TO NUMCOLUMNS! - 1
		Y% = YINIT%
		TB% = SCRNCTR% - NUMCHAR% / 2 * NUMCOLUMNS! + NUMCHAR% * C%
		DO
			GET #2, I%, SORTDATA1
			IF SORTDATA1.RECNUM <> -1 THEN
				POINTER%(I%) = SORTDATA1.RECNUM
				LOCATE Y%, TB%
				PRINT USING "##.) &"; I%; SORTDATA1.MODNAME
				I% = I% + 1
				Y% = Y% + 1
			END IF
		LOOP WHILE SORTDATA1.RECNUM <> -1 AND Y% - YINIT% < NUMLINES!

	NEXT C%
 
	LOCATE Y%, TB%
	PRINT USING "##.) Exit"; I%

	CLOSE #2

	DO
		LOCATE 23, SCRNCTR% - NUMCHAR% / 2 * NUMCOLUMNS!
		PRINT "Enter Selection ";
		INPUT SEL%
	LOOP WHILE SEL% < 1 OR SEL% > I%

	IF SEL% = I% THEN
		SPECS.MODNAME = "EXIT"
	ELSE
		OPEN "C:\ATE\DSCDATA\DSCOUT.DAT" FOR RANDOM AS #1 LEN = LEN(SPECS)
		GET #1, POINTER%(SEL%), SPECS
		CLOSE #1
		IF SENSORNUM% = 1 AND (SPECS.MAXIN > 10 OR SPECS.MININ < -10) THEN
			CLS
			
			LOCATE 10, 5
			BEEP
			PRINT "This module requires high voltage input."
			PRINT
			PRINT TAB(5); "Turn on the high voltage power supply and connect it to the test head."
			PRINT
			PRINT TAB(5); "DO NOT PROBE THE CIRCUITRY DURING TEST"
			PRINT
			PRINT TAB(5); "Remove the yellow connector from the meter +/-SENSE terminals."
			CALL CONTINUE

			END IF
		IF SPECS.OUTSIGTYPE = "CURRENT" THEN
			CALL SETOUTLOAD(IOUTSEN!)           'Set current output sense resistor
												'Connect CH2 to output
												'Measure current output sense resistor
			CALL WRITEDVM("S0", 0)              'Slow reading rate
			IOUTSEN.MEAS! = MEASRES!(IOUTSEN!, 3, TE$)
			CALL WRITEDVM("S1", 0)
		ELSE
			CALL SETOUTLOAD(1E+10)              'Connect CH2 to output
			IOUTSEN.MEAS! = 1
			
		END IF
	END IF


	SEL% = POINTER%(SEL%)       'pass back to main code for use in SAVEDATA
   
END SUB

'*******************************
SUB HEADERA (SN$)

	IF PON% = 1 THEN
	LPRINT TAB(5); "DATAFORTH CORPORATION"; TAB(51); "Phone: (520) 741-1404"
	LPRINT TAB(5); "3331 E. Hemisphere Loop"; TAB(51); "Fax:   (520) 741-0762"
	LPRINT TAB(5); "Tucson, AZ  85706  USA"; TAB(51); "email: info@dataforth.com"
	LPRINT
	LPRINT TAB(33); "TEST DATA SHEET"
	LPRINT TAB(5);
	FOR X = 5 TO 75
		LPRINT "~";
	NEXT
	LOCATE 8
	LPRINT TAB(5); "Date: "; DATE$
	LPRINT TAB(5); "Model: "; SPECS.MODNAME
	LPRINT TAB(5); "SN: "; TAB(12); SN$
	LPRINT

	END IF
	
END SUB

'*******************************
SUB HEADERB (TESTTITLE$)
	   
	'print test title, model under test, and sn at top of screen
	   
	TITLEN = LEN(TESTTITLE$) + LEN(SPECS.MODNAME)
	PRINT TAB(40 - TITLEN / 2); SPECS.MODNAME; " "; TESTTITLE$;
	IF SN$ <> "" THEN
		PRINT TAB(66); "SN: "; SN$      'print sn if available
	ELSE
		PRINT
	END IF
	LOCATE , 40 - TITLEN / 2
	FOR L = 1 TO TITLEN + 1                 'underline test title
		PRINT "-";
	NEXT
	PRINT

END SUB

'*******************************
FUNCTION ILIMIT$ (POSI%)

'   Test the output current limit
'   Inputs: POSI%, indicates positive or negative current limit

	MAXOUT! = SPECS.MAXOUT
	MODLOAD! = SPECS.LOAD3 * 2                      'Set load value (mA)
	IOUTLIM! = SPECS.ILIMIT
	OUTTYPE$ = SPECS.OUTSIGTYPE
   
	IF POSI% = 1 THEN
		MODIN! = SPECS.MAXIN
		MODOUT! = SPECS.MAXOUT                      'V or A
		IF OUTTYPE$ = "VOLTAGE" THEN
			BIDIR$ = "Positive"
		END IF
		POL% = 1
	ELSE
		MODIN! = SPECS.MININ
		MODOUT! = SPECS.MINOUT                      'V or A
		BIDIR$ = "Negative"
		POL% = -1
	END IF
	   
	IMAXOUT! = SPECS.ILIMIT * POL%

	CLS
	TESTTITLE$ = BIDIR$ + " Current Limit Test"
	CALL HEADERB(TESTTITLE$)
	LOCATE 10, 10
	IF OUTTYPE$ = "CURRENT" THEN
		PRINT "This test is not performed on model "; SPECS.MODNAME
		EXIT FUNCTION
	ELSE
		PRINT "Testing, please wait."
	END IF
	  
	IF OUTTYPE$ = "CURRENT" THEN
		RL! = IOUTSEN.MEAS!
	ELSE                                            'Voltage output modules
		RL! = 1 / (1 / 2000 + 1 / 1000 + 1 / 200 + 1 / 100)'MEASRES!(MODLOAD!, 5, TE$)
		CALL SETOUTLOAD(RL!)                        'Sets load to 61 ohms
		CALL SETDAC2(MODIN!, 1, 0)                  'Sets DAC for + or - f.s. in
	END IF

	CALL WRITEDVM(VODC$, MAXOUT! * IOUTSEN.MEAS!)
	CALL LOADMUX(2, CON%)

	MEASILIM! = READDVM!(.0005) / RL! * 1000!       'measured current limit (mA)
	 
	IF ABS(MEASILIM!) > ABS(IOUTLIM!) THEN
		IL$ = "FAIL"
		SOUND 1000, .5
	ELSE
		IL$ = "PASS"
	END IF

	LOCATE 10, 10: PRINT "Status: "; 
	
	IF  LEFT$(IL$, 4) = "FAIL" THEN COLOR 12, 0
	PRINT IL$;
	COLOR 11, 0
	
	PRINT SPC(20);
	PRINT TAB(10); USING "Measured & current limit = ####.# mA"; BIDIR$; MEASILIM!
	PRINT TAB(10); USING "Maximum & current limit = ###.# mA"; BIDIR$; IOUTLIM! * POL%
	   
	ILIMIT$ = IL$ + STR$(MEASILIM!) + "1"       'append # decimal places
	  
	IF OUTTYPE$ = "VOLTAGE" THEN                'Remove output load, DSCA49
		CALL SETOUTLOAD(5000!)
	END IF
	CALL RESETTH                                'Reset the test head
	CALL PAUSE(1!)

END FUNCTION

'*******************************
SUB INDIVID (SEL%, ITERATION%)
	   
	DIM TSIM!(105), OUTCALC!(105), OUTMEAS!(105), ERROROUT!(105), ACCSTAT$(105)
   
	SNM% = SENSORNUM%
	MINOUT! = SPECS.MINOUT
	OUTTYPE$ = SPECS.OUTSIGTYPE

	IF SNM% = 2 THEN                                'Check if current input
		CALL WRITEDVM("S0", 0)                      'Slow reading rate
		IINSEN.MEAS! = MEASRES!(IINSEN!, 2, TE$)    'Measure current source sense R
		CALL WRITEDVM("S1", 0)                      'Medium reading rate

	ELSE
		IINSEN.MEAS! = 1
	END IF
	   
	IF SPECS.ISMAXFL <> 0 THEN
		MODILIM! = SPECS.ISMAXFL
	ELSE
		MODILIM! = SPECS.ISMAXNL
	END IF

	DO
		SL$ = SETPOWER$(PSADDR%, 24!, FUNC$)       'Set Vsupply to 24.0V
		SELECT CASE SEL%

			CASE 1
			
				IF SETPOWER$(PSADDR%, 999!, ISTAT$) = "RCS=01" THEN    'Check for over-current
					SL$ = SETPOWER$(PSADDR%, 24!, FUNC$)   'Set Vsupply to 24.0V
				END IF
				A$ = SUPPLYI$(0)

			CASE 2

				IF SETPOWER$(PSADDR%, 999!, ISTAT$) = "RCS=01" THEN    'Check for over-current
					SL$ = SETPOWER$(PSADDR%, 24!, FUNC$)   'Set Vsupply to 24.0V
				END IF
				A$ = SUPPLYI$(1)

			CASE 3
			
				ITER2% = ITERATION%
				WHILE ITER2% > 0                    'Offset/Gain calibration loop
					A$ = OFFSETCAL$(CAL%)           'Offset calibration
					A$ = GAINCAL$(CAL%)             'Gain calibration
					ITER2% = ITER2% - 1
				WEND                                'Tests for Offset/Gain calibration end
			CASE 4
				FOR T% = 1 TO 3
					SELECT CASE T%
						CASE 1
							OUTLOAD! = SPECS.LOAD3
							A$ = LINTEST$(ACC$, OUTLOAD!, TSIM!(), OUTCALC!(), OUTMEAS!(), ERROROUT!(), ACCSTAT$())           'Performs linearity and
													'accuracy tests
							A$ = ACC$               'Assigns results of accuracy test
						CASE 2
							IF OUTTYPE$ = "VOLTAGE" THEN
								OUTLOAD! = SPECS.LOAD2
								A$ = LINTEST$(ACC$, OUTLOAD!, TSIM!(), OUTCALC!(), OUTMEAS!(), ERROROUT!(), ACCSTAT$())           'Performs linearity and
													'accuracy tests
								A$ = ACC$           'Assigns results of accuracy test
							END IF
						CASE 3
							IF OUTTYPE$ = "VOLTAGE" THEN
								OUTLOAD! = SPECS.LOAD1
								A$ = LINTEST$(ACC$, OUTLOAD!, TSIM!(), OUTCALC!(), OUTMEAS!(), ERROROUT!(), ACCSTAT$())           'Performs linearity and
													'accuracy tests
								A$ = ACC$           'Assigns results of accuracy test
							END IF
					END SELECT
				NEXT
CALL INTERLUDE
			CASE 5
				POSI% = 1                           'Test positive current limit
				A$ = ILIMIT$(POSI%)
				IF OUTTYPE$ = "VOLTAGE" AND MINOUT! <> 0 THEN
					POSI% = 0                       'Test negative current limit
					A$ = ILIMIT$(POSI%)
				END IF
			CASE 6
			
				A$ = OVERRANGE$                     'Test module overrange
			CASE 7
			
				A$ = SUPPLYSEN$
			CASE 8
			
				A$ = INPUTR$
			CASE 9
				CALL GENSET(GENAMPL!)
				A$ = FREQRESP$(GENAMPL!)
			CASE 10
			
				A$ = STEPRESP$
			CASE 11
			
				A$ = OUTNOISE$
			CASE 12
			
				A$ = IOUTMAXL$(1)
				IF LEFT$(SPECS.MODNAME, 9) = "DSCA39-07" THEN
					CALL CONTINUE
					B$ = IOUTMAXL$(0)
				END IF
		END SELECT
			   
		CALL CONTINUE
	   
		SL$ = SETPOWER$(PSADDR%, 999!, LOOPOFF$)           'Disable power supply
	   
	LOOP WHILE REPEAT$(SPECS.MODNAME) <> "N"
   
	IF SNM% = 1 AND (SPECS.MAXIN > 10 OR SPECS.MININ < -10) THEN
		PRINT
		PRINT TAB(5); "Insert the yellow connector into the meter +/-SENSE terminals."
		CALL CONTINUE
	END IF

END SUB

'*******************************
FUNCTION INPUTR$
	   
	MAXIN! = SPECS.MAXIN                    '+f.s. input voltage (V)
	MININ! = SPECS.MININ                    '-f.s. input voltage (V)
	MINOUT! = SPECS.MINOUT                  'V or A
	MAXOUT! = SPECS.MAXOUT
	SPEC! = SPECS.INPUTRES                  'Meg ohms
	INRANGE! = MAXIN! - MININ!
	ORANGE! = MAXOUT! - MINOUT!

	CLS
	TESTTITLE$ = "Input Resistance & Bias Current Test"
	CALL HEADERB(TESTTITLE$)
	LOCATE 10, 10

	SNM% = SENSORNUM%
	   
	IF SNM% = 2 THEN                                'Check for V in only
		PRINT "This test is not performed on model "; SPECS.MODNAME
		EXIT FUNCTION
	ELSE
		PRINT "Testing, please wait."
	END IF

	CALL SETDAC2(MAXIN!, 1, 0)
	   
'    CALL LOADMUX(2, CON%)                           'Measure output
'    CALL WRITEDVM(VODC$, MAXOUT! * IOUTSEN.MEAS!)
'    OUT1! = READDVM!(.001)
	   
	CB0VAL% = CB0VAL% OR &H20                       'Insert large resistor in series
	CALL SETTH(CB0VAL%, CB0%)                       'with input.
   
	CALL LOADMUX(2, CON%)                           'Turns on mux, reset w/ resistor insert
	CALL WRITEDVM(VODC$, MAXOUT! * IOUTSEN.MEAS!)
	OUT2! = READDVM!(.001)                          'Measure output
	CALL LOADMUX(2, COFF%)

'    DELTAOUT! = (OUT2! - OUT1!) * 1000!                'Output change
'    MAXDELTA! = (MAXOUT! - MODULEOUT!((SPEC! / (SPEC! + RSERIES!)) * MAXIN!)) / ORANGE! * 100 'max change (%)

	PEDOS! = MODULEOUT!(0!)
	XFERFN! = ORANGE! / INRANGE!
	RINMEAS! = ABS((OUT2! - PEDOS!) * RSERIES! / (MAXIN! * XFERFN! - OUT2!) / 1000000!)
	   
	IF RINMEAS! > 1000 THEN RINMEAS! = 1000
	   
	IF RINMEAS! >= SPEC! THEN
		INR$ = "PASS"
	ELSE
		INR$ = "FAIL"
		SOUND 1000, .5
	END IF

	LOCATE 10, 10
	PRINT "Status: "; 
	
	IF  LEFT$(INR$, 4) = "FAIL" THEN COLOR 12, 0
	PRINT INR$;
	COLOR 11, 0
	
	
	PRINT SPC(20);
	PRINT TAB(10); "Measured input resistance is";
	PRINT TAB(50); USING "   #### Mohms"; RINMEAS!
	PRINT TAB(10); "Required input resistance is";
	PRINT TAB(50); USING ">= #### Mohms"; SPEC!

	INPUTR$ = INR$ + STR$(RINMEAS!) + "0"
	   
	CALL RESETTH                                'Reset the test head
	CALL PAUSE(1!)

END FUNCTION

'*******************************
FUNCTION IOUTMAXL$ (MINMAX%)

	'test operation of current output modules with max load resistance.

	SENSOR$ = SPECS.SENTYPE
	PLUSFSIN! = SPECS.MAXIN
	MINUSFSIN! = SPECS.MININ
	MINOUT! = SPECS.MINOUT                  'V or A
	MAXOUT! = SPECS.MAXOUT
	ORANGE! = MAXOUT! - MINOUT!             'Output range (V or A)
	SNM% = SENSORNUM%
	OUTTYPE$ = SPECS.OUTSIGTYPE
	MAXLOAD! = SPECS.MAXLOAD
	MINLOAD! = 5                            '5 ohms, used to test DSCA39-07
	MAXACCERR! = SPECS.ACCURACY1 * 2        '%
	COMPLIANCE! = SPECS.COMPLIANCE          'Max change in output w/ load (%)
											'Allows for 5 ohm lead resistance
											'and 0.1% output change
	CLS
	IF MINMAX% = 1 THEN
		TESTTITLE$ = "Maximum Output Load Test"
	ELSE
		TESTTITLE$ = "Minimum Output Load Test"
	END IF

	CALL HEADERB(TESTTITLE$)
	LOCATE 10, 10
	
	IF OUTTYPE$ = "VOLTAGE" THEN
		PRINT "This test is not performed on model "; SPECS.MODNAME
		EXIT FUNCTION
	ELSE
		PRINT "Testing, please wait."
	END IF
   
	MAXIN! = PLUSFSIN!                              'Calc. maxin volts
	IF SNM% = 2 THEN
		MAXINMEAS! = SETCURRENTIN!(MAXIN!)
	ELSE
		CALL SETDAC2(MAXIN!, 1, 0)        'Sets DAC
	END IF

	CALL LOADMUX(2, CON%)                           'Measure output
	CALL WRITEDVM(VODC$, MAXOUT! * IOUTSEN.MEAS!)
	
	OUTNOMLOAD! = READDVM!(.0001 * ORANGE!) / IOUTSEN.MEAS! '.01% tolerance
	CALL LOADMUX(2, COFF%)

	SL$ = SETPOWER$(PSADDR%, 999!, LOOPOFF$)               'Disable power supply

	IF MINMAX% = 1 THEN
		CALL SETOUTLOAD(MAXLOAD!)                   'Set max load resistor
		IOUTSEN2.MEAS! = MEASRES!(MAXLOAD!, 5, TE$)
		TESTLIM! = COMPLIANCE!
	ELSE
		CALL SETOUTLOAD(MINLOAD!)                   'Set min load resistor
		IOUTSEN2.MEAS! = MEASRES!(MINLOAD!, 4, TE$)
		TESTLIM! = MAXACCERR!
	END IF

	SL$ = SETPOWER$(PSADDR%, 24!, FUNC$)                   'Set Vsupply to 24.0V
	CALL WRITEDVM(VODC$, MAXOUT! * IOUTSEN2.MEAS!)
	CALL LOADMUX(2, CON%)                           'Measure output
	CALL LOADMUX(2, CON%)                           'Doesn't always set first time.
   
	OUTMAXLOAD! = READDVM!(.0001 * ORANGE!) / IOUTSEN2.MEAS! '.01% tolerance
	PERCOUTCHG! = (OUTMAXLOAD! - OUTNOMLOAD!) / ORANGE! * 100!
   
	IF ABS(PERCOUTCHG!) <= TESTLIM! THEN
		ML$ = "PASS"
	ELSE
		ML$ = "FAIL"
		SOUND 1000, .5
	END IF
			
	LOCATE 15, 10
	PRINT "Status: "; 
	
	IF  LEFT$(ML$, 4) = "FAIL" THEN COLOR 12, 0
	PRINT ML$;
	COLOR 11, 0
	
	PRINT SPC(30);
	PRINT TAB(10); "Change in output current at max load is";
	PRINT TAB(50); USING " +###.### %"; PERCOUTCHG!
	PRINT TAB(10); "Max. change in output current is";
	PRINT TAB(50); USING "+/-##.### %"; TESTLIM!
   
	IOUTMAXL$ = ML$ + STR$(PERCOUTCHG!) + "2"
  
	CALL RESETTH                                    'Reset the test head
	CALL SETOUTLOAD(IOUTSEN!)                       'Set current output sense resistor
	CALL PAUSE(1!)

END FUNCTION

FUNCTION LINTEST$ (ACC$, OUTLOAD!, TSIM!(), OUTCALC!(), OUTMEAS!(), ERROROUT!(), ACCSTAT$())
	'Sub to perform the accuracy/linearity tests and create the accuracy test results section of
	'the datasheet. Removed code to print to file since that is now accomplished by the
	'"DATASHEETWRITE" sub. The datasheet data can still be sent to two places (screen and
	'printer), so when updating remember to make any changes to both the "PRINT" and "LPRINT"
	'sections (PWR: 2013-11-12).
	'
	SENSOR$ = SPECS.SENTYPE
	MININ! = SPECS.MININ
	MAXIN! = SPECS.MAXIN
	MINOUT! = SPECS.MINOUT                  'V or A
	MAXOUT! = SPECS.MAXOUT
	ORANGE! = MAXOUT! - MINOUT!              'Output range (V or A)

	LOAD1! = SPECS.LOAD1
	LOAD2! = SPECS.LOAD2
	LOAD3! = SPECS.LOAD3
  
	CLS
	IF SPECS.OUTSIGTYPE = "VOLTAGE" THEN
		UNIT$ = "V"
		SCALE! = 1!
		IF OUTLOAD! > 1 THEN            'Output load in mA
			CALL SETOUTLOAD(MAXOUT! / OUTLOAD! * 1000)
		END IF
	   
		IF OUTLOAD! = LOAD1! THEN
			MAXACCERR! = SPECS.ACCURACY1    '%
			MAXLINERR! = SPECS.LINEAR1
		ELSEIF OUTLOAD! = LOAD2! THEN
			MAXACCERR! = SPECS.ACCURACY2
			MAXLINERR! = SPECS.LINEAR2
		ELSE
			MAXACCERR! = SPECS.ACCURACY3
			MAXLINERR! = SPECS.LINEAR3
		END IF

		CLS
		TESTTITLE$ = "Accuracy, Load Current = " + STR$(OUTLOAD!) + " mA"
	ELSE
		UNIT$ = "mA"
		SCALE! = 1000!
		TESTTITLE$ = "Accuracy Test"
		OUTLOAD! = 0                            'Flag for header printing
		MAXACCERR! = SPECS.ACCURACY1            '%
		MAXLINERR! = SPECS.LINEAR1
	END IF
   
	CALL HEADERB(TESTTITLE$)
	  
	SNM% = SENSORNUM%
	IF SNM% = 2 THEN                        'Current input modules
		INPUTTYPE$ = "Iin (mA)"
		SCALE% = 1000
	ELSE
		INPUTTYPE$ = " Vin (V)"            'Voltage input modules
		SCALE% = 1
	END IF

	INRANGE! = (MAXIN! - MININ!) / SCALE% * IINSEN.MEAS!
'
	PRINT
	PRINT TAB(23); "Calculated"; TAB(38); "Measured"
	PRINT TAB( 9); INPUTTYPE$;
	PRINT TAB(22); "Output ("; UNIT$; ")"; TAB(37); "Output ("; UNIT$; ")*";
	PRINT TAB(53); "Error (%)"; TAB(67); "Status"
	PRINT TAB(8); "----------"; TAB(22); "-----------"; TAB(37); "-----------";
	PRINT TAB(52); "----------"; TAB(66); "--------"
   
	'8             22             37             52            66  
	' Iin (mA)
	'  Vin (V)     Output (mA)    Output (mA)     Error (%)     Status
	'----------    -----------    -----------    ----------    --------
	' #####.##
	' +####.##
	'+###.###        +##.###        +##.###      +###.###        &&&&

	IF PON% = 1 AND OUTLOAD! <= 1 THEN
		LPRINT TAB(34); "ACCURACY TEST"
		LPRINT
		LPRINT TAB(23); "Calculated"; TAB(38); "Measured"

		IF SNM% = 2 THEN                        'Current input modules
			LPRINT TAB(9); "Iin (mA)";
'                  
		ELSE                                    'Voltage input modules
			LPRINT TAB(10); "Vin (V)";
'          
		END IF
		LPRINT TAB(22); "Output ("; UNIT$; ")"; TAB(37); "Output ("; UNIT$; ")*";
		LPRINT TAB(53); "Error (%)"; TAB(67); "Status"
		LPRINT TAB(8); "----------"; TAB(22); "-----------"; TAB(37); "-----------";
		LPRINT TAB(52); "----------"; TAB(66); "--------"
	END IF

	INC% = 0                                        'Array element
	NUMPTS% = 5                                     'Number of points in test

	IF LOGDAT% > 0 THEN
		INPUT "Enter the number of points to be tested  "; NUMPTS%
		INPUT "Enter file name "; F$
		F$ = "C:\QB45\JJM\" + F$
		OPEN F$ FOR OUTPUT AS #3
		WRITE #3, (MAXIN! - MININ!)
		WRITE #3, MININ!
		WRITE #3, NUMPTS%
	END IF
	   
	FAILED% = 0
	LINSTEP! = (MAXIN! - MININ!) / (NUMPTS% - 1)
													'LINSTEP!/2 ensures NUMPTS% test point
	FOR MODIN! = MININ! TO MAXIN! + LINSTEP! / 2 STEP LINSTEP!
		INC% = INC% + 1
		IF INC% = 1 THEN
			SL$ = SETPOWER$(PSADDR%, 24!, FUNC$)       'Set Vsupply to 24.0V
		END IF
		IF SNM% = 2 THEN
			MEASIN! = SETCURRENTIN!(MODIN!)
		ELSE
			CALL SETDAC2(MODIN!, 1, 0)    'Sets input voltage
			CALL WRITEDVM("S0", 0)                  'Slow reading rate for max accuracy
			CALL LOADMUX(1, CON%)
			CALL WRITEDVM(VODC$, MODIN!)              'Sets meter to read
			MEASIN! = READDVM!(INRANGE! * MAXACCERR! / 100! / 10!)'10x better than accuracy
			IF ABS(MODIN!) > 10.1 AND ABS(MODIN!) - ABS(MEASIN!) > 1 THEN
													  'Power supply current limit tripped
				PRINT "Multiple MUX channels are ON"
				PRINT "Notify engineering about problem"
				CONTINUE
'                SL$ = SETPOWER$(VINADDR%, ABS(MODIN!), FUNC$) 'Set Vin
'                CALL pause(2!)
'                MEASIN! = READDVM!(INRANGE! * MAXACCERR! / 100! / 10!)'10x better than accuracy
			END IF
			CALL LOADMUX(1, COFF%)
		END IF
		   
		CALL WRITEDVM("S1", 0)                      'Medium reading rate
		   
		TSIM!(INC%) = MEASIN!
		OUTCALC!(INC%) = MODULEOUT!(TSIM!(INC%))
	
		CALL LOADMUX(2, CON%)
		CALL WRITEDVM(VODC$, OUTCALC!(INC%) * IOUTSEN.MEAS!) 'Sets meter to read output
		 
		OUTMEAS!(INC%) = READDVM!(.00005 * ORANGE!) / IOUTSEN.MEAS!'.005% tolerance
		ERROROUT!(INC%) = (OUTMEAS!(INC%) - OUTCALC!(INC%)) / ORANGE! * 100! 'Error (%)
		IF LOGDAT% > 0 THEN WRITE #3, ERROROUT!(INC%)
		CALL LOADMUX(2, COFF%)
	   
		IF SNM% <> 2 AND ABS(MODIN!) > 10.1 THEN             '10.1V limit matches that of SETDAC2
			SL$ = SETPOWER$(VINADDR%, 999!, LOOPOFF$)  'Disable power supply
		END IF

		IF ABS(ERROROUT!(INC%)) <= MAXACCERR! THEN
			ACCSTAT$(INC%) = "PASS"
		ELSE
			ACCSTAT$(INC%) = "FAIL"
			FAILED% = FAILED% + 1
			SOUND 800, .5
		END IF
													'Voltage or current input modules
		PRINT TAB(8); USING "+###.###        +##.###        +##.###      +###.###"; TSIM!(INC%); OUTCALC!(INC%) * SCALE!; OUTMEAS!(INC%) * SCALE!; ERROROUT!(INC%);
		IF  LEFT$(ACCSTAT$(INC%), 4) = "FAIL" THEN COLOR 12, 0
		PRINT TAB(68); ACCSTAT$(INC%)
		COLOR 11, 0
		
		IF PON% = 1 AND OUTLOAD! <= 1 THEN
													 'Voltage or current input modules
			LPRINT TAB(8); USING "+###.###        +##.###        +##.###      +###.###"; TSIM!(INC%); OUTCALC!(INC%) * SCALE!; OUTMEAS!(INC%) * SCALE!; ERROROUT!(INC%);
			LPRINT TAB(68); ACCSTAT$(INC%)
		END IF
		
		IF ABS(ERROROUT!(INC%)) > ABS(ACCERR!) THEN
			ACCERR! = ERROROUT!(INC%)
		END IF
			   
		'Variables used to calculate the best fit line using linear regression.
		SMODIN! = SMODIN! + TSIM!(INC%)
		SSMODIN! = SSMODIN! + TSIM!(INC%) ^ 2
		SERR! = SERR! + ERROROUT!(INC%)
		SPROD! = SPROD! + TSIM!(INC%) * ERROROUT!(INC%)
								'If DAC limitation was exceeded, reset MODIN!
		IF INC% = 1 AND ABS(MODIN! - MININ!) > VERNMIN! THEN MODIN! = MININ!
	   
	NEXT
			   
	IF LOGDAT% > 0 THEN CLOSE #3

	IF FAILED% = 0 THEN
		ACC$ = "PASS"
	ELSE
		ACC$ = "FAIL"
		SOUND 800, .5
	END IF

	SLOPE! = ((NUMPTS% * SPROD!) - (SMODIN! * SERR!)) / ((NUMPTS% * SSMODIN!) - (SMODIN! ^ 2))
	OFFSET1! = ((SERR! * SSMODIN!) - (SMODIN! * SPROD!)) / ((NUMPTS% * SSMODIN!) - (SMODIN! ^ 2))
	BFERR! = ABS(BESTFIT!(SLOPE!, OFFSET1!, TSIM!(), NUMPTS%, ERROROUT!()))
	IF ABS(BFERR!) <= MAXLINERR! THEN
		BF$ = "PASS"
	ELSE
		SOUND 600, .5
		BF$ = "FAIL"
	END IF

	PRINT
	PRINT TAB(10); "Accuracy status: "; 
	
	IF  LEFT$(ACC$, 4) = "FAIL" THEN COLOR 12, 0
	PRINT ACC$
	COLOR 11, 0
	
	PRINT TAB(10); "Measured accuracy error is";
	PRINT TAB(50); USING " +###.### %"; ACCERR!
	PRINT TAB(10); "Maximum accuracy error is";
	PRINT TAB(50); USING "+/-##.### %"; MAXACCERR!
	PRINT
	PRINT TAB(10); "Linearity status: "; 
	
	IF BF$ = "FAIL" THEN COLOR 12, 0
	PRINT BF$
	COLOR 11, 0
	
	PRINT TAB(10); "Measured best-fit linearity error is";
	PRINT TAB(50); USING "+/-###.### %"; BFERR!
	PRINT TAB(10); "Maximum best-fit linearity error is";
	PRINT TAB(50); USING "+/- ##.### %"; MAXLINERR!

	ACC$ = ACC$ + STR$(ACCERR!) + "3"           '% span
	LINTEST$ = BF$ + STR$(BFERR!) + "3"         'append # decimal places
		   
	IF SPECS.OUTSIGTYPE = "VOLTAGE" THEN        'Remove output load, DSCA49
		CALL SETOUTLOAD(5000!)
	END IF
	CALL RESETTH                                'Reset the test head
	CALL PAUSE(1!)

END FUNCTION

'*******************************
SUB LOGIT (STATUS$(), SN$, TSIM!(), OUTCALC!(), OUTMEAS!(), ERROROUT!(), ACCSTAT$(), TOTLRESPEC!)

	IF FAILS%(STATUS$()) = 0 OR LEFT$(SN$, 6) = "1000HR" THEN
	   
		OPEN "C:\ATE\DSCLOG\" + RTRIM$(MID$(SPECS.MODNAME, 5, 8)) + ".DAT" FOR APPEND AS #4
		OPEN "C:\ATE\LOGPATH.ADR" FOR INPUT AS #7
			INPUT #7, LOGPATH$
		CLOSE #7
		OPEN LOGPATH$ + "\DSCLOG\" + RTRIM$(MID$(SPECS.MODNAME, 5, 8)) + ".DAT" FOR APPEND AS #8
	   
		NUMPTS% = 5

		'builds a sequential file.  20 records required for 1 module
		WRITE #4, SPECS.MODNAME
		WRITE #8, SPECS.MODNAME

		FOR INC% = 1 TO NUMPTS%
			WRITE #4, TSIM!(INC%), OUTCALC!(INC%), OUTMEAS!(INC%), ERROROUT!(INC%), ACCSTAT$(INC%)
			WRITE #8, TSIM!(INC%), OUTCALC!(INC%), OUTMEAS!(INC%), ERROROUT!(INC%), ACCSTAT$(INC%)
		NEXT

		FOR INC% = 0 TO 14 STEP 5
			WRITE #4, STATUS$(INC% + 1), STATUS$(INC% + 2), STATUS$(INC% + 3), STATUS$(INC% + 4), STATUS$(INC% + 5)
			WRITE #8, STATUS$(INC% + 1), STATUS$(INC% + 2), STATUS$(INC% + 3), STATUS$(INC% + 4), STATUS$(INC% + 5)
		NEXT
	   
		WRITE #4, STATUS$(16), STATUS$(17), STATUS$(18)
		WRITE #8, STATUS$(16), STATUS$(17), STATUS$(18)

		WRITE #4, SN$, DATE$
		WRITE #8, SN$, DATE$
			   
		CLOSE #4
		CLOSE #8
   
	END IF

END SUB

'*******************************
FUNCTION MEASRES! (OHM!, RESNUM%, TE$)
	  
	'Measure specified resistance
	'Legend:
	'RESNUM%        Resistor
	'-------        --------
	'   1           On board supply current sense resistor, not used.
	'   2           Input current source sense resistor, 192 ohm
	'   3           Output current sense resistor,  192 ohm
	'   4           Output current sense resistor,  5 ohm
	'   5           Output current sense or load resistor
	'               TE1055; 500 or 750 ohm
	'               TE1054; 100, 200, 1000 or 2000 ohm
	'
	'* TE$ not used in this program.
	   
	SELECT CASE RESNUM%                     'Branches to the specific measurement
		CASE 1
			CALL WRITEDVM(OHM2$, OHM! / 1000)       'Sets meter
			CALL LOADMUX(3, CON%)                   'Sets mux channel
			MEASTOL! = .001                         '0.1% measurement tolerance
			INITTOL! = .2                           '20% initial tolerance
		CASE 2
			'Update commands below for Vout and Iout test heads
			CALL WRITEDVM(OHM4$, OHM! / 1000)       'Sets meter
			CALL LOADMUX(3, CON%)                   'Sets mux channel
			CALL LOADMUX(3, CON%)                   'Sets mux channel
			CALL LOADMUX(7, CON%)                   'set sense channel
			MEASTOL! = .00001                       '0.001% measurement tolerance
			INITTOL! = .05                          '5% initial tolerance
			CLS
			LOCATE 10, 10: PRINT "Measuring Input Sense Resistor.  Please Wait..."
		CASE 3
			CB1VAL% = CB1VAL% OR &H1                '0 0 0 0  0 0 0 1
			CALL SETTH(CB1VAL%, CB1%)               'Disconnect +OUT from load
													'for accurate 4-wire ohms meas.
			CALL WRITEDVM(OHM4$, OHM! / 1000)       'Sets meter
			CALL LOADMUX(2, CON%)                   'Sets mux channel
			CALL LOADMUX(8, CON%)                   'set sense channel
			MEASTOL! = .00001                       '0.001% measurement tolerance
			INITTOL! = .05                          '5% initial tolerance
			CLS
			LOCATE 10, 10: PRINT "Measuring Load Resistor.  Please Wait..."
		CASE 4
			CB1VAL% = CB1VAL% OR &H1                '0 0 0 0  0 0 0 1
			CALL SETTH(CB1VAL%, CB1%)               'Disconnect +OUT from load
													'for accurate 4-wire ohms meas.
			CALL WRITEDVM(OHM4$, OHM! / 1000)       'Sets meter
			CALL LOADMUX(2, CON%)                   'Sets mux channel
			CALL LOADMUX(6, CON%)                   'set sense channel
			MEASTOL! = .00001                       '0.001% measurement tolerance
			INITTOL! = .05                          '5% initial tolerance
		CASE 5
			CB1VAL% = CB1VAL% OR &H1                '0 0 0 0  0 0 0 1
			CALL SETTH(CB1VAL%, CB1%)               'Disconnect +OUT from load

			CALL WRITEDVM(OHM2$, OHM! / 1000)       'Sets meter
			CALL LOADMUX(2, CON%)                   'Sets mux channel
'
			MEASTOL! = .001                         '0.1% measurement tolerance
			INITTOL! = .05                          '5% initial tolerance

'       CASE 6 NOT USED
'       CASE 7 NOT USED
'       CASE 8 NOT USED
	END SELECT

	TEMP! = READDVM!(OHM! * MEASTOL!)        'Measure resistor value to +/-0.01%
	   
	IF OHM! > 1! AND ABS(TEMP! - OHM!) > (OHM! * INITTOL!) THEN   'Test for 15% deviation
		CLS
		LOCATE 10, 10
		PRINT "Measured resistance = "; TEMP!; " ohms."
		PRINT TAB(10); "Required resistance = "; OHM!; "ohms."
		PRINT TAB(10); "Check system setup and restart program."
		PRINT TAB(10); "If the failure continues, notify engineering."
		SL$ = SETPOWER$(PSADDR%, 999!, LOOPOFF$)           'Turn off loop power
		END
	END IF

	IF RESNUM% = 3 OR RESNUM% = 4 OR RESNUM% = 5 THEN
		CB1VAL% = CB1VAL% AND &HFE                  '1 1 1 1  1 1 1 0
		CALL SETTH(CB1VAL%, CB1%)                   'Connect Iout to load
	END IF

	CALL WRITEDVM(VODC$, 1!)                        'Resets meter
	CALL LOADMUX(0, CON%)                           'Clears mux

	MEASRES! = TEMP!                                'Passes resistor value back

END FUNCTION

'*******************************
FUNCTION MENU1%
	   
	' ***** PROGRAM CONTROL MENU 1 *****
	COLOR 15, 0, 0      ' cjt these 2 lines get rid of screen artifacts
	CLS
	COLOR 14, 0, 0
	LOCATE 5, 23: PRINT "DSCA39,49-xx AUTOMATED TEST EQUIPMENT"
	LOCATE 6, 23: PRINT "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
	COLOR 11, 0, 0
	LOCATE  8, 24: PRINT "1.) Functional Test"
	LOCATE  9, 24: PRINT "2.) Pre-Burn-In Complete Test"
	LOCATE 10, 24: PRINT "    (No Printout)"
	LOCATE 11, 24: PRINT "3.) Post-Burn-In Complete Test"
	LOCATE 12, 24: PRINT "    (Optional Printout)"
	LOCATE 13, 24: PRINT "4.) Sealed Module Retest"
	LOCATE 14, 24: PRINT "    (No Adjustments, Optional Printout)"
	LOCATE 15, 24: PRINT "5.) Individual Test Menu"
	LOCATE 17, 24: PRINT "6.) Return to the FAMILY TYPE menu"
	LOCATE 20, 16: PRINT ""
	LOCATE 22, 16: PRINT "Press the F10 key at any time to exit the program."
	DO
		I$ = INKEY$
	LOOP WHILE VAL(I$) < 1 OR VAL(I$) > 6

	MENU1% = VAL(I$)

END FUNCTION

'*******************************
FUNCTION MENU2%
	
	CLS
	COLOR 14, 0, 0
	
	LOCATE 2, 23: PRINT "DSCA39,49-xx AUTOMATED TEST EQUIPMENT"
	LOCATE 3, 22: PRINT "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
	COLOR 11, 0, 0
	LOCATE 5, 9: PRINT "1.) Supply Current Test, Min. Output Load"
	LOCATE 6, 9: PRINT "2.) Supply Current Test, Max. Output Load"
	LOCATE 7, 9: PRINT "3.) Offset & Gain Calibration"
	LOCATE 8, 9: PRINT "4.) Linearity / Accuracy Test"
	LOCATE 9, 9: PRINT "5.) Current Limit Test"
	LOCATE 10, 9: PRINT "6.) Over-Range Test"
	LOCATE 11, 9: PRINT "7.) Power Supply Sensitivity Test"
	LOCATE 12, 9: PRINT "8.) Input Resistance & Bias Current Test"
	LOCATE 13, 9: PRINT "9.) Frequency Response Test"
	LOCATE 14, 9: PRINT "A.) Step Response Test"
	LOCATE 15, 9: PRINT "B.) Output Noise Test"
	LOCATE 16, 9: PRINT "C.) Output Current @ Min./Max. Load Test (DSCA39 ONLY)"
	LOCATE 17, 9: PRINT "D.) Notes On ATE Operation"
	LOCATE 18, 9: PRINT "E.) Printer Output = ";


	IF PON% > 0 THEN
		LOCATE 18, 31: PRINT "ON"
	ELSE
		LOCATE 18, 31: PRINT "OFF"
	END IF
   
	LOCATE 19, 9: PRINT "F.) Log Linearity Test Data = ";
	IF LOGDAT% > 0 THEN
		LOCATE 19, 31: PRINT "ON"
	ELSE
		LOCATE 19, 31: PRINT "OFF"
	END IF
   
	LOCATE 20, 9: PRINT "G.) Exit to the DSCA Main Menu"
	LOCATE 21, 9: PRINT "Enter your selection.  "
   
	DO
		DO
			I$ = UCASE$(INKEY$)
		LOOP WHILE I$ = ""
		C% = ASC(I$)
	LOOP WHILE (C% < 49 OR C% > 57) AND (C% < 65 OR C% > 72)
		
	IF C% > 57 THEN
		MENU2% = C% - 55
	ELSE
		MENU2% = C% - 48
	END IF

END FUNCTION

'*******************************
FUNCTION MENU3%
	   
	CLS
	COLOR 14, 0, 0
	LOCATE 3, 26: PRINT "Module Family Selection Menu"
	LOCATE 4, 26: PRINT "---------------------------"
	COLOR 11, 0, 0
	LOCATE 6, 34: PRINT "1.) DSCA39-xx"
	LOCATE 7, 34: PRINT "2.) DSCA49-xx"
	LOCATE 8, 34: PRINT "3.) Return to the FAMILY TYPE menu"
				   
	DO
		I$ = INKEY$
	LOOP WHILE VAL(I$) < 1 OR VAL(I$) > 3

	M3% = VAL(I$)

	IF M3% = 1 THEN                             'Parallel port, mux & test head addresses
		TE$ = "TE1055"                          'Current output test head
		FILENAME$ = "KDSCIOUT.ADR"
	ELSE
		TE$ = "TE1054"                          'Voltage output test head
		FILENAME$ = "KDSCVOUT.ADR"
	END IF

	CALL GETADD(FILENAME$)              'Gets the Parallel port, Mux and Testhead address
	MENU3% = M3%

END FUNCTION

SUB NOTES
	'Notes on ATE operation
	CLS
	LOCATE 5, 25: PRINT "AUTOMATED TEST EQUIPMENT NOTES"
	LOCATE 7, 10: PRINT "1.) During the calibration subroutines the following keystrokes"
	LOCATE 8, 14: PRINT "can be used:"
	LOCATE 10, 14: PRINT "(C)alibrate will exit the routine and continue"
	LOCATE 11, 14: PRINT "program execution."
	LOCATE 13, 14: PRINT "(T)roubleshoot will pause the program.  The DVM will continuously"
	LOCATE 14, 14: PRINT "display the calibration reading."
	LOCATE 16, 10: PRINT "2.) Press (F10) at any time to break out of the program and"
	LOCATE 17, 14: PRINT "return to DOS."
	
	CALL CONTINUE

END SUB

'*******************************
SUB LOGMODFILES(STATUS$(), SN$, TSIM!(), OUTCALC!(), OUTMEAS!(), ERROROUT!(), ACCSTAT$(), TSPEC$(), TOTLRESPEC!, TX%)
	'Sub to write the data log, datasheet, and work order status files (and inform the
	'operator of these operations)

	KEY(10) OFF	'Deactivates F10 key

	CALL INTERLUDE
	'CLS
	PRINT ""	'Blank line
	PRINT TAB(10); "Logging test results."
	CALL LOGIT(STATUS$(), SN$, TSIM!(), OUTCALC!(), OUTMEAS!(), ERROROUT!(), ACCSTAT$(), TOTLRESPEC!)      'Logs test data to disk
	PRINT TAB(10); "Writing datasheet file."
	CALL DATASHEETWRITE (SN$, STATUS$(), TSIM!(), OUTCALC!(), OUTMEAS!(), ERROROUT!(), ACCSTAT$(), TSPEC$(), TX%, OUTLOAD!)
	PRINT TAB(10); "Writing status to work order status file."
	CALL WORKORDERLINE(FAILS%(STATUS$()), SN$)
	CALL DATASHEETPRINT (SN$, STATUS$())
	'CALL INTERLUDE
	COLOR 11, 0, 0	'Cyan on black background

	KEY(10) ON	'Reactivates F10 key
	
END SUB

