Sample Interface Code for Validyne MFC113 A/D Card

Introduction

The Validyne MFC113 is an 8-channel, 12-bit A/D input card with programmable gain. It can be used as a general-purpose analog interface for DC voltages that range from a few mV to +/-10 Vdc. This page describes the MFC113.C source code file, as used to communicate with the MFC113 in the Linux operating system.

The program shown in the MFC113.C listing is designed to show how to initiate A/D conversions and retrieve a reading from the MFC113 to be displayed in Vdc. To keep the code as straightforward as possible, just a single reading from MFC113 input channel 0 is requested, read and displayed. The program then waits for a keyboard input. If the input is 'x', the program terminates, otherwise a new reading is displayed. This code was written using the VI editor and compiled with the GNU C compiler running under Red Hat Linux Ver 4.2. This code can be easily modified and adapted for more sophisticated data acquisition programs.

Complete Data Logging Program Available

A complete data logging program for the MFC113 is available by e-mail request to Validyne Engineering, sales@validyne.com. The logging software will run with or without the MFC113 so that the logging functions can be tested in the user's system prior to buying the card. This software is described in detail at www.validyne.com/cpe.htm.

Detailed Description of MFC113.C Source Code

Header Information
The MFC113.C listing is heavily commented, but there are a few important things to keep in mind. The MFC113 card occupies 8 contiguous addresses in the PC I/O space. To access these addresses under the GNU compiler, you must include the asm/io.h library in your code. A full description of how to access the PC I/O under Linux can be found in the "Linux I/O Port mini-HowTo", by Riku Saikkonen, available through the Linux Documentation Project. This is an essential reference for those working with PC interface cards; almost every type of ISA interface card requires access via the I/O address space. The mini-HowTo also includes a good discussion on how to program timing delays in GNU C.
Starting Address Settings
The MFC113 starting I/O address is set via a DIP switch on the circuit board. This setting must be reflected in the outb() and inb() function calls. A detailed drawing of how to set the MFC113 DIP switch is given at www.validyne/cpecfg.htm.

Note that to enable access to the I/O ports, you must call ioperm(0x380,8,1) prior to using the inb() or outb() functions. The arguments to the ioperm() function reflect the starting I/O address of the card (see the mini-HowTo for details on the syntax).

Note also that in order to use the I/O ports the program must be compiled by the root, and any compiled executable must be also be run by the root.

Gain and Channel Settings
The gain range and input channel number of the MFC113 are set prior to each conversion request. In the MFC113.C code, the variables Gain and Channel are used to set this parameters. These may be re-defined prior to each conversion request (as part of a scanning loop, for example). In order that the correct calculated value in Vdc is obtained, the Gain variable must be matched with the corresponding FS (for Full Scale) variable value, as shown below:


FS = +/-5 Vdc, Gain = 0
FS = +/- 10 Vdc, Gain = 8
FS = +/-1 Vdc, Gain = 10
FS = +/-0.5 Vdc, Gain = 12
FS = +/-0.01 Vdc, Gain = 14

The Reading variable will be calculated correctly for Vdc only if this relationship is held.

Program Flow
The MFC113.C program flow is extremely simple. The MFC113 gain and channel number are set, the conversion requested, and the high and low bytes of the converted analog input are read. The voltage reading is calculated and displayed. When a keyboard input is detected, the program converts again or terminates if the input is 'x'.

Rather crude delay loops have been placed after each I/O call so that the MFC113 has time to respond. This is most important after starting the A/D conversion. The delays shown are tied to a single variable name (Delay) and may be easily changed to optimize the conversion process. This code was developed and tested on a 486, 33 MHz PC, so faster machines will probably require a larger Delay value.

If the conversions do not seem to be producing the correct value, try increasing the value of Delay.

Compiling
In order to compile the MFC113.C source code, you must use the GNU -O option. A typical compiler command line would look like: gnu -O mfc113.c

Customizing
Once this program is running in your system, the basic set, request, read functions for the MFC113 can be lifted out of the MFC113.C example program and used as part of a larger data acquisition program. This could allow data logging of up to 8 channels, each with a separate gain. Scale and offset factors could be included to linearize sensor outputs into engineering units. And alarming functions could be added.