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



DEFINT A-Z
DECLARE SUB A822Init (adr)
DECLARE SUB A822AD (Channel, UB, Gain)
DECLARE SUB A822DACH1 (voldata)
DECLARE SUB A822DACH2 (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

      INPUT "Input Voltage (0-5V) = "; davol!
    
      IF davol! = 99 THEN END
    
    
      DAdata = INT(davol! * 4095 / DArange) 'Voltage conversion D/A Data
    
      A822DACH1 DAdata                   'Write D/A Data to DA Channel 1
      A822DACH2 DAdata                   'Write D/A Data to DA Channel 2

    
      LOCATE 5, 20
      PRINT USING " D/A Channel 1 Vo=##.#### V"; davol!
      LOCATE 7, 20
      PRINT USING " D/A Channel 2 Vo=##.#### V"; davol!
     
     
WEND


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 Gain = .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

IF UB = 1 THEN ADGain = ADGain + &H4     'IF UB=0 then Bipolar Mode
                                         'IF UB=1 Then Unipolar Mode



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

IF UB = 1 THEN

   A822.ADVI = (A822.ADData) * 10 / Gain / 4096

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

END IF



END SUB

SUB A822DACH1 (DACdata)

IF DACdata >= &HFFF THEN DACdata = &HFFF


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



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

END SUB

SUB A822DACH2 (DACdata)

IF DACdata >= &HFFF THEN DACdata = &HFFF


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

OUT A822.IOAdr + &H6, DACLow
OUT A822.IOAdr + &H7, 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

