ADC setup with DMA and timer
- December 29, 2023
- 1 reply
- 4855 views
I'm having some trouble setting up ADC1 on an STM32F401CEU6 to use DMA and pacing the conversions with a timer.
I want to use 2 channels in scan mode with each ADC channel doing conversion every other time the timer triggers the channel. In other words, for each rising edge of TRGO I want the channels to be sampled and converted in an interleaved fashion.
I have timer3 set to 16 kHz and I verified its output by enabling channel 2 to go to pin PA7. I verified with a scope that the output has edges at the 16 kHz rate. Channel 1 is set to PWM with no output.
With ADC1 set up as follows:
Scan Conversion Mode: Enabled
Continuous Conversion Mode: Disabled
Discontinuous Conversion Mode: Disabled
DMA Continuous Requests: Enabled:
I see data being transferred to the buffer and channels 0 and 1 are being sampled, but sample rate isn't 8 kHz per channel. I verified this with a 1Khz square wave being fed into ADC0 and I see a varying number of high and low samples. I should see 8 samples for each cycle. To me it looks like the ADC trigger is not being paced properly by the timer or the DMA is transferring to many conversions.
I've attached the generated code (main.c) and the ioc file (adc-dma.ioc) so someone can take a look at what MX as generated. To see if there are any glaring errors.