
'*******************************************************************         
'*                  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

A821Init &H220           ' Set A821 Base Address

readloop = 0
WHILE readloop = 0
FOR i = 0 TO 255

    A821DO i, i          ' Wtite Digital Output Low Byte & High Bite
    A821DI
    DIlow = a821.DIlow
    DIhigh = a821.DIhigh

   
    LOCATE 2.5

   
   
    PRINT "Digital Output Low Byte Data= "; i

    PRINT "     Digital Input Low Byte Data="; DIlow

    PRINT "Digital Output High Byte Data ="; i

    PRINT "     Digital Input High ?Byte Data="; DIhigh

    
    

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


NEXT i







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

