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

📄 aio.c

📁 是uC/OS II的作者写的东西关于IO应用,强烈推荐
💻 C
📖 第 1 页 / 共 3 页
字号:
/*
*********************************************************************************************************
*                                          Analog I/O Module
*
*                            (c) Copyright 1999, Jean J. Labrosse, Weston, FL
*                                          All Rights Reserved
*
* Filename   : AIO.C
* Programmer : Jean J. Labrosse
*********************************************************************************************************
*/

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

#define   AIO_GLOBALS
#include "includes.h"

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

static  OS_STK      AIOTaskStk[AIO_TASK_STK_SIZE];
static  OS_EVENT   *AIOSem;

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

        void        AIOTask(void *data);

static  void        AIInit(void);
static  void        AIUpdate(void);

static  void        AOInit(void);
static  void        AOUpdate(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  AICfgCal (INT8U n, FP32 gain, FP32 offset)
{
    INT8U err;
    AIO  *paio;


    if (n < AIO_MAX_AI) {
        paio               = &AITbl[n];               /* Point to Analog Input structure               */
        OSSemPend(AIOSem, 0, &err);                   /* Obtain exclusive access to AI 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 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..AIO_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  AICfgConv (INT8U n, FP32 gain, FP32 offset, INT8U pass)
{
    INT8U err;
    AIO  *paio;


    if (n < AIO_MAX_AI) {
        paio                = &AITbl[n];              /* Point to Analog Input structure               */
        OSSemPend(AIOSem, 0, &err);                   /* Obtain exclusive access to AI 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->AIOPassCnts   = pass;
        OSSemPost(AIOSem);                                                /* 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..AIO_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  AICfgScaling (INT8U n, void (*fnct)(AIO *paio), void *arg)
{
    AIO *paio;


    if (n < AIO_MAX_AI) {
        paio                  = &AITbl[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*/
/*
*********************************************************************************************************
*                                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..AIO_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  AIGet (INT8U n, FP32 *pval)
{
    AIO  *paio;


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

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

static  void  AIInit (void)
{
    INT8U   i;
    AIO    *paio;


    paio = &AITbl[0];
    for (i = 0; i < AIO_MAX_AI; i++) {
        paio->AIOBypassEn     =  FALSE;           /* Analog channel is not bypassed                     */
        paio->AIORaw          =  0x0000;          /* Raw counts of ADC or DAC                           */
        paio->AIOEU           = (FP32)0.0;        /* Engineering units of AI channel                    */
        paio->AIOGain         = (FP32)1.0;        /* Total gain                                         */
        paio->AIOOffset       = (FP32)0.0;        /* Total offset                                       */
        paio->AIOLim          =       0;
        paio->AIOPassCnts     =       1;          /* Pass counts                                        */
        paio->AIOPassCtr      =       1;          /* Pass counter                                       */
        paio->AIOCalGain      = (FP32)1.0;        /* Calibration gain                                   */
        paio->AIOCalOffset    = (FP32)0.0;        /* Calibration offset                                 */
        paio->AIOConvGain     = (FP32)1.0;        /* Conversion gain                                    */
        paio->AIOConvOffset   = (FP32)0.0;        /* Conversion offset                                  */
        paio->AIOScaleIn      = (FP32)0.0;        /* Input  to scaling function                         */
        paio->AIOScaleOut     = (FP32)0.0;        /* Output of scaling function                         */
        paio->AIOScaleFnct    = (void *)0;        /* No function to execute                             */
        paio->AIOScaleFnctArg = (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  AIOInit (void)
{
    INT8U   err;


    AIInit();
    AOInit();
    AIOInitIO();
    AIOSem = OSSemCreate(1);                     /* Create a mutual exclusion semaphore for AIOs       */
    OSTaskCreateExt(AIOTask, (void *)0, &AIOTaskStk[AIO_TASK_STK_SIZE], AIO_TASK_PRIO,
                    AIO_TASK_PRIO, &AIOTaskStk[0], AIO_TASK_STK_SIZE, (void *)0, OS_TASK_OPT_SAVE_FP);
}

⌨️ 快捷键说明

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