ADC:
1. The internal ADC module of STM32 has three ADC1, ADC2, and ADC3, which are independent of each other, so they can be sampled synchronously.
The input clock of 2ADC must not exceed 14MHz. It is generated by PCLK2 frequency division. It should be configured in RCC_CFGR, and then there is no clock division configuration bit in the ADC's own register.
3. ADC conversion time: STM32F103xx enhanced production, 1μ s when the clock is 56MHz (the clock is 1.17 μs at 72MHz)
4. The conversion accuracy of the ADC is set to 12 bits by default. Input range: ADC input range: V REF- ≤ VIN ≤ VREF+
5. There are 18 channels, of which 16 channels are external, two channels are internal, the internal temperature sensor is connected to ADC1_IN16, and the internal reference voltage V REFINT is connected to ADC1_IN17.
6. Startup mode of conversion: There are external triggers, internal peripheral triggers such as TIMx, and software trigger, one trigger to convert one group, software enable mode by setting ADON of ADC_CR2 register.
7. The concept of two groups, a. rule group: the ADC conversion sequence used in general; b. the injection group, which has a higher priority than the conversion sequence in the rule group, and when the rule group is being converted, the share Triggered the injection group, he will interrupt the ongoing conversion of the rule group, knowing that the injection group conversion is complete, and again with the rule group conversion.
8 In general, there are two ways to convert rules, that is, even independent single conversion mode, intermittently start to convert n channels, n configurable, continuous conversion, knowing that the setting is stopped. In continuous scan mode, one start channel will be converted back and forth in the sequence. However, the rule channel group has only one data register ADCx_DR, so the data value of the last conversion must be read before the next conversion is completed, otherwise it will be Overlay, which is generally enabled DMA request, so that each time the data stimulus conversion is completed, EOC is generated, and a DMA request is also generated, and the DMA transfers the data in the DR to the memory unit.
In the single conversion mode, it is necessary to query the EOC after each completion or use the interrupt to read the data, and then the software starts the next conversion. During this period, if the sequence of the conversion is to be changed, the channel can be written to the next acquisition. It is the same as the previous mode of using AD, but it takes time.
In the discontinuous mode, it is known to convert this sequence by setting n in the entire SQR register group. Temporarily unused.
8. ADC sampling time configuration, configured in the sampling time register, the time required for one conversion TCONV = sampling time + 12.5 = 14 cycles, 12.5 is the conversion cycle.
9. The ordered data is only 12, less than 16 bits. Therefore, the seventh mode should be set to facilitate data extraction. In the injection group, the offset of the product can be set, that is, the conversion result is equal to the sampled value minus the offset value. It may be a negative value, so it is right-aligned, and the sign bit is expanded at the high position. The upper 4 bits of the rule group are all 0.
10. The ADC conversion threshold can be configured. Similar to the watchdog function, the ADC_HTR and ADC_LTR registers are respectively configured with upper and lower limits. If the interior is not in this range, the interrupt flag can be generated. The user can choose to enter the interrupt.
11. External trigger mode, mainly the peripheral trigger signal, TIM interrupt time to trigger the ADC conversion start, to achieve the purpose of controlling the sampling time, it is not necessary to write the timer interrupt separately.
12. Dual ADC use, dual ADC has many medium modes, the most likely to use is the dual ADC rule synchronous conversion mode, can refer to the Datasheet to configure.
Rule single conversion configuration mode:
Turn off CONT, turn off SCAN mode, set n value equal to 1, ADC independent mode, basically initialize the structure.
Then write the channel number to a location in the SQR group and configure the conversion sampling time for that channel.
To enable ADCx, it is important to perform ADC calibration at the end. Otherwise, calibration may be inaccurate. Calibration includes reset calibration, AD calibration, and the conversion can not be started until the calibration is completed.
Each time the conversion completes the query EOC flag, then the DR data is read and the next conversion is enabled, a new conversion channel can also be written.
Void Init_ADC()
{
ADC_InitTypeDef ADC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_ADC1, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
RCC_ADCCLKConfig(RCC_PCLK2_Div6);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
ADC_DeInit(ADC1);
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = ENABLE; //This is when using continuous scan mode
ADC_InitStructure.ADC_ConTInuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 2;
// ADC_InitStructure.ADC_ScanConvMode = DSIABLE;
// ADC_InitStructure.ADC_ConTInuousConvMode = DISABLE;
// ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_RegularChannelConfig(ADC1,ADC_Channel_0, 1, ADC_SampleTime_239Cycles5 ); //
ADC_RegularChannelConfig(ADC1,ADC_Channel_1, 2, ADC_SampleTime_239Cycles5 ); //
/* Enable ADC1 DMA */
ADC_DMACmd(ADC1, ENABLE);
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE);
/* Enable ADC1 reset calibration register */
ADC_ResetCalibration(ADC1);
While(ADC_GetResetCalibrationStatus(ADC1))
{
;
}
/* Start ADC1 calibration */
ADC_StartCalibration(ADC1);
While(ADC_GetCalibrationStatus(ADC1))
{
;
}
}
Read function:
Void GetADValue()
{
// ADC_RegularChannelConfig(ADC1
all in one energy;all in one battery,home battery,home energy storage,household power storage,home solar power,all in one system
Shenzhen Enershare Technology Co.,Ltd , https://www.enersharepower.com