#include   <stdio.h>
#include   "A822.h"

int   Buffer[2000];

int wCardMode;
main(void)
{
   int	DesireCount=2000;     /* The count which We desire to transfer */
   int	NowCount;	      /* The count which is transfering now */
   int	ErrorCode;
   int	i,j,c1,c2;
   float  volt,min,max,min2,max2,err,rep;

    for(i=0; i<DesireCount; i++) Buffer[i]=1024;
    wCardMode=A822_SetCardModel(A8112_Mode);

    /*Warring:A8112 only support Single-End*/
    A822_Initialize(0,0x220,1,9);
    A822_AD_SetChGainMode(0,A822_BI_1,DMA_MODE);
    /* The clock rate of pacer trigger is equal to 2M/(c1*c2). */
    printf("\nC1 C2="); scanf("%d %d",&c1,&c2);
    printf("\nMin Max="); scanf("%f %f",&min,&max);
    err=0; rep=0; min2=5.0; max2=-5.0;
for(;;)
{
   rep++;
   printf("\nc1=%d,c2=%d:rep=%.1f,err=%.1f:[%f,%f]",c1,c2,rep,err,min2,max2);
   if( (ErrorCode=A822_AD_DMA_Start(Buffer,DesireCount,c1,c2)) )
   {
       putch(0x07);
       printf("A822_AD_DMA_Start() error !!!    ErrorCode=%d\n",ErrorCode);
       exit(1);
   }
   while(A822_AD_DMA_Status() )
   {
   if (kbhit()!=0) {getch(); return;}
   };
   A822_AD_DMA_Stop();

    for(i=0,j=0; i<DesireCount;  i++)
    {
    Buffer[i]=Buffer[i]&0x0fff;

    volt=(float)(Buffer[i]-2048)/2048.0*5.0;
    if (volt<min2) min2=volt;
    if (volt>max2) max2=volt;
    if (i==0) printf(" : volt[0]=%f",volt);
    if ((volt<min) || (volt>max))
	{
	err++;
	printf("\nmin=%f, max=%f,volt=%f",min,max,volt);
	break;
	}
    }
    delay(300);
    if (kbhit()!=0) {getch(); return;}
}
}
