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



DEFINT A-Z
DECLARE SUB A821Init (adr)
DECLARE SUB A821AD (Channel, Gain)
DECLARE SUB A821DA (voldata)
DECLARE SUB A821DI ()
DECLARE SUB A821DO (dol, doh)

TYPE A821DATA
    
     IOAdr   AS INTEGER           'A821PG 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 A821 AS A821DATA
 
CLS
DArange = 5              'Analog Output Range 0~5 V
                         'IF Analog Output Rang 0~10V
                         'Let DArange=10


A821Init &H220           ' Set A821 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
     
      A821DA DAdata                   'Write D/A Data

     
      LOCATE 5, 20
      PRINT USING " D/A Vo=##.#### V"; davol!
     

      FOR delay = 1 TO 1000: NEXT delay
     
     
      IF INKEY$ = "q" THEN readloop = 1

WEND


END



SUB A821AD (Channel, Gain)
ChkAD = 0
ErrCoun = 0



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




ADGain = 0

IF Gain = 2 OR Gain = 10 THEN ADGain = 1
IF Gain = 4 OR Gain = 100 THEN ADGain = 2
IF Gain = 8 OR Gain = 1000 THEN ADGain = 3



A821.ErrorMesag = 0

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


WHILE ChkAD = 0

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

WEND
IF A821.ErrorMesag <> 0 THEN EXIT SUB

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

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

END SUB

SUB A821DA (DACdata)


DAChigh = INT(DACdata / 256)
DAClow = DACdata - DAChigh * 256


OUT A821.IOAdr + &H4, DAClow
OUT A821.IOAdr + &H5, DAChigh

END SUB

SUB A821DI

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

END SUB

SUB A821DO (dol, doh)

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

END SUB

SUB A821Init (adr)

ChkIo = 0
ChkNn = 0

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

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

