Skip to main content
Visitor II
May 23, 2025
Question

How to sample improve ADC speed?

  • May 23, 2025
  • 3 replies
  • 669 views

My application senario: sample the sensor signal every second, and transmit through UART. 

to save power supply, I select ADC single mode, and used average as filter.

void ConfigureADC(void)
{
ADC1->CFGR1 |= ADC_CFGR1_AUTOFF;
ADC1->CFGR2 = (ADC1->CFGR2 & (~ADC_CFGR2_CKMODE))
| (ADC_CFGR2_OVSE | ADC_CFGR2_OVSR_2 | ADC_CFGR2_OVSR_1 | ADC_CFGR2_OVSR_0| ADC_CFGR2_OVSS_2); 
ADC1->SMPR |= ADC_SMPR_SMP_0 | ADC_SMPR_SMP_1 | ADC_SMPR_SMP_2; 
ADC1->CR |= ADC_CR_ADEN; 
ADC->CCR |= ADC_CCR_VREFEN|ADC_CCR_TSEN; 
}


unsigned int SignleADC(unsigned long int channel)
{
ADC1->CHSELR = channel; 
ADC1->CR |= ADC_CR_ADSTART;
while ((ADC1->ISR & ADC_ISR_EOC) == 0) 
{
}
ADC1->CR |= ADC_CR_ADSTP;
return ADC1->DR;
}

unsigned int Sensor_Read(void)
{
unsigned int gas[128];
unsigned long int read=0;
for(unsigned char i=0;i<128;i++)
gas[i] = SignleADC(ADC_CHSELR_CHSEL5);
for(unsigned char i=0;i<128;i++)
read += gas[i];

read = read>>7;
return((unsigned int)read);
}

Now the run time for Sensor_Read is about 700ms, it's too long, how can I improve the speed?

 

    This topic has been closed for replies.

    3 replies

    Graduate II
    May 23, 2025

    The most obvious would be to sum/average over fewer samples, or use the longer sample/hold to negate the need.

    STM32L09Author
    Visitor II
    May 23, 2025

    thanks, but 128 is minimum, otherwise the noise too high

    Super User
    May 23, 2025

    Convert with DMA in background. Perform averaging on demand when you send out sample.

    Technical Moderator
    May 23, 2025

    Hello @STM32L09 ,

    In next time please use </> button to paste your code. See this post. I've edited your post.

     

    STM32L09Author
    Visitor II
    May 24, 2025

    got it.

    STM32L09Author
    Visitor II
    May 24, 2025

    currently my idea is:

    1. Configure ADC and DMA to sample PA0 and PA5 16 times

    2. Re-configure ADC and DMA to sample PA5 256-16 times

    repeat step 1.