21ic问答首页 - N32G031 ADC采样50Hz正弦波有效值波动严重、
N32G031 ADC采样50Hz正弦波有效值波动严重、
发顺丰顺丰2022-10-31
现象:芯片计算出来的有效值波动较大,最大最小值偏差59%。抓了ADC输入信号是稳定的。均方根算法验证过是正确的,带入固定数据算得也是对的。采样方式:软件触发,0.625ms使能1次,共采集16次,在定时器中使能 ADC_EnableSoftwareStartConv(ADC,ENABLE);
AD读取方式:AD中断里读取ADC_GetDat(ADC);
初始化:
void ADC_Initial(void)
{
ErrorStatus HSIStartUpStatus;
ADC_InitType ADC_InitStructure;
NVIC_InitType NVIC_InitStructure;
/* Configure and enable ADC interrupt */
NVIC_InitStructure.NVIC_IRQChannel = ADC_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* Enable ADC clocks */
RCC_EnableAHBPeriphClk(RCC_AHB_PERIPH_ADC, ENABLE);
/* enable ADC 1M clock */
RCC_EnableHsi(ENABLE);
/* Wait til1 HSI is ready*/
HSIStartUpStatus = RCC_WaitHsiStable();
if(HSIStartUpStatus == SUCCESS)
{
}
else
{
/* If HSI fails to start-up, the application will have wrong clock configuration. User can add here some code to deal with this error*/
/* Go to infinitel1oop*/
while(1)
{
}
}
RCC_ConfigAdc1mClk(RCC_ADC1MCLK_SRC_HSI, RCC_ADC1MCLK_DIV8);
/* RCC_ADCHCLK_DIV16*/
ADC_ConfigClk(ADC_CTRL3_CKMOD_AHB, RCC_ADCHCLK_DIV16);
/* ADC configuration ------------------------------------------------------*/
ADC_InitStructure.MultiChEn = ENABLE;
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);
/* ADC regular channels configuration */
ADC_ConfigRegularChannel(ADC, ADC_CH_4_PA4, 1, ADC_SAMP_TIME_29CYCLES5);
// ADC_ConfigRegularChannel(ADC, ADC_CH_5_PA5, 2, ADC_SAMP_TIME_29CYCLES5);
/* Regular discontinuous mode channel number configuration */
ADC_ConfigDiscModeChannelCount(ADC, 1);
/* Enable regular discontinuous mode */
ADC_EnableDiscMode(ADC, ENABLE);
/* Enable interrupt */
ADC_ConfigInt(ADC, ADC_INT_ENDC, ENABLE);
ADC_EnableVrefint(ENABLE);
/* Enable ADC */
ADC_Enable(ADC, ENABLE);
/*wait ADC is ready to use*/
while(!ADC_GetFlagStatusNew(ADC, ADC_FLAG_RDY))
;
/*wait ADC is powered on*/
while(ADC_GetFlagStatusNew(ADC, ADC_FLAG_PD_RDY))
;
}
AD读取方式:AD中断里读取ADC_GetDat(ADC);
初始化:
void ADC_Initial(void)
{
ErrorStatus HSIStartUpStatus;
ADC_InitType ADC_InitStructure;
NVIC_InitType NVIC_InitStructure;
/* Configure and enable ADC interrupt */
NVIC_InitStructure.NVIC_IRQChannel = ADC_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* Enable ADC clocks */
RCC_EnableAHBPeriphClk(RCC_AHB_PERIPH_ADC, ENABLE);
/* enable ADC 1M clock */
RCC_EnableHsi(ENABLE);
/* Wait til1 HSI is ready*/
HSIStartUpStatus = RCC_WaitHsiStable();
if(HSIStartUpStatus == SUCCESS)
{
}
else
{
/* If HSI fails to start-up, the application will have wrong clock configuration. User can add here some code to deal with this error*/
/* Go to infinitel1oop*/
while(1)
{
}
}
RCC_ConfigAdc1mClk(RCC_ADC1MCLK_SRC_HSI, RCC_ADC1MCLK_DIV8);
/* RCC_ADCHCLK_DIV16*/
ADC_ConfigClk(ADC_CTRL3_CKMOD_AHB, RCC_ADCHCLK_DIV16);
/* ADC configuration ------------------------------------------------------*/
ADC_InitStructure.MultiChEn = ENABLE;
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);
/* ADC regular channels configuration */
ADC_ConfigRegularChannel(ADC, ADC_CH_4_PA4, 1, ADC_SAMP_TIME_29CYCLES5);
// ADC_ConfigRegularChannel(ADC, ADC_CH_5_PA5, 2, ADC_SAMP_TIME_29CYCLES5);
/* Regular discontinuous mode channel number configuration */
ADC_ConfigDiscModeChannelCount(ADC, 1);
/* Enable regular discontinuous mode */
ADC_EnableDiscMode(ADC, ENABLE);
/* Enable interrupt */
ADC_ConfigInt(ADC, ADC_INT_ENDC, ENABLE);
ADC_EnableVrefint(ENABLE);
/* Enable ADC */
ADC_Enable(ADC, ENABLE);
/*wait ADC is ready to use*/
while(!ADC_GetFlagStatusNew(ADC, ADC_FLAG_RDY))
;
/*wait ADC is powered on*/
while(ADC_GetFlagStatusNew(ADC, ADC_FLAG_PD_RDY))
;
}
您需要登录后才可以回复 登录 | 注册