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

  1. /***********************************************************************
  2.  
  3. FILE
  4.     dac2.c  -  driver for Metrabyte DAC-02 digital to analog i/o board
  5.  
  6. ROUTINES
  7.     da_init    -  initialize D/A
  8.     da_limits  -  return output limits of D/A
  9.     da_write   -  D/A output
  10.  
  11. REMARKS
  12.     The DAC-02 is a 2-channel 12-bit DAC using 8 consequetive i/o
  13.     addresses.  Data has to be "left-justified" and sent out in 2 bytes,
  14.     least significant byte first.  One can also use the 16-bit i/o
  15.     instructions, but we will not do so here.
  16.  
  17. LAST UPDATE
  18.     1 May 1985
  19.  
  20.     Copyright (c) 1985  D.M. Auslander and C.H. Tham
  21.  
  22. ***********************************************************************/
  23.  
  24. /***********************************************************************
  25.                             I M P O R T S
  26. ***********************************************************************/
  27.  
  28. #include <stdio.h>
  29.  
  30. #include "envir.h"          /* environment declarations */
  31. #include "inout.h"          /* i/o mapping macros */
  32. #include "dac2.h"           /* exported declarations for this module */
  33.  
  34.  
  35. /***********************************************************************
  36.                      P R I V A T E     D A T A
  37. ***********************************************************************/
  38.  
  39. #define DABASE  0x330           /* base address of D/A */
  40.  
  41. #define DAMIN   -2048           /* min. output value accepted */
  42. #define DAMAX    2047           /* max. output value accepted */
  43.  
  44. #define DALO0   DABASE          /* D/A 0 low byte */
  45. #define DAHI0   DABASE+1        /* D/A 0 high byte */
  46. #define DALO1   DABASE+2        /* D/A 1 low byte */
  47. #define DAHI1   DABASE+3        /* D/A 1 high byte */
  48.  
  49.  
  50. /***********************************************************************
  51.                    E N T R Y     R O U T I N E S
  52. ***********************************************************************/
  53.  
  54. /*----------------------------------------------------------------------
  55. PROCEDURE
  56.     DA_LIMITS  -  returns D/A output limits.
  57.  
  58. SYNOPSIS
  59.     void da_limits(lo, hi)
  60.     int *lo, *hi;
  61.  
  62. PARAMETERS
  63.     lo  -  pointer to low limit
  64.     hi  -  pointer to high limit
  65.  
  66. REMARKS
  67.     The limits are returned by means of side effects.  This routine 
  68.     enables other routines to perform limit checks in a more device
  69.     independent manner.
  70.  
  71. LAST UPDATE
  72.     1 May 1985
  73. ----------------------------------------------------------------------*/
  74.  
  75. void da_limits(lo, hi)
  76. int *lo, *hi;
  77. {
  78.  
  79.     *lo = DAMIN;
  80.     *hi = DAMAX;
  81.  
  82. }
  83.  
  84.  
  85.  
  86. /*----------------------------------------------------------------------
  87. FUNCTION
  88.     DA_WRITE  -  write output to D/A
  89.  
  90. SYNOPSIS
  91.     int da_write(chan, value)
  92.     int chan, value;
  93.  
  94. PARAMETERS
  95.     chan  -  channel number (0 or 1)
  96.     value -  output value
  97.  
  98. RETURNS
  99.     0  if all is well,  -1 if error
  100.  
  101. REMARKS
  102.     The channel number is checked, but no range checking is performed on
  103.     the output value in the interest of speed.  The calling routine is 
  104.     responsible for such checks - that's what da_limits() is for.
  105.  
  106.     The DAC uses an offset binary format where 0 will put out -10 volts
  107.     and 4095 will put out +10 volts.
  108.  
  109.     The output value is scaled such that:
  110.  
  111.             -2048     --->     -10V
  112.                 0     --->       0V
  113.             +2047     --->     +10V
  114.  
  115. LAST UPDATE
  116.     1 May 1985
  117. ----------------------------------------------------------------------*/
  118.  
  119. int da_write(chan, value)
  120. int chan, value;
  121. {
  122.     int rval;       /* error indication return value */
  123.     
  124.  
  125.     value = 0x800 + value;          /* convert to offset binary */
  126.  
  127.     switch (chan)
  128.     {
  129.         case 0:     /* channel 0 */
  130.  
  131.             out(DALO0, value << 4);         /* send low byte */
  132.             out(DAHI0, value >> 4);         /* send high byte */
  133.  
  134.             rval = 0;                       /* indicate all O.K. */
  135.  
  136.             break;
  137.         
  138.         case 1:     /* channel 1 */
  139.  
  140.             out(DALO1, value << 4);
  141.             out(DAHI1, value >> 4);
  142.  
  143.             rval = 0;
  144.  
  145.             break;
  146.  
  147.         default:    /* bad channel number */
  148.  
  149.             rval = -1;
  150.             break;
  151.     }
  152.  
  153.     return(rval);
  154. }
  155.  
  156.  
  157.  
  158. /***********************************************************************
  159.           I N I T I A L I Z A T I O N    R O U T I N E
  160. ***********************************************************************/
  161.  
  162. /*----------------------------------------------------------------------
  163. PROCEDURE
  164.     DA_INIT  -  initialize D/A
  165.  
  166. SYNOPSIS
  167.     void da_init()
  168.  
  169. REMARKS
  170.     Initialzie both output channels to 0 volts.
  171.  
  172. LAST UPDATE
  173.     1 May 1985
  174. ----------------------------------------------------------------------*/
  175.  
  176. void da_init()
  177. {
  178.  
  179.     out(DALO0, 0x00);               /* zero channel 0 */
  180.     out(DAHI0, 0x80);
  181.  
  182.     out(DALO1, 0x00);               /* zero channel 1 */
  183.     out(DAHI1, 0x80);
  184.  
  185. }
  186.  
  187.