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

📄 drvadc.c

📁 cortex-m0 LCD1602程序
💻 C
📖 第 1 页 / 共 5 页
字号:
/*    None.                                                                                                */
/*                                                                                                         */
/* Returns:                                                                                                */
/*    TRUE:  the conversion action is finished.                                                            */
/*    FALSE: the conversion action is in progress.                                                         */
/*                                                                                                         */
/* Description:                                                                                            */
/*    Check whether the conversion action is finished or not.                                              */
/*---------------------------------------------------------------------------------------------------------*/
uint32_t DrvADC_IsConversionDone(void)
{
    if(ADC->ADSR.ADF==0)
        return FALSE;
    else
        return TRUE;
}

/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvADC_GetConversionData                                                                      */
/*                                                                                                         */
/* Parameters:                                                                                             */
/*    u8ChannelNum [in]:  Specify the ADC channel. It could be 0~7.                                        */
/*                                                                                                         */
/* Returns:                                                                                                */
/*    The 32 bits conversion result. It is generated by extending the original 12 bits conversion result.  */
/*                                                                                                         */
/* Description:                                                                                            */
/*    Get the conversion result of the specified ADC channel.                                              */
/*                                                                                                         */
/*---------------------------------------------------------------------------------------------------------*/
int32_t DrvADC_GetConversionData(uint8_t u8ChannelNum)
{
    if(ADC->ADCR.DIFFEN==0)       /* Single-end input mode */
    {
        return ((int32_t)(ADC->ADDR[u8ChannelNum].RSLT & 0xFFF));
    }
    else                          /* Differential input mode */
    {
        if(ADC->ADCR.DMOF==0)     /* unsigned format */
            return (ADC->ADDR[u8ChannelNum].RSLT);
        else                      /* 2's complement */
        {
            uint16_t u16AdcData = ADC->ADDR[u8ChannelNum].RSLT;
            if( u16AdcData&0x8000 )
                return (0xFFFF0000|u16AdcData);
            else
                return (int32_t)u16AdcData;
        }
    }
}

/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvADC_EnablePDMA                                                                             */
/*                                                                                                         */
/* Parameters:                                                                                             */
/*    None.                                                                                                */
/*                                                                                                         */
/* Returns:                                                                                                */
/*    None.                                                                                                */
/*                                                                                                         */
/* Description:                                                                                            */
/*    Enable the PDMA data transfer function.                                                              */
/*    When PDMA transfer is enabled, the ADIE bit must be set to '0'.                                      */
/*---------------------------------------------------------------------------------------------------------*/
void DrvADC_EnablePDMA(void)
{
    ADC->ADCR.ADIE = 0;
    ADC->ADCR.PTEN = 1;
}

/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvADC_DisablePDMA                                                                            */
/*                                                                                                         */
/* Parameters:                                                                                             */
/*    None.                                                                                                */
/*                                                                                                         */
/* Returns:                                                                                                */
/*    None.                                                                                                */
/*                                                                                                         */
/* Description:                                                                                            */
/*    Disable the PDMA data transfer function.                                                             */
/*---------------------------------------------------------------------------------------------------------*/
void DrvADC_DisablePDMA(void)
{
    ADC->ADCR.PTEN = 0;
}

/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvADC_IsDataValid                                                                            */
/*                                                                                                         */
/* Parameters:                                                                                             */
/*   u8ChannelNum [in]:  Specify the ADC channel. It could be 0~7.                                         */
/*                                                                                                         */
/* Returns:                                                                                                */
/*    TRUE: valid.                                                                                         */
/*    FALSE: invalid.                                                                                      */
/*                                                                                                         */
/* Description:                                                                                            */
/*    Check whether the conversion data is valid or not.                                                   */
/*---------------------------------------------------------------------------------------------------------*/
uint32_t DrvADC_IsDataValid(uint8_t u8ChannelNum)
{
    volatile uint8_t u8ChannelSelBitwise[8]={ 1, 2, 4, 8, 0x10, 0x20, 0x40, 0x80 };
    
    if(u8ChannelNum>7)
        return FALSE;
    if( ADC->ADSR.VALID & u8ChannelSelBitwise[u8ChannelNum] )
        return TRUE;
    else
        return FALSE;
}

/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvADC_IsDataOverrun                                                                          */
/*                                                                                                         */
/* Parameters:                                                                                             */
/*   u8ChannelNum [in]:  Specify the ADC channel. It could be 0~7.                                         */
/*                                                                                                         */
/* Returns:                                                                                                */
/*    TRUE: overrun.                                                                                       */
/*    FALSE: non-overrun.                                                                                  */
/*                                                                                                         */
/* Description:                                                                                            */
/*    Check whether the conversion data is overrun or not.                                                 */
/*---------------------------------------------------------------------------------------------------------*/
uint32_t DrvADC_IsDataOverrun(uint8_t u8ChannelNum)
{
    volatile uint8_t u8ChannelSelBitwise[8]={ 1, 2, 4, 8, 0x10, 0x20, 0x40, 0x80 };
    
    if(u8ChannelNum>7)
        return FALSE;
    if( ADC->ADSR.OVERRUN & u8ChannelSelBitwise[u8ChannelNum] )
        return TRUE;
    else
        return FALSE;
}

/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvADC_EnableADCCmp0                                                                          */
/*                                                                                                         */
/* Parameters:                                                                                             */
/*    u8CmpChannelNum [in]:  Specify the channel number that want to compare.                              */
/*    CmpCondition    [in]:  Specify the compare condition.                                                */
/*                           LESS_THAN for the condition of "less than the compare data";                  */
/*                           GREATER_OR_EQUAL for the condition of "greater or equal to the compare data." */
/*    u16CmpData      [in]:  Specify the compare data.                                                     */
/*    u8CmpMatchCount [in]:  Specify the compare match count.                                              */
/*                                                                                                         */
/* Returns:                                                                                                */
/*    E_DRVADC_ARGUMENT: one of the input arguments is out of the range.                                   */
/*    E_SUCCESS: the compare function is enabled.                                                          */
/*                                                                                                         */
/* Description:                                                                                            */
/*    Enable the ADC result monitor 0 and configure the necessary settings.                                */
/*---------------------------------------------------------------------------------------------------------*/
int32_t DrvADC_EnableADCCmp0(uint8_t u8CmpChannelNum, E_ADC_CMP_CONDITION CmpCondition, uint16_t u16CmpData, uint8_t u8CmpMatchCount)
{
    if(u8CmpChannelNum>7)
        return E_DRVADC_ARGUMENT;
    
    if(u16CmpData>0x0FFF)
        return E_DRVADC_ARGUMENT;
    
    if(u8CmpMatchCount>0x0F)
        return E_DRVADC_ARGUMENT;
    
    ADC->ADCMPR[0].CMPCH     = u8CmpChannelNum;
    ADC->ADCMPR[0].CMPCOND   = CmpCondition;
    ADC->ADCMPR[0].CMPD      = u16CmpData;
    ADC->ADCMPR[0].CMPMATCNT = u8CmpMatchCount;

⌨️ 快捷键说明

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