📄 aio.c
字号:
/*$PAGE*/
/*
*********************************************************************************************************
* ANALOG I/O MANAGER TASK
*
* Description : This task is created by AIOInit() and is responsible for updating the analog inputs and
* analog outputs.
* AIOTask() executes every AIO_TASK_DLY milliseconds.
* Arguments : None.
* Returns : None.
*********************************************************************************************************
*/
void AIOTask (void *data)
{
INT8U err;
data = data; /* Avoid compiler warning */
for (;;) {
OSTimeDlyHMSM(0, 0, 0, AIO_TASK_DLY); /* Delay between execution of AIO manager */
OSSemPend(AIOSem, 0, &err); /* Obtain exclusive access to AI channels */
AIUpdate(); /* Update all AI channels */
OSSemPost(AIOSem); /* Release AI channels (Allow high prio. task to run) */
OSSemPend(AIOSem, 0, &err); /* Obtain exclusive access to AO channels */
AOUpdate(); /* Update all AO channels */
OSSemPost(AIOSem); /* Release AO channels (Allow high prio. task to run) */
}
}
/*$PAGE*/
/*
*********************************************************************************************************
* SET THE STATE OF THE BYPASSED ANALOG INPUT CHANNEL
*
* Description : This function is used to set the engineering units of a bypassed analog input channel.
* This function is used to simulate the presense of the sensor. This function is only
* valid if the bypass 'switch' is open.
* Arguments : n is the analog input channel (0..AIO_MAX_AI-1).
* val is the value of the bypassed analog input channel:
* Returns : 0 if successfull.
* 1 if you specified an invalid analog input channel number.
* 2 if AIOBypassEn was not set to TRUE
*********************************************************************************************************
*/
INT8U AISetBypass (INT8U n, FP32 val)
{
AIO *paio;
if (n < AIO_MAX_AI) {
paio = &AITbl[n]; /* Faster to use a pointer to the structure */
if (paio->AIOBypassEn == TRUE) { /* See if the analog input channel is bypassed */
OS_ENTER_CRITICAL();
paio->AIOEU = val; /* Yes, then set the new value of the channel */
OS_EXIT_CRITICAL();
return (0);
} else {
return (2);
}
} else {
return (1);
}
}
/*$PAGE*/
/*
*********************************************************************************************************
* SET THE STATE OF THE BYPASS SWITCH
*
* Description : This function is used to set the state of the bypass switch. The analog input channel is
* bypassed when the 'switch' is open (i.e. AIOBypassEn is set to TRUE).
* Arguments : n is the analog input channel (0..AIO_MAX_AI-1).
* state is the state of the bypass switch:
* FALSE disables the bypass (i.e. the bypass 'switch' is closed)
* TRUE enables the bypass (i.e. the bypass 'switch' is open)
* Returns : 0 if successfull.
* 1 if you specified an invalid analog input channel number.
*********************************************************************************************************
*/
INT8U AISetBypassEn (INT8U n, BOOLEAN state)
{
if (n < AIO_MAX_AI) {
AITbl[n].AIOBypassEn = state;
return (0);
} else {
return (1);
}
}
/*$PAGE*/
/*
*********************************************************************************************************
* UPDATE ALL ANALOG INPUT CHANNELS
*
* Description : This function processes all of the analog input channels.
* Arguments : None.
* Returns : None.
*********************************************************************************************************
*/
static void AIUpdate (void)
{
INT8U i;
AIO *paio;
paio = &AITbl[0]; /* Point at first analog input channel */
for (i = 0; i < AIO_MAX_AI; i++) { /* Process all analog input channels */
if (paio->AIOBypassEn == FALSE) { /* See if analog input channel is bypassed */
paio->AIOPassCtr--; /* Decrement pass counter */
if (paio->AIOPassCtr == 0) { /* When pass counter reaches 0, read and scale AI */
paio->AIOPassCtr = paio->AIOPassCnts; /* Reload pass counter */
paio->AIORaw = AIRd(i); /* Read ADC for this channel */
paio->AIOScaleIn = ((FP32)paio->AIORaw + paio->AIOOffset) * paio->AIOGain;
if ((void *)paio->AIOScaleFnct != (void *)0) { /* See if function defined */
(*paio->AIOScaleFnct)(paio); /* Yes, execute function */
} else {
paio->AIOScaleOut = paio->AIOScaleIn; /* No, just copy data */
}
paio->AIOEU = paio->AIOScaleOut; /* Output of scaling fnct to E.U. */
}
}
paio++; /* Point at next AI channel */
}
}
/*$PAGE*/
/*
*********************************************************************************************************
* CONFIGURE THE CALIBRATION PARAMETERS OF AN ANALOG OUTPUT CHANNEL
*
* Description : This function is used to configure an analog output channel.
* Arguments : n is the analog output channel to configure (0..AIO_MAX_AO-1)
* gain is the calibration gain
* offset is the calibration offset
* Returns : 0 if successfull.
* 1 if you specified an invalid analog output channel number.
*********************************************************************************************************
*/
INT8U AOCfgCal (INT8U n, FP32 gain, FP32 offset)
{
INT8U err;
AIO *paio;
if (n < AIO_MAX_AO) {
paio = &AOTbl[n]; /* Point to Analog Output structure */
OSSemPend(AIOSem, 0, &err); /* Obtain exclusive access to AO channel */
paio->AIOCalGain = gain; /* Store new cal. gain and offset into struct */
paio->AIOCalOffset = offset;
paio->AIOGain = paio->AIOCalGain * paio->AIOConvGain; /* Compute overall gain */
paio->AIOOffset = paio->AIOCalOffset + paio->AIOConvOffset; /* Compute overall offset */
OSSemPost(AIOSem); /* Release AO channel */
return (0);
} else {
return (1);
}
}
/*$PAGE*/
/*
*********************************************************************************************************
* CONFIGURE THE CONVERSION PARAMETERS OF AN ANALOG OUTPUT CHANNEL
*
* Description : This function is used to configure an analog output channel.
* Arguments : n is the analog channel to configure (0..AIO_MAX_AO-1).
* gain is the conversion gain
* offset is the conversion offset
* pass is the value for the pass counts
* Returns : 0 if successfull.
* 1 if you specified an invalid analog output channel number.
*********************************************************************************************************
*/
INT8U AOCfgConv (INT8U n, FP32 gain, FP32 offset, INT16S lim, INT8U pass)
{
INT8U err;
AIO *paio;
if (n < AIO_MAX_AO) {
paio = &AOTbl[n]; /* Point to Analog Output structure */
OSSemPend(AIOSem, 0, &err); /* Obtain exclusive access to AO channel */
paio->AIOConvGain = gain; /* Store new conv. gain and offset into struct */
paio->AIOConvOffset = offset;
paio->AIOGain = paio->AIOCalGain * paio->AIOConvGain; /* Compute overall gain */
paio->AIOOffset = paio->AIOCalOffset + paio->AIOConvOffset; /* Compute overall offset */
paio->AIOLim = lim;
paio->AIOPassCnts = pass;
OSSemPost(AIOSem); /* Release AO channel */
return (0);
} else {
return (1);
}
}
/*$PAGE*/
/*
*********************************************************************************************************
* CONFIGURE THE SCALING PARAMETERS OF AN ANALOG OUTPUT CHANNEL
*
* Description : This function is used to configure the scaling parameters associated with an analog
* output channel.
* Arguments : n is the analog output channel to configure (0..AIO_MAX_AO-1).
* arg is a pointer to arguments needed by the scaling function
* fnct is a pointer to a scaling function
* Returns : 0 if successfull.
* 1 if you specified an invalid analog output channel number.
*********************************************************************************************************
*/
INT8U AOCfgScaling (INT8U n, void (*fnct)(AIO *paio), void *arg)
{
AIO *paio;
if (n < AIO_MAX_AO) {
paio = &AOTbl[n]; /* Faster to use a pointer to the structure */
OS_ENTER_CRITICAL();
paio->AIOScaleFnct = (void (*)())fnct;
paio->AIOScaleFnctArg = arg;
OS_EXIT_CRITICAL();
return (0);
} else {
return (1);
}
}
/*$PAGE*/
/*
*********************************************************************************************************
* ANALOG OUTPUTS INITIALIZATION
*
* Description : This function initializes the analog output channels.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -