' {$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.
'060507 added read of loadOnOff and transmit to surface to verify the state of the load.
        'Pin assignments
ComTx       PIN 0            'surface comms recieve pin
ComRx       PIN 1            'surface comms transmit pin
OnePPSOnOff PIN 5            'enables 1pps
LoadOnOff   PIN 7            'Turns the external load on/off
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         'freq count time
CountTime = 1000           'preset to 10 sec
Tacq      CON            6        '12uS pulse to start aquisition on 1286
Cycles VAR Word            'counter for pulsin loop

fast = "n"
LOW LoadOnOff
LOW OnePPSOnOff
'Temperature colection

TempCycle:
DEBUG "fast = ",fast,"  ",CR
PULSOUT TempStrt, 2          'output 6uS pulse to start Max6575H
PULSIN TempStrt,0,TempInt       'get pulse duration of internal sensor
PULSIN TempStrt,0,TempExt       'get pulse duration of external sensor
'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


'One PPS verification
Cycles = 0
DO WHILE (Cycles < 8)          'Pulse occurs 1/sec, PULSIN cmd times out 131msec
                               'this loop allows for catching most of the pulses
PULSIN OnePPS, 1, FreqPW       'sample OnePPS clock pulse width
Cycles =Cycles + 1
FreqPW = FreqPW * 2            '2us multiplier for BS2
IF FreqPW > 0 THEN             'Once a pulse is caught exit the loop
  Cycles = 8
ENDIF
'DEBUG "1PPS PW        ", DEC FreqPW, CR
LOOP
IF fast = "s" THEN
  CountTime = 10000
  COUNT OnePPS, CountTime, Freq          'Sample OnePPS for 10 sec
  DEBUG "1PPS Freq      ", DEC Freq, CR
ENDIF


'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 Freq, ",",DEC FreqPW,",", DEC VDC400,",", DEC VDC48,",",fast,",","*","," ,CR,$A]
SERIN ComRx,84,2000, No_Data, [fast]
IF fast = "l" THEN
  HIGH LoadOnOff
ENDIF
IF fast = "u" THEN
  LOW LoadOnOff
ENDIF
DEBUG "State of load: ", BIN1 LoadOnOff, CR
DEBUG "State o 1PPS : ", BIN1 OnePPSOnOff, CR
IF fast = "D" THEN
  LOW OnePPSOnOff
ELSEIF fast = "E" THEN
  HIGH OnePPSOnOff
ENDIF
No_Data:
GOTO TempCycle
END