Lanqiao Cup Embedded-ADC Related

No nonsense, just start:

Let me say one more thing. I have written a lot of precautions in the code, so everyone must read the code carefully, as are other articles.

Theoretical part

Basic knowledge:

ADC: Analog/digital conversion, that is, convert an analog signal into a digital signal representing a certain ratio of voltage value.

The ADC of the stm32103 series has 12-bit resolution, and the number of bits determines the accuracy. The more the number of bits, the higher the accuracy of the collected signal.

The clock frequency of ADC cannot exceed 14MHZ, otherwise it will lead to inaccurate acquisition.

The ADC channel group has regular channels and injection channels. And can realize single, multiple conversion and scan modes.

There are also some basic knowledge of ADC, please refer to the STM32 reference manual, I posted it here:
Insert picture description here
Basic knowledge of ADC, everyone understands more. The investigation of ADC in the Provincial Blue Bridge Cup is quite simple. Everyone continues to look down.

Configuration steps:

The investigation of the ADC on the CT117 development board is to measure the voltage value of the potentiometer, which is the blue thing next to the LED light. Connect via PB0. Therefore, only investigating one channel will not involve injection channels and multiple conversions, which is much simpler.

Insert picture description here
Configuration steps:

  1. Turn on the PB clock and ADC1 clock.
  2. Reset ADC1 and set ADC1 frequency division factor at the same time
  3. Initialize ADC1 parameters, set the working mode of ADC1 and related information of the regular channel.
  4. Enable ADC and perform calibration.
  5. Configure rule channel parameters
  6. Turn on software conversion
  7. Wait for the conversion to complete and read the ADC value.

Code part

void ADC_Config()
{
    
    
 GPIO_InitTypeDef GPIO_InitStructure;
 ADC_InitTypeDef  ADC_InitStructure;
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_ADC1,ENABLE);//开启所需IO口和所需ADC的时钟
 
 GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AIN;//模拟输入
 GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;
 GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
 GPIO_Init(GPIOB,&GPIO_InitStructure);
 
 ADC_DeInit(ADC1);//复位ADC1
 RCC_ADCCLKConfig(RCC_PCLK2_Div6);//设置ADC分频因子,最低必须是6分频。
 //为什么最低是六分频?因为ADC的时钟在APB2总线上面,之前说过时钟频率不能大于14MHZ,所以是72/6<14。
 
 ADC_InitStructure.ADC_ContinuousConvMode=DISABLE;//单次转换
 ADC_InitStructure.ADC_DataAlign=ADC_DataAlign_Right;//ADC右对齐
 ADC_InitStructure.ADC_ExternalTrigConv=ADC_ExternalTrigConv_None;
 ADC_InitStructure.ADC_Mode=ADC_Mode_Independent;//独立模式
 ADC_InitStructure.ADC_NbrOfChannel=1;//顺序
 ADC_InitStructure.ADC_ScanConvMode=DISABLE;//扫描模式
 ADC_Init(ADC1,&ADC_InitStructure);//ADC初始化
 
 ADC_RegularChannelConfig(ADC1,ADC_Channel_8,1,ADC_SampleTime_239Cycles5);//ADC规则通道配置

 ADC_Cmd(ADC1,ENABLE);

 ADC_ResetCalibration(ADC1); //使能复位校准 
 while(ADC_GetResetCalibrationStatus(ADC1)); //等待复位校准结束
 ADC_StartCalibration(ADC1);  //开启AD校准
 while(ADC_GetCalibrationStatus(ADC1));  //等待校准结束
}

float Read_ADC(void)//读取函数
{
    
    
 float ADC_VALUE;
 
 ADC_SoftwareStartConvCmd(ADC1,ENABLE);//开启软件转换
 while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC ));//等待转换完成
 ADC_VALUE = ADC_GetConversionValue(ADC1)*3.30/0xfff;//ADC是12位的寄存器,要把3.3V电压值分为0xfff份。

 return ADC_VALUE;
}

float ADC_Value_Average(void)//这个函数是对捕获得到的10个ADC值取一下平均,以使结果更准确
{
    
     
 int i = 0,j=0;
 
 float ADC_Value_Array[10] ={
    
    0};
 float ADC_Average = 0;
 for(i=0;i<10;i++)
 {
    
    
   ADC_Value_Array[i] = Read_ADC();
 }
 for(j=1;j<10;j++)
 {
    
    
  ADC_Value_Array[0] += ADC_Value_Array[j];
 }
 ADC_Average = ADC_Value_Array[0]/10;
 
 return ADC_Average;
}

1. You also need to master several functions, all of which can be found in the adc.h library function:
Insert picture description here
2. After the function is turned on by "ADC_SoftwareStartConvCmd(ADC_TypeDef* ADCx, FunctionalState NewState);", you need to wait for the end of the regular channel conversion. Read the value. In the reference manual (importance of the reference manual), you can find that the EOC flag position 1 is the conversion is completed. All written as in the code above.
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_43690936/article/details/105346470