Analog to Digital Converter

This page describes the ADC section of the DAQ subsystem available on Athena IV.

13.2 Analog-to-Digital Input Ranges and Resolution

13.2.1 Overview

Athena IV uses a 16-bit A/D converter. The full range of numerical values for a 16-bit number is 0 - 65535. However, the A/D converter uses two’s complement notation, so the A/D value is interpreted as a signed integer, ranging from –32768 to +32767.

The smallest change in input voltage that can be detected is 1/(216), or 1/65536, of the full-scale input range. This smallest change results in an increase or decrease of 1 in the A/D code, and is referred to as 1 LSB (1 Least Significant Bit).

The analog inputs on Athena IV have three configuration options.

  • Single-ended or differential mode

  • Unipolar or bipolar mode

  • Input range (gain)

The single-ended/differential and unipolar/bipolar modes are configured using jumper block J26, and apply to all inputs. The input range selection is done in software.

13.2.2 Input Range Selection

You can select a gain setting for the inputs, which causes them to be amplified before they reach the A/D converter. The gain setting is controlled in software, which allows it to be changed on a channel-by-channel basis. In general, you should select the highest gain (smallest input range) that allows the A/D converter to read the full range of voltages over which the input signals will vary. However, a gain that is too high causes the A/D converter to clip at either the high end or low end, and you will not be able to read the full range of voltages on your input signals.

13.2.3 Input Range Table

The table below indicates the analog input range for each possible configuration. The polarity is set using jumper block J26, and the gain is set with the G1 and G0 bits in the register at Base+3. The Gain value in the table is provided for clarity. Note that the single-ended vs. differential setting has no impact on the input range or the resolution.

Polarity

G1

G0

Input Range

Resolution 1LSB

Bipolar

0

0

±10V

305µV

Bipolar

0

1

±5V

153µV

Bipolar

1

0

±2.5V

76µV

Bipolar

1

1

±1.25V

38µV

Unipolar

0

0

Invalid

Invalid

Unipolar

0

1

0 - 10V

153µV

Unipolar

1

0

0 - 5V

76µV

Unipolar

1

1

0 - 2.5V

38µV

13.3 Performing an A/D Conversion

13.3.1 Introduction

This section describes the steps involved in performing an A/D conversion on a selected input channel using direct programming (without the driver software). Perform an A/D conversion according to the following steps. Each step is discussed in detail, below.

  1. Select the input channel.

  2. Select the input range.

  3. Wait for analog input circuit to settle

  4. Initiate an A/D conversion

  5. Wait for the conversion to finish

  6. Read the data from the board

  7. Convert the numerical data to a meaningful value

13.3.2 Select the Input Channel

To select the input channel to read, write a low-channel/high-channel pair to the channel register at Base+2. The low four bits select the low channel, and the high four bits select the high channel. When you write any value to this register, the current A/D channel is set to the low channel.

For example, to set the board to channel 4 only, write 0x44 to Base+2. To set the board to read channels 0 through 15, write 0xF0 to Base+2. When you perform an A/D conversion, the current channel automatically increments to the next channel in the selected range. Therefore, to perform A/D conversions on a group of consecutively-numbered channels, you do not need to write the input channel prior to each conversion. For example, to read from channels 0 - 2, write 0x20 to base+2. The first conversion is on channel 0, the second will be on channel 1 and the third will be on channel 2. The channel counter wraps around to the beginning so the fourth conversion will be on channel 0, again.

If you are sampling the same channel repeatedly, set both high and low to the same value as in the first example, above. On subsequent conversions, you do not need to set the channel again.

13.3.3 Select the Input Range

Select the input range from among the available ranges. If the range is the same as for the previous A/D conversion it does not need to be set again. Write this value to the input range register at Base+3.

For example, for ±5V range (gain of 2), write 0x01 to Base+3.

13.3.4 Wait for Analog Input Circuit to Settle

After writing to the channel register, Base+2, or the input range register, Base+3, allow time for the analog input circuit to settle before starting an A/D conversion. The board has a built-in 10μS timer to assist with the wait period. Monitor the WAIT bit at Base+3, bit 5. When the bit value is 1, the circuit is actively settling on the input signal. When the value is 0, the board is ready to perform A/D conversions.

13.3.5 Perform an A/D Conversion on the Current Channel

After the above steps are completed, start the A/D conversion by writing to Base+0. This write operation only triggers the A/D if AINTE = 0 (interrupts are disabled). When AINTE = 1, the A/D can only be triggered by the on-board counter/timer or an external signal. This protects against accidental triggering by software during a long-running interrupt-based acquisition process.

outp(base,0x80);

13.3.6 Wait for the Conversion to Finish

The A/D converter chip takes up to five microseconds to complete one A/D conversion. Most processors and software can operate fast enough so that if you try to read the A/D converter immediately after starting the conversion, the read will occur faster than the A/D conversion and return invalid data. Therefore, the A/D converter provides a status signal to indicate whether it is busy or idle. This bit can be read back from the status register at Base+3, bit 7. When the A/D converter is busy (performing an A/D conversion), the bit value is 1 and the program must wait. When the A/D converter is idle (conversion is done and data is available), this bit value is 0 and the program may read the data.

The following statement is a simple example of this operation.

while (inp(base+3) & 0x80); // Wait for conversion to finish before proceeding

The above example could hang your program if there is a hardware fault and the bit is stuck at 1. A better solution is to use a loop with a timeout, as shown below:

int checkstatus() // returns 0 if ok, -1 if error 
int i; 
for (i = 0; i < 10000; i++) 
{ if !(inp(base+3) & 0x80) then return(0); // conversion completed } 
return(-1); // conversion did not complete

13.3.7 Read the Data from the Board

Once the conversion is complete, you can read the data back from the A/D converter. The data is a 16-bit value and is read back in two 8-bit bytes. The LSB must be read from the board before the MSB because the data is inserted into the board’s FIFO in that order. Unlike other registers on the board, the A/D data may only be read one time, because each time a byte is read from the FIFO the internal FIFO pointer advances and that byte is no longer available. Reading data from an empty FIFO returns unpredictable results.

The following pseudo-code illustrates how to read and construct the 16-bit A/D value.

LSB = inp(base); 
MSB = inp(base+1); 
Data = MSB * 256 + LSB; // combine the 2 bytes into a 16-bit value 

The final data are interpreted as a 16-bit signed integer in the range -32768 to +32767.

NOTE: The data range always includes both positive and negative values, even if the board is set to a unipolar input range. The data must now be converted to volts or other engineering units by using a conversion formula, as discussed below.

In scan mode, the behavior is the same except when the program initiates a conversion, all channels in the programmed channel range will be sampled once and the data will be stored in the FIFO. The FIFO depth register increments by the scan size. When STS goes low, the program should read out the data for all channels.

13.3.8 Convert the numerical data to a meaningful value

Once the A/D value is read, it needs to be converted to a meaningful value. The first step is to convert it back to the actual measured voltage. Afterwards, you may need to convert the voltage to some other engineering units. For example, the voltage may come from a temperature sensor and the voltage would then need to be converted to the corresponding temperature, according to the temperature sensor’s characteristics.

Since there are a large number of possible input devices, this secondary step is not included here. Only conversion to input voltage is described. However, you can combine both transformations into a single formula if desired.

To convert the A/D value to the corresponding input voltage, use the following formulas:

Conversion Formula for Bipolar Input Ranges

Input voltage = A/D value / 32768 Full-scale input range

Example: Given, Input range is 5V and A/D value is 17761. Therefore,

Input voltage = 17761 / 32768 5V = 2.710V.

For a bipolar input range,

1 LSB = 1/32768 * Full-scale voltage

The table, below, shows the relationship between A/D code and input voltage for a bipolar input range (VFS = Full scale input voltage).

A/D Code

Input Voltage Symbolic Formula

Input Voltage for 5V Range

-32768

-VFS

-5.0000V

-32767

-VFS + 1 LSB

-4.9998V

...

...

...

-1

-1 LSB

-0.00015V

0

0

0.0000V

1

+1 LSB

0.00015V

...

...

...

32767

VFS - 1 LSB

4.9998V

Conversion Formula for Unipolar Input Ranges

Input voltage = (A/D value + 32768) / 65536 Full-scale input range

Example: Given, Input range is 0-5V and A/D value is 17761.

Therefore,

Input voltage = (17761 + 32768) / 65536 5V = 3.855V.

For a unipolar input range, 1 LSB = 1/65536 * Full-scale voltage.

The following table illustrates the relationship between A/D code and input voltage for a unipolar input range (VFS = Full scale input voltage)

A/D Code

Input Voltage Symbolic Formula

Input Voltage for 5V Range

-32768

0V

0.0000V

-32767

1 LSB (VFS / 65536)

0.000076V

...

...

...

-1

VFS / 2 - 1 LSB

2.4999V

0

VFS / 2

2.5000V

1

VFS / 2 + 1 LSB

2.5001V

...

...

...

32767

VFS - 1 LSB

4.9999V

13.4 A/D Scan, Interrupt and FIFO Operation

The control bits SCANEN (scan enable) and AINTE (A/D interrupt enable) in conjunction with the FIFO determine the behavior of the SBC during A/D conversions and interrupts.

At the end of an AD conversion, the 16-bit A/D data is latched into the 8-bit FIFO in an interleaved fashion: first LSB, then MSB. A/D Data is read out of the FIFO with 2 read operations, first Base + 0 (LSB) and then Base + 1 (MSB).

When SCANEN = 1, each time an A/D trigger occurs, the SBC performs an A/D conversion on all channels in the channel range programmed in Base + 2. When SCANEN = 0, each time an A/D trigger occurs, the SBC performs a single A/D conversion and then advance to the next channel and wait for the next trigger.

During interrupt operation (AINTE = 1), the FIFO fills up with data until it reaches the threshold programmed in the FIFO threshold register, and then the interrupt request occurs. If AINTE = 0, the FIFO threshold is ignored and the FIFO continues to fill up.

If the FIFO reaches its limit of 48 samples, then the next time an A/D conversion occurs the Overflow flag OVF is set. In this case the FIFO does not accept any more data, and its contents are preserved and may be read out. In order to clear the overflow condition, the program must reset the FIFO by writing to the FIFORST bit in Base + 1, or a hardware reset must occur.

In Scan mode (SCANEN = 1), the FIFO threshold should be set to a number at least equal to the scan size and in all cases equal to an integral number of scans. For example if the scan size is 8 channels, the FIFO threshold should be set to 8, 16, 24, 32, 40, or 48, but not less than 8. This way the interrupt occurs at the end of the scan, and the interrupt routine can read in a complete scan or set of scans each time it runs.

In non-scan mode (SCANEN = 0), the FIFO threshold should be set to a level that minimizes the interrupt rate but leaves enough time for the interrupt routine to respond before the next A/D conversion occurs. Remember that no data is available until the interrupt occurs, so if the rate is slow the delay to receive A/D data may be long. Therefore for slow sample rates the FIFO threshold should be small. If the sample rate is high, the FIFO threshold should be high to reduce the interrupt rate. However remember that the remaining space in the FIFO determines the time the interrupt routine has to respond to the interrupt request. If the FIFO threshold is too high, the FIFO may overflow before the interrupt routine responds. A good rule of thumb is to limit the interrupt rate to no more than 1,000-2,000 per second in Windows and Linux or 10,000 per second in DOS. Experimentation may be necessary to determine the optimum FIFO threshold for each application.

The table below describes the SBC’s behavior for each of the 4 possible cases of AINTE and SCANEN. The given interrupt software behavior describes the operation of the Diamond Systems Universal Driver software. If you write your own software or interrupt routine you should conform to the described behavior for optimum results.

The following table describes the register settings for the A/D operating modes. (LOW and HIGH channels referenced in the table are the 4-bit channel numbers in Base+2.)

AINTE

Base+4, bit 0

SCANE

Base+2, bit 1

Operation

0

0

Single A/D conversions are triggered by write to B+0. STS stays high during the A/D conversion.

No interrupt occurs.

The user program monitors STS (Base+3, bit 7) and reads A/D data when STS goes low.

0

1

A/D scans are triggered by write to B+0. All channels between LOW and HIGH are sampled.

STS stays high during the entire scan (multiple A/D conversions). No interrupt occurs.

The user program monitors STS (Base+3, bit 7) and reads A/D data when STS goes low.

1

0

Single A/D conversions are triggered by the source selected with ADCLK (Base+4, bit 4).

STS stays high during the A/D conversion.

A/D interrupt occurs when the FIFO reaches its programmed threshold.

The interrupt routine reads the number of samples equal to the FIFO threshold (Base+5, bits 0-5).

1

1

A/D scans are triggered by the source selected with ADCLK (Base+4, bit 4). STS stays high during the entire scan (multiple A/D conversions).

A/D interrupt occurs when the FIFO reaches its programmed threshold.

The interrupt routine reads the number of samples equal to the FIFO threshold (Base+5, bits 0-5).

Last updated