STM32-ADC-PB0电位器

#include "project.h"
#include "adc.h"

void ADC_Initt(void)
{
    
    
		ADC_InitTypeDef ADC_InitStructure;
		GPIO_InitTypeDef GPIO_InitStructure;

		RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOC, ENABLE);//使能ADC1通道时钟

		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;							//PB0 作为模拟通道输入引脚    
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;						//模拟输入引脚
		GPIO_Init(GPIOB, &GPIO_InitStructure);
	
		ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;					//ADC工作模式:ADC1和ADC2工作在独立模式
		ADC_InitStructure.ADC_ScanConvMode = DISABLE;						//模数转换工作在单通道模式
		ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;					//模数转换工作在单次转换模式
		ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; //转换由软件而不是外部触发启动
		ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;				//ADC数据右对齐
		ADC_InitStructure.ADC_NbrOfChannel = 1;								//顺序进行规则转换的ADC通道的数目
		ADC_Init(ADC1, &ADC_InitStructure);									//根据ADC_InitStruct中指定的参数初始化外设ADCx的寄存器

		ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 1, ADC_SampleTime_13Cycles5);ADC1,ADC通道,采样时间为13.5周期

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

uint16_t Get_Adc(void)//设置指定ADC的规则组通道,一个序列,采样时间
{
    
    
		uint16_t value;
		ADC_SoftwareStartConvCmd(ADC1, ENABLE);								//使能指定的ADC1的软件转换启动功能	
		while(!ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC));						//等待转换结束
		return value = ADC_GetConversionValue(ADC1); 						//返回最近一次ADC1规则组的转换结果
}


猜你喜欢

转载自blog.csdn.net/weixin_43352501/article/details/103835692