#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
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]);
}
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
- Popular Resources
- Popular amplifiers
Recommended Content
Latest Microcontroller Articles
He Limin Column
Microcontroller and Embedded Systems Bible
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
MoreSelected Circuit Diagrams
MorePopular Articles
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
MoreDaily News
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
Guess you like
- [Synopsys IP Resources] How 5G affects chip design
- Microcontroller Basics
- [Application Sharing] Power Amplifier Based on Flexible Piezoelectric Ultrasonic Transducer Testing and Experimental Application
- What does DIN VDE V 0884-11:2017-01 mean for digital isolator certification?
- Self-built houses in rural areas VS buying houses in cities
- DSP establishes C environment function c_int00()
- First look at the GD32L233C-START development board
- ESP32-S2 mini Development Board
- Answer questions to win prizes: Azure Sphere IoT solution quiz ranking competition, how long can you dominate the screen?
- Which PCB teaching video is more reliable?