/* ADPCMCIA.C The calling syntax is: [yp] = ADPCMCIA(interval,data_num,channel,'ioadr') Kazuyuki Kobayashi Feb 2, 1998 Copyright (c) 1998 Gerox(c) All Rights Reserved for Visual C++ Version 4.0 March 4, 1998 for Watcom C++ Version 10.5J */ #include #ifdef __WATCOMC__ #include #endif #include #include "mex.h" /* Input Arguments */ /* Output Arguments */ #define YP_OUT plhs[0] #include #include #include #ifndef __WATCOMC__ #define outp _outp #define inpw _inpw #define inp _inp #endif union _tag { long l; char c[4]; }; #define ADR 0x240 /* I/O address */ #define CHLS 8 /* number of channels */ static void adpcmcia( double yp[], double *interval, double *data_num, double *channel, double *ioaddr ) { int ModeData = 1;/* mode data */ union _tag ClockData = { 799 };/* sampling clock */ union _tag CountData = { 9 };/* sampling count */ int AiData, i, Count = 0; int j; unsigned short ioadr = (unsigned short)*ioaddr; CountData.l = (long) *data_num; ClockData.l = (long)(*interval *1e7) - 1L; if((int)*channel == 1) ModeData = 0; outp(ioadr+6, 0 );/* initialize */ outp(ioadr+6, 1 );/* set sampling mode */ outp(ioadr+7, ModeData ); outp(ioadr+6, 2 );/* set sampling clock */ outp(ioadr+7, ClockData.c[0] ); outp(ioadr+7, ClockData.c[1] ); outp(ioadr+7, ClockData.c[2] ); outp(ioadr+6, 3 );/* set sampling count */ outp(ioadr+7, CountData.c[0] ); outp(ioadr+7, CountData.c[1] ); if((int)*channel == 1) outp(ioadr+2,0); else outp(ioadr+2, (int)*channel-1 );/* sampling start */ for(j = 0;j < *data_num;j++) { while (!(inp(ioadr+2) & 2)); for ( i = 0; i < *channel; i++ ) { AiData = inpw(ioadr ); /* input data */ yp[(int)*data_num * i + j] = (double)AiData * 20. / 4096. - 10.; } if(!inp(ioadr+2) &3) break; } return; } void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ) { double *yp; double *t,*y; unsigned int m,n; double interval; double data_num; double channel; double ioadr; char buf[256]; int buflen = 255; int status; int i; /* Check for proper number of arguments */ if (nrhs == 3); else if(nrhs == 5); else if (nrhs != 4) { mexPrintf(" This ADPCMCIA function is for CONTEC AD12-8(PM) \n"); mexPrintf(" AD/DA PCMCIA card for MATLAB Version 5. \n"); mexPrintf(" In order to use this fucntion,please check \n"); mexPrintf(" the AD/DA PCMCIA card I/O address under Windows 95 \n"); mexPrintf(" default I/O setting is 0x240\n\n"); mexPrintf(" Usage: adpcmcia(interval,data_num,channel,'ioadr')\n\n"); mexPrintf(" Example: ADPCMCIA(0.01,1000,2,'0x240') \n"); mexPrintf(" : ADPCMCIA(0.01,100,2) \n\n"); mexPrintf(" interval range :%f - %f Sec\n",1048576.0/1e7,100./1e7); mexPrintf(" data_num*channel :1-16384\n"); mexPrintf(" channel :1-8\n\n"); mexPrintf(" Gerox(c) 1997 All Rights Reserved\n"); mexErrMsgTxt(""); } else if (nlhs > 1) { mexErrMsgTxt("ADPCMCIA requires one output argument."); } /* Check the dimensions of Input parameters. The parameter 0 to 2 should be Numeriacal and double. */ for(i = 0;i < 3;i++) { if (!mxIsNumeric(prhs[i]) || mxIsComplex(prhs[i]) || mxIsSparse(prhs[i]) || !mxIsDouble(prhs[i])){ mexErrMsgTxt("ADPCMCIA requires that Input be numeric number."); } } /* The 4-th paramter is optional. If function found 4-th parameter, check string or not and convert hex value from the string, otherwise set default hex value (0x240) */ if(nrhs > 3) { if (!mxIsChar(prhs[3])) { mexErrMsgTxt("\nADPCMCIA requires that Input parameter be Char. such as '0x240'"); } status = mxGetString(prhs[3],buf,buflen); if(status == 0); else mexErrMsgTxt("\nADPCMCIA requires that input be Char."); } interval = mxGetScalar(prhs[0]); data_num = mxGetScalar(prhs[1]); channel = mxGetScalar(prhs[2]); if(nrhs > 3) { sscanf(buf,"%x",&i); ioadr = (double)i; } else ioadr = ADR; /* Create a matrix for the return argument */ YP_OUT = mxCreateDoubleMatrix((int)data_num,(int) channel, mxREAL); /* Assign pointers to the various parameters */ yp = mxGetPr(YP_OUT); if(interval > 1048576.0/1e7) { mexPrintf("Min sampling = %f [Sec]\n",1048576./1e7); interval = 1048576.0/1e7; } if(interval < 100.0/1e7) { mexPrintf("Max sampling = %f [Sec]\n",100./1e7); interval = 100.0/1e7; } if(nrhs != 5) { mexPrintf(" Gerox(c) 1997 All Rights Reserved\n"); mexPrintf(" interval = %g,datanum = %g,channel = %g,ioadr = 0x%x\n Please wait %g Sec \a\n", interval,data_num,channel,(int)ioadr,interval*data_num); } adpcmcia(yp,&interval,&data_num,&channel,&ioadr); if(nrhs != 5) mexPrintf("\a"); return; }