/**********************************************************************/ /* This program is developed by Borland C++ 3.1 */ /***********************************************************************/ /* Demo 32: Monitoring the incoming data for first 16 A/D channels */ /* When the incoming data of ch:N exceed the threadhold */ /* then the bit of DO:N be turned On. */ /* NOTICE: Because the DO is 16 bits, use the first 16 A/D channels to */ /* test this demo for PCI-1202L, PCI-1202H user. */ /***********************************************************************/ #define MYLIB #include "P1202.H" WORD ProgramPIC(WORD wSampleRateDiv, WORD wChannelStatus[], WORD wChannelConfig[], WORD Threshold[]); WORD CheckingThreshold(); WORD For_PIC_Control(WORD i); void For_Disable_Timer0(void); WORD wAddrTimer,wAddrCtrl,wAddrDio,wAddrAdda; WORD wTestData[100]; int main() { int i,j; WORD wBoards,wRetVal,wVal,wRet; WORD wSamplingRateDiv; float fVal,fAdVals[5]; short nVal; WORD wChannelStatus[32],wChannelConfig[32],wChannelThreshold[32]; clrscr(); /* initiaing P1202 card and detect how many P1202 card in PC */ wRetVal=P1202_DriverInit(&wBoards); printf("Threr are %d P1202 Cards in this PC\n",wBoards); if( wBoards==0 ) { putch(0x07); putch(0x07); putch(0x07); printf("There are no P1202 card in this PC !!!\n"); exit(0); } /* dump every P1202 card's configuration address space */ printf("The Configuration Space -> Timer Control DIO AD/DA\n"); for(i=0; i 0000H\n"); P1202_Do(0x0000); /* Digital output */ getch(); printf("Digital Output -> 0001H\n"); P1202_Do(0x0001); /* Digital output */ getch(); /******** preparing parameter to ProgramPIC() ***************/ for(i=0; i<32; i++) { /* the status for each channel, 1: to scan 0: no scan */ wChannelStatus[i]=0; /* the config code for each channel */ wChannelConfig[i]=0; } wChannelStatus[0]=1; /* to monitor the incoming data of ch:0 */ wChannelStatus[1]=1; /* to monitor the incoming data of ch:1 */ wChannelThreshold[0]=0x0c00; // set the threshold value of ch:0 wChannelThreshold[0]=0x0c00; // set the threshold value of ch:1 wSamplingRateDiv=100; // the Sampling rate is 8M/100=330K wRet=ProgramPIC(wSamplingRateDiv, wChannelStatus, wChannelConfig, wChannelThreshold); for(i=0; i<20; i++) { printf("%d-> %d | %d-> %d\n",i,wTestData[i],i+20,wTestData[i+20]); } printf("wRet=%d\n",wRet); /***************************************************************/ P1202_DriverClose(); return 0; } /***************** Programning 1202's PIC ******************************/ WORD wMappingCh[32],wMappingThreshold[32]; WORD wG2Total,wG2Count,wG2Stop,wG2ThreadStatus,wG2Count; WORD ProgramPIC(WORD wSampleRateDiv, WORD wChannelStatus[], WORD wChannelConfig[], WORD Threshold[]) { WORD wVal,i,wConfig,wRet; DWORD dwTime; /* STEP_1 : clear_scan_first */ For_Disable_Timer0(); wG2Total=0; wVal=For_PIC_Control(0xC000); /* 11?0 00?? ???? ???? cmd_000=reset */ if(wVal!=0) { printf("Error in ProgramPIC 1...\n"); return(wVal); } /* STEP_2 : add_to_scan */ for(i=0; i<32; i++) { if(wChannelStatus[i]!=0) { /*** adding the channel no for mapping ***/ wMappingCh[wG2Total]=i; wMappingThreshold[wG2Total]=Threshold[i]; wG2Total++; wConfig = (wChannelConfig[i])&0x0f; wConfig = wConfig << 6; wConfig += i; wConfig+= 0xD000; /* this is set channel config command */ wVal=For_PIC_Control(wConfig); if(wVal!=0) { printf("Error in ProgramPIC 1...\n"); return(wVal); } } } /* STEP_3 : start_to_scan */ wVal=For_PIC_Control(0xD400); /* 11?1 01?? ???? ???? cmd_101=start scan */ if (wVal!=0) return(wVal); /* Clear FIFO */ outport(wAddrCtrl,0x2000); /* Bit15=0=clear FIFO, Bit13=1=not PIC cmd */ outport(wAddrCtrl,0xA000); /* Bit15=1=no reset FIFO, BIT13=1=not PIC cmd */ //dwG2P1=0; //wG2Count= wDataCount; //dwG2Group=(DWORD)(wG2Total*wDataCount); //if(dwG2Group>9000) return(InvalidateData); printf("wG2Total=%d\n",wG2Total); for(i=0; i100000) { return(TimeOut); } } inport(wAddrAdda)&0xffff; } wRet=CheckingThreshold(); return( wRet ); } /*** Checking the incoming data to decide if bit DO:n have to turn ON ****** // wThreadStatus : 0x01=MagicScan start // 0x02=timeout 0x08=FIFO overflow **************************************************************************/ WORD CheckingThreshold() { WORD wChIndex,wVal,wDigitalOut,wCount; DWORD dwT; char ch; wChIndex=0; wCount=0; dwT=0; for(;;) { if (dwT>600000) { wG2ThreadStatus=2; For_Disable_Timer0(); putch(0x07); printf("*** TimeOut ***\n"); return TimeOut; } wVal=inport(wAddrCtrl)&0x60; if(wVal==0x20) { wG2ThreadStatus=8; For_Disable_Timer0(); putch(0x07); printf("*** FifoOverflow ***\n"); return FifoOverflow; } if(wVal!=0x60) dwT++; else { wVal=inport(wAddrAdda)&0x0fff; if( wCount<100 ) { wTestData[wCount]=wVal; wCount++; } if (wVal>wMappingThreshold[wChIndex] ) { wDigitalOut=(1 << wMappingCh[wChIndex]); P1202_Do(wDigitalOut); /* turn on bit DO:n */ } dwT=0; } wChIndex++; if( wChIndex==wG2Total ) wChIndex=0; if( kbhit() ) if( (ch=getch())==27 ) // ESC hit return NoError; } } /*----------------------------------------------------------------------*/ WORD For_PIC_Control(WORD i) { WORD j; if ((inport(wAddrCtrl)&0x04)==0) { outport(wAddrCtrl,0xffff); /* send a recovery to PIC */ } j=0; while ((inport(wAddrCtrl)&0x04)==0) { j++; if (j>65530) return(AdControllerError); /* time out */ } i = i & 0xDFFF; /* set pic low !!! */ outport(wAddrCtrl,i); j=0; while ((inport(wAddrCtrl)&0x04)!=0) { j++; if (j>65530) return(AdControllerError); /* time out */ } outport(wAddrCtrl,(WORD)(i | 0x2000)); /* set pic high !!! */ j=0; while ((inport(wAddrCtrl)&0x04)==0) { j++; if (j>65530) return(AdControllerError); /* time out */ } return(0); } void For_Disable_Timer0(void) { outport((WORD)(wAddrTimer+3*4), 0x34); /* disable timer_0 */ outport((WORD)(wAddrTimer+0*4), 0x01); outport((WORD)(wAddrTimer+0*4), 0x00); }