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.
open the cubeMx and select two channels from ADC
Go to ADC setting and make sure that the setting is as below
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
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
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
Compile the code and start debugging.
Add display_val (for channel 0) and display_val (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.
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.
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).
. . . . . DOWNLOAD . . . . .
You can buy me acontrollerstech
coffeeSensor by clicking DONATE OR Just click DOWNLOAD to download the code
- WavePlayer using STM32 Discovery January 16, 2021
- How to use SPI in LPC2148 December 26, 2020
- STM32 ADC MULTI CHANNEL WITHOUT DMA December 24, 2020
- Send and Receive data to PC without UART (STM32 USB COM) December 10, 2020
- STM32 GPIO INPUT Configuration December 4, 2020
- STM32 GPIO OUTPUT Config using REGISTERS November 24, 2020