Embedded Blue Bridge Cup ADC program configuration process and sampling examples

Blue Bridge Cup Embedded ADC Configuration

First of all, remember that the pin and the corresponding function
ADC1 pin is the 8th channel of PB0 ADC1
Insert picture description here

Then we visit the official firmware library to find the file corresponding to the configuration ADC. The
path is as follows
STM32 firmware library v3.5\STM32F10x_StdPeriph_Lib_V3.5.0\Project\STM32F10x_StdPeriph_Examples\ADC\ADC1_DMA\main.c
we take their main.c ADC1 configuration content and place Come in,
put it in the initialization function that we edited ourselves

#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);
}

Then put the structure into

ADC_InitTypeDef ADC_InitStructure;

Then we have to make some changes. The
official is to initialize the ADC_14 channel

  ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 1,

We want to change to ADC_8 channel

  ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 1,

Then we need to initialize the GPIO port we use PB0 APB2 clock,
which can be found in the source file

  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);

Change to 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);

Then don't forget the clock

RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOB, ENABLE);

How do we get this value in the end?
First, we define a 16-bit variable
and then we use an official already defined function
ADC_GetConversionValue (ADC1)
This function is hard to remember if you want to write it down to remember in front of middle and Value Get a little note of it is probably just fine Conversion
designed to Take the ADC value,
we use this function to assign it to him

		ADC_value=ADC_GetConversionValue(ADC1);

Then we can get the value of our ADC.
Below is the complete code
adc.c code

#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);
}

Below is the code of ADC.H

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

#endif


Main function code

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);
      } 
	
}

Guess you like

Origin blog.csdn.net/m0_46179894/article/details/108192980