stm32f051 adc单次软件选择循环采样

本文详细介绍了STM32微控制器中ADC模块的配置过程,包括使能GPIO与时钟、配置模拟输入引脚、初始化ADC参数、设置采样时间和启动ADC转换等步骤。适用于希望深入了解STM32 ADC使用的开发者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

void ADC1_Config(void)
{
  ADC_InitTypeDef     ADC_InitStruct;
  GPIO_InitTypeDef    GPIO_InitStruct;
	 
 /* Enable  GPIOA clock */ 
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);//使能GPIOA时钟
  /* ADC1 Periph clock enable */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);//使能ADC1时钟

  /* Configure PA.0 as analog input */
  GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;
  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AN;
  GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL  ;
  GPIO_Init(GPIOA, &GPIO_InitStruct);		//设置PA0为模拟输入,PA0对应的是通道0
  
  ADC总共19个通道,16个外部通道,一个温度,一个电压,还有一个自己内部的Vbat通道;
	
  /* ADC1 DeInit */  
  ADC_DeInit(ADC1);  
  /* Initialize ADC structure */
  ADC_StructInit(&ADC_InitStruct);
  /* Configure the ADC1 in continous mode withe a resolutuion equal to 12 bits  */
  ADC_InitStruct.ADC_Resolution = ADC_Resolution_12b;  //12位的分辨率
  ADC_InitStruct.ADC_ContinuousConvMode = ENABLE; //循环采样,意思就是在整个程序可以被进行多次转换,单次转换的话整个程序生命周期只能被触发一次
  ADC_InitStruct.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;软件触发
  ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right; 右对齐
  ADC_InitStruct.ADC_ScanDirection = ADC_ScanDirection_Upward; 
  ADC_Init(ADC1, &ADC_InitStruct); 
		
  ADC_ChannelConfig(ADC1, ADC_Channel_0, ADC_SampleTime_55_5Cycles); 设置采样通道,一定要把通道号和引脚的串口号对应起来
  
  /* ADC Calibration */
  ADC_GetCalibrationFactor(ADC1);   //adc校准
  ADC_Cmd(ADC1, ENABLE);     使能adc

  /* Wait the ADCEN falg */
  while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADEN)); 
  
  /* ADC1 regular Software Start Conv */ 
  ADC_StartOfConversion(ADC1);   开始ADC转换
}

ADCData[Num]=ADC_GetConversionValue(ADC1)      在实际程序中可以用这个函数进行采样


转载于:https://my.oschina.net/u/2252538/blog/650966

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值