GD32 ADC detection voltage

 

The reference voltage of GD32 is not found in the specification, but due to the "certain relationship" between GD32 and STM32. . . The internal reference voltage of STM32 is 1.2V, so GD32 is also 1.2V. (Finally, it is found through calculation that it should be 1.2V)

 

The value output by the ADC is a digital quantity without a unit.

The maximum digital quantity of a 12-bit ADC is 4096, so the ADC output value can only be between 0 and 4096

ADC resolution 12 bits = 4096, sampling reference power supply is 3.3V, so from 0-3.3V according to the resolution calculation, the sampling value per bit is (3.3/4096)

Today, I will give you a brief introduction to the common sense of ADC devices.

 

ADC, analog-to-digital converter, the function is to convert analog voltage into digital.

 

The concept sounds vague, let’s be practical: connect the line of the voltage you want to measure to the pin used to test the voltage of the ADC, the ADC module will detect this voltage and automatically convert it into a number. We read this number, and then know the corresponding relationship between this number and the voltage, we can know the current voltage.

 

Some single-chip microcomputers have ADC modules inside, and ADC pins for testing are drawn outside the single-chip microcomputer.

Some single-chip microcomputers do not have an ADC module, and a separate ADC chip can be used. A single ADC chip generally has an interface for communicating with the single-chip microcomputer. The common ones are 8-bit parallel port, I2C port, and SPI port. The single-chip microcomputer is connected with the external ADC module through the communication interface to read the conversion value of the ADC chip.

Resolution

Whether it is the ADC inside the microcontroller or the independent ADC chip, there is a resolution index.

The general resolutions are 8-bit, 10-bit, 12-bit, 16-bit, and 24-bit.

We must first know that a bit is a bit, which is the smallest unit in the computer world, and 8 bits are 1 byte.

8 digits, the maximum value is 255, and the range is from 0 to 255.

With 10 digits, the maximum value is 1023, and the range is from 0 to 1023.

With 12 digits, the maximum value is 4095, and the range is from 0 to 4095.

14 digits, the maximum value is 16383, and the range is from 0 to 16383.

16-digit number, the maximum value is 65535, and the range is from 0 to 65535.

24 digits, the maximum value is 16777215, and the range is from 0 to 16777215.

Why can "bits" represent resolution?

Suppose we want to measure a voltage signal of 0~5V.

Measured with an 8-bit resolution ADC, when the input voltage is 0V, the number obtained is 0, and when the input voltage is 5V, the number obtained is 255.

Measured with a 12-bit resolution ADC, when the input voltage is 0V, the number obtained is 0, and when the input voltage is 5V, the number obtained is 4095.

For an 8-bit ADC, every time the number obtained increases by 1, the voltage actually increases by 5/256=0.0195V.

For a 16-bit ADC, every time the number obtained increases by 1, the voltage actually increases by 5/4096=0.0012V.

It can be seen that there is a difference in voltage resolution between 8-bit and 16-bit. For example, when I measure voltage, I use 8-bit and 16-bit to test respectively, and the numbers obtained are all 1, which is less than 0.0195 for 8-bit. V may all be 1, but if the number measured by the 16-bit ADC is 1, then we can know that the voltage is about 0.0012V, is it very accurate. So the resolution to use depends on your hardware conditions and your sampling accuracy requirements

 

For another example, we know that the accurate value of the current voltage to be measured is 0.015V, then the number we get with an 8-bit ADC is 1. When you get 1, you can only think of it as 1*5/256=0.0195V The voltage.

Then if you use a 12-bit ADC to measure the voltage of 0.015V, the number you get is 12 or 13. If it is 12, we convert it, and it will be considered as 12*5/4096=0.0147V, if the number is 13 If we turn it around, we think it is 13*5/4095=0.0159V.

 

Comparing the numbers, you can see that the higher the resolution, the more accurate the voltage can be measured.

========================================================================================================================================

The following is a summary of STM32 in the blog of the netizen I found. It seems that the ADC of GD32 and STM32 are very similar.

There is no SPEC of STM32, here I posted the part of the internal reference voltage in the SPEC of GD32:

 

 

 

 

Our project needs to simply test the power supply voltage Vbat, the circuit is like this. (Here only pay attention to PA7 and PA6)

 

 

 

The multiplexing function of PA7 is ADC0

Look at the functions of PA7's PIN:

 

Default is PA7 which is GPIO pin

Alternat function has ADC01_17, which can be used as the 7th Channel of ADC0 or ADC1

Therefore, to enable the PA7 PIN function, the following work needs to be done:

 /* enable GPIOA clock */
    rcu_periph_clock_enable(RCU_GPIOA);
	rcu_periph_clock_enable(RCU_AF);/* 这里AF就是指我需要用Alternate Function的意思,复用的功能的Clock要打开 */
	/* enable ADC1 clock */
    rcu_periph_clock_enable(RCU_ADC1);
    /* config ADC clock */
    rcu_adc_clock_config(RCU_CKADC_CKAPB2_DIV4);
	/* config the GPIO as analog mode */
    gpio_init(GPIOA, GPIO_MODE_AIN, GPIO_OSPEED_MAX, GPIO_PIN_7); /* GPIO_MODE_AIN 配置为Analog In mode */

 

The configuration of PA6 is relatively simple

	/* enable GPIOA clock */
    rcu_periph_clock_enable(RCU_GPIOA);
	/* configure PA6 GPIO port */ 
	gpio_init(GPIOA, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_6);
	/* set PA6 output */
	gpio_bit_set(GPIOA,GPIO_PIN_6);

 

The configuration in the middle is not mentioned, there are still more:

Just talk about the final calculation method:

/* 
* 这个Vref_adc 表示的是1.2V的参考电压,用当前的分辨率测试到的ADC值是这么多 ,
*/
Vref_adc =adc_regular_channel_data_read(ADC0, ADC_CHANNEL_17);		

/*
* 这个Pin7_adc 表示的是Vbat/2的参考电压,用当前的分辨率测试到的ADC值是这么多,
* 所以采用比例的方式  1.2V : Vref_adc = Vbat/2 : Pin7_adc
* Vbat /2 = (1.2 * Pin7_adc) / Vref_adc
* Vbat = 1.2 * Pin7_adc * 2 / Vref_adc
*/
Pin7_adc =adc_regular_channel_data_read(ADC1, ADC_CHANNEL_7);

Vbat = 1.2 * Pin7_adc * 2 / Vref_adc

Regarding this correspondence, netizens have an explanation: Use ST MCU internal reference voltage to monitor power supply voltage and others-STM32/8 (51hei.com)

The following is how netizens calculate SMT32 to obtain ADCpin voltage from internal reference voltage. It can be seen that they are all the same.

 

ADC reference voltage in STM32

Each STM32 chip has an internal reference voltage, which is equivalent to a standard voltage measurement point, which is connected to channel 17 of ADC1 inside the chip.

According to the data in the data sheet, the typical value of this reference voltage is 1.20V, the minimum value is 1.16V, and the maximum value is 1.24V. This voltage basically does not change with the external power supply voltage.

Many people confuse this reference voltage with the ADC reference voltage. The ADC reference voltage is provided through Vref+. For models with 100 pins or more, Vref+ is led off-chip, and the pin name is Vref+; for models with 64 pins and less than 64 pins, Vref+ is connected to the VCC signal line inside the chip, and not led outside, so the AD reference voltage is VCC On the voltage.

When the ADC's external reference voltage fluctuates or VCC changes because Vref+ is connected to VCC inside the chip, if the accuracy of the ADC measurement is not high, this internal reference voltage can be used to obtain the voltage value measured by the ADC.

The specific method is to read the ADC measurement value of the reference voltage before measuring the voltage value of a certain channel and record it as ADrefint; then read the ADC conversion value of the channel to be measured and record it as ADchx; then the voltage to be measured is:

Vchx = Vrefint * (ADchx/ADrefint)

Among them, Vrefint is the reference voltage = 1.20V (STM32F107).

How to use VDDA as the ADC reference voltage, when the measured signal voltage exceeds this range, you can use precision resistor divider or amplifier divider, or choose a suitable external voltage reference chip.

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/yangkunhenry/article/details/112757855