Why isn't my ADC with DMA setup updating the variable correctly on the STM32F407ZGT6?
I'm attempting to run through two ADC channels sequentially on my STM32F407ZGT6 using DMA to capture values from two independent potentiometers, one on each channel. The program doesn't crash, but the variable
sensor_val
isn't being updated as I would desire.
I'm making use of the DMA2_Stream0 with Channel 0 for ADC1, and my ADC1 is configured to read from PB1 (channel 9) and PA1 (channel 1). I followed a tutorial closely but avoided triggering the ADC with a timer for now. I've also referenced an example related to this issue. However, my ADC callback isn't being invoked. Based on my understanding, the expected sequence should be:
ADC1->SR EOC --> ADC->CR1 EOCIE --> DMA2_Stream0_IRQHandler() --> dma_ADC_callback()
Should I include a periodic call to read through the ADC in order to resolve this?
All the ADC/DMA functions are in separate .c/.h files. I've tried declaring sensor_val
as both a global and extern variable within the adc.c
file, but the result remains the same. Below is a simplified version of my code:Solution:Jump to solution
To fix your ADC issue, ensure the ADC is properly configured for multi-channel conversion, and verify that the DMA is correctly set up to transfer data to
sensor_val
. Check that the DMA callback is linked to the interrupt handler and that interrupt's are enabled. If you're not using a timer, make sure the ADC starts conversions automatically. Finally, use debugging tools to confirm that the callback is triggering and data is being transferred as expected.5 Replies
Yes You need to include a periodic call , You can use a timer to trigger ADC conversions at regular interval
Also,the issue stems from not enabling the DMA circular mode for ADC1. Add this line after enabling ADC DMA:
ADC1->CR2 |= ADC_CR2_DDS; // Enable DMA circular mode for ADC1
This should allow the DMA to continuously transfer data from the ADC to memory without CPU intervention, updating sensor_val as expected.
Thanks for your suggestion @wafa_ath ! I'll add a timer to trigger the ADC conversions periodically and check if it solves the issue. Initially, I was trying to avoid using a timer, but now it seems essential for continuous conversion mode functionality.
@Sterling Code:
Solution
To fix your ADC issue, ensure the ADC is properly configured for multi-channel conversion, and verify that the DMA is correctly set up to transfer data to
sensor_val
. Check that the DMA callback is linked to the interrupt handler and that interrupt's are enabled. If you're not using a timer, make sure the ADC starts conversions automatically. Finally, use debugging tools to confirm that the callback is triggering and data is being transferred as expected.