2025-05-23 7:51 AM - last edited on 2025-05-23 8:58 AM by Andrew Neil
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?
2025-05-23 7:58 AM
The most obvious would be to sum/average over fewer samples, or use the longer sample/hold to negate the need.
2025-05-23 8:11 AM
thanks, but 128 is minimum, otherwise the noise too high
2025-05-23 8:17 AM
Convert with DMA in background. Perform averaging on demand when you send out sample.
2025-05-23 8:22 AM