cancel
Showing results for 
Search instead for 
Did you mean: 

Changing sampling time on STM32 MCU ADC?

MCrew.1
Associate III

I am working on a project where I need 6000 samples per second converted by the ADC. In STM32CubeMX, I have the following configurations set for the ADC:

0693W0000059A8wQAE.pngWhat parameters can I change so that I am getting 6000 samples per second? Do I need to configure a timer? My understanding is that a sampling time of 12.5 cycles provides 12-bit resolution, and another 2.5 cycles is required for the sample period for a total of 15 cycles.

6 REPLIES 6
TDK
Guru

6000 sps is pretty slow. You would need to slow the ADC clock down considerably to achieve that in free-running mode. A better solution would be to set up a timer to trigger the ADC.

Be more descriptive than "STM32". There are thousands of chips that fit that description.

If you feel a post has answered your question, please click "Accept as Solution".
MCrew.1
Associate III

My apologies, I am using the STM32L476 chip. How would you set up the timer to trigger the ADC? I am fairly new to this MCU

RomainR.
ST Employee

I would recommend you to use these settings to get an ADC Conversion Time of 6126 sample/s at 12-bit resolution.

Configure Clock Tree HSI + PLL to reach SYSCLK=80Mhz.

Then, enable PLLSA1 (Asynchronous Clock) as ADC123 clock source (see the capture below).

0693W0000059BCVQA2.png 

Finally configure your ADC in the following way with a Sampling time of 640.5 cycles.

0693W0000059BEWQA2.png 

f_ADC=16MHz (PLLSA1)

ADC_Prescaler = 4

f_ADC=4MHz

TConv = 1/4MHz * (12.5+640.5) = 163.3µs --> 6126 sample/sec

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

RomainR.
ST Employee

If you know the number of sample yo need to convert, set ADC in continuous mode and use HAL_ADC_DMA() API to fill your RAM buffer without any timer.

Best regards,

rrom

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

In one second, I need exactly 6000 samples and I need to do this continuously (every second). I am ideally getting a sample and then converting it immediately as I need this value to control other parameters, so I do not think this method will work. Is there a way to sample and convert at exactly 6000 samples per second?

Thanks

RomainR.
ST Employee

try it with a timer this way. 

You precisely need to convert 6000 samples per second. This gives 1/6000, one ADC conversion every ~166.7µs.

Use a Basic Timer (ie TIM6) and configure it using internal TRGO signal to runr ADC conversion.

If your Clock System is 80MHz (also APB bus) apply a prescaler of 8 on the Timer to have a resolution of 0.1µs.

Then set the timer reload value to 1667.

Your ADC must be configured with the Trigger input on the TRGO corresponding to the Timer, and conversion Time must be less than 166.7µs timer period.

ADC will do a conversion every 1667 * 0.1µs, this configuration will allow you to have your 6000 samples continuously every 1.0002sec.

Enable ADC EOC interrupts, which will allow you to retrieve each result in the call-back, or count sample or what else.

I hope this will help you.

Keep me informed

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.