STM32 ADC and DMA multi-channel processing

Publisher:Bby1978Latest update time:2016-10-07 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
#define ADC1_DR_Address ((uint32_t)0x4001244C)//ADC1 DR register base address
DMA_InitTypeDef DMA_InitStructure; //DMA
ADC_InitTypeDef ADC_InitStructure; //ADC 
#define ADC_CH 2 //Number of channels
#define ADC_num 10 //Number of acquisition points
vu16 After_filter[ADC_CH]; //Used to store the result after averaging
vu16 ADCConvertedValue[ADC_num][ADC_CH];//AD data cache
/***********************************************************************************
* Function name: DMA_Configuration(void) 
* Function: DMA controller settings
* Parameter variables: NONE
* Global variables: NONE
* Calling function:
* Modification time: 
* Version: V1.0 
* Status: ADC1 uses DMA1 channel CH1
**********************************************************************************/
void DMA_Configuration(void)
{
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); //Enable DMA clock
  //DMA1 channel1 configuration ----------------------------------------------
  DMA_DeInit(DMA1_Channel1);
  DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;
  DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&ADCConvertedValue;//Store ADC value
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;//Data destination address RAM
  DMA_InitStructure.DMA_BufferSize = ADC_CH*ADC_num;//Allocate internal RAM
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //Peripheral address does not changeDMA_InitStructure.DMA_MemoryInc
  = DMA_MemoryInc_Enable; //Memory address incrementsDMA_InitStructure.DMA_PeripheralDataSize
  = DMA_PeripheralDataSize_HalfWord; //Data width is 16
  bitsDMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; //Data width is 16
  bitsDMA_InitStructure.DMA_Mode = DMA_Mode_Circular; //Working circular storage modeDMA_InitStructure.DMA_Priority
  = DMA_Priority_High; //High priorityDMA_InitStructure.DMA_M2M
  = DMA_M2M_Disable; //Data memory to memory transferDMA_Init
  (DMA1_Channel1, &DMA_InitStructure); //
  // Enable DMA1 channel1 
  DMA_Cmd(DMA1_Channel1, ENABLE);
}
/***************************************************************************
* Function name: ADC_Configuration 
* Function: ADC function settings use DMA bus channel 1
* Parameter variable: NONE
* Global variable: NONE
* Calling function:
* Modification time: 
* Version: V1.0 
* Status: Debugging completed
*******************************************************************************/
void ADC_Configuration(void)
{
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); // Enable ADC1 clock
  // ADC1 configuration ------------------------------------------------------
  ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; // ADC1 and ADC2 work independently
  ADC_InitStructure.ADC_ScanConvMode = ENABLE; //Work in multiple channels
  ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; //Work in continuous mode
  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; //Select software trigger
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //Data right alignment
  ADC_InitStructure.ADC_NbrOfChannel = ADC_CH; //N channels
  ADC_Init(ADC1, &ADC_InitStructure); // ADC1 initialization
  ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 1, ADC_SampleTime_239Cycles5); //GPIO.4 UA
  ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 2, ADC_SampleTime_239Cycles5); //GPIOA.5 UB
  
  
  ADC_DMACmd(ADC1, ENABLE); //Enable DMA ADC1 
  ADC_Cmd(ADC1, ENABLE); //Enable ADC1 
  ADC_ResetCalibration(ADC1); //Reset the calibration register of the specified ADCwhile
  (ADC_GetResetCalibrationStatus(ADC1)); //Wait for resetting the calibration register of the specified
  ADCADC_StartCalibration(ADC1); //Start the calibration procedure of the specified ADCwhile 
  (ADC_GetCalibrationStatus(ADC1)); // Check the end of ADC1 calibration  
  ADC_SoftwareStartConvCmd(ADC1, ENABLE); // Enable or disable the software conversion start function of the specified ADC
}
/**********************************************************************************
* Function name: GetVolt(u16 advalue)
* Function function: Get the value of ADC and convert binary to decimal
* Parameter variable: NONE
* Global variable: NONE
* Calling function:
* Modification time: 
* Modification content: 
* Version: V1.0 
* Status:
**************************************************************************/
u16 GetVolt(u16 advalue) 

return (u16)(advalue * 330 / 4096); //The result is enlarged 100 times to facilitate the decimal below
}  
/******************************************************************************
* Function name: void Filter(void)
* Function function: Average value function
* Parameter variable: NONE
* Global variable: NONE
* Call function:
* Modification time: 
* Modification content: 
* Version: V1.0 
* Status:
**************************************************************************/ 
void Filter(void) 
{  
int sum = 0; 
u8 count,temp_i; 
    for(temp_i=0;temp_i         { 
            for ( count=0;count                   { 
                  sum += ADCConvertedValue[count][temp_i]&0xFFFC; 
                  }  
              After_filter[temp_i]=sum/ADC_num; 
              sum=0;  
        } 

/******************************************************************************
* Function name: Data_conversion()
* Function function: Data conversion 
* Parameter variable: NONE
* Global variable: NONE
* Calling function:
* Modification time: 
* Modification content: 
* Version: V1.0 
* Status:
******************************************************************************/
void Data_conversion()
{
    Filter(); //Get operation data
  // RT.UB= GetVolt(After_filter[0]); 
  // RT.UA= GetVolt(After_filter[1]); 
  
}
Keywords:STM32 Reference address:STM32 ADC and DMA multi-channel processing

Previous article:STM32F107 external crystal oscillator 25MHz configured to run at 72MHz
Next article:Solution to STM32 IAP not being able to run user programs

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号