📄 drvadc.c
字号:
/*---------------------------------------------------------------------------------------------------------*/
void DrvADC_DisableADCCmp1Int(void)
{
ADC->ADCMPR[1].CMPIE = 0;
}
/*---------------------------------------------------------------------------------------------------------*/
/* Function: ADC_IRQHandler */
/* */
/* Parameters: */
/* None. */
/* */
/* Returns: */
/* None. */
/* */
/* Description: */
/* ADC Interrupt Service Routine. */
/* According to the interrupt flags to call the corresponding callback function. */
/*---------------------------------------------------------------------------------------------------------*/
void ADC_IRQHandler(void)
{
if(ADC->ADSR.ADF==1)
{
if(g_ptADCCallBack)
g_ptADCCallBack(g_pu32UserData[0]);
/* clear the A/D conversion flag */
/* "ADC->ADSR.ADF = 1;" is not recommended. It may clear CMPF0 and CMPF1. */
outpw(ADC_ADSR, (inpw(ADC_ADSR)&(~0x7))|0x1);
}
if(ADC->ADSR.CMPF0==1)
{
if(g_ptADCMP0CallBack)
g_ptADCMP0CallBack(g_pu32UserData[1]);
/* clear the A/D compare flag 0 */
/* "ADC->ADSR.CMPF0 = 1;" is not recommended. It may clear ADF and CMPF1. */
outpw(ADC_ADSR, (inpw(ADC_ADSR)&(~0x7))|0x2);
}
if(ADC->ADSR.CMPF1==1)
{
if(g_ptADCMP1CallBack)
g_ptADCMP1CallBack(g_pu32UserData[2]);
/* clear the A/D compare flag 1 */
/* "ADC->ADSR.CMPF1 = 1;" is not recommended. It may clear ADF and CMPF0. */
outpw(ADC_ADSR, (inpw(ADC_ADSR)&(~0x7))|0x4);
}
}
/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvADC_GetConversionRate */
/* */
/* Parameters: */
/* None. */
/* */
/* Returns: */
/* Conversion rate. */
/* */
/* Description: */
/* Return the A/D conversion rate (sample/second.) */
/* The conversion rate depends on the clock source of ADC clock. */
/* It needs about 27 ADC clocks to complete an A/D conversion. */
/* In NUC1x0xxxCx with HCLK clock source, it needs 23 ADC clocks to complete an A/D conversion. */
/*---------------------------------------------------------------------------------------------------------*/
uint32_t DrvADC_GetConversionRate(void)
{
if( SYSCLK->CLKSEL1.ADC_S == EXTERNAL_12MHZ ) /* external 4~24MHz crystal clock */
{
return (__XTAL/(SYSCLK->CLKDIV.ADC_N + 1)/27);
}
else if( SYSCLK->CLKSEL1.ADC_S == INTERNAL_PLL ) /* PLL clock */
{
return ( DrvSYS_GetPLLClockFreq()/(SYSCLK->CLKDIV.ADC_N + 1)/27);
}
else if( SYSCLK->CLKSEL1.ADC_S == INTERNAL_HCLK ) /* HCLK */
{
return ( DrvSYS_GetHCLKFreq()/(SYSCLK->CLKDIV.ADC_N + 1)/23);
}
else /* internal 22.1184MHz RC oscillator clock */
{
return (__IRC22M/(SYSCLK->CLKDIV.ADC_N + 1)/27);
}
}
/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvADC_EnableExtTrigger */
/* */
/* Parameters: */
/* TriggerCondition [in]: Specify the trigger condition. */
/* It could be LOW_LEVEL / HIGH_LEVEL / FALLING_EDGE / RISING_EDGE. */
/* */
/* Returns: */
/* None. */
/* */
/* Description: */
/* Allow the external trigger pin STADC (PB8) to be the trigger source of ADC. */
/* Software must configure the external trigger pin as an input pin in advance. */
/* The trigger condition could be low-level/high-level/falling-edge/positive-edge. */
/* TRGE bit and ADST bit should be clear to 0 before changing TRGS bit. */
/*---------------------------------------------------------------------------------------------------------*/
void DrvADC_EnableExtTrigger(E_ADC_EXT_TRI_COND TriggerCondition)
{
ADC->ADCR.ADST = 0; /* Stop A/D conversion */
ADC->ADCR.TRGEN = 0; /* Disable the trigger source of STADC pin */
ADC->ADCR.TRGS = 0; /* Select the STADC pin as the hardware trigger source */
ADC->ADCR.TRGCOND = TriggerCondition; /* Set the trigger condition */
ADC->ADCR.TRGEN = 1; /* Enable the trigger source of STADC pin */
}
/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvADC_DisableExtTrigger */
/* */
/* Parameters: */
/* None. */
/* */
/* Returns: */
/* None. */
/* */
/* Description: */
/* Prohibit the external ADC trigger. */
/*---------------------------------------------------------------------------------------------------------*/
void DrvADC_DisableExtTrigger(void)
{
ADC->ADCR.TRGEN = 0;
}
/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvADC_StartConvert */
/* */
/* Parameters: */
/* None. */
/* */
/* Returns: */
/* None. */
/* */
/* Description: */
/* Set the ADST bit of ADCR to start the A/D conversion action. */
/*---------------------------------------------------------------------------------------------------------*/
void DrvADC_StartConvert(void)
{
/* clear the A/D conversion interrupt flag (ADF) */
/* "ADC->ADSR.ADF = 1;" is not recommended. It may clear CMPF0 and CMPF1 */
outpw(ADC_ADSR, (inpw(ADC_ADSR)&(~0x7))|0x1);
ADC->ADCR.ADST = 1;
}
/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvADC_StopConvert */
/* */
/* Parameters: */
/* None. */
/* */
/* Returns: */
/* None. */
/* */
/* Description: */
/* Clear the ADST bit of ADCR to stop the A/D conversion action. */
/*---------------------------------------------------------------------------------------------------------*/
void DrvADC_StopConvert(void)
{
ADC->ADCR.ADST = 0;
}
/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvADC_IsConversionDone */
/* */
/* Parameters: */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -