home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / cpm / utils / asmutl / hd64180a.lbr / IOAD.AZM / IOAD.ASM
Assembly Source File  |  1991-08-04  |  7KB  |  299 lines

  1. ;----------------------------------------------------------------
  2. ;      This module reads the I/O Bus Driven A-To-D card
  3. ;
  4. ; Written     By Richard Holmes        06-12-86
  5. ; Last Update By Richard Holmes        06-12-86
  6. ;----------------------------------------------------------------
  7. ;
  8.     maclib    z80
  9.     maclib    core
  10. ;
  11.     public    ini$ad,rd$ad
  12.     public    scan$ad
  13. ;
  14.     extrn    cie,coe,cst,pdde,ptxt
  15.     extrn    phde,crlf
  16. ;
  17. ;----------------------------------------------------------------
  18. ;     ---- A to D equates ----
  19. ;
  20. ; Note that all the signals are LOW going to that to generate the actual
  21. ; bit image ANDs and not or's must be used.
  22. cs$bar    equ    0000$1110b        ; Chip select
  23. rd$bar    equ    0000$1101b        ; Read from ADC-1205
  24. wr$bar    equ    0000$1011b        ; Write to DC-1205
  25. ss$bar    equ    0000$0101b        ; Status from ADC-1205
  26. ;
  27. ; Setup what the bits mean
  28. ;
  29. sars    equ    6            ;bit 6 = Conversion in progress
  30. byst    equ    2            ;    2 = 0 for high byte, 1 for low
  31. eoc    equ    1            ;    1 = 1 for end of conversion
  32. int    equ    0            ;    0 = 1 for data ready to be read
  33. ;
  34. ; Setup the port addresses
  35. ;
  36. data    equ    @gp3a            ; General purpose port 3
  37. cntl    equ    data + 1
  38. mode    equ    data + 3        ; 8255 mode
  39. ;
  40. ; 8255 modes of operation
  41. ;
  42. rd$mod    equ    090h            ; B = outputs, A = inputs
  43. wr$mod    equ    080h            ; B = outputs, A = outputs
  44. ;
  45. ;----------------------------------------------------------------
  46. ;    Initialize the A to D chip by disabling all its inputs
  47. ;
  48. ;----------------------------------------------------------------
  49. ;
  50. ini$ad:
  51.     mvi    a,rd$mod
  52.     out    mode
  53.     mvi    a,cs$bar and wr$bar and ss$bar    ; Chip reset
  54.     out    cntl
  55.     mvi    a,0ffh
  56.     out    cntl            ; All inputs high = dead effectively
  57.     ret
  58. ;
  59. ;----------------------------------------------------------------
  60. ;       Read the A-D channel. Return data in HL.
  61. ;
  62. ; On entry
  63. ;   A = channel
  64. ;
  65. ; On Exit
  66. ;  HL = data
  67. ;  All registers except A and HL preserved
  68. ;  A to D error sets HL to 
  69. ;                         FFFF for converter busy on entry
  70. ;                         FFFE for cannot begin conversion
  71. ;              FFFD for cannot complete conversion
  72. ;----------------------------------------------------------------
  73. ;
  74. rd$ad:
  75.     push    d
  76.     push    b
  77. ; Select read status mode
  78.     mvi    a,rd$mod
  79.     out    mode            ; 8255 reading data port
  80. ;
  81. ; Status is on the data port now
  82. ;
  83.     mvi    b,50            ; Tries
  84. wait$ready:
  85.     mvi    a,cs$bar and ss$bar
  86.     out    cntl            ; Read status to see whats going on
  87.     in    data
  88.     push    psw
  89.     mvi    a,0ffh
  90.     out    cntl
  91.     pop    psw
  92. ;
  93.     bit    sars,a            ; Are we converting ?
  94.     jrz    start$conversion
  95.     djnz    wait$ready
  96.     lxi    h,0ffffh        ; Converter busy on entry
  97.     jr    ioad$end
  98. ;
  99. ; Here we can start the conversion.
  100. ;
  101. start$conversion:
  102.     mvi    a,cs$bar and wr$bar    ; Selects a write = start conversion
  103.     out    cntl
  104.     nop
  105.     mvi    a,0ffh
  106.     out    cntl            ; Let it work a little
  107. ; A small delay and see if conversion started
  108.     nop
  109.     nop
  110.     nop
  111.     nop
  112.     mvi    a,cs$bar and ss$bar    ; Status read, SARS must be high
  113.     out    cntl
  114.     in    data            ; Read the status now from the chip
  115. ; Save data, de-select A-D, restore data
  116.     push    psw
  117.     mvi    a,0ffh
  118.     out    cntl            ; Turn off the logic again;
  119.     pop    psw
  120. ;
  121.     bit    sars,a            ; SARS high = converting
  122.     jrnz    ad$started
  123.     mvi    a,0fffeh        ; Cannot start flag
  124.     jr    ioad$end
  125. ;
  126. ; Here the A-D started. We wait a little the try to read the data
  127. ;
  128. ad$started:
  129.     mvi    b,35            ; this is ~ 90uS
  130. ad$wait:
  131.     nop
  132.     djnz    ad$wait
  133. ;
  134. ;Load a re-try counter now to get data back from converter
  135. ;
  136.     mvi    b,20            ; Retry counter to ensure conversion
  137. ad$read:
  138.     mvi    a,cs$bar and ss$bar    ; Select a status read only
  139.     out    cntl            ; Read mode now, real quick
  140.     in    data
  141. ; Save data, de-select A-D, restore data
  142.     push    psw
  143.     mvi    a,0ffh
  144.     out    cntl            ; Turn off the logic again;
  145.     pop    psw
  146. ; Test for end of conversion.
  147.     bit    int,a            ; 1 = end of conversion, read the data
  148.     jrnz    get$data
  149.     djnz    ad$read
  150. ; Here, the converter timed out. Signal it.
  151.     mvi    a,0fffdh
  152.     jr    ioad$end
  153. ;
  154. ; Here, A = the status with end of conversion indicated.
  155. ; We now see if it is the high byte and if so, read data.
  156. ;
  157. get$data:
  158.     bit    byst,a
  159.     jrz    get$data$high        ; byst low = high byte in latches
  160. ; Here we got a low byte
  161.     mvi    a,cs$bar and rd$bar
  162.     out    cntl            ; Read the first (high byte);
  163.     nop
  164.     mvi    a,0ffh
  165.     out    cntl
  166.     nop
  167. ;
  168. get$data$high:
  169.     lxi    d,0            ; Compare
  170.     mvi    b,10
  171. ;
  172. ; Loop here to get data and make sure stable
  173. ;
  174. get$data$loop:
  175.     mvi    a,cs$bar and rd$bar
  176.     out    cntl            ; Read the first (high byte);
  177.     nop
  178.     nop                ; Allow cables to settle ?
  179.     nop
  180.     in    data            ; Read chip
  181.     ani    0001$1111b        ; 12 bit A-D remember. 13th = sign
  182.     mov    h,a
  183.     mvi    a,0ffh
  184.     out    cntl            ; This clock second byte into latches
  185.     nop
  186.     nop
  187.     mvi    a,cs$bar and rd$bar
  188.     out    cntl            ; Read the first (high byte);
  189.     nop
  190.     in    data            ; Read chip
  191.     mov    l,a
  192.     mvi    a,0ffh
  193.     out    cntl            ; This clock second byte into latches
  194. ;
  195. ; See if data stable
  196. ;
  197.     mov    a,l
  198.     cmp    e            ; L = E ?
  199.     jrnz    not$same
  200.     mov    a,h
  201.     cmp    d
  202.     jrz    ioad$end
  203. ;
  204. not$same:
  205.     xchg                ; Hl -> DE looking for a match
  206.     djnz    get$data$loop
  207.     lxi    h,0
  208. ;
  209. ioad$end:
  210.     pop    b
  211.     pop    d
  212.     ret
  213. ;
  214. ;----------------------------------------------------------------
  215. ; Scan the A-D converter and display the values as they change.
  216. ; Indicate any error and any change in the error status.
  217. ;
  218. ; On Exit
  219. ;  All registers presumed lost
  220. ;----------------------------------------------------------------
  221. ;
  222. scan$ad:
  223.     call    ini$ad            ; Initialize converter
  224.     call    rd$ad
  225.     jr    new$ad
  226. ;
  227. ; Now, read and display any changes.
  228. ; Exit when any key is pressed.
  229. ;
  230. scan$loop:
  231.     call    cst
  232.     jnz    scan$end
  233.     call    rd$ad            ; Read converter
  234.     lda    old$ad            ; Compare to previous values
  235.     cmp    l            ; Same ?
  236.     jrnz    new$ad
  237.     lda    old$ad + 1
  238.     cmp    h            ; Same here = both the same. No change
  239.     jrz    scan$loop
  240. new$ad:
  241.     shld    old$ad
  242.     call    crlf
  243. ; Is it an Error ?
  244.     mov    a,h
  245.     cpi    0ffh            ; Error flag
  246.     jrz    ad$error
  247. ; Else display the value
  248.     xchg
  249.     call    pdde
  250.     jmp    scan$loop
  251. ;
  252. ad$error:
  253.     xchg
  254.     call    phde
  255.     lxi    d,error0
  256.     mov    a,e            ; E = the sub-code of the error
  257.     cpi    0ffh            ; Converter busy at start
  258.     jrz    ad$msg
  259. ;
  260.     lxi    d,error1
  261.     cpi    0feh            ; Cannot start conversion
  262.     jrz    ad$msg
  263. ;
  264.     lxi    d,error2
  265.     cpi    0fdh
  266.     jrz    ad$msg
  267. ; Else a silly error
  268.     lxi    d,errorN
  269. ad$msg:
  270.     call    ptxt
  271.     jmp    scan$loop
  272. ;
  273. scan$end:
  274.     call    cie
  275.     ret
  276. ;
  277. error0:    db    ' Cannot start - Converter busy$'
  278. error1:    db    ' Cannot start - Converter wont start$'
  279. error2:    db    ' Cannot complete conversion - Stuck$'
  280. errorN:    db    ' Un-Recognized A-D error$'
  281. ;
  282. ; ---- Data ----
  283. ;
  284.     dseg
  285. old$ad    ds    2
  286. ;
  287. ; ------------------------------
  288. ; ----                      ----
  289. ; ---- End of program - Bye ----
  290. ; ----                      ----
  291. ; ------------------------------
  292. ;
  293.     end
  294. ;
  295. ;
  296.