×
单片机 > 单片机程序设计 > 详情

STM32F3的ADC使用DMA模式传输转换数据

发布时间:2020-08-27 发布时间:
|

本文使用ADC转换电位器输出的电压值,并用DMA模式传输转换的结果,每8次采样转换取平均值,做一个简单的数字滤波。

ADC的详细配置与使用

见之前的日记STM32中ADC的使用,只是最后增加一步配置DMA:

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的配置

(摘自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.

重要代码:


  1. #include "ADC_DMA.h"  

  2.   

  3. #define DATANUM 8  

  4. uint8_t FLAG = 0;                                 //转换次数标志位  

  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();//实现采样输入引脚的配置,配置为模拟输入模式  

  15.   

  16.     //......其他  

  17.     ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_2;  

  18.     ADC_CommonInitStructure.ADC_DMAMode = ADC_DMAMode_Circular;  

  19.     //.......其他  

  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);//开中断  

  25.   

  26.     ADC_DMAConfig(USING_ADC, ADC_DMAMode_Circular);//配置ADC_DMA,非常重要  

  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;//ADC数据寄存器DR的地址  

  54.     DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&CONV_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++;//在主函数中检查FLAG 并惊醒处理  

  69.             //计算转换数据的个数  

  70.     if(FLAG>DATANUM-1)  

  71.                 {  

  72.                     FLAG=0;  

  73.                 }  

  74.       

  75.     ADC_ClearITPendingBit(USING_ADC, ADC_IT_EOC);  

  76. }  

  77.   

  78. //获取转换结果的平均值,做滤波处理  

  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/DATANUM;  

  91.     voltage = average*3300/0xFFF;  

  92.   

  93.     return voltage;  

  94. }  

  95. //配置中断  

  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. }  



关键字:STM32F3  ADC  DMA模式  转换数据

『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
C51 特殊功能寄存器SFR的名称和地址