atod - analog-to-digital converter user functions


#include <atod.h>

adread (chan, pvalue)
int chan, *pvalue;

adset (nsamp, pindex, perrcnt, chan, vec)
int nsamp, *pindex, *perrcnt;
int chan, *vec;

adsetv (nsamp, pindex, perrcnt, cv, vv)
int nsamp, *pindex, *perrcnt;
int *cv, **vv;



Two sets of subroutines serve as interfaces to the analog-to-digital converter. adread is for collecting a single sample; the set of adset, adsetv, adbeg, adend is for collecting multiple samples under interrupt control. Both are described below.

Single Sampling
The adread macro reads a single analog-to-digital converted value under program control (interrupts disabled). It starts a conversion for the channel specified in chan , waits until the end-of-conversion flag is set, then stores the converted value at address passed as pvalue and returns 0.

Multiple Sampling
The subroutine package consisting of adset, adsetv, adbeg, and adend controls the collection of multiple analog-to-digital input samples under interrupt control. It collects and automatically stores multiple samples from one or more channels.

adset initializes the analog-to-digital converter for sampling a single channel chan(1 - 16) and storing the results in the vector beginning at vec. adsetv initializes the analog-to-digital converter for sampling multiple channels. cv points to an array of channels to sample; vv points to an array of pointers to vectors for storing the samples. cv and vv must contain the same number of elements and the last element in each array must be 0.

nsamp is the maximum number of samples to collect (the storage arrays must be dimensioned at least as large as nsamp ).

pindex is a pointer to an index for keeping track of the last position filled in the sampling vectors. While adset and adsetv intitially set the index to -1, the user may reset it at any time afterwards. To begin storing samples at the nth element in the array (where n = 0 to nsamp -1), the index must be set to n-1. For example, to begin storing samples at array element 4 pindex must be set to 3; to begin storing samples at array element 0 pindex must be set to -1.

perrcnt is the address of an error count that will be incremented each time a conversion error occurs (this will happen if the sampling rate is too fast or if the handling of an interrupt is delayed too long by some other process). Calling adset or adsetv clears the error count.

adset and adsetv enable the end-of-conversation interrupt. They also call signal(2F) with the arguments SIGADC and SIG_IGN to specify that the end-of-conversion signal should be ignored. The user may choose not to ignore the signal by calling signal(2F) after calling adset and adsetv and specifying the address of a function for catching this signal.

While adset and adsetv specify control, adbeg begins the sampling process. Invoking adbeg allows pulses from an external oscillator to start conversions. The rate of the oscillator must be adjusted by hand. Each time the start-conversion signal occurs the index is incremented, a) one sample for each specified channel is collected, b) each sample is stored at the next location in the user-supplied vector, and c) the signal SIGADC is sent. If the user has not specified a funciton for catching the signal it will be ignored; otherwise, the signal function will be called. Sampling will continue until the value at pindex reaches nsamp or until the macro adend is invoked. To sample indefinitely, refilling the storage vectors, the user may reset the index to -1 before it reaches nsamp.


* adsetv example
* collects 100 samples from three channels

#include <stdio_p.h>
#include <atod_u.h>

#define nsamp 100

int v1[nsamp], v2[nsamp], v3[nsamp];
int index, errcnt;
int chvec[]={1,2,3,0};
int *vvec[]={v1,v2,v3,0};

int main() {
  if (adsetv(nsamp,&index,&errcnt,chvec,vvec)==-1)
  errexit(1,"adset error\n");

  while(index < nsamp) {
        .		/*Code wich may examine the samples already
        .                  collected or change the value of index */
  if(errcnt > 0)
  printf(stderr,"atod errors = %d\n",errcnt);





signal(2F) , atod(4F)


If incomplete information is passed to adset or adsetv (for example, the number of channels does nto match the number of vectors, nsamp is equal to 0, or pindex is equal to NULL) - 1 is returned. If an illegal channel number is specified to adset, adsetv, or adread -1 is returned. adread returns -1 if the conversion error flag is set.