⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 task28xadc.c

📁 cap口例程
💻 C
📖 第 1 页 / 共 2 页
字号:
/*
*********************************************************************************************************
*                                          Analog Input Module
*
*
* Filename   : task28xadc.c
* Programmer : luo.xin
*********************************************************************************************************
*/

/*
*********************************************************************************************************
*                                           INCLUDE FILES
*********************************************************************************************************
*/

#define   ADC_GLOBALS
#include "includes.h"

/*
*********************************************************************************************************
*                                          LOCAL VARIABLES
*********************************************************************************************************
*/

static  OS_STK      ADCTaskStk[ADC_TASK_STK_SIZE];
static  OS_EVENT    *ADCSem;

/*
*********************************************************************************************************
*                                      LOCAL FUNCTION PROTOTYPES
*********************************************************************************************************
*/

        void        AdcTask(void *data);

static  void        InitAdc(void);
static  void        AdcUpdate(void);

/*$PAGE*/
/*
*********************************************************************************************************
*                 CONFIGURE THE CALIBRATION PARAMETERS OF AN ANALOG INPUT CHANNEL
*
* Description : This function is used to configure an analog input channel.
* Arguments   : n        is the analog input channel to configure:
*               gain     is the calibration gain
*               offset   is the calibration offset
* Returns     : 0        if successfull.
*               1        if you specified an invalid analog input channel number.
*********************************************************************************************************
*/

INT8U  AdcCfgCal (INT8U n, FP32 gain, FP32 offset)
{
    INT8U err;
    ADC  *paio;


    if (n < ADC_MAX_AI) {
        paio               = &ADCTbl[n];              /* Point to Analog Input structure               */
        OSSemPend(ADCSem, 0, &err);                   /* Obtain exclusive access to AI channel         */
        paio->ADCCalGain   = gain;                    /* Store new cal. gain and offset into struct    */
        paio->ADCCalOffset = offset;
        paio->ADCGain      = paio->ADCCalGain   * paio->ADCConvGain;      /* Compute overall gain      */
        paio->ADCOffset    = paio->ADCCalOffset + paio->ADCConvOffset;    /* Compute overall offset    */
        OSSemPost(ADCSem);                                                /* Release AI channel        */
        return (0);
    } else {
        return (1);
    }
}

/*$PAGE*/
/*
*********************************************************************************************************
*                    CONFIGURE THE CONVERSION PARAMETERS OF AN ANALOG INPUT CHANNEL
*
* Description : This function is used to configure an analog input channel.
* Arguments   : n        is the analog channel to configure (0..ADC_MAX_AI-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 input channel number.
*********************************************************************************************************
*/

INT8U  AdcCfgConv (INT8U n, FP32 gain, FP32 offset, INT8U pass)
{
    INT8U err;
    ADC  *paio;


    if (n < ADC_MAX_AI) {
        paio                = &ADCTbl[n];             /* Point to Analog Input structure               */
        OSSemPend(ADCSem, 0, &err);                   /* Obtain exclusive access to AI channel         */
        paio->ADCConvGain   = gain;                   /* Store new conv. gain and offset into struct   */
        paio->ADCConvOffset = offset;
        paio->ADCGain       = paio->ADCCalGain   * paio->ADCConvGain;     /* Compute overall gain      */
        paio->ADCOffset     = paio->ADCCalOffset + paio->ADCConvOffset;   /* Compute overall offset    */
        paio->ADCPassCnts   = pass;
        OSSemPost(ADCSem);                                                /* Release AI channel        */
        return (0);
    } else {
        return (1);
    }
}

/*$PAGE*/
/*
*********************************************************************************************************
*                      CONFIGURE THE SCALING PARAMETERS OF AN ANALOG INPUT CHANNEL
*
* Description : This function is used to configure the scaling parameters associated with an analog
*               input channel.
* Arguments   : n        is the analog input channel to configure (0..ADC_MAX_AI-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 input channel number.
*********************************************************************************************************
*/

INT8U  AdcCfgScaling (INT8U n, void (*fnct)(ADC *paio), void *arg)
{
    AIO *paio;


    if (n < ADC_MAX_AI) {
        paio                  = &ADCTbl[n];            /* Faster to use a pointer to the structure      */
        OS_ENTER_CRITICAL();
        paio->ADCScaleFnct    = (void (*)())fnct;
        paio->ADCScaleFnctArg = arg;
        OS_EXIT_CRITICAL();
        return (0);
    } else {
        return (1);
    }
}

/*$PAGE*/
/*
*********************************************************************************************************
*                                GET THE VALUE OF AN ANALOG INPUT CHANNEL
*
* Description : This function is used to get the currect value of an analog input channel (in engineering
*               units).
* Arguments   : n     is the analog input channel (0..ADC_MAX_AI-1).
*               pval  is a pointer to the destination engineering units of the analog input channel
* Returns     : 0        if successfull.
*               1        if you specified an invalid analog input channel number.
*                        In this case, the destination is not changed.
*********************************************************************************************************
*/

INT8U  AdcGet (INT8U n, FP32 *pval)
{
    ADC  *paio;


    if (n < ADC_MAX_AI) {
        paio  = &ADCTbl[n];
        OS_ENTER_CRITICAL();           /* Obtain exclusive access to ADC channel                        */
        *pval = paio->ADCEU;           /* Get the engineering units of the analog input channel        */
        OS_EXIT_CRITICAL();            /* Release ADC channel                                           */
        return (0);
    } else {
        return (1);
    }
}

/*$PAGE*/
/*
*********************************************************************************************************
*                                     ANALOG INPUTS INITIALIZATION
*
* Description : This function initializes the analog input channels.
* Arguments   : None
* Returns     : None.
*********************************************************************************************************
*/

static  void  AdcInit (void)
{
    INT8U   i;
    ADC    *paio;


    paio = &ADCTbl[0];
    for (i = 0; i < ADC_MAX_AI; i++) {
        paio->ADCBypassEn     =  FALSE;           /* Analog channel is not bypassed                     */
        paio->ADCRaw          =  0x0000;          /* Raw counts of ADC or DAC                           */
        paio->ADCEU           = (FP32)0.0;        /* Engineering units of ADC channel                    */
        paio->ADCGain         = (FP32)1.0;        /* Total gain                                         */
        paio->ADCOffset       = (FP32)0.0;        /* Total offset                                       */
        paio->ADCLim          =       0;
        paio->ADCPassCnts     =       1;          /* Pass counts                                        */
        paio->ADCPassCtr      =       1;          /* Pass counter                                       */
        paio->ADCCalGain      = (FP32)1.0;        /* Calibration gain                                   */
        paio->ADCCalOffset    = (FP32)0.0;        /* Calibration offset                                 */
        paio->ADCConvGain     = (FP32)1.0;        /* Conversion gain                                    */
        paio->ADCConvOffset   = (FP32)0.0;        /* Conversion offset                                  */
        paio->ADCScaleIn      = (FP32)0.0;        /* Input  to scaling function                         */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -