其他分享
首页 > 其他分享> > #蓝桥杯嵌入式#ADC的配置

#蓝桥杯嵌入式#ADC的配置

作者:互联网

参考资料:
      蓝桥杯嵌入式备赛手册
      蓝桥杯嵌入式开发经验分享(7.ADC的配置)
总目录:目录

文章目录

硬件连接

一路AD采样的软件驱动

初始化函数

ADC读取函数

adc.c

#include "adc.h"

void adc_Init()
{
  ADC_InitTypeDef ADC_InitStructure;
  GPIO_InitTypeDef GPIO_InitStructure;
	
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOB, ENABLE);

  /* Configure PC.04 (ADC Channel14) as analog input -------------------------*/
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
  GPIO_Init(GPIOC, &GPIO_InitStructure);
  
  /* ADC1 configuration ------------------------------------------------------*/
  ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
  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_InitStructure.ADC_NbrOfChannel = 1;
  ADC_Init(ADC1, &ADC_InitStructure);

  /* ADC1 regular channel14 configuration */ 
  ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 1, ADC_SampleTime_55Cycles5);
  
  /* Enable ADC1 */
  ADC_Cmd(ADC1, ENABLE);

  /* Enable ADC1 reset calibration register */   
  ADC_ResetCalibration(ADC1);
  /* Check the end of ADC1 reset calibration register */
  while(ADC_GetResetCalibrationStatus(ADC1));

  /* Start ADC1 calibration */
  ADC_StartCalibration(ADC1);
  /* Check the end of ADC1 calibration */
  while(ADC_GetCalibrationStatus(ADC1));
}

u16 get_ADC(void)
{
	ADC_SoftwareStartConvCmd(ADC1, ENABLE);
	while(!ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC));
	return ADC_GetConversionValue(ADC1);
}

main.c

	adc_Init();
	while(1)
	{
		value = 3.3 * get_ADC() / 4096;
		sprintf((char*)ADC, "PB0 Voltage:%.2f", value);
		LCD_DisplayStringLine(Line4 ,ADC);
		Delay_Ms(500);
	}

标签:AD,嵌入式,蓝桥,InitStructure,ADC,ADC1,GPIO,adc
来源: https://blog.csdn.net/weixin_43476037/article/details/114852669