STM-32-ADC analog-to-digital conversion (AD single-channel conversion)


ADC analog to digital conversion

This article mainly introduces STM32ADC analog-to-digital conversion, and makes a summary of the ADC resource peripherals in STM32, which can also be regarded as a review of previous knowledge, including AD single-channel acquisition and AD multi-channel acquisition.


1. ADC analog -> digital conversion

1. Basic introduction of ADC

ADC can convert analog signal into digital signal and is a bridge from analog circuit to digital circuit.

STM32 ADC

When the ADC reads and converts the I/O pin, it reads the voltage on the pin, converts it into a digital data, and stores it inADC->DR registerinside. that's itAnalog to Digital Conversion. We only need to read the value in the ADC-DR register to get the converted number. At the same time, an EOC conversion end flag bit is generated, and an interrupt can be triggered through this flag.

The following figure is the timing diagram of the ADC : as you can see,After the ADC conversion is completed, an ADC conversion flag bit EOC is generated.
insert image description here

Several important features:

  • 12-bit continuous approximation ADC . The successive approximation here refers to the working mode of the ADC. 12-bit refers to the resolution of the ADC. Generally, how many bits are used to represent the 12-bit AD value, and the corresponding range is 0-2^12-1. That is, 0-4095 , that is, the range represented by converting to a digital signal.
  • 1us conversion time . The conversion time is the conversion frequency, and 1us means that it takes 1us from the start of AD conversion to the generation of the result.
  • input voltage range ,0-3.3v. The result of the conversion is 0-4095

2. ADC input channel and two conversion units

  1. The ADC in STM32 has 18 input channels , including 16 external channels and 2 internal signal sources .

insert image description here

  • 16 external channels, that is, 16 GPIO ports, can be directly connected to the analog signal, and the pin can directly measure the voltage.
  • 2 internal signal sources including internal temperature sensor and internal reference voltage. The internal temperature sensor can measure the temperature of the cpu. The internal reference voltage is a reference voltage of about 1.2v, which does not change with the change of the external voltage.
  1. The two conversion units are the two conversion units of the rule group and the injection group .

The 16 external channels can be converted as a series of conversions in any order on any number of channels to form a group conversion.

  • A rule group consists of up to 16 transitions . That is, there are 16 channels in the rule group, and 16 channels can be continuously converted in the ADC continuous conversion mode. This group is the most commonly used.

And often used with DMA, that is, the data converted by the ADC is stored in the ADC->DR register. When the data of a channel is converted and stored in the DR register, the previous data is not read, and the next data will overwrite the previous data. . This results in loss of data.

  • The injection group has 4 channels, mostly used for emergencies , with a maximum of 4 channels.

You can see the figure below, which is the input channel and two conversion units of the ADC explained above.
insert image description here


3.ADC trigger source

ADC trigger source, including hardware trigger and software trigger .

  • Software trigger , software trigger can directly use software to trigger, that is, use library functions in software, soThe ADC is ready to start converting after using the software trigger. Generally, there is no special requirement, and software trigger is used.
  • Hardware triggering , including signals injected into groups and rule groups. For example, timer capture, external interrupt pin to trigger, and each channel of the timer, as well as the trigger signal caused by the output of TRGO's timer master mode.

4. ADC continuous conversion or single conversion, non-scanning or scanning mode

Continuous conversion or single conversion

Mainly to see after ADC conversionDo you need a trigger source to trigger the channel again?, for ADC conversion.

  • Continuous conversion means that after an ADC conversion is completed, it will not stop, but immediately start the next round of conversion, and then continue. That is, it only needs to be triggered once, and then it can be converted all the time.
  • For a single conversion , it needs to be triggered again after an ADC conversion is completed before the ADC conversion can continue.

Non-scan mode or scan mode

  • Non-scanning mode , in this mode, only the sequence 1 position of the converted channel is valid. That is, only the first channel is valid and can perform ADC conversion
  • In scan mode , the sequence X position is valid, and multiple sequences can be directly selected for ADC conversion, and can be repeated when selecting channels, that is, sequence X can be selected multiple times, and repeated channels can be selected in the sequence.

5. ADC data alignment

As we all know, the data register is a 16-bit register. And the ADC is 12 bits .

After the ADC conversion is completed, the generated data is a twelve-bit data. In this way, the value saved in the ADC->DR data register after the conversion is completed is a twelve-bit data. Therefore, whether the data is left-aligned or right-aligned is generated.

  • The data is left-aligned , and the ADC->DR data register has sixteen bits, that is, the high
    -order data is the converted ADC data, and the lower four-bits are 0
    .The corresponding DR is sixteen times larger than the actual result.
  • The data is right-aligned , the lower 12 of the ADC->DR data register is the data converted by the ADC, and the upper four bits are 0. The corresponding DR is the data converted by the ADC .Generally use data right alignment

6. ADC conversion time

The steps of ADC conversion:Sample -> Hold -> Quantize -> Encode

The time of ADC conversion is to set a sampling switch before quantization and encoding to collect external voltage. After the external voltage is stored, disconnect the sampling switch, and then perform AD conversion. (During the process of quantization and encoding, the voltage is always kept constant.)

Therefore, the time from turning on the sampling switch to collect the external voltage to turning off the sampling switch for AD conversion is the conversion time of the ADC.

where the total conversion time is:TCONV = sampling time + 12.5 cycles

When ADCCLK=14MHz, the sampling time is 1.5 cycles
TCONV = 1.5 + 12.5 = 14 cycles = 1μs


Second, the configuration of the ADC

After studying the above parts, I have an overall understanding of the ADC. The following is how to configure the ADC and write a program to realize the ADC analog-to-digital conversion.

1. RCC turns on the clock

First of all, we need to turn on the ADC, the ADC frequency division clock ADCCLK, and the GPIO corresponding to the selected channel.

	RCC_ADCCLKConfig(RCC_PCLK2_Div6);  //6分频
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);  //ADC分频时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);  //开启GPIOA时钟

Note that the GPIO port corresponding to the ADC should be configured according to the pin definition diagram in the manual.
insert image description here


2. Configure GPIOs

Here I choose channel 6 of ADC1, and the corresponding GPIO port is GPIOA6

The mode of the pin here should be configured as an analog input

	GPIO_InitTypeDef GPIO_InitStruct;
	GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AIN;   	//模拟输入
	GPIO_InitStruct.GPIO_Pin=GPIO_Pin_6;		//GPIOA6
	GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz; 
	GPIO_Init(GPIOA,&GPIO_InitStruct);

3. Select the input channel of the rule group or injection group

Select the input channel of the rule group. Here I choose ADC1, channel 6 of ADC1, and the sampling clock is 55. When configuring other channels, the same is true.

//配置ADC转换通道  选择ADC1通道6 序列1  采样时钟为55
	ADC_RegularChannelConfig(ADC1,ADC_Channel_6,1,ADC_SampleTime_55Cycles5);

4. Configure ADC initialization

ADC initialization is done through ADC_Init().

Let's talk about these parameters

  • ADC_ContinuousConvMode, Does the ADC convert continuously? Optional single conversion (DISABLE) or continuous conversion (ENABLE).
  • ADC_DataAlign, the transformed data alignment. Optional left or right alignment.
  • ADC_ExternalTrigConvWhether to trigger the ADC externally, hardware trigger or software trigger can be selected.
  • ADC_Mode, ADC mode, we generally use a single ADC conversion. Of course, dual ADCs can also be used for conversion.
  • ADC_ScanConvMode, is the ADC scanning continuously? The continuous scanning of ADC can be used in conjunction with DMA, and the effect is excellent! stay in the next part.
  • ADC_NbrOfChannel, specify how many channels will be used in scan mode. This parameter defaults to sequence 1 in non-scanning mode. This parameter is only useful in scan mode.
//ADC初始化
	ADC_InitTypeDef ADC_InitStruct;
	ADC_StructInit(&ADC_InitStruct);
	ADC_InitStruct.ADC_ContinuousConvMode=ENABLE;		//连续转换 否 单次转换
	ADC_InitStruct.ADC_DataAlign=ADC_DataAlign_Right;   //右对齐
	ADC_InitStruct.ADC_ExternalTrigConv=ADC_ExternalTrigConv_None;   //外部中断否
	ADC_InitStruct.ADC_Mode=ADC_Mode_Independent;   //单ADC触发
	ADC_InitStruct.ADC_NbrOfChannel=6;    			//选择ADC1的6号通道
	ADC_InitStruct.ADC_ScanConvMode=DISABLE;			//连续扫描  否 单次扫描
	ADC_Init(ADC1,&ADC_InitStruct);

5. Switch control ADC

Turn on the ADC, even if the ADC is enabled

//ADC使能
	ADC_Cmd(ADC1,ENABLE);

6. Enable software trigger ADC

Generally, the software is selected to trigger the ADC, and the library function can be used directly here.

//软件触发ADC
ADC_SoftwareStartConvCmd(ADC1,ENABLE);

7. Calibrate the ADC

Calibration greatly reduces quasi-accuracy errors due to variations in the internal capacitor bank

	//校准ADC
	ADC_ResetCalibration(ADC1);
	while(ADC_GetResetCalibrationStatus(ADC1)==SET);
	ADC_StartCalibration(ADC1);
	while(ADC_GetCalibrationStatus(ADC1)==SET); 

8. Read ADC converted data

After the above configuration, the ADC reads the analog input of the port, converts it into a digital, and stores it in the ADC->DR register. We only need to read the DR register to know the value converted by the ADC.

uint16_t AD_GetValue(void)
{
    
    
	while(ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC)==RESET);	//规则组转换完成标志位 EOC为RESET时 转换未完成 EOC为空,执行空循环
	return ADC_GetConversionValue(ADC1);
}


Phenomena of testing and procedures

I choose to debug the data on the serial port, that is, send the converted data to the serial port assistant, and read the AD converted data.

For specific articles about using the serial port, you can read my previous article: STM32-Serial Port Communication (Receiving and Sending of the Serial Port)

procedural phenomenon

It can be seen that the value after AD conversion fluctuates and jumps.
insert image description here


3. Summary

The above is the relevant content about ADC analog-to-digital conversion, which is a summary after my own study.

The function of ADC conversion is very powerful, and it is also very convenient to use

Mainly clarify the following points

1. ADC的通道,根据引脚定义表来选择
2. ADC的触发源,软件触发or硬件触发
3. ADC的模式选择,单次转换or连续转换。扫描模式or非扫描模式。
4. EOC标志位的理解

The writing is hasty, if there is any mistake, welcome to correct me.

Guess you like

Origin blog.csdn.net/cyaya6/article/details/127812315