嵌入式蓝桥杯ADC程序配置过程及采样实例

蓝桥杯嵌入式ADC配置

首先还是要记住引脚 和对应的功能
ADC1引脚是PB0 ADC1的第8个通道
在这里插入图片描述

然后我们拜访官方给的固件库 找到配置ADC相应的文件
路径如下
STM32固件库v3.5\STM32F10x_StdPeriph_Lib_V3.5.0\Project\STM32F10x_StdPeriph_Examples\ADC\ADC1_DMA\main.c
我们取他们的main.c 的ADC1 configuration 内容放置进来
放入我们自己编辑好初始化函数中

#include adc.h
void my_adc_init()
{
    
    

  /* ADC1 configuration ------------------------------------------------------*/
  ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
  ADC_InitStructure.ADC_ScanConvMode = ENABLE;
  ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
  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_14, 1, ADC_SampleTime_55Cycles5);

  /* Enable ADC1 DMA */
  ADC_DMACmd(ADC1, ENABLE);
  
  /* 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));
     
  /* Start ADC1 Software Conversion */ 
  ADC_SoftwareStartConvCmd(ADC1, ENABLE);
}

然后接着把结构体放入

ADC_InitTypeDef ADC_InitStructure;

然后我们要稍微做一些更改
官方是初始化的是ADC_14通道

  ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 1,

我们要改成ADC_8通道

  ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 1,

然后我们要初始化GPIO口 我们用的PB0 APB2时钟
在源文件就可以找到的

  GPIO_InitTypeDef GPIO_InitStructure;

  /* Configure PC.04 (ADC Channel14) as analog input -------------------------*/
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
  GPIO_Init(GPIOC, &GPIO_InitStructure);

改成PB0

  GPIO_InitTypeDef GPIO_InitStructure;

  /* Configure PC.04 (ADC Channel14) as analog input -------------------------*/
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
  GPIO_Init(GPIOB, &GPIO_InitStructure);

然后不要忘记时钟

RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOB, ENABLE);

最后我们怎么得到这个值呢?
首先我们定义一个16位的变量
然后我们用一个官方已经定义好的函数
ADC_GetConversionValue(ADC1)
这个函数要记下来 如果难记 就记住前面Get 和Value 中间稍微记一下大概是Conversion就好了
专门用来取ADC的值
我们用这个函数赋给他

		ADC_value=ADC_GetConversionValue(ADC1);

然后就可以得到我们ADC的值
以下是完整代码
adc.c 代码

#include "adc.h"
void My_ADC_Init()
{
    
    
	 GPIO_InitTypeDef GPIO_InitStructure;
	 ADC_InitTypeDef ADC_InitStructure;
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOB, ENABLE);
  /* Configure PB0 (ADC Channel8) as analog input -------------------------*/
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
  GPIO_Init(GPIOB, &GPIO_InitStructure);

	  /* ADC1 configuration ------------------------------------------------------*/
  ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
  ADC_InitStructure.ADC_ScanConvMode = ENABLE;
  ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
  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 channel18 configuration */ 
  ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 1, ADC_SampleTime_55Cycles5);

  /* Enable ADC1 DMA */
  ADC_DMACmd(ADC1, ENABLE);
  
  /* 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));
     
  /* Start ADC1 Software Conversion */ 
  ADC_SoftwareStartConvCmd(ADC1, ENABLE);
}

以下是ADC.H的代码

#ifndef __ADC_H
#define __ADC_H
#include "stm32f10x.h"
void My_ADC_Init(void);

#endif


主函数代码

int main(void)
{
    
    

		TIM2_Config();
		SysTick_Config(SystemCoreClock/1000);
		LED_Init();
		BEEP_Init();
		TIM2_Config();
		STM3210B_LCD_Init();
		USART_Config();
		LCD_Clear(Blue);
		TIM3_PWM_Init();
		LCD_SetBackColor(Yellow);
		LCD_DisplayStringLine(Line0,"    hello   world       ");
		RTC_Configuration();
		Time_Adjust(23,59,55);
		printf("hello world");
		My_ADC_Init();
    while(1)
		{
    
    
			ADC_value=ADC_GetConversionValue(ADC1);
			printf("ADC = %d\n",ADC_value);
      } 
	
}

猜你喜欢

转载自blog.csdn.net/m0_46179894/article/details/108192980