You are here: Register Set > ADC Data (Offset=0)
STX104 Reference Manual
ContentsIndexHome
Example

Examples of how to read the ADC data: 

 

8-Bit Read in C/C++:
union { unsigned int word; unsigned char byte[2]; } ad_value[16];
...
adc_value(channel).byte[0] = inp(base_address+0);
adc_value(channel).byte[1] = inp(base_address+1);
/* adc_value(channel).word now contains the full ADC word value */
...

 

16-Bit Read in C/C++:
unsigned int ad_value[16];
...
adc_value(channel) = inpw(base_address+0);
...
8-Bit Read and FIFO enabled in C/C++:
union { unsigned int word; unsigned char byte[2]; } ad_value[16];
...
if ( fifo_not_empty() == true ) /* function to check fifo status */
{
 adc_value(channel).byte[0] = inp(base_address+0);
 adc_value(channel).byte[1] = inp(base_address+1);
 /* adc_value(channel).word now contains the full ADC word value */
}
...

 

16-Bit Read and FIFO enabled in C/C++:
unsigned ad_value[16];
...
if ( fifo_not_empty() == true ) /* function to check fifo status */
{
adc_value(channel) = inpw(base_address+0);
}
...

 

16-Bit Read with FIFO enabled in C/C++ using REP INSW:
typedef unsigned int WORD;

void insw(WORD port, void *buf, int count)
{
 _ES = FP_SEG(buf); /* Segment of buf */
 _DI = FP_OFF(buf); /* Offset of buf */
 _CX = count; /* Number to read */
 _DX = port; /* Port */
 asm REP INSW;
}

void main()
{
 WORD *data[512];
 ...
 insw(0x300, data, 512); /* assumes 512 samples (or two blocks) in FIFO */
 ...
}

 

An example shown below illustrating how to set the gain and use it to determine the actual input voltage.
/****************************************************************************
   Apex Embedded Systems
   Revised: 03MAR08
   STX104 Analog Input Demo
****************************************************************************/

#include <conio.h>
#include <stdio.h>
#include <dos.h>

/****************************************************************************/
float adc_gain[8] = { 20.0/65536.0, 10.0/65536.0, 5.0/65536.0, 2.5/65536.0,
              10.0/65536.0,  5.0/65536.0, 2.5/65536.0, 1.25/65536.0 };
float adc_offset[8] = { -10.0, -5.0, -2.5, -1.25, 0.0, 0.0, 0.0, 0.0 };

/****************************************************************************/
float Adc_Voltage ( void )
{
    long sum;
    int index;
    float voltage;

    /* take several samples and average */
    sum = 0;
    for ( index=0; index <256; index++ )
    {
       outportb(0x300, 0x00); /* start a sample */
       while ( (inportb(0x308) & 0x80) != 0x00 ); /* wait for sample */
       sum = sum + ((long)inpw(0x300));
    }
    sum = sum / 256;
    index = inportb( 0x30B ) & 0x07;
    voltage = ( (float) sum ) * adc_gain[index] + adc_offset[index];
    return voltage;
}

/****************************************************************************/
void main()
{
      int x,y;
      unsigned char channels;

      channels = 0x00; /* scan channel zero only */
      /* base address set to factory default at 0x300 */
      /* initialize STX104 */
      outportb(0x309, 0x00); /* no interrupts, no DMA and s/w trigger */

      /* Applies to firmware revision 080214H only: for non-zero first_channel
         please write to channel register twice to correct for errata issue.  This
         is transparent for other firmware revisions (simply means that acquisition
         controller is reset twice).
      */
      outportb(0x302, channels);
      outportb(0x302, channels);
      while ((inportb(0x308) & 0x80 ) == 0x80 ); /* wait       */

      printf("0) +10.0V   input\n");
      printf("1)  +5.0V   input\n");
      printf("2)  +2.5V   input\n");
      printf("3)  +1.25V  input\n");
      printf("9) Quit this test\n");

      do {
           y = inportb( 0x30B ) & 0x07;
           if ( (y & 0x04) == 0x00 ) printf("Bipolar:  ");
           else printf("Unipolar: ");
           y = y & 0x03;
           printf(" Gain=");
           if ( y == 0 )      printf("10V,   ");
           else if ( y == 1 ) printf(" 5V,   ");
           else if ( y == 2 ) printf(" 2.5V, ");
           else if ( y == 3 ) printf(" 1.25V,");
           printf(" Voltage = %+10.6f        \r", Adc_Voltage());

           if ( kbhit() )
           {
              y = getch();
              /* set gain */
              if ( y == '0' )      { x = 0;    outportb( 0x30B, x ); }
              else if ( y == '1' ) { x = 1;    outportb( 0x30B, x ); }
              else if ( y == '2' ) { x = 2;    outportb( 0x30B, x ); }
              else if ( y == '3' ) { x = 3;    outportb( 0x30B, x ); }
              else if ( y == '9' ) x = 9;
           }
       } while ( x != 9 );
       printf("\n\n");
}
Copyright © 1997-2008 by Apex Embedded Systems. All rights reserved. Updated on Wednesday, April 02, 2008.