STM32 ADC study notes

Successive approximation ADC: The schematic diagram of the successive approximation ADC is as shown in the figure below.
Why is it called the successive approximation ADC?
It is not difficult to see from the circuit diagram that there is an operational amplifier. , the output is 1, otherwise it is 0, the output here is given to the following counter, the clock CLK of the counter is always counting, when the value generated by the front amplifier is 1, the value of the counter will rise, otherwise it will fall, the count value It will also be given to the following DAC (including the ADC in the ADC). After the DAC receives the value, it will generate a voltage value, and input it to the inverting input of the amplifier, and compare it with the voltage of the non-inverting input. After a period of comparison, it will approach After that, the values ​​of the two will be close to balance. At this time, the value of the counter will be transmitted to a CPU, and this value is the final voltage value collected by the ADC.
insert image description here
The specific process is shown in the figure below:
insert image description here
Conversion time: The so-called conversion time is the time it takes for the two voltages of the amplifier to approach each other.
Why is there a fixed time in the ADC cycle besides the conversion time?
Because after the data conversion is completed, the data needs to be stored, generally 1.5 clock cycles.

STM32 DACs

The maximum resolution of STM32 DAC is 12 bits, which can be configured as 8 bits. The resolution here is different from the precision we usually say. If the precision is 12 bits, it means that 3.3V should be divided into 4096 How small it must be, so the resolution is not equal to the precision.

What is left-right alignment of data?
Since the AD/DA of STM32 is twelve-bit, and the data register is sixteen-bit, there will be extra bits, so the data can be left-aligned or right-aligned in the register.

STM32 ADC

The resolution of the ADC is twelve bits, which can be configured as 10, 6, and 8 bits, and the data supports left and right alignment.
Working mode:
Independent mode: STM32 has two or three ADCs. If only one of the ADCs is used and the others are not used, the mode at this time is called independent mode. Multiple mode
: Multiple ADCs of STM32 are used together
Single: each time The ADC needs to be turned on continuously for use
: there is no need to turn on the ADC repeatedly, and it will return to the first ADC acquisition after completing an ADC acquisition cycle, continuously.
Intermittent: This mode needs to be used when using multiple channels of the ADC to collect a data (not very clear) The
clock frequency of the ADC cannot exceed 14M on the F103 series, otherwise an error will occur in the CUBEMX configuration
. , including temperature, reference voltage, etc.

The relationship between sampling time and cycle: sampling time is not a cycle, and a fixed data storage time is also added.
In addition, the longer the sampling time, the more accurate the data, otherwise the monthly inaccuracy.

Use of ADCs

Common ground is required before collecting voltage, giant pit! ! ! !

blocking mode

Disadvantages: Occupies CPU, low efficiency
Steps to use:
step1: Start ADC The HAL_ADC_Start(ADC_HandleTypeDef* hadc);passed parameter is the handle structure of ADC, such as &hadc1. There are two main functions of this function, turn on the adc, turn on the adc conversion; there is also a function
HAL_ADC_Stop(ADC_HandleTypeDef* hadc);to turn off the adc module.
Step2: Query the EOC flag bit and let the CPU wait at this position until the acquisition is completed or the waiting time exceeds the set time. The parameter passed in through the function HAL_ADC_PollForConversion(ADC_HandleTypeDef* hadc, uint32_t Timeout);
is the adc handle structure, and there is a timeout waiting time
step3: start the acquisition of adc, read the data of the register

cubeMX configuration process

insert image description here

Because it is a single-channel polling method to collect data, so use the independent mode
Data alignment: Right-aligned
Scan mode: Off
Continuous mode: Off
Intermittent mode: Off
Regular conversion mode: Enable
Conversion channel: 1
Trigger mode: Software trigger (roughly Means to use function to trigger conversion)
conversion sequence: 1
injection channel: not enabled
Watchdog mode: not enabled (probably means that the high and low thresholds can be set, and the interrupt alarm will be triggered if it exceeds this range)

kile5

ADC initialization function

hadc1.Instance = ADC1;
  hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
  hadc1.Init.ContinuousConvMode = DISABLE;
  hadc1.Init.DiscontinuousConvMode = DISABLE;
  hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  hadc1.Init.NbrOfConversion = 1;
  if (HAL_ADC_Init(&hadc1) != HAL_OK)
  {
    
    
    Error_Handler();
  }

In the initialization function, all the configurations mentioned are concretely reflected.

float  Get_ADC_Value(void)
{
    
    
	HAL_ADC_Start (&hadc1 );//开启ADC转换
	HAL_ADC_PollForConversion (&hadc1 ,50);//EOC查询
	return HAL_ADC_GetValue (&hadc1 )*FACTO;//获取adc的值
}

The ADC value acquisition function in the polling mode, the return value of the function is the collected voltage value.
FACTO is a macro definition, and its function is to convert the collected value into a voltage. Principle: Since the ADC is 12-bit, the value corresponding to 3.3 V is 2 to the 12th power, which is 4096, so FACTO = 3.3 can be obtained by proportional operation / 4096 ;
Main function code:

  Vol_Value =  get_adc_value();
  printf("ADC1_IN1 VOL value: %.2fV\r\n",Vol_Value);
  HAL_Delay(50);

pay attention:

**Before turning on the multi-channel scanning mode, it is necessary to modify the conversion channel to the number of channels to be converted. If the number of conversion alternatives is one, the scanning or intermittent mode cannot be turned on.
insert image description here
Turn on the scan and intermittent mode in the polling mode
insert image description here
Here
insert image description here
is a means to group the ADC channels, for example, if there are two ADC channels, if it is set to 1, the channels are divided into two groups, that is to say, the number here Indicates how many channels are in each group.

In query mode, if you want to use multi-channel conversion, you need to enable the intermittent mode, otherwise the data will be inaccurate.

Interrupt mode**

Steps:
step1: Turn on the ADC, turn on the conversion, and enable the interrupt
step2: Query the EOC flag. In the interrupt mode, this step will not occupy the CPU, and it will be completed automatically.
Step3: Read the register data
Advantages: The CPU will not be occupied
Configuration process: and Compared with the query method, in this mode, the interrupt is mainly enabled, and the rest of the configuration is the same
insert image description here
Code:
After the initialization is completed, the interrupt needs to be enabled so that the system can enter the first interrupt

HAL_ADCEx_Calibration_Start(&hadc1);    //AD校准
HAL_ADC_Start_IT (&hadc1 );```
```c

Next is the interrupt callback function, which completes the data collection and restarts the interrupt inside the function.

void  HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
{
    
    
	ADC_Vlue = HAL_ADC_GetValue(&hadc1);   //获取AD值
	printf("ADC1 Reading : %d \r\n",ADC_Vlue);
	printf("PA3 True Voltage value : %.4f \r\n",ADC_Vlue*3.3f/4096);
	HAL_ADC_Start_IT (&hadc1 );
}

DMA mode

Process: step1: start ADC
step2: configure DMA buffer
step3: read data when needed

DMA configuration

insert image description here

insert image description here

software code

Turn on DMA calibration and ADAM function
insert image description here
to read data
insert image description here

Summary of the use of ADC:

The above content is only a single-channel ADC acquisition, and no more than two channels have been added. The STM32 ADC is powerful and has a variety of working modes. For other working modes, I will come back when I use them in the future. Bar! ! !

Guess you like

Origin blog.csdn.net/qq_52111026/article/details/120102101