STM32 ADC1 Single-channel continuous conversion, the conversion data is transmitted via DMA to the USART1 data register and sent

Source: Internet
Author: User

ADC single channel continuous data acquisition, through the DMA transmission to the serial port to send to the host computer. The DMA transfer allows the data to bypass the CPU and the data does not need to enter or leave the SRAM. The DMA controller uses the system bus, directly transmits the data of the ADC data register to the serial port to send the information register, the serial port sends the data register the write operation will trigger the serial port transmission, thus sends the data to the upper computer. In the DMA transfer process, the CPU can do other operations, DMA and CPU time-sharing using the system bus.

In order for the ADC1 conversion to be synchronized with the serial port, the DMA transfer mode is set to normal (one transmission), that is, after the transfer of the specified amount of data, the DMA will automatically shut down and no longer respond to the DMA request (when the transfer mode is in cycle mode, when the DMA configuration is complete, when there is a DMA request signal, DMA starts to work, continuously transmits data through the system bus, turns on ADC conversion complete interrupt, and re-turns DMA in interrupt function. Such Every time the ADC completes a data conversion, it triggers a DMA transfer, the DMA automatically shuts down after the transfer is complete, and when the next ADC conversion is complete, the DMA is switched on again, so that the ADC1 conversion is synchronized with the serial port sending again and again. And throughout the process, the CPU can do other work.

The following is the core code:

ADC1 GPIO Configuration:

    gpio_inittypedef gpio_initstructure;    Rcc_apb2periphclockcmd (Rcc_apb2periph_gpiob, ENABLE);   // turn on the GPIOB peripheral clock  (STM32 need to turn on the corresponding peripheral clock before setting the register operation)    Gpio_initstructure.gpio_pin = gpio_pin_0;     = Gpio_mode_ain;        // set as analog input    Gpio_init (Gpiob, &gpio_initstructure);               // PB0 for ADC1 Channel 8 analog input

ADC1 Mode configuration:

Rcc_ahbperiphclockcmd (RCC_AHB2PERIPH_ADC1, ENABLE);//turn on the ADC1 peripheral clockAdc_inittypedef adc_initstructure; Adc_initstructure.adc_mode= Adc_mode_independent;//Independent ADC ModuleAdc_initstructure.adc_scanconvmode = DISABLE;//no scan mode, scan mode for multi-channel acquisitionAdc_initstructure.adc_continuousconvmode = ENABLE;//turn on continuous conversion mode, that is, ADC conversion is performed continuouslyAdc_initstructure.adc_externaltrigconv = Adc_externaltrigconv_none;//do not use external trigger conversionsAdc_initstructure.adc_dataalign = Adc_dataalign_right;//capturing data right-justifiedAdc_initstructure.adc_nbrofchannel =1;//number of channels to convert 1Adc_init (ADC1, &adc_initstructure);         Adc_itconfig (ADC1, ADC_IT_EOC, ENABLE); //Open ADC1 Conversion complete interruptrcc_adcclkconfig (RCC_PCLK2_DIV8)//Configure the ADC clock for the PCLK2 8-point frequencyAdc_regularchannelconfig (ADC1, Adc_channel_8,1, ADC_SAMPLETIME_55CYCLES5);//Configure ADC1 Channel 8, channel conversion sequence 1, conversion time 55.5 clock cyclesAdc_dmacmd (ADC1, ENABLE);//Open ADC1 DMA request, which will trigger DMA start transfer after ADC conversion is completeAdc_cmd (ADC1, ENABLE);//Open ADC1Adc_resetcalibration (ADC1);//Reset Calibration Register     while(Adc_getresetcalibrationstatus (ADC1));//Wait for calibration register reset to completeAdc_startcalibration (ADC1);//ADC Calibration     while(Adc_getcalibrationstatus (ADC1));//wait for ADC calibration to completeAdc_softwarestartconvcmd (adc1,enable)//turn on ADC software trigger

DMA1 configuration:

Dma_inittypedef dma_initstructure;  Rcc_ahbperiphclockcmd (RCC_AHBPERIPH_DMA1, ENABLE); //turn on the DMA1 peripheral clockDma_deinit (DMA1_CHANNEL1);//Default SettingsDma_initstructure.dma_peripheralbaseaddr = (u32) &ADC1->DR;//peripheral pointing to ADC1 data registersDma_initstructure.dma_memorybaseaddr = (u32) &USART1->DR;//memory point USART1 send data registerDma_initstructure.dma_dir = DMA_DIR_PERIPHERALSRC;//direction by Adc-->usart1Dma_initstructure.dma_buffersize =2;//send two data (due to ADC accuracy of 12 bits, data register 16 bits, two bytes sent)Dma_initstructure.dma_peripheralinc = dma_peripheralinc_enable;//peripheral per byte self-increment 1 point to ADC1 data registerDma_initstructure.dma_memoryinc = dma_memoryinc_disable;//memory points to the serial port to send data register unchanged (serial port data width is 8 bits)Dma_initstructure.dma_peripheraldatasize = Dma_peripheraldatasize_byte;//data width 1 bytesDma_initstructure.dma_memorydatasize = Dma_memorydatasize_byte;//data width 1 bytesDma_initstructure.dma_mode = Dma_mode_normal;//one-time transmissiondma_initstructure.dma_priority = Dma_priority_high;//High-prioritydma_initstructure.dma_m2m = dma_m2m_disable;//ProhibitDma_init (Dma1_channel1, &dma_initstructure);                     Dma_cmd (Dma1_channel1, ENABLE); //The DMA channel for the DMA1 channel 1,ADC1 is DMA1 Channel 1

DMA Request mapping is attached:

Serial

STM32 ADC1 Single-channel continuous conversion, the conversion data is transmitted via DMA to the USART1 data register and sent

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.