21ic问答首页 - N32WB01 内部adc测Vcc的问题
N32WB01 内部adc测Vcc的问题
zhtmz2024-09-26
软件是根据ADC获取电压偏移函数写的,adc_val = ADC16bit_SingleRead_Val(ADC_CTRL_CH_6,20);这个函数应该能直接获取到Vcc的电压值,但是读了20次的adc值都是0,这是什么原因?
代码:
uint16_t ADC16bit_SingleRead_Val(uint16_t Channel , uint8_t avg_num)
{
int32_t sum = 0;
uint16_t i,null_num;
uint16_t adc_val;
GPIO_InitType GPIO_InitStructure;
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOB, ENABLE);
/* Enable peripheral clocks */
/* Enable ADC clocks */
RCC_EnableAHBPeriphClk(RCC_AHB_PERIPH_ADC, ENABLE);
RCC_ConfigAdcClk(RCC_ADCCLK_SRC_AUDIOPLL);
/* enable ADC 4M clock */
RCC_Enable_ADC_CLK_SRC_AUDIOPLL(ENABLE);
/* configer ADC */
ADC_SetOverSampleCounter(ADC,3);
ADC_EnableBypassFilter(ADC, ENABLE);
ADC_ConfigChannel(ADC, Channel);
ADC_ConfigContinuousMode(ADC, DISABLE);
ADC_Enable(ADC, ENABLE);
ADC_EnableTS(ADC, ENABLE);
/* drop invalid data */
delay_n_10us(100); //1ms
/* get adc data */
null_num = 0;
for(i = 0; i<avg_num; i++)
{
ADC_Enable(ADC, ENABLE);
while(ADC_GetFlagStatus(ADC,ADC_FLAG_DONE) == RESET);
ADC_ClearFlag(ADC,ADC_FLAG_DONE);
adc_val = ADC_GetDat(ADC);
NS_LOG_INFO("adc_val = %d\r\n",adc_val);
if(adc_val == 0)
{
null_num++;
continue;
}
NS_LOG_INFO("adc_val != 0\r\n");
sum += adc_val;
delay_n_10us(10);
}
ADC_Enable(ADC, DISABLE);
adc_val = sum/(avg_num-null_num);
return adc_val;
}
代码:
uint16_t ADC16bit_SingleRead_Val(uint16_t Channel , uint8_t avg_num)
{
int32_t sum = 0;
uint16_t i,null_num;
uint16_t adc_val;
GPIO_InitType GPIO_InitStructure;
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOB, ENABLE);
/* Enable peripheral clocks */
/* Enable ADC clocks */
RCC_EnableAHBPeriphClk(RCC_AHB_PERIPH_ADC, ENABLE);
RCC_ConfigAdcClk(RCC_ADCCLK_SRC_AUDIOPLL);
/* enable ADC 4M clock */
RCC_Enable_ADC_CLK_SRC_AUDIOPLL(ENABLE);
/* configer ADC */
ADC_SetOverSampleCounter(ADC,3);
ADC_EnableBypassFilter(ADC, ENABLE);
ADC_ConfigChannel(ADC, Channel);
ADC_ConfigContinuousMode(ADC, DISABLE);
ADC_Enable(ADC, ENABLE);
ADC_EnableTS(ADC, ENABLE);
/* drop invalid data */
delay_n_10us(100); //1ms
/* get adc data */
null_num = 0;
for(i = 0; i<avg_num; i++)
{
ADC_Enable(ADC, ENABLE);
while(ADC_GetFlagStatus(ADC,ADC_FLAG_DONE) == RESET);
ADC_ClearFlag(ADC,ADC_FLAG_DONE);
adc_val = ADC_GetDat(ADC);
NS_LOG_INFO("adc_val = %d\r\n",adc_val);
if(adc_val == 0)
{
null_num++;
continue;
}
NS_LOG_INFO("adc_val != 0\r\n");
sum += adc_val;
delay_n_10us(10);
}
ADC_Enable(ADC, DISABLE);
adc_val = sum/(avg_num-null_num);
return adc_val;
}
您需要登录后才可以回复 登录 | 注册