STM32F3 ADC uses DMA mode to transfer conversion data

Publisher:jiaohe1Latest update time:2017-09-30 Source: eefocusKeywords:STM32F3 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

This article uses ADC to convert the voltage value output by the potentiometer, and uses DMA mode to transmit the conversion result. The average value is taken every 8 sampling conversions to perform a simple digital filter.

Detailed configuration and use of ADC

See the previous diary on the use of ADC in STM32 , just add a step to configure DMA at the end:

DMA for ADC channels features configuration
 To enable the DMA mode for ADC channels group, use the ADC_DMACmd() function.
 To configure the DMA transfer request, use ADC_DMAConfig() function.

DMA Configuration

(摘自STM32F3官方用户手册UM1581 User manual)
1. Enable The DMA controller clock using
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE) function for DMA1 or using RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2, ENABLE) function for DMA2.
2. Enable and configure the peripheral to be connected to the DMA channel (except for internal SRAM / FLASH memories: no initialization is necessary).
3. For a given Channel, program the Source and Destination addresses, the transfer Direction, the Buffer Size, the Peripheral and Memory Incrementation mode and Data
Size, the Circular or Normal mode, the channel transfer Priority and the Memory-to-Memory transfer mode (if needed) using the DMA_Init() function.
4. Enable the NVIC and the corresponding interrupt(s) using the function
DMA_ITConfig() if you need to use DMA interrupts.
5. Enable the DMA channel using the DMA_Cmd() function.
6. Activate the needed channel Request using PPP_DMACmd() function for any PPP peripheral except internal SRAM and FLASH (ie. SPI, USART ...) The function allowing this operation is provided in each PPP peripheral driver (ie. SPI_DMACmd for SPI peripheral).
7. Optionally, you can configure the number of data to be transferred when the channel
is disabled (ie. after each Transfer Complete event or when a Transfer Error occurs) using the function DMA_SetCurrDataCounter(). And you can get the number of remaining data to be transferred using the function DMA_GetCurrDataCounter() at run time (when the DMA channel is enabled and running).
8. To control DMA events you can use one of the following two methods:
a. Check on DMA channel flags using the function DMA_GetFlagStatus().
b. Use DMA interrupts through the function DMA_ITConfig() at initialization phase and DMA_GetITStatus() function into interrupt routines in communication phase. After checking on a flag you should clear it using DMA_ClearFlag() function. And after checking on an interrupt event you should clear it using DMA_ClearITPendingBit()function.

Important code:


  1. #include "ADC_DMA.h"  

  2.   

  3. #define DATANUM 8  

  4. uint8_t FLAG = 0; //Conversion times flag  

  5. uint16_t CONV_RESULTS[DATANUM];  

  6. void ADC_Config(void)  

  7. {  

  8.     ADC_InitTypeDef       ADC_InitStructure;  

  9.     ADC_CommonInitTypeDef ADC_CommonInitStructure;  

  10.       

  11.     RCC_ADCCLKConfig(RCC_ADC12PLLCLK_Div2);  

  12.     RCC_AHBPeriphClockCmd(RCC_AHBPeriph_ADC12,ENABLE);  

  13.       

  14.     //GPIO_Config(); //Configure the sampling input pin to analog input mode  

  15.   

  16.     //......other  

  17.     ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_2;  

  18.     ADC_CommonInitStructure.ADC_DMAMode = ADC_DMAMode_Circular;  

  19.     //.......other  

  20.     ADC_CommonInit(USING_ADC,&ADC_CommonInitStructure);  

  21.   

  22.     //ADC Init,ADC_Init(USING_ADC,&ADC_InitStructure);  

  23.       

  24.     ADC_ITConfig(USING_ADC, ADC_IT_EOC, ENABLE); //Enable interrupt  

  25.   

  26.     ADC_DMAConfig(USING_ADC, ADC_DMAMode_Circular); //Configure ADC_DMA, very important  

  27.   

  28.     ADC_DMACmd(USING_ADC, ENABLE);//打开ADC_DMA  

  29.   

  30.     DMA_Config();  

  31.   

  32.     ADC_Cmd(USING_ADC,ENABLE);  

  33.   

  34.     while(!ADC_GetFlagStatus(USING_ADC, ADC_FLAG_RDY));  

  35.   

  36.     ADC_StartConversion(USING_ADC);   

  37.   

  38. }  

  39.   

  40. void GPIO_Config(void)  

  41. {  

  42.     //.......  

  43.     //......  

  44. }  

  45. void DMA_Config(void)  

  46. {  

  47.     DMA_InitTypeDef DMA_InitStructure;  

  48.   

  49.     RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);  

  50.   

  51.     DMA_DeInit(DMA1_Channel1);  

  52.       

  53.     DMA_InitStructure.DMA_PeripheralBaseAddr = ADC_DATA_ADDR; //Address of ADC data register DR  

  54.     DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&CONV_RESULTS; //Address to store conversion results  

  55.     DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;  

  56.     DMA_InitStructure.DMA_BufferSize = DATANUM;  

  57.     //.....  

  58.       

  59.     DMA_Init(DMA1_Channel1, &DMA_InitStructure);  

  60.       

  61.   

  62.     DMA_Cmd(DMA1_Channel1, ENABLE);  

  63.   

  64. }  

  65. void ADC1_2_IRQHandler (void)  

  66. {  

  67.   

  68.     FLAG++; // Check FLAG in the main function and wake up the process  

  69.             // Calculate the number of converted data  

  70.     if(FLAG>DATANUM-1)  

  71.                 {  

  72.                     FLAG=0;  

  73.                 }  

  74.       

  75.     ADC_ClearITPendingBit(USING_ADC, ADC_IT_EOC);  

  76. }  

  77.   

  78. //Get the average value of the conversion result and do filtering  

  79. uint16_t GetAverage(void)  

  80. {  

  81.     u8 i=0;  

  82.     u16 average=0;  

  83.     u32 sum=0,voltage=0;  

  84.       

  85.     for(i=0;i

  86.         {  

  87.             sum += CONV_RESULTS[i];  

  88.         }  

  89.       

  90.     average = sum/data;  

  91.     voltage = average*3300/0xFFF;  

  92.   

  93.     return voltage;  

  94. }  

  95. //Abort placement  

  96. void NVIC_Config(void)  

  97. {  

  98.     NVIC_InitTypeDef NVIC_InitStruct;  

  99.       

  100.     NVIC_InitStruct.NVIC_IRQChannel =ADC1_IRQn;  

  101.     //........  

  102.       

  103.     NVIC_Init(&NVIC_InitStruct);  

  104. }  


Source code download:

http://download.csdn.net/detail/wind4study/8674381


Keywords:STM32F3 Reference address:STM32F3 ADC uses DMA mode to transfer conversion data

Previous article:STM32 key scan/key interrupt/external interrupt
Next article:STM32 asymmetric PWM mode realizes dynamic phase shift

Recommended ReadingLatest update time:2024-11-15 18:00

ADC0809 Conversion Program
Block Diagram Circuit   program                             ORG 05A0H 05A0    758153      SE11:  MOV    SP,#53H 05A3    757E00              MOV    7EH,#00H 05A6 757D08 MOV 7DH, #08H 05A9    757C00              MOV    7CH,#00H 05AC 757B09 MOV 7BH,#09H 05AF    757A10              MOV    7AH,#10H 05B2 757910 MOV 79
[Microcontroller]
ADC0809 Conversion Program
STM8 library function study notes ADC
Note: All instructions are based on IAR for STM8 1.10 SP3, library version 1.1.1, STM8S105C4 The library contains two modules, ADC1 and ADC2, which are 10-bit successive approximation types and can be configured for single or continuous conversion. ADC1 includes scanning mode, continuous mode with buffer, and analog w
[Microcontroller]
The difference between the regular channel and the injection channel of the STM32 ADC
Each ADC module of STM32 can switch to different input channels and perform conversions through the internal analog multiplexer. STM32 has specially added a variety of group conversion modes, which can be set by the program to automatically sample and convert multiple analog channels one by one. There are two ways t
[Microcontroller]
Single chip digital voltmeter (ADC0809)
1. Brief Introduction     The digital voltmeter is designed using the analog-to-digital conversion chip ADC0809. The digital voltmeter designed in the example can measure the input voltage value in the range of 0 to 5V, and display the collected voltage value through a 4-digit LED digital tube. The example measures
[Microcontroller]
Single chip digital voltmeter (ADC0809)
Design of carbon dioxide detector based on ADC acquisition and data processing system
introduction In the process of oil exploration, carbon dioxide detection is an important well logging task, which provides a reference for subsequent geological interpretation and evaluation. The gases released from the drilling fluid include a variety of hydrocarbon gases, hydrogen, carbon dioxide, etc. Before using
[Test Measurement]
Design of carbon dioxide detector based on ADC acquisition and data processing system
FL2440 driver addition (5) ADC driver study notes
As can be seen from the figure, the analog ADC is divided into two parts, one is the touch screen function, and the other is the ordinary ADC function. Two interrupts, INT_TC and INT_ADC, can be generated respectively. The ADC module has a total of 8 channels for analog signal input, namely AIN0, AIN1, AIN2, A
[Microcontroller]
MCU Experiment Board 2011_V1.1-ADC0809 Practice Program
    Schematic diagram:   ADC0809 control timing:       test program /************MCU teaching experiment board******************/ /*Function description: ADC0809 exercise */ /*Author: Zheng Wen */ /*Written on: 2011.3.1 */ /*Connect to external crystal oscillator 11.0592 MHZ */
[Microcontroller]
MCU Experiment Board 2011_V1.1-ADC0809 Practice Program
How to Improve High-Speed ​​ADC Clock Signals
It is normal to expect performance that matches the datasheet signal-to-noise ratio (SNR) value when you use a high-speed analog-to-digital converter (ADC). When you test the SNR of an ADC, you might connect a low-jitter clock device to the converter's clock input pins and apply a reasonably low-noise input signal. If
[Power Management]
How to Improve High-Speed ​​ADC Clock Signals
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号