Bird
Raised Fist0
Embedded Cprogramming~10 mins

DMA with ADC for continuous sampling in Embedded C - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - DMA with ADC for continuous sampling
Start ADC Conversion
↓
ADC samples analog input
↓
ADC conversion complete interrupt triggers
↓
DMA transfers ADC data to memory buffer
↓
Check if buffer full
↓
Continue
↓
Restart ADC Conversion for next sample
The ADC continuously samples an analog input. Each conversion triggers DMA to transfer data to memory. When the buffer fills, data is processed, and sampling continues.
Execution Sample
Embedded C
void setup() {
  ADC_Start();
  DMA_Start(buffer, size);
}

void ADC_IRQHandler() {
  DMA_Transfer();
}
Starts ADC and DMA for continuous sampling; DMA transfers ADC data on each conversion interrupt.
Execution Table
StepADC StateDMA ActionBuffer IndexBuffer Full?Next Action
1Start conversionIdle0NoWait for conversion
2Conversion completeTransfer sample to buffer[0]1NoStart next conversion
3Conversion completeTransfer sample to buffer[1]2NoStart next conversion
4Conversion completeTransfer sample to buffer[2]3NoStart next conversion
5Conversion completeTransfer sample to buffer[3]4NoStart next conversion
6Conversion completeTransfer sample to buffer[4]5NoStart next conversion
7Conversion completeTransfer sample to buffer[5]6NoStart next conversion
8Conversion completeTransfer sample to buffer[6]7NoStart next conversion
9Conversion completeTransfer sample to buffer[7]8NoStart next conversion
10Conversion completeTransfer sample to buffer[8]9NoStart next conversion
11Conversion completeTransfer sample to buffer[9]10YesProcess buffer and restart
12Start conversionIdle0NoWait for conversion
ExitBuffer processedDMA reset buffer index--Continuous sampling continues
💡 Buffer reaches full size (10 samples), triggers processing, then sampling restarts.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6After 7After 8After 9After 10Final
buffer_index0123456789100
buffer_fullNoNoNoNoNoNoNoNoNoNoYesNo
Key Moments - 3 Insights
Why does the buffer_index reset to 0 after reaching 10?
Because when the buffer is full (see step 11 in execution_table), the program processes the data and resets the index to start filling the buffer again for continuous sampling.
Does the ADC stop sampling when the buffer is full?
No, the ADC keeps sampling continuously. The DMA transfers data and the buffer is processed in the background, then sampling continues immediately (see steps 11 and 12).
What triggers the DMA to transfer data from ADC to memory?
Each ADC conversion complete interrupt triggers the DMA transfer, as shown in each 'Conversion complete' step in the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the buffer_index after step 5?
A5
B4
C3
D6
💡 Hint
Check the 'Buffer Index' column at step 5 in the execution_table.
At which step does the buffer become full?
AStep 11
BStep 9
CStep 10
DStep 12
💡 Hint
Look for 'Buffer Full?' column showing 'Yes' in the execution_table.
If the buffer size was increased to 20, how would the buffer_index change at step 11?
AIt would be 11
BIt would be 20
CIt would be 10
DIt would reset to 0
💡 Hint
Refer to variable_tracker and execution_table logic for buffer_index increments.
Concept Snapshot
DMA with ADC for continuous sampling:
- ADC converts analog input continuously.
- Each conversion triggers DMA to transfer data to a memory buffer.
- Buffer index increments with each sample.
- When buffer is full, data is processed and buffer resets.
- Sampling and DMA transfer continue without CPU blocking.
Full Transcript
This visual execution shows how DMA works with ADC for continuous sampling. The ADC starts converting analog signals. When a conversion finishes, an interrupt triggers DMA to move the data to a buffer in memory. The buffer index increases by one each time. When the buffer fills up, the program processes the data and resets the buffer index to zero. Then the ADC continues sampling and DMA transfers data again. This cycle repeats continuously without stopping the CPU. The execution table tracks each step, showing ADC state, DMA action, buffer index, and whether the buffer is full. The variable tracker shows how buffer_index and buffer_full change over time. Key moments clarify common confusions about buffer reset, continuous sampling, and DMA triggers. The quiz tests understanding of buffer indexing and buffer full conditions. This helps beginners see exactly how DMA and ADC work together for efficient continuous data sampling.

Practice

(1/5)
1. What is the main advantage of using DMA with ADC in continuous sampling mode?
easy
A. It disables ADC interrupts to save power.
B. It transfers ADC data to memory automatically without CPU intervention.
C. It allows manual control of each ADC conversion.
D. It increases the ADC resolution beyond hardware limits.

Solution

  1. Step 1: Understand DMA role with ADC

    DMA (Direct Memory Access) moves data from ADC to memory automatically, reducing CPU load.
  2. Step 2: Identify continuous sampling benefit

    Continuous mode means ADC keeps sampling without CPU restarting it each time.
  3. Final Answer:

    It transfers ADC data to memory automatically without CPU intervention. -> Option B
  4. Quick Check:

    DMA automates data transfer = It transfers ADC data to memory automatically without CPU intervention. [OK]
Hint: DMA automates ADC data transfer, no CPU needed [OK]
Common Mistakes:
  • Thinking DMA increases ADC resolution
  • Confusing manual and automatic data transfer
  • Assuming DMA disables ADC interrupts
2. Which of the following is the correct way to configure DMA for circular mode with ADC in embedded C?
easy
A. DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
B. DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
C. DMA_InitStructure.DMA_Mode = DMA_Mode_Manual;
D. DMA_InitStructure.DMA_Mode = DMA_Mode_Interrupt;

Solution

  1. Step 1: Recall DMA modes for continuous ADC sampling

    DMA_Mode_Circular allows buffer to be reused endlessly for continuous data.
  2. Step 2: Match correct DMA mode setting

    DMA_Mode_Normal stops after one transfer; DMA_Mode_Circular repeats automatically.
  3. Final Answer:

    DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; -> Option A
  4. Quick Check:

    Circular mode enables continuous buffer reuse = DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; [OK]
Hint: Use DMA_Mode_Circular for endless ADC data buffering [OK]
Common Mistakes:
  • Using DMA_Mode_Normal for continuous sampling
  • Confusing DMA_Mode_Manual which doesn't exist
  • Selecting interrupt mode instead of circular
3. Given the following code snippet, what will be the content of the buffer after 3 ADC conversions with DMA in circular mode?
uint16_t adc_buffer[4] = {0};
// DMA and ADC configured in circular mode
// ADC conversions produce values: 100, 200, 300
// DMA transfers these values into adc_buffer sequentially
// After 3 conversions, what is adc_buffer content?
medium
A. [300, 200, 100, 0]
B. [0, 100, 200, 300]
C. [100, 200, 300, 0]
D. [100, 200, 0, 300]

Solution

  1. Step 1: Understand DMA circular buffer filling

    DMA fills buffer sequentially from index 0 upwards with ADC values.
  2. Step 2: Map ADC values to buffer after 3 transfers

    First 3 elements get 100, 200, 300; last element remains 0 as no 4th conversion yet.
  3. Final Answer:

    [100, 200, 300, 0] -> Option C
  4. Quick Check:

    Buffer fills sequentially = [100, 200, 300, 0] [OK]
Hint: DMA fills buffer from start sequentially [OK]
Common Mistakes:
  • Assuming buffer shifts values on each new sample
  • Thinking DMA fills buffer backwards
  • Confusing buffer initialization with data content
4. Identify the error in this DMA + ADC continuous sampling setup code snippet:
ADC_InitTypeDef ADC_InitStructure;
DMA_InitTypeDef DMA_InitStructure;

// ADC and DMA configuration omitted for brevity
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
DMA_Init(DMA1_Channel1, &DMA_InitStructure);
ADC_Cmd(ADC1, ENABLE);
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
medium
A. DMA_Mode_Normal should be DMA_Mode_Circular for continuous sampling.
B. ADC_SoftwareStartConvCmd should be called before DMA_Init.
C. DMA1_Channel1 is not valid for ADC1 DMA transfers.
D. ADC_Cmd should be called after starting conversion.

Solution

  1. Step 1: Check DMA mode for continuous ADC sampling

    DMA_Mode_Normal stops after one transfer; continuous sampling needs DMA_Mode_Circular.
  2. Step 2: Verify other calls order and parameters

    Calling ADC_SoftwareStartConvCmd after DMA_Init is correct; DMA1_Channel1 is valid for many MCUs; ADC_Cmd before conversion start is correct.
  3. Final Answer:

    DMA_Mode_Normal should be DMA_Mode_Circular for continuous sampling. -> Option A
  4. Quick Check:

    Continuous DMA needs circular mode = DMA_Mode_Normal should be DMA_Mode_Circular for continuous sampling. [OK]
Hint: Use circular mode for continuous DMA transfers [OK]
Common Mistakes:
  • Using normal mode for continuous DMA
  • Misordering ADC and DMA initialization calls
  • Assuming DMA channel is invalid without checking MCU
5. You want to continuously sample 8 ADC channels using DMA in circular mode into a buffer of size 16 (uint16_t). Which approach correctly handles the buffer to process the latest samples without data loss?
hard
A. Disable circular mode and manually restart DMA after each 8 samples.
B. Process the entire buffer only after DMA transfer completes once, then restart DMA.
C. Use polling to check ADC conversion complete flag before reading buffer.
D. Use half-transfer and transfer-complete DMA interrupts to process buffer halves alternately.

Solution

  1. Step 1: Understand circular DMA buffer usage

    Circular DMA fills buffer continuously; half-transfer and transfer-complete interrupts signal when half/full buffer is ready.
  2. Step 2: Choose method to avoid data loss

    Processing halves alternately on interrupts prevents overwriting unprocessed data and ensures continuous handling.
  3. Final Answer:

    Use half-transfer and transfer-complete DMA interrupts to process buffer halves alternately. -> Option D
  4. Quick Check:

    Half/full DMA interrupts enable safe continuous processing = Use half-transfer and transfer-complete DMA interrupts to process buffer halves alternately. [OK]
Hint: Use DMA half/full interrupts to process buffer safely [OK]
Common Mistakes:
  • Waiting for full buffer before processing causes data loss
  • Disabling circular mode loses continuous sampling benefit
  • Polling ADC flags wastes CPU and misses DMA data
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard