the 12-bit ADC is a successive approximation analog-to-digital converter. It has up to 18 channels and can measure 16 external and 2 internal signal sources. The input clock of the ADC must not exceed 14MHZ, which is generated by the PCLK2 frequency divider. if the analog voltage converted by the ADC is below the low threshold or higher than the high threshold, the AWD analog watchdog status bit is set.
ADC is typically used with DMA here is simply a library to configure the ADC to continuously scan to implement ADC applications.
Configure DMA:
voidDma_config (void) {dma_inittypedef dma_initstructure;//defining the DMA initialization structureDma_deinit (DMA_CHANNEL1);//Resetting the DMA Channel 1DMA_INITSTRUCTURE.DMA_PERIPHERALBASEADDR = adc1_dr_address;//Define DMA channel peripheral base Address =adc1_dr_addressDma_initstructure.dma_memorybaseaddr = (u32) &ADC_ConvertedValue;//defining DMA Channel memory addressesDma_initstructure.dma_dir = DMA_DIR_PERIPHERALSRC;//specify the peripheral as the source addressDma_initstructure.dma_buffersize =1;//Defining the DMA buffer size 1Dma_initstructure.dma_peripheralinc = dma_peripheralinc_disable;//The current peripheral register address is unchangedDma_initstructure.dma_memoryinc = dma_memoryinc_disable;//The current memory address is not changedDma_initstructure.dma_peripheraldatasize = Dma_peripheraldatasize_halfword;//Define Peripheral data width 16 bitsDma_initstructure.dma_memorydatasize = Dma_memorydatasize_halfword;//defines the memory data width 16 bitsDma_initstructure.dma_mode = Dma_mode_circular;//DMA channel operation mode bit ring buffer modedma_initstructure.dma_priority = Dma_priority_high;//DMA Channel Priority Highdma_initstructure.dma_m2m = dma_m2m_disable;//Disable DMA channel memory to memory transferDma_init (Dma_channel1, &dma_initstructure);//Initializing DMA Channel 1Dma_cmd (Dma_channel1, ENABLE);//Enable DMA Channel 1}
To configure the operation of the ADC:
voidAdc_config (void) {adc_inittypedef adc_initstructure;//defining the ADC initialization struct-body variableAdc_initstructure.adc_mode = adc_mode_independent;//ADC1 and ADC2 work in standalone modeAdc_initstructure.adc_scanconvmode = ENABLE;//Enable ScanAdc_initstructure.adc_continuousconvmode = ENABLE;//ADC conversion works in continuous modeAdc_initstructure.adc_externaltrigconv = Adc_externaltrigconv_none;//There is software control conversionAdc_initstructure.adc_dataalign = Adc_dataalign_right;//right-justified conversion dataAdc_initstructure.adc_nbrofchannel =1;//Channel 1 for conversion channelAdc_init (ADC1, &adc_initstructure);//Initializing the ADCAdc_regularchannelconfig (ADC1, Adc_channel_14,1, ADC_SAMPLETIME_28CYCLES5); //ADC1 Select Channel 14, Sequencer level 1, sampling time 239.5 cyclesAdc_dmacmd (ADC1, ENABLE);//Enable ADC1 Module DMAAdc_cmd (ADC1, ENABLE);//Enable to ADC1Adc_resetcalibration (ADC1);//Resetting the ADC1 calibration Register while(Adc_getresetcalibrationstatus (ADC1));//wait for ADC1 calibration reset to completeAdc_startcalibration (ADC1);//Start ADC1 Calibration while(Adc_getcalibrationstatus (ADC1));//wait for ADC1 calibration to completeAdc_softwarestartconvcmd (ADC1, ENABLE);//enable ADC1 software to start converting}
(1) The first parameter is Adc_mode, which is set to standalone mode:
Adc_initstructure.adc_mode = adc_mode_independent;
In this mode, the dual ADC cannot be synchronized, and each ADC interface works independently. So if you don't need the ADC to sync or just use an ADC, it should be set to standalone mode.
(2) The second parameter is Adc_scanconvmode, which is set to DISABLE.
Adc_initstructure.adc_scanconvmode = DISABLE;
If only one channel is used,disable is available, and if multiple channels are used, it must be set to ENABLE.
(3) The third parameter is Adc_continuousconvmode, which is set to ENABLE, which is a continuous conversion. If set to DISABLE, it is a one-time conversion. The difference between the two is that the conversion is not stopped until all the data conversion is complete, whereas a single conversion stops when the data is converted and the conversion is triggered again. So if you need to collect 1024 data or more at a time, continuous conversion is used.
(4) The fourth parameter is Adc_externaltrigconv, that is, select the external trigger mode. There are only three kinds of words here:
1, the first type is the simplest software trigger, the parameter is adc_externaltrigconv_none. Remember to call the library function when you are set up:
Adc_softwarestartconvcmd (ADC1, ENABLE);
This trigger will not start.
2, the second type is the timer channel output trigger. There are several:adc_externaltrigconv_t1_cc1,adc_externaltrigconv_t1_cc2,adc_externaltrigconv_t2_cc2,
adc_externaltrigconv_t3_t and adc_externaltrigconv_t4_cc4. Timer output trigger is more troublesome, also need to set the corresponding timer. In
ADC_EXTERNALTRIGCONV_T2_CC2 trigger For example set the corresponding timer:
voidTim2_configuration (void) {tim_timebaseinittypedef tim_timebasestructure; Tim_ocinittypedef tim_ocinitstructure; Tim_timebasestructure.tim_prescaler=4; Tim_timebasestructure.tim_countermode=tim_countermode_up; Tim_timebasestructure.tim_period=0XFF; Tim_timebasestructure.tim_clockdivision=0; Tim_timebasestructure.tim_repetitioncounter=0; Tim_timebaseinit (TIM2,&tim_timebasestructure); Tim_ocinitstructure.tim_ocmode=tim_ocmode_pwm1; Tim_ocinitstructure.tim_outputstate=tim_outputstate_enable; Tim_ocinitstructure.tim_pulse=0X7F; Tim_ocinitstructure.tim_ocpolarity=Tim_ocpolarity_low; Tim_ocinitstructure.tim_ocidlestate=Tim_ocidlestate_set; Tim_oc2init (TIM2,&tim_ocinitstructure); Tim_cmd (TIM2, ENABLE); Tim_ctrlpwmoutputs (TIM2, ENABLE);}
This setting can then be triggered with the output of Timer 2, as for the period of the trigger, set the time of TIM2. Don't repeat it here.
3, the third is the external pin trigger , for the rule channel, select Exti line 11 and Tim8_trgo as the external trigger event, while the injection channel group selects Exti line 15 and TIM8_CC4 as an external triggering event.
(5) The fifth parameter is adc_dataalign, which is set to adc_dataalign_right right alignment. It is recommended to use right alignment because it is easier to process data. Of course, if you want to start transmitting data from a high, then the advantage of left alignment is obvious.
(6) The sixth parameter is Adc_nbrofchannel, as the name implies: the number of channels. If you are collecting data from multiple channels, you need to set this parameter. In addition, you might define the order of each channel in the configuration function of the rule Channel group, such as:
Adc_regularchannelconfig (adc1,adc_channel_13,1, adc_sampletime_13cycles5); Adc_regularchannelconfig (adc1,adc_channel_14,2, ADC_SAMPLETIME_13CYCLES5);
One thing to note when multichannel data transfer is that: If an array is adc_valuetab[4] and two channels are set: Channel 1 and channel 2, after the conversion,Adc_valuetab[0] and Adc_valuetab [2] The data for channel 1 is stored, while adc_valuetab[1] and adc_valuetab[3] store the data of Channel 2. If the array size is large, then so on.
Add: When using DMA to transfer data, you need to set the peripheral address and memory address, the peripheral address of course is the ADC address, and the memory address if the use of 8-bit data, memory must be defined as 8-bit buffer; 16-bit data format, the memory is a 16-bit buffer, cannot be defined as 32 bits or more, otherwise, the data will be wrong.
STM32 ADC Base and multichannel sampling