
DEFINT A-Z

IoBase = &H220
TimeOut = 30000

OUT IoBase + 4, &H0 'Clear Command_FIFO & Data_FIFO
'GOSUB DemoTest2     'DI Configuration test
OUT IoBase + 4, &H0 'Clear Command_FIFO & Data_FIFO
'GOSUB DemoTest3     'DIO test
OUT IoBase + 4, &H0 'Clear Command_FIFO & Data_FIFO
'GOSUB DemoTest4
OUT IoBase + 4, &H0 'Clear Command_FIFO & Data_FIFO
'GOSUB DemoTest5
OUT IoBase + 4, &H0 'Clear Command_FIFO & Data_FIFO
'GOSUB DemoTest6
OUT IoBase + 4, &H0 'Clear Command_FIFO & Data_FIFO
GOSUB DemoTest7

END

'----------------------------------------------
'DI Configuration test
DemoTest2:
    FOR II = 0 TO 255
    Arg1 = II
    GOSUB WriteDiMode
    GOSUB ReadDiMode
    PRINT "Test2:DI Configuration test -->("; Arg1; ","; RetArg1; ")"
    GOSUB DelayTime
    NEXT II
    RETURN

'----------------------------------------------
'refer to LD_WriteDiMode
'input argument : Arg1= Di Mode
'return  1 : OK
'        0 : time out
WriteDiMode:
    GOSUB WaitWriteCommand
    IF RetVal THEN
       OUT IoBase, &H52 'WRITE_DI_MODE_CMD
       OUT IoBase, Arg1 'MODE
       RetVal = 1
       RETURN
       END IF
    RetVal = 0
    RETURN

'----------------------------------------------
'refer to LD_ReadDiMode
'return  1 : OK --> RetArg1=Di Mode
'        0 : time out
ReadDiMode:
    GOSUB WaitWriteCommand
    IF RetVal THEN
       OUT IoBase, &H92 'READ_DI_MODE_CMD
       GOSUB WaitReadData
       IF RetVal THEN
	  RetArg1 = INP(IoBase + 4) AND &HFF
	  RetVal = 1
	  RETURN
	  END IF
       RetVal = 0
       RETURN
       END IF
    RetVal = 0
    RETURN

'----------------------------------------------
'local subroutine only
'return  1 : OK
'        0 : time out
WaitWriteCommand:
    FOR I = 0 TO TimeOut
    CmdEmpty = INP(IoBase) AND &H4
    IF NOT CmdEmpty THEN
       RetVal = 1
       RETURN
    END IF
    NEXT I
    RetVal = 0
    RETURN

'----------------------------------------------
'local subroutine only
'return  1 : OK
'        0 : time out
WaitReadData:
    FOR I = 0 TO TimeOut
    ReadReady = INP(IoBase) AND &H2
    IF (ReadReady=&H2) THEN
       RetVal = 1
       RETURN
    END IF
    NEXT I
    RetVal = 0
    RETURN

'----------------------------------------------
'local subroutine only
'
DelayTime:
    FOR I = 0 TO 300
    NEXT I
    RETURN


'----------------------------------------------
'DIO test
DemoTest3:
    FOR II = 0 TO 255
    Arg1 = II
    GOSUB WriteDo
    GOSUB ReadDo
    DoVal = RetArg1
    GOSUB DelayTime
    GOSUB ReadDi
    PRINT "Test3:DIO test -->("; Arg1; ","; DoVal; ","; RetArg1; ")"
    GOSUB DelayTime
    NEXT II
    RETURN

'----------------------------------------------
'refer to LD_WriteDo
'input argument : Arg1= Do Value
'return  1 : OK
'        0 : time out
WriteDo:
    GOSUB WaitWriteCommand
    IF RetVal THEN
       OUT IoBase, &H51 'WRITE_DI_MODE_CMD
       OUT IoBase, Arg1 'Do value
       RetVal = 1
       RETURN
       END IF
    RetVal = 0
    RETURN

'----------------------------------------------
'refer to LD_ReadDo
'return  1 : OK --> RetArg1=Do value (read back)
'        0 : time out
ReadDo:
    GOSUB WaitWriteCommand
    IF RetVal THEN
       OUT IoBase, &H91 'READ_Do
       GOSUB WaitReadData
       IF RetVal THEN
	  RetArg1 = INP(IoBase + 4) AND &HFF
	  RetVal = 1
	  RETURN
       END IF
       RetVal = 0
       RETURN
    END IF
    RetVal = 0
    RETURN

'----------------------------------------------
'refer to LD_ReadDi
'return  1 : OK --> RetArg1=Di value
'        0 : time out
ReadDi:
    GOSUB WaitWriteCommand
    IF RetVal THEN
       OUT IoBase, &H90 'READ_DI
       GOSUB WaitReadData
       IF RetVal THEN
	  RetArg1 = INP(IoBase + 4) AND &HFF
	  RetVal = 1
	  RETURN
       END IF
       RetVal = 0
       RETURN
    END IF
    RetVal = 0
    RETURN

'----------------------------------------------
'refer to LD_SetChannelGain
'input argument : Arg1= wChannel,Arg2= wGain
'return  1 : OK
'        0 : time out
SetChannelGain:
    GOSUB WaitWriteCommand
    IF RetVal THEN
       OUT IoBase, &H54 'CMD_WRITE_PGA
       OUT IoBase, (Arg1 * 8 + INT(Arg2 / 2) * 16 + (Arg2 MOD 2) * 32)
       RetVal = 1
       RETURN
    END IF
    RetVal = 0
    RETURN

'----------------------------------------------
'refer to LD_ReadChannelGain
'return  1 : OK --> RetArg1=wChannel, RetArg2=wGain
'        0 : time out
ReadChannelGain:
    GOSUB WaitWriteCommand
    IF RetVal THEN
       OUT IoBase, &H94 'CMD_READ_PGA
       GOSUB WaitReadData
       IF RetVal THEN
	  T2 = INP(IoBase + 4) AND &HFF
	  T1 = T2 AND &H8
	  IF (T1) THEN RetArg1 = 1 ELSE RetArg1 = 0

	  T1 = T2 AND &H30
	  IF (T1 = &H30) THEN RetArg2 = 3
	  IF (T1 = &H20) THEN RetArg2 = 1
	  IF (T1 = &H10) THEN RetArg2 = 2
	  IF (T1 = &H0) THEN RetArg2 = 0

	  RetVal = 1
	  RETURN
       END IF
       RetVal = 0
       RETURN
    END IF
    RetVal = 0
    RETURN

'----------------------------------------------
'test
DemoTest4:
    FOR II = 0 TO 3
    Arg1 = 0
    Arg2 = II
    GOSUB SetChannelGain
    GOSUB ReadChannelGain
    PRINT "Test4:Arg1=0,Arg2="; Arg2; ",RetArg1="; RetArg1; ",RetArg2="; RetArg2
    NEXT II

    FOR II = 0 TO 3
    Arg1 = 1
    Arg2 = II
    GOSUB SetChannelGain
    GOSUB ReadChannelGain
    PRINT "Test4:Arg1=1,Arg2="; Arg2; ",RetArg1="; RetArg1; ",RetArg2="; RetArg2
    NEXT II
    RETURN

'----------------------------------------------
'refer to LD_SetPacerTimer
'input argument : Arg1= pacer divider count
'return  1 : OK
'        0 : time out
SetPacerTimer:
    GOSUB WaitWriteCommand
    IF RetVal THEN
       OUT IoBase, &H57 'CMD_SET_TIMER
       OUT IoBase, INT(Arg1 / 256)
       OUT IoBase, (Arg1 MOD 256)
       RetVal = 1
       RETURN
    END IF
    RetVal = 0
    RETURN

'----------------------------------------------
'refer to LD_ReadPacerTimer
'return  1 : OK --> RetArg1=pacer timer count
'        0 : time out
ReadPacerTimer:
    GOSUB WaitWriteCommand
    IF RetVal THEN
       OUT IoBase, &H97 'CMD_READ_TIMER
       GOSUB WaitReadData
       IF RetVal THEN
	  T1 = INP(IoBase + 4) AND &HFF
	  GOSUB WaitReadData
	  T2 = INP(IoBase + 4) AND &HFF
	  RetArg1 = T1 * 256 + T2
	  RetVal = 1
	  RETURN
       END IF
       RetVal = 0
       RETURN
    END IF
    RetVal = 0
    RETURN

'----------------------------------------------
'refer to LD_ReadAd
'by polling
'input argument : none
'return  1 : OK   --> RetArg1=12 bits AD data
'        0 : time out
ReadAd:
    GOSUB WaitWriteCommand
    IF RetVal THEN
       OUT IoBase, &HA0 'CMD_READ_AD
       GOSUB WaitReadData
       IF RetVal THEN
	  T1 = INP(IoBase + 4) AND &HFF
	  GOSUB WaitReadData
	  T2 = INP(IoBase + 4) AND &HFF
	  RetArg1 = T1 * 16 + T2 / 16
	  RetVal = 1
       RETURN
       END IF
       RetVal = 0
       RETURN
    END IF
    RetVal = 0
    RETURN

'----------------------------------------------
'test
DemoTest5:
     Arg1 = 0
     GOSUB WriteDaBias	 'set DA BIAS Value=0
     Arg1 = 1		 'channel_1
     Arg2 = 1		 'gain=10
     GOSUB SetChannelGain
     Arg1 = 999 	 '1K
     GOSUB SetPacerTimer

    FOR II = 0 TO 10
    GOSUB DelayTime	'delay for settling time
    NEXT II

    GOSUB ReadAd	'read out bug data

    PRINT "---------------------------------------------"
    FOR II = 0 TO 20
    GOSUB ReadAd	'polling
    PRINT "Test5: Ret="; RetVal; ",AD VALUE = "; RetArg1
    NEXT II
    RETURN

'----------------------------------------------
'refer to LD_WriteDaBias
'input argument : Arg1= 12 bits Da data
'return  1 : OK
'        0 : time out
WriteDaBias:
    GOSUB WaitWriteCommand
    IF RetVal THEN
       OUT IoBase, &H53 'CMD_WRITE_BIAS
       OUT IoBase, INT(Arg1 / 16)
       OUT IoBase, ((Arg1 MOD 16) * 16)
       RetVal = 1
       RETURN
       END IF
    RetVal = 0
    RETURN

'----------------------------------------------
'refer to LD_ReadDaBias
'return  1 : OK --> RetArg1=DA 12 bits data read back
'        0 : time out
ReadDaBias:
    GOSUB WaitWriteCommand
    IF RetVal THEN
       OUT IoBase, &H93 'CMD_READ_BIAS
       GOSUB WaitReadData
       IF RetVal THEN
	  T1 = INP(IoBase + 4) AND &HFF
	  GOSUB WaitReadData
	  T2 = INP(IoBase + 4) AND &HFF
	  RetArg1 = T1 * 16 + T2 / 16
	  RetVal = 1
	  RETURN
       END IF
       RetVal = 0
       RETURN
    END IF
    RetVal = 0
    RETURN


'----------------------------------------------
'test
DemoTest6:
    FOR II = 0 TO 4095
    Arg1 = II
    GOSUB WriteDaBias	'write DA BIAS Value
    PRINT "Test6: II = "; II
    NEXT II
    RETURN

'----------------------------------------------
'refer to LD_StartAdsPacer
'input argument : Arg1= count
'return  1 : OK
'        0 : time out
StartAdsPacer:
    GOSUB WaitWriteCommand
    IF RetVal THEN
       OUT IoBase, &HA2 'CMD_AD_SAMPLE
       OUT IoBase, Arg1 'count
       RetVal = 1
       RETURN
    END IF
    RetVal = 0
    RETURN

'----------------------------------------------
'refer to LD_ReadFifo
'input argument : none
'return  1 : OK   --> RetArg1=read 12 bits AD data from DATA_FIFO
'        0 : time out
ReadFifo:
       GOSUB WaitReadData
       IF RetVal THEN
	  T1 = INP(IoBase + 4) AND &HFF
       ELSE
	  RetVal = 0
	  RETURN
       END IF

       GOSUB WaitReadData
       IF RetVal THEN
	  T2 = INP(IoBase + 4) AND &HFF
       ELSE
	  RetVal = 0
	  RETURN
       END IF

       RetArg1 = T1 * 16 + T2 / 16
       RetVal = 1
       RETURN

'----------------------------------------------
'test
DemoTest7:
    Arg1 = 0
    GOSUB WriteDaBias	'set DA BIAS Value=0
    Arg1 = 1		'channel_1
    Arg2 = 0		'gain=1
    GOSUB SetChannelGain
    Arg1 = 999		'1K
    GOSUB SetPacerTimer

    FOR II = 0 TO 10
    GOSUB DelayTime	'delay for settling time
    NEXT II

    Arg1 = 1		'perform 1*256 AD operations
    GOSUB StartAdsPacer

    FOR II = 0 TO 255
    GOSUB ReadFifo	'read 12 bits AD data
    PRINT "(";RetArg1; ")";
    NEXT II
    RETURN


