Application Example of the combination of stm32f103 adc1 and DMA

Source: Internet
Author: User

# DefineADC1_Chanel00 # defineADC1_Chanel11 # defineADC1_Chanel22 # defineADC1_Chanel33 # region // = the number of open AD channels ============================#defineadc _ ENB_NUM1 # define ADC_CYC_Cycles5 ready // set the adconversion rate uint16_t AD_Value [ADC_ENB_NUM]; // the region where the DMA data is stored # define ADC1_DR_Address (uint32_t) 0x4001244C) // base address of the ADC1 data register //******************************* * ***** // Function Name: ADC1_Config // function: adc1 Initialization Configuration // entry parameter: NONE // exit parameter: NONE // return value: none //************************************* **/void ADC1_Config (void) {ADC_InitTypeDef ADC_InitStructure; RCC_APB2PeriphClockCmd (RCC_APB2Periph_ADC1, ENABLE); // enable adc/* DMA1 channel1 configuration ready */RCC_ADCCLKConfig (RCC_PCLK2_Div6 ); // ADC clock Division: 72/6 = 12 M/* ADC1 configuration frequency */ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; // independent Conversion Mode: ADC_InitStructure.ADC_ScanConvMode = ENABLE; // ENABLE scan mode De = ENABLE; // ENABLE the Continuous Conversion Mode. Disable = ADC_ExternalTrigConv_None; // The ADC external switch. Disable the ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right status. // The alignment mode is 12-bit, right alignment mode ADC_InitStructure.ADC_NbrOfChannel = ADC_ENB_NUM; // Number of enable channels, one ADC_Init (ADC1, & ADC_InitStructure);/* ADC1 regular channel9 configuration * // ADC Channel group, the sampling sequence of the 9th channels is 1. The conversion time is ADC_RegularChannelConfig (ADC1, ADC_Channel_9, 1, ADC_CYC _ Cycles5); // ADC_RegularChannelConfig (ADC1, ADC_Channel_8, 2, ADC_SampleTime_55Cycles5); ADC_DMACmd (ADC1, ENABLE); // ADC command to ENABLE ADC_Cmd (ADC1, ENABLE ); // enable ADC1ADC_ResetCalibration (ADC1); // re-calibrate while (ADC_GetResetCalibrationStatus (ADC1); // wait for the re-calibration to complete ADC_StartCalibration (ADC1 ); // start calibration while (ADC_GetCalibrationStatus (ADC1); // wait for the calibration to complete ADC_SoftwareStartConvCmd (ADC1, ENABLE); // The ADC continuously updates the RAM zone through DMA. // ADC_TempSensorVrefintCmd (ENABLE );} //*************************************/ /function name: DMA_Config_ADC1 // function: DMA Initialization Configuration // entry parameter: NONE // exit parameter: NONE // return value: none //************************************* **/void DMA_Config_ADC1 (void) {DMA_InitTypeDef DMA_InitStructure; RCC_AHBPeriphClockCmd (enabled, ENABLE); // ENABLE the DMA clock DMA_DeInit (DMA1_Channel1); // ENABLE DMA1's first channel resume = ADC1_DR_Add Ress; // The peripheral base address of DMA = (uint32_t) & AD_Value [0]; // The memory storage base address is a private array DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; // The DMA conversion mode is SRC. After the peripherals move to the memory DMA_InitStructure.DMA_BufferSize = Num_Adc_Chanel; // The DMA cache size, N buffers = DMA_PeripheralInc_Disable; // After receiving the data, the device address cannot be moved back to DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; // after the device address is disabled, the target memory address is moved back to DMA_Ini TStructure. DMA_PeripheralDataSize = bytes; // defines the peripheral data width as 16 bytes = DMA_MemoryDataSize_HalfWord; // the size of the DMA moving data. HalfWord is 16 bytes = DMA_Mode_Circular; // The conversion mode, loop cache mode. Strong = strong; // high DMA Priority Ratio = DMA_M2M_Disable; // disable DMA_Init (DMA1_Channel1, & DMA_InitStructure) in M2M mode;/* Enable DMA1 channel1 */DMA_Cmd (DMA1_Channel1, ENABLE);} // === adc gpio initialization ====== void ADC1_GPIOInit (void) {// ----------- PB samples // VR port PB1GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; // pin 1GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // 50 m clock speed GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; // input mode GPIO_Init (GPIOB, & GPIO_InitStructure );} // ===== instance application ========/// ADC1 initialization settings ADC1_GPIOInit (); ADC1_Config (); DMA_Config_ADC1 (); // dma settings // -- main program ----- void mian (void) {while (1) {while (! F_10MS); F_10MS = 0; Tmp = AD_Value [0]; // read AD results in the cache }}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.