#include<stdio.h>
#include<conio.h>
#include"A812.h"

int Base0=0x220;
int Base1=0x240;
int Base2=0x260;
int Buf[1000];

void main(void)
{ int err;
  int Dio;
  int quit=0;
  int i;
  float rate=5.0/2048.0;
  int c1,c2;
  int NowCount;

  err=A812_Initialize(0,Base0,-1,-1); /* card 0 : NO DMA,NO IRQ */
  if(err){
     if(err==CheckBoardError){
	printf("Address 0 Error!\n");
     }
     else if(err==CardNumError){
	printf("Card Number Error!(range:0-7)\n");
     }
     return;
  }
  err=A812_Initialize(1,Base1,-1,11);  /* card 1 : NO DMA ,IRQ=11*/
  if(err){
     if(err==CheckBoardError){
	printf("Address 1 Error!\n");
     }
     else if(err==CardNumError){
	printf("Card Number Error!(range:0-7)\n");
     }
     return;
  }

  err=A812_Initialize(2,Base2,1,15);  /* card 2 : DMA=1,IRQ=15 */
  if(err){
     if(err==CheckBoardError){
	printf("Address 2 Error!\n");
     }
     else if(err==CardNumError){
	printf("Card Number Error!(range:0-7)\n");
     }
     return;
  }

  printf("\nCard 0 [polling test]\n");
  printf("Press any key to begin test\n");
  printf("then Press ESC to end test\n");
  getch();
  A812_ActiveBoard(0);
  A812_AD_SetChGainMode(0,A812_BI_1,POLLING_MODE);
  A812_Delay(4*2);     /* delay the 4 uS settling time */
  while(!quit){
     if(kbhit() && getch()==27) break;
     A812_AD_PollingArray(Buf,1000);
     for(i=0;i<1000;i+=100){
	 cprintf("[%3d]%6.3f\n\r",i,rate*(Buf[i]-2048));
     }
  }

  printf("\nCard 1 [interrupt test]\n");
  printf("Press any key to begin test\n");
  printf("then Press ESC to end test\n");
  getch();
  A812_ActiveBoard(1);
  A812_AD_SetChGainMode(0,A812_BI_1,INTERRUPT_MODE);
  A812_Delay(4*2);     /* delay the 4 uS settling time */
  c1=20;
  c2=20;
  while(!quit){
	A812_AD_INT_Start(Buf,1000,c1,c2);
	do {
	   NowCount=A812_AD_INT_Count();
	   if(kbhit() && getch()==27) {
	      quit=1;
	      break;
	   }
	} while( NowCount<1000 );
	A812_AD_INT_Stop( );
	if(kbhit() && getch()==27) break;
	for(i=0;i<1000;i+=100){
	    cprintf("[%3d]%6.3f\n\r",i,rate*(Buf[i]-2048));
	}
  }

  printf("\nCard 2 [DMA test]\n");
  printf("Press any key to begin test\n");
  printf("then Press ESC to end test\n");
  getch();
  A812_ActiveBoard(2);
  A812_AD_SetChGainMode(0,A812_BI_1,DMA_MODE);
  A812_Delay(4*2);     /* delay the 4 uS settling time */
  c1=6;
  c2=6;
  quit=0;
  while(!quit){
     A812_AD_DMA_Start(Buf,1000,c1,c2);
     while(A812_AD_DMA_Status()){
	if(kbhit() && getch()==27) {
	   quit=1;
	   break;
	}
     };
     A812_AD_DMA_Stop();
     if(kbhit() && getch()==27) break;
     for(i=0;i<1000;i+=100){
	 Buf[i]&=0xfff;   /* in DMA mode must change bit 12 to 0 */
	 cprintf("[%3d]%6.3f\n\r",i,rate*(Buf[i]-2048));
     }
  }
}
