' {$STAMP BS2}
' {$PBASIC 2.5}
' MARSPortTestr.BS2 runs a program to sample the various functions of the MARS Port Testing Tool.
' Temperature in the Controller housing and in the load housing is sensed with a Max 6575, Analog levels
'of the 48 and 400 vdc supplied byt the MARS node are sensed at a rated load of 750 watts, Timing signal is sampled
'is sampled and tested for presence, pulse width is sampled over a 10 sec interval.

        'Pin assignments
ComTx       PIN 0            'surface comms recieve pin
ComRx       PIN 1            'surface comms transmit pin
TempDat     PIN 7            'Temperature in from Max6575
TempStrt    PIN 8            'Start conversion signal to MAX6575
OnePPS      PIN 9            '1PPS signal from opto isolator
CNVST       PIN 10           'analog data conversion start and channel selection
AnaDat      PIN 11           'digitized analog data
SCLK        PIN 12           'clocks data out of MAX1286 on AnaDat(DOUT)

        'Variable Assignments
VDC400    VAR Word         'analog value ch1 from MAX1286
VDC48     VAR Word         'analog value ch2 from MAX1286
TempInit  VAR Word         'initial temp variable
TempInt   VAR Word         'Temperature inside the control can
TempExt   VAR Word         'Temperature inside the load
Freq      VAR Word         'Frequency in Hz of input
FreqPW    VAR Word         'Pulse width of 1PPS signal
fast      VAR Word         'run faster ignoring the 1hz count
CountTime VAR Word         'count time for frequency
CountTime = 1000          'preset to 10 secs
Tacq      CON            6        '12uS pulse to start aquisition on 1286

'Temperature colection

TempCycle:
PULSOUT TempStrt, 2          'output 6uS pulse to start Max6575H
PULSIN TempStrt,0,TempInt       'get pulse duration of internal sensor
PULSIN TempStrt,0,TempExt
'DEBUG "Temp Int Raw ", DEC TempInt, " Temp Ext Raw ", DEC TempExt, CR
TempExt = ((TempExt)* 2/5)
TempInt = ((TempInt)* 2/5)
DEBUG "Internal Temp: ", DEC TempInt, CR        'print the pulse width
DEBUG "External Temp: ", DEC TempExt, CR
'IF TempInt = 0 THEN
'  DEBUG "No Internal sensor found", CR
    'END
'ENDIF

'One PPS verification

PULSIN OnePPS, 0, FreqPW         'sample OnePPS clock pulse width
FreqPW = FreqPW * 2              '2us multiplier for BS2
COUNT OnePPS, CountTime, Freq           'Sample OnePPS for 10 sec
DEBUG "1PPS Freq      ", DEC Freq, CR
DEBUG "1PPS PW        ", DEC FreqPW, CR
DEBUG "CountTime = ", DEC CountTime, CR
PAUSE 1000

'Voltage Level Aquisition
LOW CNVST
PULSOUT CNVST,5                     'start conversion
PAUSE 1
SHIFTIN AnaDat,SCLK,MSBPRE,[VDC400\12]
PULSOUT CNVST,5
PULSOUT CNVST,5
PAUSE 1
SHIFTIN AnaDat,SCLK,MSBPRE,[VDC48\12]
DEBUG "400VDC =     ", DEC VDC400, " AIN1", CR
DEBUG "48VDC =      ", DEC VDC48, " AIN2", CR

'Surface Comms
SEROUT 0,84,[DEC TempExt , ",",DEC TempInt,",",DEC OnePPS, ",",DEC FreqPW,",", DEC VDC400,",", DEC VDC48,",","*",","]
SERIN ComRx,84,[STR fast\1]
DEBUG "Fast in =", fast, CR
PAUSE 2000
IF fast = "f" THEN
  CountTime = 1000
  DEBUG "Fast in =", fast, CR
ELSE
  CountTime = 1000
ENDIF
DEBUG "Fast =",  fast, CR
GOTO TempCycle
END