# ADC Reading Sequence with DMA on STM32

Setup

ADC is configured to sample one regular channel, trigger sourced set to Timer 2 TRGO event.

<figure><img src="/files/CwQrQolw7X5m2wqE2XdC" alt=""><figcaption></figcaption></figure>

DMA is configured to use normal mode and 16bit transfer size.

<figure><img src="/files/dWWsWQmH1HsYGF9fq08t" alt=""><figcaption></figcaption></figure>

Timer 2 is configured to generate a 10 kHz signal on TRGO with counter update event.

<figure><img src="/files/PzOvX70sug54FNbOF52P" alt=""><figcaption></figcaption></figure>

Code

```c

#define ADC_SAMPLES    128

uint8_t completed = 0;
uint16_t adc_data_buffer[ADC_SAMPLES];

void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc) {
  // this function will be invoked after DMA finishes all transfers
  completed = 1;
}

void APP_init() {
  // start the timer, which is the trigger source of ADC conversion
  HAL_TIM_Base_Start(&htim2);
}

void APP_main() {
  HAL_ADC_Start_DMA(&hadc1, (uint32_t *)adc_data_buffer, ADC_SAMPLES);

  while (!completed) {}
  completed = 0;
  
  for (size_t i=0; i<ADC_SAMPLES; i+=1) {
    sprintf(str, "0 %d 4096\n", adc_data_buffer[i]);
    HAL_UART_Transmit(&huart2, (uint8_t *)str, strlen(str), 100);
  }

  HAL_Delay(100);
}

```

The terminal correctly prints the audio waveform of a 800 Hz test sound.

<figure><img src="/files/zL2fMV1R8uvD6nfrEuyF" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://tk233.gitbook.io/notes/stm32/analog/adc-reading-sequence-with-dma-on-stm32.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
