本例程是用HAL库读取两个通道的ADC值再滤波

//input your code:
#define set_filtering_times 50        //滤波次数
unsigned long adc_filtering[2]={0,0};//ADC缓存

for(a=0;a<2;a++)                     //HAL读取两个通道
{
    HAL_ADC_Start(&hadc);
    while(HAL_ADC_PollForConversion(&hadc,0xffff)!=HAL_OK);
    adc_filtering[a]+=HAL_ADC_GetValue(&hadc); //读到的数据加给缓存                
}
HAL_ADC_Stop(&hadc);    
            
filtering_times+=1;                              //累积次数
if(filtering_times==set_filtering_times)         //次数够了以后 把缓存除次数就是ADC值
{
    filtering_times=0;
    adc_value[0]=adc_filtering[0]/set_filtering_times;
    adc_filtering[0]=0;
    adc_value[1]=adc_filtering[1]/set_filtering_times;
    adc_filtering[1]=0;
                
    
}
Last modification:June 28th, 2020 at 06:51 pm
如果觉得我的文章对你有用,请随意赞赏