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

📄 aio.c

📁 是uC/OS II的作者写的东西关于IO应用,强烈推荐
💻 C
📖 第 1 页 / 共 3 页
字号:
* Arguments   : None
* Returns     : None.
*********************************************************************************************************
*/

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


    paio = &AOTbl[0];
    for (i = 0; i < AIO_MAX_AO; 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;          /* Maximum count of an analog output channel          */
        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*/
/*
*********************************************************************************************************
*                                SET THE VALUE OF AN ANALOG OUTPUT CHANNEL
*
* Description : This function is used to set the currect value of an analog output channel
*               (in engineering units).
* Arguments   : n     is the analog output channel (0..AIO_MAX_AO-1).
*               val   is the desired analog output value in Engineering Units
* Returns     : 0     if successfull.
*               1     if you specified an invalid analog output channel number.
*********************************************************************************************************
*/

INT8U  AOSet (INT8U n, FP32 val)
{
    if (n < AIO_MAX_AO) {
        OS_ENTER_CRITICAL();
        AOTbl[n].AIOEU = val;          /* Set the engineering units of the analog output channel       */
        OS_EXIT_CRITICAL();
        return (0);
    } else {
        return (1);
    }
}

/*$PAGE*/
/*
*********************************************************************************************************
*                           SET THE STATE OF THE BYPASSED ANALOG OUTPUT CHANNEL
*
* Description : This function is used to set the engineering units of a bypassed analog output channel.
* Arguments   : n     is the analog output channel (0..AIO_MAX_AO-1).
*               val   is the value of the bypassed analog output channel:
* Returns     : 0     if successfull.
*               1     if you specified an invalid analog output channel number.
*               2     if AIOBypassEn is not set to TRUE
*********************************************************************************************************
*/

INT8U  AOSetBypass (INT8U n, FP32 val)
{
    AIO *paio;


    if (n < AIO_MAX_AO) {
        paio = &AOTbl[n];                        /* Faster to use a pointer to the structure           */
        if (paio->AIOBypassEn == TRUE) {         /* See if the analog output channel is bypassed       */
            OS_ENTER_CRITICAL();
            paio->AIOScaleIn = 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 output channel
*               is bypassed when the 'switch' is open (i.e. AIOBypassEn is set to TRUE).
* Arguments   : n      is the analog output channel (0..AIO_MAX_AO-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 output channel number.
*********************************************************************************************************
*/

INT8U  AOSetBypassEn (INT8U n, BOOLEAN state)
{
    INT8U err;


    if (n < AIO_MAX_AO) {
        AOTbl[n].AIOBypassEn = state;
        return (0);
    } else {
        return (1);
    }
}

/*$PAGE*/
/*
*********************************************************************************************************
*                                   UPDATE ALL ANALOG OUTPUT CHANNELS
*
* Description : This function processes all of the analog output channels.
* Arguments   : None.
* Returns     : None.
*********************************************************************************************************
*/

static  void  AOUpdate (void)
{
    INT8U     i;
    AIO      *paio;
    INT16S    raw;


    paio = &AOTbl[0];                            /* Point at first analog output channel               */
    for (i = 0; i < AIO_MAX_AO; i++) {           /* Process all analog output channels                 */
        if (paio->AIOBypassEn == FALSE) {        /* See if analog output channel is bypassed           */
            paio->AIOScaleIn = paio->AIOEU;      /* No                                                 */
        }
        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                      */
            if ((void *)paio->AIOScaleFnct != (void *)0) { /* See if function defined                  */
                (*paio->AIOScaleFnct)(paio);               /* Yes, execute function                    */
            } else {
                paio->AIOScaleOut = paio->AIOScaleIn;      /* No,  bypass scaling function             */
            }
            raw = (INT16S)(paio->AIOScaleOut * paio->AIOGain + paio->AIOOffset);
            if (raw > paio->AIOLim) {                      /* Never output > maximum DAC counts        */
                raw = paio->AIOLim;
            } else if (raw < 0) {                          /* DAC counts must always be >= 0           */
                raw = 0;
            }
            paio->AIORaw = raw;
            AOWr(i, paio->AIORaw);                         /* Write counts to DAC                      */
        }
        paio++;                                            /* Point at next AO channel                 */
    }
}

/*$PAGE*/
#ifndef CFG_C
/*
*********************************************************************************************************
*                                      INITIALIZE PHYSICAL I/Os
*
* Description : This function is called by AIOInit() to initialize the physical I/O used by the AIO
*               driver.
* Arguments   : None.
* Returns     : None.
*********************************************************************************************************
*/

void  AIOInitIO (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..AIO_MAX_AI-1).
* Returns     : The raw ADC counts from the physical device.
*********************************************************************************************************
*/

INT16S  AIRd (INT8U ch)
{
    /* This is where you will need to provide the code to read your ADC(s).                            */
    /* AIRd() 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. */
    /* AIRd() 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);
}

/*$PAGE*/
/*
*********************************************************************************************************
*                                       UPDATE PHYSICAL OUTPUTS
*
* Description : This function is called to write the 'raw' counts to the proper analog output device
*               (i.e. DAC).  It is up to this function to direct the DAC counts to the proper DAC if more
*               than one DAC is used.
* Arguments   : ch     is  the DAC logical channel number (0..AIO_MAX_AO-1).
*               cnts   are the DAC counts to write to the DAC
* Returns     : None.
*********************************************************************************************************
*/

void  AOWr (INT8U ch, INT16S cnts)
{
    ch   = ch;
    cnts = cnts;

    /* This is where you will need to provide the code to update your DAC(s).                          */
    /* AOWr() is passed a 'LOGICAL' channel number.  You will have to convert this logical channel     */
    /* number into actual physical port locations (or addresses) where your DACs are located.          */
    /* AOWr() is responsible for writing the counts to the selected DAC based on a logical number.     */
}
#endif

⌨️ 快捷键说明

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