0
0
Embedded Cprogramming~20 mins

DMA with ADC for continuous sampling in Embedded C - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
DMA ADC Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this DMA ADC buffer processing code?

Consider the following embedded C code snippet that uses DMA to continuously sample ADC values into a buffer. What will be the printed output after the DMA transfer completes once?

Embedded C
#include <stdio.h>
#include <stdint.h>

#define BUFFER_SIZE 4

uint16_t adc_buffer[BUFFER_SIZE] = {0};

void DMA_TransferComplete_Callback() {
    for (int i = 0; i < BUFFER_SIZE; i++) {
        printf("%d ", adc_buffer[i]);
    }
    printf("\n");
}

int main() {
    // Simulate ADC values filled by DMA
    adc_buffer[0] = 100;
    adc_buffer[1] = 200;
    adc_buffer[2] = 300;
    adc_buffer[3] = 400;

    DMA_TransferComplete_Callback();
    return 0;
}
A100 200 300 0
B0 0 0 0
C100 200 300 400
D200 300 400 500
Attempts:
2 left
💡 Hint

Look at how the adc_buffer array is filled before the callback prints it.

🧠 Conceptual
intermediate
1:30remaining
Which statement best describes DMA's role in continuous ADC sampling?

In embedded systems, what is the main advantage of using DMA with ADC for continuous sampling?

ADMA allows ADC to sample data without CPU intervention, reducing CPU load.
BDMA increases the ADC sampling rate beyond hardware limits.
CDMA converts analog signals to digital automatically.
DDMA disables ADC interrupts to save power.
Attempts:
2 left
💡 Hint

Think about how DMA helps with data transfer in the background.

🔧 Debug
advanced
2:30remaining
Identify the error causing incorrect ADC data in DMA buffer

Given the code below, the DMA buffer does not contain the expected ADC values after sampling. What is the most likely cause?

Embedded C
volatile uint16_t adc_buffer[4];

void ADC_DMA_Init() {
    // DMA configuration omitted for brevity
    // Start ADC with DMA
    HAL_ADC_Start_DMA(&hadc1, (uint32_t*)adc_buffer, 4);
}

int main() {
    ADC_DMA_Init();
    // Wait for DMA transfer complete
    while(!dma_transfer_complete_flag) {}
    // Process adc_buffer here
    return 0;
}
AThe DMA transfer size is set incorrectly to 4 instead of 8.
BThe dma_transfer_complete_flag is never set to true.
CThe ADC is not started before DMA initialization.
DThe adc_buffer is not declared as volatile, so compiler optimizations may cause stale data.
Attempts:
2 left
💡 Hint

Consider how the compiler treats variables modified outside the main flow.

📝 Syntax
advanced
1:30remaining
Which option correctly configures DMA for circular ADC sampling?

Which of the following DMA initialization snippets correctly sets up circular mode for continuous ADC sampling?

Ahdma_adc.Init.Mode = DMA_SINGLE;
Bhdma_adc.Init.Mode = DMA_CIRCULAR;
Chdma_adc.Init.Mode = DMA_PINGPONG;
Dhdma_adc.Init.Mode = DMA_NORMAL;
Attempts:
2 left
💡 Hint

Continuous sampling requires the DMA to restart automatically after finishing.

🚀 Application
expert
3:00remaining
How many ADC samples are stored after 3 full DMA buffer cycles?

An ADC is configured with DMA in circular mode with a buffer size of 8 samples. The DMA continuously fills the buffer. After 3 full cycles of the DMA buffer, how many unique ADC samples have been stored in total?

A24
B8
C3
D16
Attempts:
2 left
💡 Hint

Think about how many samples fit in one buffer and multiply by the number of cycles.