ADC Using DMA (Direct Memory Access)

This is the Third article in the series of how to use ADC in STM32 and this time we will be using DMA (Direct Memory Access). To view the other two methods, visit ADC using interrupt in STM32 and ADC in STM32 using HAL 

DMA or Direct Memory Access is a method that allows and input/output device to send or receive data directly to or from the main memory, bypassing the CPU to speedup memory operations.

The advantage of using DMA is that we can use multi channel conversion. The result of the conversion will be stored in the memory and at any instance during the code, we can access that memory and  read ADC values within interrupt service routine.

In this Tutorial I will use two channels, One of them is Internal Temperature Sensor of the Cortex M4 and second is the IR sensor. I will be using STM32CubeMx, Keil IDE and HAL libraries as usual

NOTE:- Temperature sensor is being used just to show different values for the two channels. I will make a separate tutorial on how to read the Internal Temperature sensor.

CubeMX Setup

open the cubeMx and select two channels from ADC 

Select Channel 0 and Internal temperature

Go to ADC setting and make sure that the setting is as below

Resolution is 12 bit, Scan conversion- enabled, Continuous conversion – enabled, number of conversions – 2

Now we have to add a DMA request. Go to DMA Setting Tab and follow the setting in the  image below

Enable the ADC1 global interrupt in the NVIC setting tab

Generate the project and open it.

Some Insight into the code

uint32_t  adc_buf[2], adc_val[2];

As i am using two channels therefore adc_buf  should store two bytes and so does adc_val.

Write a callback function to read the values from the buffer whenever  the conversion is complete. This is same as writing a callback in interrupt method

void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
 for (int i =0; i<2;i++)
  adc_val[i] = adc_buf[i];  // store the values in adc_val from buffer


Now whenever the conversion is complete, the values present in adc_buf will be saved in adc_val, which we can read at any instant in our program. 

NOTE here that We set DMA in circular mode which means that the conversion will continue in circular mode, latest value overwriting the oldest one.

The main Function

uint32_t display_value[2];

main ()
   HAL_ADC_Start_DMA (&hadc1, (uint32_t *) adc_buf, 2);
     while (1)
            display_value[0] = adc_val[0];
            display_value[1] = adc_val[1];
             HAL_Delay (1000);


Compile the code and start debugging.

Add display_val[0] (for channel 0) and display_val[1] (for internal temperature) to the watch list and run the debugger.

Initially the channel 0, which is connected to IR sensor, shows 4095 because there is no obstacle in front of it and internal temperature sensor shows 953. We still have to convert this value to Celsius scale. We will see that part in another tutorial.

display_val[0] = 4095, display_val[1] = 953

IR sensor value changes on bringing something near it but temperature remains same. As the main() function suggests, we are reading values every 1 sec, so the change is not instant but only after a delay of 1 sec.

display_val[0] = 472, display_val[1] = 953

This is how we use DMA to for two different channels without compromising the cpu time. The converted data will be stored in memory and we can access it at any instant during the entire program.

We can use DMA to do as much conversion as we want (obviously limited by the number of channels present in the microcontroller).

Check out the VIDEO Below

Notify of
Oldest Most Voted
Inline Feedbacks
View all comments