#include<stdio.h>
#include<conio.h>
#include<sys\timeb.h>
#include"a822.h"

int Base=0x220;
int Buffer[1000];
int wCardMode;
struct timeb t1,t2;

main(void)
{
    int  i,j,err;
    float  volt,temp,t;
    unsigned int PollData;
    A822_SetCardModel(1);
    wCardMode=A822_SetCardModel(A8112_Mode);
    err=A822_Initialize(0,Base,-1,-1);
    if(err){
	   if(err==CheckBoardError){
	      printf("Address 0 Error!\n");
	      }
	   else if(err==CardNumError){
	      printf("Card Number Error!(range:0-7)\n");
	      }
	   return;
	   }

    for(i=0; i<1000; i++)  Buffer[i]=1024;
 
    /*Warring:A8112 only support Single-End*/
    A822_AD_SetChGainMode(0,A822_BI_1,POLLING_MODE);
    printf("\nWait about 10 sec. to perform 10*100K polling");
    ftime(&t1);
    for (j=0; j<1000; j++)
    A822_AD_PollingArray(Buffer,1000);
    ftime(&t2);
    t=(float)(t2.time-t1.time);
    if (t2.millitm>t1.millitm)
	{
	t+=(float)(t2.millitm-t1.millitm)/1000.0;
	}
    else
	{
	t--;
	t+=(float)(1000.0+t2.millitm-t1.millitm)/1000.0;
	}
    printf("\nStart time : %ld %d",t1.time,t1.millitm);
    printf("\nEnd   time : %ld %d",t2.time,t2.millitm);
    printf("\nTotal time : %f sec",t);
    printf("\nPerformance=%fK\n\n",100.0*10.0/t);
    for(i=0,j=0; i<1000;  i+=20,j++)
    {
	 if( Buffer[i]==0xffff )
	 {
	     putch(0x07);
	     printf("A/D convertion timeout in polling mode !!!\n");
	     exit(0);
	 }
	 temp=(float)Buffer[i];
	 volt=(temp-2048)/2048.0*5.0;
	 printf("[%04d]:%+5.3f  ",i,volt);
	 if( j%5==4 )  printf("\n");
    }
}
