📄 par4ch.c
字号:
*ExtraDecimation = -1;
return( PAR4CH_ERROR_GAIN );
}
// Compute helper values.
//
// Decimation = (int)( ((Turbo * Xtal ) / (Fconst * Sps)) - 1 );
*TurboLog = PAR4CH_GAIN_MAX - GainLog;
TurboValue = (double)(1 << *TurboLog);
TurboXtalOverConst = TurboValue * PAR4CH_XTAL / PAR4CH_FCONST;
// Compute decimation.
*Decimation = (int)( TurboXtalOverConst / Sps + 0.499999 ) - 1;
*ExtraDecimation = PAR4CH_EXTRADECIMATION_MIN;
// Decimation is too small, ie Sps is too large.
if ( *Decimation < PAR4CH_DECIMATION_MIN )
return( PAR4CH_ERROR_DECIMATION );
// Decimation is within valid range.
if ( *Decimation <= PAR4CH_DECIMATION_MAX )
return( PAR4CH_ERROR_NONE );
// Extra decimation is required ...
else {
NominalSps = TurboXtalOverConst / 6250.0L; // 50.000000 Hz
*ExtraDecimation = (int)( NominalSps / Sps + 0.499999 );
// Don't let it go beyond maximum decimation
TempSps = Sps * (double)(*ExtraDecimation);
*Decimation = (int)( TurboXtalOverConst / TempSps - 1 );
if (*Decimation > PAR4CH_DECIMATION_MAX)
*ExtraDecimation += 1; // round up
NominalSps = Sps * (double)(*ExtraDecimation);
*Decimation = (int)( TurboXtalOverConst / NominalSps - 1 );
}
return( PAR4CH_ERROR_NONE );
}
FUNCTYPE( int ) Par4chTdeToSpsGain(
int TurboLog,
int Decimation,
int ExtraDecimation,
double *Sps,
int *GainLog
) {
// Use this helper function to convert a particular set of
// TurboLog, Decimation, and ExtraDecimation values into the
// final sampling rate in Hz of the ADS1210.
//
// See the Burr Brown ADS1210 spec sheet for more information on
// the operation of the sigma delta modulator.
//
// Error check TurboLog.
if ( (TurboLog < PAR4CH_TURBO_MIN) || (TurboLog > PAR4CH_TURBO_MAX) ) {
*GainLog = -1;
*Sps = -1.0;
return( PAR4CH_ERROR_TURBO );
}
// Compute GainLog.
*GainLog = PAR4CH_GAIN_MAX - TurboLog;
// Error check Decimation and ExtraDecimation.
if ( (Decimation < PAR4CH_DECIMATION_MIN) ||
(Decimation > PAR4CH_DECIMATION_MAX) ||
(ExtraDecimation < PAR4CH_EXTRADECIMATION_MIN) ) {
*Sps = -1.0;
return( PAR4CH_ERROR_DECIMATION );
}
// Compute Sps.
*Sps = (PAR4CH_XTAL*(double)(1<<TurboLog)) /
(PAR4CH_FCONST*(Decimation+1)*ExtraDecimation);
return( PAR4CH_ERROR_NONE );
}
/* MAIN PAR4CH LIBRARY FUNCTION GROUPS:
*
* Initialization
* Execution control
* FIFO
* User LED and digital I/O
* PC interrupt
* Voltage check
*
*/
/* PAR4CH INITIALIZATION FUNCTIONS:
This is the function users should call to open the device driver and
initalize the PAR4CH before starting execution. It takes care of all
initialization. No other functions need to be called to initialize
the PAR4CH.
During initialization the ADS1210 control regsters are programmed
with values setting all board parameters like sampling rate
programmable gain etc. All ADS1210s are programmed in parallel with
the same values.
See par4ch.h for defined constants to use with the parameters passed
to Par4chOpen. To convert floating point sps (samples per second)
sampling rates to TurboLog and Decimation values, use the helper
function Par4chSpsGainToTde.
*/
FUNCTYPE( DEVHANDLE ) Par4chOpen(
char *PortName,
int PortMode,
double Sps,
double *ActualSps,
int *Error
) {
DEVHANDLE Par4chHandle;
double TrueSps;
int TurboLog, GainLog, Result, Decimation, ExtraDecimation;
// Compute decimation parameters and actual Sps.
GainLog = PAR4CH_GAIN_MIN;
Par4chSpsGainToTde(
Sps,
GainLog,
&TurboLog,
&Decimation,
&ExtraDecimation
);
Par4chTdeToSpsGain(
TurboLog,
Decimation,
ExtraDecimation,
&TrueSps,
&GainLog
);
if (ActualSps)
*ActualSps = TrueSps;
// Call advanced Open function.
Par4chHandle = Par4chFullOpen(
PortName,
PortMode,
PAR4CH_DF_SIGNED,
GainLog,
TurboLog,
Decimation,
ExtraDecimation,
PAR4CH_UNUSED,
NULL,
Error
);
return( Par4chHandle );
}
#define ERROR_RETURN( err ) { \
if ( Error ) \
*Error = err; \
\
return( BAD_DEVHANDLE ); \
\
} \
FUNCTYPE( DEVHANDLE ) Par4chFullOpen(
char *PortName,
int PortMode,
int Df,
int GainLog,
int TurboLog,
int Decimation,
int ExtraDecimation,
int Unused,
long *ADS1210_CrValue,
int *Error
) {
DEVHANDLE Par4chHandle;
INIT4CHPARMS Parm;
// Check all the initialization parameters:
// parallel port mode, data format, gain, turbo mode, and decimation
if ( ( PortMode != PAR4CH_PORT_MODE_EPP ) &&
( PortMode != PAR4CH_PORT_MODE_BPP ) &&
( PortMode != PAR4CH_PORT_MODE_ECP_BPP ) &&
( PortMode != PAR4CH_PORT_MODE_ECP_EPP ) )
ERROR_RETURN( PAR4CH_ERROR_PORT_MODE );
if ( (Df != PAR4CH_DF_OFFSET) && (Df != PAR4CH_DF_SIGNED) )
ERROR_RETURN( PAR4CH_ERROR_DATA_FORMAT );
if ( (GainLog < PAR4CH_GAIN_1) || (GainLog > PAR4CH_GAIN_16) )
ERROR_RETURN( PAR4CH_ERROR_GAIN );
if ( (TurboLog < PAR4CH_TURBO_1) || (TurboLog > PAR4CH_TURBO_16) )
ERROR_RETURN( PAR4CH_ERROR_TURBO );
if ( (GainLog+TurboLog < PAR4CH_GAINTURBO_MIN) ||
(GainLog+TurboLog > PAR4CH_GAINTURBO_MAX) )
ERROR_RETURN( PAR4CH_ERROR_GAIN_TURBO_PRODUCT );
if ( (Decimation < PAR4CH_DECIMATION_MIN) ||
(Decimation > PAR4CH_DECIMATION_MAX) )
ERROR_RETURN( PAR4CH_ERROR_DECIMATION );
if ( (ExtraDecimation < PAR4CH_EXTRADECIMATION_MIN) )
ERROR_RETURN( PAR4CH_ERROR_DECIMATION );
// Set up parameters to be passed to driver.
Parm.ParPortMode = PortMode;
Parm.Df = Df;
Parm.GainLog = GainLog;
Parm.TurboLog = TurboLog;
Parm.Decimation = Decimation;
Parm.ExtraDecimation = ExtraDecimation;
Parm.ADS1210_CrValue = 0;
Parm.Error = PAR4CH_ERROR_NONE;
// Open the device driver.
Par4chHandle = OsDriverOpen( PortName );
if ( Par4chHandle == BAD_DEVHANDLE ) {
Par4chLastDriverError = OsGetLastError();
ERROR_RETURN( PAR4CH_ERROR_DRIVER_NOT_OPEN );
}
// Initialize the A/D board.
if ( !DevIoReadWrite(
Par4chHandle,
IOCTL_PAR4CH_INIT,
&Parm,
sizeof(Parm),
&Parm,
sizeof(Parm)
) )
Parm.Error = PAR4CH_ERROR_DRIVER_REQUEST_FAILED;
// Save CrValue set by kernel init for optional output.
if (ADS1210_CrValue)
*ADS1210_CrValue = Parm.ADS1210_CrValue;
// Trap DevIo failure and DevIo success but Kernel Init failure.
// Driver was successfully opened, so close it.
if ( Parm.Error != PAR4CH_ERROR_NONE ) {
Par4chLastDriverError = OsGetLastError();
Par4chClose( Par4chHandle );
ERROR_RETURN( Parm.Error );
}
// Finish.
if ( Error )
*Error = Parm.Error;
return( Par4chHandle );
}
FUNCTYPE( int ) Par4chClose( DEVHANDLE Par4chHandle ) {
// Only close open drivers.
if (Par4chHandle == BAD_DEVHANDLE) {
Par4chLastDriverError = ERROR_SERVICE_DISABLED;
return( 1 );
}
// Close driver.
if ( !OsDriverClose(Par4chHandle) )
return( 0 );
Par4chLastDriverError = 0L;
return( 1 );
}
/* PAR4CH EXECUTION CONTROL FUNCTIONS:
These functions start and stop acquisition on the PAR4CH. Once
acquisition has been stopped, it is best to call Par4chClose and
Par4chOpen before starting again.
*/
FUNCTYPE( void ) Par4chStart( DEVHANDLE Par4chHandle ) {
int dummy;
DevIoReadWrite(
Par4chHandle,
IOCTL_PAR4CH_START,
&dummy,
sizeof(int),
NULL,
0
);
}
FUNCTYPE( void ) Par4chStop( DEVHANDLE Par4chHandle ) {
int dummy;
DevIoReadWrite(
Par4chHandle,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -