使用N32G032内部的ADC,锂电池使用2个47k电阻分压,计算得到的电压误差比较大,但是分压的电阻越小,则误差也越小。实际电压是2.05V,采样后计算为2.16,这是芯片自身的特性吗?
现在主时钟配置为48MHz,ADC时钟是4M,采样周期是600,看资料这个状态外部允许的外部输入阻抗是几百k:
https://bbs.21ic.com/icview-3253266-1-1.html
有谁遇到过类似的问题吗?
以下是代码:
void Adc_Init(void)
{
GPIO_InitType GPIO_InitStructure;
DMA_InitType DMA_InitStructure;
ADC_InitType ADC_InitStructure;
ADC_InitTypeEx ADC_InitStructureEx;
/* Enable DMA clocks */
RCC_EnableAHBPeriphClk(RCC_AHB_PERIPH_DMA, ENABLE);
/* Enable GPIOC clocks */
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA, ENABLE);
/* Enable ADC clocks */
RCC_EnableAHBPeriphClk(RCC_AHB_PERIPH_ADC, ENABLE);
/* enable ADC 1M clock */
RCC_EnableHsi(ENABLE);
RCC_ConfigAdc1mClk(RCC_ADC1MCLK_SRC_HSI, RCC_ADC1MCLK_DIV8); //这里必须是 1M 的时钟
/* RCC_ADCHCLK_DIV3*/
ADC_ConfigClk(ADC_CTRL3_CKMOD_AHB, RCC_ADCHCLK_DIV12); //48/12 = 4M // 最高 18 MHz
//ADC 中有的引脚
GPIO_InitStruct(&GPIO_InitStructure);
/* Configure PA.01 as analog input -------------------------*/
GPIO_InitStructure.Pin = GPIO_PIN_5;
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_ANALOG;
GPIO_InitStructure.GPIO_Pull = GPIO_NO_PULL;
GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_LOW;
GPIO_InitStructure.GPIO_Current = GPIO_DC_HIGH;
GPIO_InitStructure.GPIO_Alternate = GPIO_NO_AF;
GPIO_InitPeripheral(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.Pin = GPIO_PIN_4;
GPIO_InitPeripheral(GPIOA, &GPIO_InitStructure);
/* DMA channel1 configuration ----------------------------------------------*/
DMA_DeInit(DMA_CH1);
DMA_InitStructure.PeriphAddr = (uint32_t)&ADC->DAT;
DMA_InitStructure.MemAddr = (uint32_t)&ADCConvertedValue[0];
DMA_InitStructure.Direction = DMA_DIR_PERIPH_SRC;
DMA_InitStructure.BufSize = 3;
DMA_InitStructure.PeriphInc = DMA_PERIPH_INC_DISABLE;
DMA_InitStructure.DMA_MemoryInc = DMA_MEM_INC_ENABLE;
DMA_InitStructure.PeriphDataSize = DMA_PERIPH_DATA_SIZE_HALFWORD;
DMA_InitStructure.MemDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.CircularMode = DMA_MODE_CIRCULAR;
DMA_InitStructure.Priority = DMA_PRIORITY_HIGH;
DMA_InitStructure.Mem2Mem = DMA_M2M_DISABLE;
DMA_Init(DMA_CH1, &DMA_InitStructure);
DMA_RequestRemap(DMA_REMAP_ADC, DMA, DMA_CH1, ENABLE);
/* Enable DMA channel1 */
DMA_EnableChannel(DMA_CH1, ENABLE);
/* ADC configuration ------------------------------------------------------*/
ADC_InitStructure.MultiChEn = ENABLE;
ADC_InitStructure.ContinueConvEn = ENABLE;
ADC_InitStructure.ExtTrigSelect = ADC_EXT_TRIGCONV_NONE;
ADC_InitStructure.DatAlign = ADC_DAT_ALIGN_R;
ADC_InitStructure.ChsNumber = ADC_CHANNEL_NUM;
ADC_Init(ADC, &ADC_InitStructure);
ADC_InitStructureEx.ClkMode = ADC_CTRL3_CKMOD_AHB;//
ADC_InitStructureEx.EndcIntEn = DISABLE;//禁用注入结束中断
ADC_InitStructureEx.JendcIntEn = DISABLE;//禁用规则结束中断
ADC_InitStructureEx.RefSel = DISABLE;//是否内部2.4V为参考
ADC_InitStructureEx.VrefEn = ENABLE;//使能内部参考
ADC_InitEx(ADC,&ADC_InitStructureEx);
/* ADC regular channel1 configuration */
//每个通道的时长是 (600+12)/4 = 153us @4MHz
//每个通道的时长是 (600+12)/12 = 51us @12MHz
ADC_ConfigRegularChannel(ADC, ADC_CH_5_PA5, 1, ADC_SAMP_TIME_600CYCLES5);
ADC_ConfigRegularChannel(ADC, ADC_CH_4_PA4, 2, ADC_SAMP_TIME_600CYCLES5);
ADC_ConfigRegularChannel(ADC, ADC_CH_17, 3, ADC_SAMP_TIME_600CYCLES5); //内部参照电压, 1.16---1.26 , 标准 1.21V
/* Enable ADC DMA */
ADC_EnableDMA(ADC, ENABLE);
/* Enable ADC */
ADC_Enable(ADC, ENABLE);
/*wait ADC is ready to use*/
while(!ADC_GetFlagStatusNew(ADC, ADC_FLAG_RDY))//等待这个寄存器为 1
;
/*wait ADC is powered on*/
while(ADC_GetFlagStatusNew(ADC, ADC_FLAG_PD_RDY))//等待这个寄存器为 0
;
/*wait ADC VREFRDY is ready*/
while(!ADC_GetFlagStatusNew(ADC, ADC_FLAG_VREF_RDY))
;
/* Start ADC Software Conversion */
ADC_EnableSoftwareStartConv(ADC, ENABLE);
} |