
'*******************************************************************        
'*                  A822 DEMO Program For Quic Basic               *
'*                  Function For A/D                               *
'*******************************************************************



DEFINT A-Z
DECLARE SUB A822Init (adr)
DECLARE SUB A822AD (Channel, Gain)
DECLARE SUB A822DA (voldata)
DECLARE SUB A822DI ()
DECLARE SUB A822DO (dol, doh)

TYPE A822DATA
    
     IOAdr   AS INTEGER           'A822PG Base Address
     ADVI    AS SINGLE            'Analog Input Voltage
     ADData  AS INTEGER           'A/D Conversion data
     DIHigh  AS INTEGER           'Digital Input High Byte
     DILow   AS INTEGER           'Digital Input Low Byte

     ErrorMesag  AS INTEGER

END TYPE
DIM SHARED A822 AS A822DATA
 
CLS
DArange = 5              'Analog Output Range 0~5 V
                         'IF Analog Output Rang 0~10V
                         'Let DArange=10


A822Init &H220           ' Set A822 Base Address

readloop = 0

WHILE readloop = 0

      FOR ch = 0 TO 15


      A822AD ch, 1               ' Set A/D Channel & Gain
      ADVI! = A822.ADVI          ' A822PGH Gain= 0.5,1,5,10,,50,100,500,1000
                                 ' A822PGL Gain=0.5,1,2,4,8

     
      LOCATE 2 + ch, 5
      PRINT USING " A/D Channel ## "; ch;
      PRINT USING " A/D V=##.#### V"; ADVI!
     
      
     
      IF INKEY$ = "q" THEN readloop = 1
      NEXT ch

WEND


END             '======================================================  END


SUB A822AD(Channel,UB, Gain)  ' ========================Set A822 Channel & Gain

ChkAD = 0
ErrCoun = 0

IF Channel > 15 THEN A822.ErrorMesag = 2
IF A822.IOAdr = 0 THEN A822.ErrorMesag = -1


ADGain = 0

if ub=0 then

   IF Gain = 0.5 THEN ADGain=&h8
   IF Gain = 2 OR Gain = 10 THEN ADGain = &h1
   IF Gain = 4 OR Gain = 100 THEN ADGain = &h2
   IF Gain = 8 OR Gain = 1000 THEN ADGain = &h3
   IF Gain = 5 THEN ADGain = &h9
   IF Gain = 50 THEN ADGain=&H10
   IF Gain = 500 THEN ADGain=&h11
end if


A822.ErrorMesag = 0

OUT A822.IOAdr + &HB, 1
OUT A822.IOAdr + &HA, Channel
OUT A822.IOAdr + &H9, ADGain
OUT A822.IOAdr + &HC, 0


WHILE ChkAD = 0

     
      ADHigh = INP(A822.IOAdr + &H5)
      IF ADHigh <= 15 THEN ChkAD = 1
      ErrCoun = ErrCoun + 1
      IF ErrCoun >= 1000 THEN A822.ErrorMesag = 4: EXIT SUB

WEND
IF A822.ErrorMesag <> 0 THEN EXIT SUB

ADLow = INP(A822.IOAdr + &H4)
A822.ADData = ADHigh * 256 + ADLow

A822.ADVI = (A822.ADData - 2048) * 5 / Gain / 2048

END SUB

SUB A822DA (DACdata)


DACHigh = INT(DACdata / 255)
DACLow = INT(DACdata - DACHigh * 255)



OUT A822.IOAdr + &H4, DACLow
OUT A822.IOAdr + &H5, DACHigh

END SUB

SUB A822DI

A822.DILow = INP(A822.IOAdr + &H6)
A822.DIHigh = INP(A822.IOAdr + &H7)

END SUB

SUB A822DO (dol, doh)

OUT A822.IOAdr + &HD, dol
OUT A822.IOAdr + &HE, doh

END SUB

SUB A822Init (adr)

ChkIo = 0
ChkNn = 0

A822.IOAdr = adr
OUT A822.IOAdr + &HB, 1
OUT A822.IOAdr + &HC, 0

WHILE ChkIo = 0
      IF INP(A822.IOAdr + &H5) <= 15 THEN ChkIo = 1
      ChkNn = ChkNn + 1
      IF ChkNn = 5000 THEN A822.ErrorMesag = 1: ChkIo = 1
WEND
END SUB

