/**********************************************************************/ /* This program is developed by Turbo C 2.0 */ /***********************************************************************/ /* 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-1802L, PCI-1802H user. */ /***********************************************************************/ #define MYLIB #include "P180X.H" WORD ProgramPIC(WORD wSampleRateDiv, WORD wChannelStatus[], WORD wChannelConfig[], WORD Threshold[]); WORD CheckingThreshold(); WORD For_PIC_Control(WORD i); void For_Disable_Timer0(void); void StopPIC(void); WORD wAddrTimer,wAddrCtrl,wAddrDio,wAddrAdda; WORD wTestData[100]; WORD wChangeTimes; /* To see how many time the digital output change */ 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 P180X card and detect how many P180X card in PC */ wRetVal=P180X_DriverInit(&wBoards); printf("Threr are %d P180X Cards in this PC\n",wBoards); if( wBoards==0 ) { putch(0x07); putch(0x07); putch(0x07); printf("There are no P180X card in this PC !!!\n"); exit(0); } /* dump every P180X card's configuration address space */ printf("The Configuration Space -> Timer Control DIO AD/DA\n"); for(i=0; i 0000H Press any key...\n"); P180X_Do(0x0000); /* Digital output */ getch(); printf("Digital Output Testing -> 0001H Press any key...\n"); P180X_Do(0x0001); /* Digital output */ getch(); printf("Digital Output Testing -> 0002H Press any key...\n"); P180X_Do(0x0002); /* Digital output */ getch(); printf("Digital Output Testing -> 0003H Press any key...\n"); P180X_Do(0x0003); /* Digital output */ getch(); printf("Digital Output Testing -> 0004H Press any key...\n"); P180X_Do(0x0004); /* Digital output */ getch(); printf("Digital Output Testing -> 0005H Press any key...\n"); P180X_Do(0x0005); /* Digital output */ getch(); printf("Digital Output testing -> 0000H Press any Key...\n"); P180X_Do(0x0000); /* Digital output */ printf("Enter the Freqency Divisor(Ex:8000 -> 1K):"); scanf("%d",&wSamplingRateDiv); printf("The Sampling rate -> %6.2fK\n",8000.0/wSamplingRateDiv); /******** preparing parameter to ProgramPIC() ***************/ wChangeTimes=0; 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[2]=1; /* to monitor the incoming data of ch:2 */ wChannelStatus[3]=1; /* to monitor the incoming data of ch:2 */ wChannelThreshold[0]=0x0E00; /* set the threshold value of ch:0 */ wChannelThreshold[2]=0x0E00; /* set the threshold value of ch:2 */ wChannelThreshold[3]=0x0E00; /* set the threshold value of ch:2 */ /* the Sampling rate is 8M/wSamplingRateDiv */ wRet=ProgramPIC(wSamplingRateDiv, wChannelStatus, wChannelConfig, wChannelThreshold); printf("wRet=%d\n",wRet); printf("wChangeTimes=%d\n",wChangeTimes); StopPIC(); /***************************************************************/ P180X_DriverClose(); return 0; } /***************** Programning 180X's PIC ******************************/ WORD wMappingCh[32],wMappingThreshold[32]; WORD wG2Total,wG2Count,wG2Stop,wG2ThreadStatus,wG2Count; WORD wErrorLog[200]; 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 */ printf("Dump the setting information...\n"); printf("wG2Total=%d\n",wG2Total); for(i=0; i100000) { return(TimeOut); } } inport(wAddrAdda)&0xffff; } wRet=CheckingThreshold(); return( wRet ); } void StopPIC() { For_Disable_Timer0(); } /*** 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,i,wExceedCount; WORD wOutStatus; /* store the Digital Output Bit status for a sweep */ WORD wOldOutStatus; DWORD dwT; char ch; wChIndex=0; wCount=0; wExceedCount=0; dwT=0; wOldOutStatus=wOutStatus=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); if ( (wVal & 0x0fff )>wMappingThreshold[wChIndex] ) { wDigitalOut=(1 << wMappingCh[wChIndex]); wOutStatus=wOutStatus | wDigitalOut; /*---------------------------*/ wErrorLog[wExceedCount++]=wVal; P180X_Do(wExceedCount); /* Digital output */ if( wExceedCount>199 ) { StopPIC(); for(i=0; i<200; i++) { printf("i=%02d Error Data: %04x\n",i,wErrorLog[i]); if( i%20==19 ) getch(); } exit(0); } } else { wDigitalOut=~((1 << wMappingCh[wChIndex])); wOutStatus=wOutStatus & wDigitalOut; } dwT=0; wCount++; wChIndex++; if( wChIndex==wG2Total ) { wChIndex=0; if( wOldOutStatus!=wOutStatus ) { wChangeTimes++; P180X_Do(wOutStatus); /* turn on bit DO:n */ if( wChangeTimes>199 ) { StopPIC(); for(i=0; i<200; i++) { printf("i=%02d Error Data: %04x\n",i,wErrorLog[i]); if( i%20==19 ) getch(); } exit(0); } } wOldOutStatus=wOutStatus; } } if( wCount==30000 ) { wCount=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); }