home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 15 / 15.iso / s / s300 / 1.ddi / CHAP1 / DASH8.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-05  |  5.8 KB  |  220 lines

  1. /***********************************************************************
  2.  
  3. FILE
  4.     dash8.c  -  driver for Metrabyte DASH-8 analog to digital i/o board
  5.  
  6. ROUTINES
  7.     ad_init    -  initialize A/D
  8.     ad_start   -  start an A/D conversion
  9.     ad_read    -  read A/D channel
  10.     ad_wread   -  start conversion and wait for result
  11.  
  12. REMARKS
  13.     The DASH-8 is a 8-channel 12-bit ADC with a typical conversion time
  14.     of 25 microseconds (35 microseconds max.).  Analog range is +/- 5V.
  15.     Although the DASH-8 has options for faster 8-bit conversions, on-
  16.     board 8253 timer/counter, digital i/o lines and interrupt operation,
  17.     we shall not use them in the interest of simplicity.
  18.     
  19. LAST UPDATE
  20.     1 May 1985
  21.  
  22.     Copyright (c) 1985  D.M. Auslander and C.H. Tham
  23.  
  24. ***********************************************************************/
  25.  
  26. /***********************************************************************
  27.                             I M P O R T S
  28. ***********************************************************************/
  29.  
  30. #include <stdio.h>
  31.  
  32. #include "envir.h"          /* environment declarations */
  33. #include "inout.h"          /* i/o mapping macros */
  34. #include "dash8.h"          /* exported declarations for this module */
  35.  
  36.  
  37. /***********************************************************************
  38.                      P R I V A T E     D A T A
  39. ***********************************************************************/
  40.  
  41. #define MINCHAN     0           /* lowest valid channel number */
  42. #define MAXCHAN     7           /* highest valid channel number */
  43.  
  44. #define ADMAX   0x0FFF          /* max value of A/D */
  45. #define ADMIN   0               /* min value returned by A/D */
  46.  
  47. #define ADBASE  0x340           /* base address of A/D */
  48.  
  49. #define ADLO    ADBASE          /* low byte of A/D */
  50. #define ADHI    ADBASE+1        /* high byte of A/D */
  51. #define ADSTAT  ADBASE+2        /* A/D status port */
  52. #define ADCTL   ADBASE+2        /* A/D control port */
  53.  
  54. #define ADBUSY  0x80            /* A/D not-finished bit */
  55.  
  56. static char adctl = 0;          /* A/D control word */
  57.  
  58.  
  59. /***********************************************************************
  60.                    E N T R Y     R O U T I N E S
  61. ***********************************************************************/
  62.  
  63. /*----------------------------------------------------------------------
  64. PROCEDURE
  65.     AD_START  -  initiate A/D conversion
  66.  
  67. SYNOPSIS
  68.     void ad_start(channel)
  69.     int channel;
  70.  
  71. LAST UPDATE
  72.     18 November 1987
  73. ----------------------------------------------------------------------*/
  74.  
  75. void ad_start(channel)
  76. int channel;
  77. {
  78.  
  79.     if ((channel >= MINCHAN) && (channel <= MAXCHAN))
  80.     {
  81.         adctl &= ~0x7;              /* clear channel select bits */
  82.         adctl |= channel;           /* set channel select bits */
  83.  
  84.         out(ADCTL, adctl);          /* select channel */
  85.         out(ADHI, 0);               /* start 12-bit conversion */
  86.     }
  87.     else
  88.     {
  89.         printf("ad_start: channel out of range\n");
  90.     }
  91.  
  92. }
  93.  
  94.  
  95.  
  96. /*----------------------------------------------------------------------
  97. PROCEDURE
  98.     AD_READ  -  read A/D channel
  99.  
  100. SYNOPSIS
  101.     int ad_read(channel)
  102.     int channel;
  103.  
  104. RETURNS
  105.     signed 12-bit value
  106.  
  107. REMARKS
  108.     The data format is:
  109.  
  110.         DASH-8 value    voltage     return value
  111.  
  112.          0x0           -5.0000 V      0xF800
  113.          0x800               0 V           0
  114.          0xFFF         +4.9976 V      0x07FF
  115.  
  116.     The return value is transformed to a signed integer between -2048
  117.     to +2047 corresponding to -5V and +5V since there is an intuitive
  118.     correspondence between signed values and signed voltages.
  119.  
  120.     If the channel argument is out of range, ad_read() returns 0.
  121.  
  122. LAST UPDATE
  123.     18 November 1987
  124. ----------------------------------------------------------------------*/
  125.  
  126. int ad_read(channel)
  127. int channel;
  128. {
  129.     int rval;       /* return value */
  130.  
  131.  
  132.     if ((channel >= MINCHAN) && (channel <= MAXCHAN))
  133.     {
  134.         while (in(ADSTAT) & ADBUSY)         /* wait for A/D to finish */
  135.             ;
  136.         
  137.         rval = (in(ADLO) >> 4) & 0x000F;    /* get 12-bit value */
  138.         rval |= (in(ADHI) << 4) & 0x0FF0;   /* in normal format */
  139.  
  140.         rval -= 0x800;              /* convert to signed value */
  141.     }
  142.     else
  143.     {
  144.         printf("ad_read: channel out of range\n");
  145.  
  146.         rval = 0;
  147.     }
  148.  
  149.     return(rval);
  150. }
  151.  
  152.  
  153.  
  154. /*----------------------------------------------------------------------
  155. FUNCTION
  156.     AD_WREAD  -  initiate A/D and wait for result
  157.  
  158. SYNOPSIS
  159.     int ad_wread(channel)
  160.     int channel;
  161.  
  162. PARAMETER
  163.     channel  -  channel number (0..7)
  164.  
  165. RETURNS
  166.     signed 12 bit value
  167.  
  168. REMARKS
  169.     A convenient combination of ad_start() and ad_read().
  170.  
  171. LAST UPDATE
  172.     18 November 1987
  173. ----------------------------------------------------------------------*/
  174.  
  175. int ad_wread(channel)
  176. int channel;
  177. {
  178.  
  179.     if ((channel >= MINCHAN) && (channel <= MAXCHAN))
  180.     {
  181.         ad_start(channel);
  182.         return(ad_read(channel));
  183.     }
  184.     else
  185.     {
  186.         printf("ad_wread: channel out of range\n");
  187.         return(0);
  188.     }
  189.  
  190. }
  191.  
  192.  
  193.  
  194. /***********************************************************************
  195.           I N I T I A L I Z A T I O N    R O U T I N E
  196. ***********************************************************************/
  197.  
  198. /*----------------------------------------------------------------------
  199. PROCEDURE
  200.     AD_INIT  -  initialize A/D
  201.  
  202. SYNOPSIS
  203.     void ad_init()
  204.  
  205. REMARKS
  206.     Initialize to power-up state: interrupts disabled, digital outputs
  207.     zero and channel select at zero.
  208.  
  209. LAST UPDATE
  210.     1 May 1985
  211. ----------------------------------------------------------------------*/
  212.  
  213. void ad_init()
  214. {
  215.  
  216.     adctl = 0;              /* reset control word map variable */
  217.     out(ADCTL, 0);          /* reset A/D itself */
  218. }
  219.  
  220.