/**
* [url=home.php?mod=space&uid=247401]@brief[/url] ADC I/O初始化
*
*/
void ADC_PortInit(void)
{
//打开GPIO时钟
REGBITS_SET( CW_SYSCTRL->AHBEN, SYSCTRL_AHBEN_GPIOA_Msk );
//打开ADC时钟
REGBITS_SET( CW_SYSCTRL->APBEN2, SYSCTRL_APBEN2_ADC_Msk );
//set PA01 as AIN1 INPUT
PA01_ANALOG_ENABLE();
}
/**
* @brief Main function of project
*
* [url=home.php?mod=space&uid=266161]@return[/url] int
*/
int main(void)
{
ADC_InitTypeDef ADC_InitStructure;
ADC_WdtTypeDef ADC_WdtStructure;
ADC_SingleChTypeDef ADC_SingleChStructure;
//LED初始化
LED_Init();
//配置ADC测试IO口
ADC_PortInit();
//ADC默认值初始化
ADC_StructInit(&ADC_InitStructure);
//ADC工作时钟配置
#ifdef SGL_MORE_MODE_EN
ADC_InitStructure.ADC_ClkDiv = ADC_Clk_Div4;
#else
ADC_InitStructure.ADC_ClkDiv = ADC_Clk_Div1;
#endif
//ADC模拟看门狗通道初始化
ADC_WdtInit(&ADC_WdtStructure);
//配置单通道转换模式
ADC_SingleChStructure.ADC_DiscardEn = ADC_DiscardNull;
ADC_SingleChStructure.ADC_Chmux = ADC_ExInputCH1; //选择ADC转换通道,AIN1:PA01
ADC_SingleChStructure.ADC_InitStruct = ADC_InitStructure;
ADC_SingleChStructure.ADC_WdtStruct = ADC_WdtStructure;
#ifdef SGL_ONE_MODE_EN
ADC_SingleChOneModeCfg(&ADC_SingleChStructure);
ADC_ITConfig(ADC_IT_EOC, ENABLE);
#elif defined (SGL_MORE_MODE_EN)
cntSample = 0xFF; //转换次数为0xFF + 1次.
ADC_SingleChMoreModeCfg(&ADC_SingleChStructure, cntSample);
ADC_ITConfig(ADC_IT_EOC | ADC_IT_EOA, ENABLE);
#elif defined (SGL_CONTINUOUS_MODE_EN)
ADC_SingleChContinuousModeCfg(&ADC_SingleChStructure);
ADC_ITConfig(ADC_IT_EOC, ENABLE);
#else
#error "Please select ADC's running mode first in main.h!"
#endif
//ADC_ITConfig(ADC_IT_EOC | ADC_IT_EOA, ENABLE);
ADC_EnableIrq(ADC_INT_PRIORITY);
ADC_ClearITPendingAll();
//ADC使能
ADC_Enable();
ADC_SoftwareStartConvCmd(ENABLE);
while(1)
{
#ifdef SGL_MORE_MODE_EN
while(!(gFlagIrq & ADC_ISR_EOA_Msk));
gFlagIrq = 0u;
ADC_GetAccResult(&valueAdcAcc);
if(gCntEoc != (cntSample + 1))
{
PB08_SETHIGH();
PB09_SETHIGH();
while(1); //error issue.
}
gCntEoc = 0u;
#else
while(!(gFlagIrq & ADC_ISR_EOC_Msk));
gFlagIrq = 0u;
valueAdc = ADC_GetConversionValue();
#endif
PB09_TOG();
#ifndef SGL_CONTINUOUS_MODE_EN
ADC_SoftwareStartConvCmd(ENABLE); //启动下一次ADC转换
#endif
}
}
|