ADC acquisition

1、 Learning objectives

1.Understand the basic knowledge of ADC.

2.Learn the basic use of ADC and display the collected values on the serial port assistant.

ADC Introduction for STM32F1:

ADC (Analog to Digital Converter) is an analog-to-digital converter that can convert analog signals into digital signals. According to its conversion principle, it is mainly divided into three types: successive approximation type, double integration type, and voltage frequency conversion type.

The STM32 has 1 to 3 ADCs (the STM32F101/102 series only has 1 ADC), and these ADCs can be used independently or in dual mode (improving sampling rate). The STM32 ADC is a 12 bit successive approximation analog to digital converter. It has 18 channels and can measure 16 external and 2 internal signal sources. The A/D conversion of each channel can be performed in single, continuous, scan, or intermittent modes. The results of the ADC can be stored in a left aligned or right aligned 16 bit data register. The analog watchdog feature allows the application to detect if the input voltage exceeds a user-defined high/low threshold.

The STM32F103 series has a minimum of 2 ADCs, and the STM32F103C8T6 package we selected contains 2 12-bit ADCs. The maximum ADC conversion rate for STM32 is 1Mhz, which means that the conversion time is 1us (obtained when ADCCLK=14M and the sampling period is 1.5 ADC clocks). Do not allow the ADC clock to exceed 14M, otherwise the accuracy of the results will decrease. STM32 ADC performs only one conversion in a single conversion mode, which can be performed through ADC_ The ADON bit of the CR2 register (only applicable to regular channels) can be activated, or it can be activated through external triggers (applicable to regular channels and injection channels). This is when the CONT bit is 0. Taking a regular channel as an example, once the selected channel conversion is completed, the conversion results will be stored in the ADC_ In the DR register, the EOC (End of Conversion) flag will be set, and if EOCIE is set, an interrupt will be generated. The ADC will then stop until the next startup.

The ADC channel and GPIO correspondence table for STM32 is as follows:

ADC ChannelADC1ADC2ADC3
ADC channel 00PA0PA0PA0
ADC channel 01PA1PA1PA1
ADC channel 02PA2PA2PA2
ADC channel 03PA3PA3PA3
ADC channel 04PA4PA4PF6
ADC channel 05PA5PA5PF7
ADC channel 06PA6PA6PF8
ADC channel 07PA7PA7PF9
ADC channel 08PB0PB0PF10
ADC channel 09PB1PB1 
ADC channel 10PC0PC0PC0
ADC channel 11PC1PC1PC1
ADC channel 12PC2PC2PC2
ADC channel 13PC3PC3PC3
ADC channel 14PC4PC4 
ADC channel 15PC5PC5 
ADC channel 16Temperature sensor  
ADC channel 17Internal reference voltage  

 

Hardware construction

The potentiometer module and DuPont cable used in the ADC acquisition experiment need to be purchased separately, otherwise they lack important accessories and cannot complete this course. image-2023022700002

Module Wiring Diagram

3、 Program analysis

ADC initialization code:

Main function:

4、Experimental phenomenon

After downloading the program, configure the serial port assistant as shown in the figure below, adjust the potentiometer knob, and you can view the effect of real-time voltage changes between 0-3.3v in the serial port assistant receiving window.

image-2023022700003