/* admultiq3.c - xPC Target, non-inlined S-function driver for: */ /* A/D section of MultiQ3 ISA board (Quanser) */ /* Written by R.A. de Callafon, Feb. 2004, Dept. of MAE, UCSD */ /* xPC is Copyright 1996-2002 The MathWorks, Inc. */ #define S_FUNCTION_LEVEL 2 #undef S_FUNCTION_NAME #define S_FUNCTION_NAME admultiq3 #include #include #include "simstruc.h" #ifdef MATLAB_MEX_FILE #include "mex.h" #else #include #include "io_xpcimport.h" #endif /* Input Arguments */ #define NUM_PARAMS (3) #define BASE_ADDRESS_ARG (ssGetSFcnParam(S,0)) #define CHANNEL_ARG (ssGetSFcnParam(S,1)) #define SAMPLE_TIME_PARAM (ssGetSFcnParam(S,2)) /* Convert S Function Parameters to Variables of Unsigned Integer Type */ #define BASE ((uint_T) mxGetPr(BASE_ADDRESS_ARG)[0]) #define SAMPLE_TIME ((real_T) mxGetPr(SAMPLE_TIME_PARAM)[0]) #define SAMPLE_OFFSET ((real_T) mxGetPr(SAMPLE_TIME_PARAM)[1]) /* Definition of Quanser Register Adresses for AD conversion */ #define AD_CS (BASE + 0x04) /* a write to this address initiates conversion, provided the CONTROL REGISTER has been set up properly */ #define AD_DATA (BASE + 0x04) /* a double read from this 8 bit address first yields the high then the low 8 bits of the AD conversion */ #define CONTROL_REG (BASE + 0x06) /* the address of the CONTROL REGISTER */ #define STATUS_REG (BASE + 0x06) /* the address of the CONTROL REGISTER (or better STATUS REGISTER in case of reading) */ /* Definition of 16 bit CONTROL REGISTER values for the Quanser card */ #define AD_SH 0x200 /* (S/H) = disable sample and hold on the A/D (keep this 1 all the time) */ #define AD_AUTOCAL 0x100 /* (CAL) = enable autocalibartion on the A/D */ #define AD_AUTOZ 0x80 /* (AZ) = enable auto Zero on the A/D */ #define AD_MUX_EN 0x40 /* (MX) = enable the 8 channel multiplexer */ #define AD_CLOCK_4M 0x400 /* (CLK) = choose clock frequency of the A/D, (1 = 4MHz, 0 = 2 MHz) */ #define CONTROL_MUST (AD_SH | AD_CLOCK_4M) /* bit wise OR, so we keep both S/H and CLK high all the time */ /* Definition of msg string in case we need to report errors */ static char_T msg[256]; /*====================* * S-function methods * *====================*/ static void mdlCheckParameters(SimStruct *S) { } static void mdlInitializeSizes(SimStruct *S) { uint_T i; #ifndef MATLAB_MEX_FILE #include "io_xpcimport.c" #endif ssSetNumSFcnParams(S, NUM_PARAMS); if (ssGetNumSFcnParams(S) == ssGetSFcnParamsCount(S)) { mdlCheckParameters(S); if (ssGetErrorStatus(S) != NULL) { return; } } else { return; /* Parameter mismatch will be reported by Simulink */ } ssSetNumContStates(S, 0); ssSetNumDiscStates(S, 0); if (!ssSetNumInputPorts(S, 0)) return; if (!ssSetNumOutputPorts(S, mxGetNumberOfElements(CHANNEL_ARG))) return; for (i=0;i