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

#include  "ISO_AD32.H"
#define WORD int

WORD wBase=0x220,wActive=0,wSingDiff=1,wIrq=15,wDelay1,wBuf[512];

/* --------------------------------------------------------------------- */

main()
{
char c;

load_configuration();
for(;;)
{
printf("\n**********************************************************");
show_configuration();
printf("\n**********************************************************");
printf("\n* 0 : Software Library Test                              *");
printf("\n* 1 : Hardware Board Configuration Setting               *");
printf("\n* 2 : Read JP1 Setting (Single-ended or Differential)    *");
printf("\n* 3 : Read AD (fixed channel)                            *");
printf("\n* 4 : Read AD (channel scan)                             *");
printf("\n* 5 : Calibration Step 1 : VR2 (gain adjustment)         *");
printf("\n* 6 : Calibration Step 2 : V1  (offset adjustment)       *");
printf("\n***** repeat [5][6] until no adjustment ******************");
printf("\n* 7 : Calibration Step 3 : VR4 (PGA offset adjustment)   *");
printf("\n* 8 : Calibration Step 4 : VR3 (-5V adjustment)          *");
printf("\n* Q : Quit                                               *");
printf("\n**********************************************************");
printf("\nPress key to select function : ");

c=getch();
switch	(c)
	{
	case '0' : fun_0(); break;
	case '1' : fun_1(); break;
	case '2' : fun_2(); break;
	case '3' : fun_3(); break;
	case '4' : fun_4(); break;
	case '5' : fun_5(); break;
	case '6' : fun_6(); break;
	case '7' : fun_7(); break;
	case '8' : fun_8(); break;
	case 'q' :
	case 'Q' : goto ret_label;
	}
}
ret_label:
return;
}

/* -------------------------------------------------------------------- */

set_pos(int x, int y)
{
union REGS regs;
regs.h.ah=2;
regs.h.dh=y;
regs.h.dl=x;
regs.h.bh=0;
int86(0x10,&regs,&regs);
}

/* -------------------------------------------------------------------- */

clear_screen(void)
{
union REGS regs;
regs.h.ah=15;
int86(0x10,&regs,&regs);
regs.h.ah=0;
int86(0x10,&regs,&regs);

}

/* --------------------------------------------------------------------- */

load_configuration()
{
FILE *stream;
int wRetVal;

wActive=0;
stream=fopen("ISO_AD32.DAT","r");
if (stream)
   {
   fscanf(stream,"%x",&wBase);
   fscanf(stream,"%d",&wIrq);
   fscanf(stream,"%d",&wSingDiff);
				   /* for fun_5 */
				   /* for fun_5 */
   fscanf(stream,"%d",&wDelay1);   /* for fun_5 */
				   /* for fun_5 */
				   /* for fun_5 */
   printf("\nLOAD ISO_AD32.DAT OK");

   wRetVal=AD32_CheckBoard(0,wBase,wSingDiff,wIrq);
   if (wRetVal==NoError)
      {
      wActive=1;
      printf("\nSet Configuration OK");
      }
   else printf("\nSet Configuration Error, Error Code=%d",wRetVal);

   fclose(stream);
   }
else printf("\nOPEN ISO_AD32.DAT ERROR");
}

/* --------------------------------------------------------------------- */

show_configuration()
{
printf("\n");
printf("* wBase=%x",wBase);
printf(", wIrq=%d",wIrq);
if (wSingDiff == 0) printf(", Single-ended");
else printf(", Differential");
printf(" Type Analog Input     *");
}

/* --------------------------------------------------------------------- */

save_configuration()
{
FILE *stream;

stream=fopen("ISO_AD32.DAT","w");
if (stream)
   {
   fprintf(stream,"%x\n",wBase);
   fprintf(stream,"%d\n",wIrq);
   fprintf(stream,"%d\n",wSingDiff);
   fprintf(stream,"%d",wDelay1);
   printf("\nSAVE ISO_AD32.DAT OK");
   fclose(stream);
   }
else printf("\nOPEN ISO_AD32.DAT ERROR");
}

/* --------------------------------------------------------------------- */

fun_0()
{
int i;
float f;

i=AD32_ShortSub2(1,2);
printf("\nLibrary Test : AD32_ShortSub2(1,2)=%d",i);
if (i==-1) printf(" --> OK"); else printf(" --> Error");
f=AD32_FloatSub2(1.0,2.0);
printf("\nLibrary Test : AD32_FloatSub2(1.0,2.0)=%f",f);
if (f==-1.0) printf(" --> OK"); else printf(" --> Error");
i=AD32_GetVersion();
printf("\nSoftware Version=%x",i);
}

/* --------------------------------------------------------------------- */

fun_1()
{
int i,j,k,l;

printf("\nwBASE   (ex. 220) = "); scanf("%x",&i);
printf("wIrq      (ex. 15) = "); scanf("%d",&j);
printf("wSingDiff (0=single-ended) = "); scanf("%d",&k);
l=AD32_CheckBoard(0,i,k,j);
if (l==0)
   {
   wBase=i; wIrq=j; wSingDiff=k;
   wActive=1;
   printf("\nSet Configuration OK");
   save_configuration();
   return;
   }
printf("\nSet Configuration Error, Error Code=%d",l);
wActive=0;
}

/* --------------------------------------------------------------------- */

fun_2()
{
int i;

if (wActive==0)
   {
   i=AD32_CheckBoard(0,wBase,wSingDiff,wIrq);
   if (i!=0)
      {
      printf("\nHardware Board Configuration Error");
      return;
      }
   wActive=1;
   }

AD32_ReadSingDiff(&i);

printf("\nAD32_ReadSingDiff=%d",i);
if (i==0) printf(" --> Single-ended"); printf(" --> Differential");
}

/* --------------------------------------------------------------------- */

fun_3()
{
int wBoard,wBase,wIrq,wRetVal,i,j,k,flag;
int wChannel,wMode,wConfig,wSettle,wData;
int wLow,wHigh,wMin,wMax;
float f;

wRetVal=AD32_ReadActive(&wBoard,&wBase,&wIrq);
if (wRetVal==NoError) printf("\nboard=%d, base=%x, irq=%d",wBoard,wBase,wIrq);
else
     {
     printf("\nAD32_ReadActive(...) Error");
     return;
     }

wChannel=0; wMode=BIPOLAR; wConfig=CONFIG_1; wSettle=23;
wRetVal=AD32_SetChannelConfigNoScan(wChannel,wMode,wConfig,wSettle);
if (wRetVal!=NoError)
   {
   printf("\nAD32_SetChannelConfigNoScan(...) Error");
   return;
   }

wRetVal=AD32_ReadChannelConfigNoScan(&wChannel,&wMode,&wConfig);
if (wRetVal!=NoError)
   {
   printf("\nAD32_ReadChannelConfigNoScan(...) Error");
   return;
   }
printf("\nchannel=%d, mode=%d, config=%d",wChannel,wMode,wConfig);

wRetVal=AD32_ReadAdNoScan(&wData);
if (wRetVal!=NoError)
   {
   printf("\nAD32_ReadAdNoScan(...) Error");
   return;
   }
f=(float)(wData-2048)/2048.0*5.0;
printf("\ndata=%x, f=%f",wData,f);

wRetVal=AD32_StartAdsNoScan(FAST_NO_INT,1);
if (wRetVal!=NoError)
   {
   printf("\nAD32_StartAdsNoScan(...) Error");
   return;
   }

j=0; flag=0;
for (k=0; k<10; k++)
for (i<0; i<32766; i++)
    {
    AD32_ReadSystemStatus(&wData);
    if ((wData&0x02)!=0)
       {
       AD32_ReadFIFO(&wData);
       if (flag==0) {wLow=wData; flag=1;}
       else {wHigh=wData; flag=0; wBuf[j]=wLow+(wHigh<<8); j++;}
       }
    }

printf("\nTotal=%d bytes\n",j);
for (i=0; i<10; i++) printf("[%3x]",wBuf[i]);
wMin=0x7fff; wMax=0;
for (i=0; i<j; i++)
    {
    if (wBuf[i]>wMax) wMax=wBuf[i];
    if (wBuf[i]<wMin) wMin=wBuf[i];
    }
printf("\nwMin=%x, wMax=%x",wMin,wMax);

}

/* --------------------------------------------------------------------- */

fun_4()
{
int wBoard,wBase,wIrq,wRetVal,i,j,k,flag;
int wChannel,wMode,wConfig,wSettle,wData;
int wLow,wHigh,wMin,wMax;
float f,fVal;

wRetVal=AD32_ReadActive(&wBoard,&wBase,&wIrq);
if (wRetVal==NoError) printf("\nboard=%d, base=%x, irq=%d",wBoard,wBase,wIrq);
else
     {
     printf("\nAD32_ReadActive(...) Error");
     return;
     }

wRetVal=AD32_SetSampleRate((float)1234.0);
if (wRetVal==NoError) printf("\nSetSampleRate 1234 s/s OK");
else printf("\nSetSampleRate Error");

wRetVal=AD32_ReadSampleRate(&fVal);
if (wRetVal==NoError) printf("\nReadSampleRate=%.0f s/s",fVal);
else printf("\nReadSampleRate Error");

wRetVal=AD32_SetSampleRate((float)5678.0);
if (wRetVal==NoError) printf("\nSetSampleRate 5678 s/s OK");
else printf("\nSetSampleRate Error");

wRetVal=AD32_ReadSampleRate(&fVal);
if (wRetVal==NoError) printf("\nReadSampleRate=%.0f s/s",fVal);
else printf("\nReadSampleRate Error");

/* Scan sequence 3->2->1->0->3.... */
wRetVal=AD32_SetChannelScanStatus(0,3,0,CONFIG_1);
wRetVal += AD32_SetChannelScanStatus(1,2,0,CONFIG_2);
wRetVal += AD32_SetChannelScanStatus(2,1,0,CONFIG_3);
wRetVal += AD32_SetChannelScanStatus(3,0,0,CONFIG_4);
if (wRetVal==NoError) printf("\nSet Channel Scan TEST1 OK");
else printf("\nSet Channel Scan TEST1 Error");

/* Scan sequence 0->1->2->3->0.... */
wRetVal=AD32_SetChannelScanStatus(0,0,0,CONFIG_1);
wRetVal += AD32_SetChannelScanStatus(1,1,0,CONFIG_2);
wRetVal += AD32_SetChannelScanStatus(2,2,0,CONFIG_3);
wRetVal += AD32_SetChannelScanStatus(3,3,0,CONFIG_4);
if (wRetVal==NoError) printf("\nSet Channel Scan TEST2 OK");
else printf("\nSet Channel Scan TEST2 Error");

/* Conversion=FAST, scan=4 channel, total Ads=4*64=256 WORD=512 BYTE */
/* Scan sequence : 0->1->2->3->0...			*/
wRetVal=AD32_StartChannelScanAds(FAST_NO_INT,4,64);
if (wRetVal==NoError) printf("\nStart Channel Scan OK");
else printf("\nStart Channel Scan Error");

j=0;
for (k=0; k<10; k++)
for (i=0; i<32760; i++)
    {
    AD32_ReadSystemStatus(&wData);
    if ((wData&0x02)!=0)
       {
       AD32_ReadFIFO(&wData);
       if (i<5) printf("[wdata=%x]",wData);
       j++;
       }
    }
printf("\nTotal data read from FIFO = %d bytes",j);
}

/* --------------------------------------------------------------------- */

fun_5()
{
int wRetVal,i,j,k;
int wChannel,wMode,wConfig,wSettle,wData;
float f;

clear_screen();
set_pos(0,0);

wChannel=0; wMode=BIPOLAR; wConfig=CONFIG_1; wSettle=23;
wRetVal=AD32_SetChannelConfigNoScan(wChannel,wMode,wConfig,wSettle);
if (wRetVal!=NoError)
   {
   printf("\nAD32_SetChannelConfigNoScan(...) Error");
   return;
   }

printf("\nCalibration Step 1 = VR2 (gain adjustment)");
printf("\nApply stable 4.9988V to A/D channel_0");
printf("\nAdjust VR2 until DATA between 4094 and 4095");

for (;;)
    {
    for (i=0; i<wDelay1; i++)
	{
	wRetVal=AD32_ReadAdNoScan(&wData);
	if (wRetVal!=NoError)
	   {
	   printf("\nAD32_ReadAdNoScan(...) Error");
	   return;
	   }
	if (kbhit()!=0) {getch(); return;}
	}
    set_pos(0,4); printf("DATA=%d     ",wData);
    }
}

/* --------------------------------------------------------------------- */

fun_6()
{
int wRetVal,i,j,k;
int wChannel,wMode,wConfig,wSettle,wData;
float f;

clear_screen();
set_pos(0,0);

wChannel=0; wMode=BIPOLAR; wConfig=CONFIG_1; wSettle=23;
wRetVal=AD32_SetChannelConfigNoScan(wChannel,wMode,wConfig,wSettle);
if (wRetVal!=NoError)
   {
   printf("\nAD32_SetChannelConfigNoScan(...) Error");
   return;
   }

printf("\nCalibration Step 2 = V1  (offset adjustment)");
printf("\nApply stable 0V to A/D channel_0");
printf("\nAdjust V1  until DATA between 2048 and 2049");

for (;;)
    {
    for (i=0; i<wDelay1; i++)
	{
	wRetVal=AD32_ReadAdNoScan(&wData);
	if (wRetVal!=NoError)
	   {
	   printf("\nAD32_ReadAdNoScan(...) Error");
	   return;
	   }
	if (kbhit()!=0) {getch(); return;}
	}
    set_pos(0,4); printf("DATA=%d     ",wData);
    }
}

/* --------------------------------------------------------------------- */

fun_7()
{
int wRetVal,i,j,k;
int wChannel,wMode,wConfig,wSettle,wData;
float f;

clear_screen();
set_pos(0,0);

wChannel=0; wMode=BIPOLAR; wConfig=CONFIG_1; wSettle=23;
wRetVal=AD32_SetChannelConfigNoScan(wChannel,wMode,wConfig,wSettle);
if (wRetVal!=NoError)
   {
   printf("\nAD32_SetChannelConfigNoScan(...) Error");
   return;
   }

printf("\nCalibration Step 3 = VR4 (PGA offset adjustment)");
printf("\nApply stable 0V to A/D channel_0");
printf("\nAdjust VR4 until DATA between 2048 and 2049");

for (;;)
    {
    for (i=0; i<wDelay1; i++)
	{
	wRetVal=AD32_ReadAdNoScan(&wData);
	if (wRetVal!=NoError)
	   {
	   printf("\nAD32_ReadAdNoScan(...) Error");
	   return;
	   }
	if (kbhit()!=0) {getch(); return;}
	}
    set_pos(0,4); printf("DATA=%d     ",wData);
    }
}

/* --------------------------------------------------------------------- */

fun_8()
{
int wRetVal,i,j,k;
int wChannel,wMode,wConfig,wSettle,wData;
float f;

clear_screen();
set_pos(0,0);

wChannel=0; wMode=BIPOLAR; wConfig=CONFIG_1; wSettle=23;
wRetVal=AD32_SetChannelConfigNoScan(wChannel,wMode,wConfig,wSettle);
if (wRetVal!=NoError)
   {
   printf("\nAD32_SetChannelConfigNoScan(...) Error");
   return;
   }

printf("\nCalibration Step 4 = VR3 (-5V adjustment)");
printf("\nApply stable 0V to A/D channel_0");
printf("\nAdjust VR3 until DATA between 0 and 1");

for (;;)
    {
    for (i=0; i<wDelay1; i++)
	{
	wRetVal=AD32_ReadAdNoScan(&wData);
	if (wRetVal!=NoError)
	   {
	   printf("\nAD32_ReadAdNoScan(...) Error");
	   return;
	   }
	if (kbhit()!=0) {getch(); return;}
	}
    set_pos(0,4); printf("DATA=%d     ",wData);
    }
}



