Showing results for 
Search instead for 
Did you mean: 

Best way to sample 4 ADC channels using STM32F103. 1 channel needs to sample 2k per 8mS with the highest priority. All other channels must be sampled once after the 2k has been completed.


Hello All,

I am reasonably new to STM and could use some advice.

I am looking to sample 4 ADC channels.1 analog signal represents the current passing through a motor and needs to be sampled 2k times per half cycle (60hz). Towards the end of the half cycle, my current is negotiable, and it can be ignored for my purpose, so I want to set it up to trigger off my firing angle and stop after collecting 2k samples (approximately 8mS with adjustment of sampling time). After that sampling is complete, and the time is the last portion of the half cycle (approximately 0.33mS of the 8.33mS), I want to collect 1 sample after another of the other 3 channels. (I.e., 1 sample of another channel (Line Voltage), then one sample on another channel (potentiometer), and finally one sample of another channel (PCB board temperature sensor)).  

I have a few ideas to set this up, but I thought I would see what the experts recommend.

I have already set up PA4 to collect 2k ADC values using DMA once starting the DMA, and it stops when all the samples have been collected, so that is working properly. I was thinking about setting up the other ADC channels to start sampling after this, but it would require the ADC to be reset and then set back when complete. I also thought about injecting channels for the lower priority ADC sampling but not triggering them until all 2k samples have been completed. There are also a few other options in the manual, but I wanted to reach out and see what everyone think before spending days researching to pick the best option.

Any advice would be appreciated.  

Bullet points:

* (PA4) ADC channel sampling 2k in ~8mS when triggered. (high priority)

* (PA0) ADC channel sampling 1 time after the above samples are collected. (medium priority)

* (PB0) ADC channel sampling 1 time after the above samples are collected. (medium priority)

* (PA5) ADC Channel sampling 1 time after the above samples are collected. (low priority)

Note: I am only using Keil and the reference manual to program the STM32F103 MCU. I am not using any of the graphical design tools for software setup (I.E., CubeMX, CubeIDE, or any like that).

Thank you!!!


Using 2 ADCs would avoid having to reconfigure.

Ghofrane GSOURI
ST Employee

Hello @Community memberpiering​ 

First let me thank you for posting .

One possible solution to your problem is to use the ADC regular group and the ADC injected group to sample the four channels with the desired priorities.

Here's a high-level overview of how you can implement this solution:

  1. Configure the ADC regular group to sample the PA4 channel using DMA and trigger it with a hardware event (e.g., a timer update event). Set the regular group to continuous conversion mode and enable DMA requests after each conversion.
  2. Configure the ADC injected group to sample the PA0, PB0, and PA5 channels one at a time, triggered by the end of the regular group conversion. Set the injected group to single conversion mode, enable interrupt requests after each conversion, and configure the injected channel sequence order accordingly.
  3. In the DMA interrupt handler for the regular group, set a flag to indicate that the 2k samples have been collected. This flag will be used to trigger the injected group sampling.
  4. In the interrupt handler for the injected group, check which channel was just sampled, and trigger the next channel sampling accordingly. After the last channel has been sampled, reset the flag indicating that the 2k samples have been collected.
  5. Repeat the process from step 3 onwards for the next half cycle.

You can adjust the sampling time for the regular group by configuring the timer trigger accordingly. You may also want to set up the ADC to use the lowest possible sampling time for the injected group channels to minimize the time delay between the regular group and injected group conversions.