#include   <stdio.h>
#include   "A821.H"

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

    for(i=0; i<DesireCount; i++)  Buffer[i]=1024;
    A821_Initialize(0,0x220,7);
    A821_AD_SetChGainMode(0,A821_BI_1,INTERRUPT_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); /* input range */
    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( A821_AD_INT_Start(Buffer,DesireCount,c1,c2)!=0 )
    {
	putch(0x07);
	printf("A821_AD_INT_Start() error !!!\n");
	exit(1);
    }

    do
    {
       NowCount=A821_AD_INT_Count();
       if (kbhit()!=0) {getch();
			A821_AD_INT_Stop( );
			return;
			}
    } while( NowCount<DesireCount );

    A821_AD_INT_Stop( );

    for(i=0,j=0; i<DesireCount;  i++)
    {
    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);
}
}
