📄 task28xadc.c
字号:
paio->ADCScaleOut = (FP32)0.0; /* Output of scaling function */
paio->ADCScaleFnct = (void *)0; /* No function to execute */
paio->ADCScaleFnctArg = (void *)0; /* No arguments to scale function */
paio++;
}
}
/*$PAGE*/
/*
*********************************************************************************************************
* ANALOG I/O MANAGER INITIALIZATION
*
* Description : This function initializes the analog I/O manager module.
* Arguments : None
* Returns : None.
*********************************************************************************************************
*/
void AdcInit (void)
{
INT8U err;
AdcInit();
AdcInitIO();
ADCSem = OSSemCreate(1); /* Create a mutual exclusion semaphore for AIOs */
OSTaskCreateExt(ADCTask, (void *)0, &ADCTaskStk[ADC_TASK_STK_SIZE], ADC_TASK_PRIO,
ADC_TASK_PRIO, &ADCTaskStk[0], ADC_TASK_STK_SIZE, (void *)0, OS_TASK_OPT_SAVE_FP);
}
/*$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 AdcTask (void *data)
{
INT8U err;
data = data; /* Avoid compiler warning */
for (;;) {
OSTimeDlyHMSM(0, 0, 0, AIO_TASK_DLY); /* Delay between execution of ADC manager */
OSSemPend(ADCSem, 0, &err); /* Obtain exclusive access to ADC channels */
ADCUpdate(); /* Update all ADC channels */
OSSemPost(ADCSem); /* Release ADC 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..ADC_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 ADCBypassEn was not set to TRUE
*********************************************************************************************************
*/
INT8U AdcSetBypass (INT8U n, FP32 val)
{
ADC *paio;
if (n < ADC_MAX_AI) {
paio = &ADCTbl[n]; /* Faster to use a pointer to the structure */
if (paio->ADCBypassEn == TRUE) { /* See if the analog input channel is bypassed */
OS_ENTER_CRITICAL();
paio->ADCEU = 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. ADCBypassEn is set to TRUE).
* Arguments : n is the analog input channel (0..ADC_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 AdcSetBypassEn (INT8U n, BOOLEAN state)
{
if (n < ADC_MAX_AI) {
ADCTbl[n].ADCBypassEn = 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 AdcUpdate (void)
{
INT8U i;
ADC *paio;
paio = &ADCTbl[0]; /* Point at first analog input channel */
for (i = 0; i < ADC_MAX_AI; i++) { /* Process all analog input channels */
if (paio->ADCBypassEn == FALSE) { /* See if analog input channel is bypassed */
paio->ADCPassCtr--; /* Decrement pass counter */
if (paio->ADCPassCtr == 0) { /* When pass counter reaches 0, read and scale ADC */
paio->ADCPassCtr = paio->ADCPassCnts; /* Reload pass counter */
paio->ADCRaw = ADCRd(i); /* Read ADC for this channel */
paio->ADCScaleIn = ((FP32)paio->ADCRaw + paio->ADCOffset) * paio->ADCGain;
if ((void *)paio->ADCScaleFnct != (void *)0) { /* See if function defined */
(*paio->ADCScaleFnct)(paio); /* Yes, execute function */
} else {
paio->ADCScaleOut = paio->ADCScaleIn; /* No, just copy data */
}
paio->ADCEU = paio->ADCScaleOut; /* Output of scaling fnct to E.U. */
}
}
paio++; /* Point at next AI channel */
}
}
/*$PAGE*/
#ifndef CFG_C
/*
*********************************************************************************************************
* INITIALIZE PHYSICAL I/Os
*
* Description : This function is called by ADCInit() to initialize the physical I/O used by the ADC
* driver.
* Arguments : None.
* Returns : None.
*********************************************************************************************************
*/
void AdcInitIO (void)
{
/* This is where you will need to put you initialization code for the ADCs and DACs */
/* You should also consider initializing the contents of your DAC(s) to a known value. */
}
/*
*********************************************************************************************************
* READ PHYSICAL INPUTS
*
* Description : This function is called to read a physical ADC channel. The function is assumed to
* also control a multiplexer if more than one analog input is connected to the ADC.
* Arguments : ch is the ADC logical channel number (0..ADC_MAX_AI-1).
* Returns : The raw ADC counts from the physical device.
*********************************************************************************************************
*/
INT16S AdcRd (INT8U ch)
{
/* This is where you will need to provide the code to read your ADC(s). */
/* ADCRd() is passed a 'LOGICAL' channel number. You will have to convert this logical channel */
/* number into actual physical port locations (or addresses) where your MUX. and ADCs are located. */
/* ADCRd() is responsible for: */
/* 1) Selecting the proper MUX. channel, */
/* 2) Waiting for the MUX. to stabilize, */
/* 3) Starting the ADC, */
/* 4) Waiting for the ADC to complete its conversion, */
/* 5) Reading the counts from the ADC and, */
/* 6) Returning the counts to the calling function. */
return (ch);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -