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 (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 Channel | ADC1 | ADC2 | ADC3 |
---|---|---|---|
ADC channel 00 | PA0 | PA0 | PA0 |
ADC channel 01 | PA1 | PA1 | PA1 |
ADC channel 02 | PA2 | PA2 | PA2 |
ADC channel 03 | PA3 | PA3 | PA3 |
ADC channel 04 | PA4 | PA4 | PF6 |
ADC channel 05 | PA5 | PA5 | PF7 |
ADC channel 06 | PA6 | PA6 | PF8 |
ADC channel 07 | PA7 | PA7 | PF9 |
ADC channel 08 | PB0 | PB0 | PF10 |
ADC channel 09 | PB1 | PB1 | |
ADC channel 10 | PC0 | PC0 | PC0 |
ADC channel 11 | PC1 | PC1 | PC1 |
ADC channel 12 | PC2 | PC2 | PC2 |
ADC channel 13 | PC3 | PC3 | PC3 |
ADC channel 14 | PC4 | PC4 | |
ADC channel 15 | PC5 | PC5 | |
ADC channel 16 | Temperature sensor | ||
ADC channel 17 | Internal reference voltage |
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.
Module Wiring Diagram
xvoid Adc_Init(void)
{
ADC_InitTypeDef ADC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_ADC1 , ENABLE );
RCC_ADCCLKConfig(RCC_PCLK2_Div6);
//72M/6=12,ADC maximum time cannot exceed 14M
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure); // GPIOA.4
ADC_DeInit(ADC1); //Reset ADC1 and reset all registers of peripheral ADC1 to default values
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //ADC independent mode
ADC_InitStructure.ADC_ScanConvMode = DISABLE; //Single channel mode
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; //Single conversion mode
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;//The conversion is initiated by software rather than external triggers
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //ADC data right aligned
ADC_InitStructure.ADC_NbrOfChannel = 1; //The number of ADC channels that undergo regular conversion in sequence
ADC_Init(ADC1, &ADC_InitStructure); //Initialize the peripheral ADCx according to the specified parameters
ADC_Cmd(ADC1, ENABLE); //Enable specified ADC1
ADC_ResetCalibration(ADC1); //Enable reset calibration
while(ADC_GetResetCalibrationStatus(ADC1)); //Wait for the reset calibration to complete
ADC_StartCalibration(ADC1); //Enable AD calibration
while(ADC_GetCalibrationStatus(ADC1)); //Wait for calibration to complete
}
xxxxxxxxxx
int main(void)
{
bsp_init();
while(1)
{
voltage_warning();
}
}
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.