home *** CD-ROM | disk | FTP | other *** search
- /***********************************************************************
-
- FILE
- dash8.c - driver for Metrabyte DASH-8 analog to digital i/o board
-
- ROUTINES
- ad_init - initialize A/D
- ad_start - start an A/D conversion
- ad_read - read A/D channel
- ad_wread - start conversion and wait for result
-
- REMARKS
- The DASH-8 is a 8-channel 12-bit ADC with a typical conversion time
- of 25 microseconds (35 microseconds max.). Analog range is +/- 5V.
- Although the DASH-8 has options for faster 8-bit conversions, on-
- board 8253 timer/counter, digital i/o lines and interrupt operation,
- we shall not use them in the interest of simplicity.
-
- LAST UPDATE
- 1 May 1985
-
- Copyright (c) 1985 D.M. Auslander and C.H. Tham
-
- ***********************************************************************/
-
- /***********************************************************************
- I M P O R T S
- ***********************************************************************/
-
- #include <stdio.h>
-
- #include "envir.h" /* environment declarations */
- #include "inout.h" /* i/o mapping macros */
- #include "dash8.h" /* exported declarations for this module */
-
-
- /***********************************************************************
- P R I V A T E D A T A
- ***********************************************************************/
-
- #define MINCHAN 0 /* lowest valid channel number */
- #define MAXCHAN 7 /* highest valid channel number */
-
- #define ADMAX 0x0FFF /* max value of A/D */
- #define ADMIN 0 /* min value returned by A/D */
-
- #define ADBASE 0x340 /* base address of A/D */
-
- #define ADLO ADBASE /* low byte of A/D */
- #define ADHI ADBASE+1 /* high byte of A/D */
- #define ADSTAT ADBASE+2 /* A/D status port */
- #define ADCTL ADBASE+2 /* A/D control port */
-
- #define ADBUSY 0x80 /* A/D not-finished bit */
-
- static char adctl = 0; /* A/D control word */
-
-
- /***********************************************************************
- E N T R Y R O U T I N E S
- ***********************************************************************/
-
- /*----------------------------------------------------------------------
- PROCEDURE
- AD_START - initiate A/D conversion
-
- SYNOPSIS
- void ad_start(channel)
- int channel;
-
- LAST UPDATE
- 18 November 1987
- ----------------------------------------------------------------------*/
-
- void ad_start(channel)
- int channel;
- {
-
- if ((channel >= MINCHAN) && (channel <= MAXCHAN))
- {
- adctl &= ~0x7; /* clear channel select bits */
- adctl |= channel; /* set channel select bits */
-
- out(ADCTL, adctl); /* select channel */
- out(ADHI, 0); /* start 12-bit conversion */
- }
- else
- {
- printf("ad_start: channel out of range\n");
- }
-
- }
-
-
-
- /*----------------------------------------------------------------------
- PROCEDURE
- AD_READ - read A/D channel
-
- SYNOPSIS
- int ad_read(channel)
- int channel;
-
- RETURNS
- signed 12-bit value
-
- REMARKS
- The data format is:
-
- DASH-8 value voltage return value
-
- 0x0 -5.0000 V 0xF800
- 0x800 0 V 0
- 0xFFF +4.9976 V 0x07FF
-
- The return value is transformed to a signed integer between -2048
- to +2047 corresponding to -5V and +5V since there is an intuitive
- correspondence between signed values and signed voltages.
-
- If the channel argument is out of range, ad_read() returns 0.
-
- LAST UPDATE
- 18 November 1987
- ----------------------------------------------------------------------*/
-
- int ad_read(channel)
- int channel;
- {
- int rval; /* return value */
-
-
- if ((channel >= MINCHAN) && (channel <= MAXCHAN))
- {
- while (in(ADSTAT) & ADBUSY) /* wait for A/D to finish */
- ;
-
- rval = (in(ADLO) >> 4) & 0x000F; /* get 12-bit value */
- rval |= (in(ADHI) << 4) & 0x0FF0; /* in normal format */
-
- rval -= 0x800; /* convert to signed value */
- }
- else
- {
- printf("ad_read: channel out of range\n");
-
- rval = 0;
- }
-
- return(rval);
- }
-
-
-
- /*----------------------------------------------------------------------
- FUNCTION
- AD_WREAD - initiate A/D and wait for result
-
- SYNOPSIS
- int ad_wread(channel)
- int channel;
-
- PARAMETER
- channel - channel number (0..7)
-
- RETURNS
- signed 12 bit value
-
- REMARKS
- A convenient combination of ad_start() and ad_read().
-
- LAST UPDATE
- 18 November 1987
- ----------------------------------------------------------------------*/
-
- int ad_wread(channel)
- int channel;
- {
-
- if ((channel >= MINCHAN) && (channel <= MAXCHAN))
- {
- ad_start(channel);
- return(ad_read(channel));
- }
- else
- {
- printf("ad_wread: channel out of range\n");
- return(0);
- }
-
- }
-
-
-
- /***********************************************************************
- I N I T I A L I Z A T I O N R O U T I N E
- ***********************************************************************/
-
- /*----------------------------------------------------------------------
- PROCEDURE
- AD_INIT - initialize A/D
-
- SYNOPSIS
- void ad_init()
-
- REMARKS
- Initialize to power-up state: interrupts disabled, digital outputs
- zero and channel select at zero.
-
- LAST UPDATE
- 1 May 1985
- ----------------------------------------------------------------------*/
-
- void ad_init()
- {
-
- adctl = 0; /* reset control word map variable */
- out(ADCTL, 0); /* reset A/D itself */
- }
-