N32单片机软件定时读取ADC采样值,电压基准采用内部2.048V。
先ADC_Configuration()初始化ADC,再在定时任务中ADC_GetData。
void ADC_Configuration(void)
{
ADC_InitType ADC_InitStructure;
/* Enable ADC clocks */
RCC_EnableAHBPeriphClk(RCC_AHB_PERIPH_ADC, ENABLE);
/* RCC_ADCHCLK_DIV16*/
ADC_ConfigClk(ADC_CTRL3_CKMOD_AHB, RCC_ADCHCLK_DIV16);
RCC_ConfigAdc1mClk(RCC_ADC1MCLK_SRC_HSE, RCC_ADC1MCLK_DIV8); //selsect HSE as RCC ADC1M CLK Source
//
ADC_DeInit(ADC);
/* ADC configuration ------------------------------------------------------*/
ADC_InitStruct(&ADC_InitStructure);
ADC_InitStructure.MultiChEn = DISABLE;
ADC_InitStructure.ContinueConvEn = DISABLE;
ADC_InitStructure.ExtTrigSelect = ADC_EXT_TRIGCONV_NONE;
ADC_InitStructure.DatAlign = ADC_DAT_ALIGN_R;
ADC_InitStructure.ChsNumber = 1;
ADC_Init(ADC, &ADC_InitStructure);
/* Enable ADC */
(*(uint32_t*)(0x40001800+0x24)) |= (3<<19);//使能2.048V缓冲器
ADC_Enable(ADC, ENABLE);
/* Check ADC Ready */
while(ADC_GetFlagStatusNew(ADC,ADC_FLAG_RDY) == RESET);
/* Start ADC1 calibration */
ADC_StartCalibration(ADC);
/* Check the end of ADC1 calibration */
while (ADC_GetCalibrationStatus(ADC));
//
ADC_ConfigRegularChannel(ADC, ADC_CH_4_PA3, 1, ADC_SAMP_TIME_55CYCLES5);
}
uint16 ADC_GetData(void)
{
uint16 dat;
ADC_ClearFlag(ADC,ADC_FLAG_ENDCA);
ADC_ClearFlag(ADC,ADC_FLAG_STR);
dat=ADC_GetDat(ADC);
ADC_EnableSoftwareStartConv(ADC,ENABLE);//下一次再读取
return dat;
}
|
|