STM32 ADC single channel acquisition (STM32F103C8T6 ADC1 channel 0)

// STM32F103C8T6 ADC1 channel has the channel 10, are:
@ 10 feet ADC12_IN0 the PA0
@. 11 feet ADC12_IN1 PAl
@ 12 is ADC12_IN2 pin PA2
@ 13 is ADC12_IN3 pin PA3
@ 14 ADC12_IN4 pin PA4
@ 15 feet ADC12_IN5 PA5
/ / 16 feet ADC12_IN6 of PA6
//. 17 feet ADC12_IN7 PA7
// pin 18 is ADC12_IN8 PA8
//. 19 feet ADC12_IN9 PA9
// ADC12 intended to be formulated ADC1 or ADC2, not a ADC12
// the following procedure is main.c;

#include "stm32f10x.h"
#include "stm32f10x_tim.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_adc.h"

float adc_data = 0, dyzh = 0 ; // acquisition value of the variable, the value of the variable voltage display
// quoted, please indicate the source https://www.cnblogs.com/beiyhs/p/12320695.html ** north of Han Shan ***

void ADC_GPIO_Init (void) // ADC- GPIO input pins, 10 may be provided in this external input channels
{
RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOA, the ENABLE); // enable AHB prescaler to port A of switch
// RCC_APB2PeriphClockCmd ( RCC_APB2Periph_AFIO, ENABLE);

GPIO_InitTypeDef GPIO_InitStructure; // declare variable structure
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1; // define PA0, PA1 input pin of the AD
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; // the IO port of the analog input mode
//GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; // AD input port is provided in a floating
GPIO_Init (GPIOA, & GPIO_InitStructure); // A port according to the above parameters A0 / A1
}

// **************************** ********************************************
void ADC1_Init (void) / / parameters and configure the ADC acquisition
{

RCC_APB2PeriphClockCmd (RCC_APB2Periph_ADC1, ENABLE); // enable AHB peripheral ADC1 prescaler to switch
RCC_ADCCLKConfig (RCC_PCLK2_Div8); // clock divider 72M / 8 = 9M does not exceed the maximum clock 14M
ADC_DeInit (ADC1); // the ADC reset
ADC_DMACmd (ADC1, DISABLE); // Disable DMA

ADC_InitTypeDef ADC_InitStuctrue; // declare ADC structure variable
ADC_InitStuctrue.ADC_Mode = ADC_Mode_Independent; // ADC standalone mode
ADC_InitStuctrue.ADC_ScanConvMode = DISABLE; // whether to scan _ Single Channel
ADC_InitStuctrue.ADC_ContinuousConvMode = DISABLE; // if _ consecutive single
ADC_InitStuctrue.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; // do not have an external trigger, the trigger conversion software
ADC_InitStuctrue.ADC_DataAlign = ADC_DataAlign_Right; // data is right-aligned
ADC_InitStuctrue.ADC_NbrOfChannel = 1; // _ number of channels a single channel structure _ADC-
//ADC_InitStuctrue.ADC_ScanDirection=ADC_ScanDirection_Backward; // data cover / Browse direction
ADC_Init (ADC1, & ADC_InitStuctrue); // set by the above parameter ADC1

ADC_RegularChannelConfig (ADC1, ADC_Channel_0,. 1, ADC_SampleTime_1Cycles5);
// ADC peripheral configuration and content of specific docking function (ADC port. 1, ADC channel 0, transformation number - of a conversion, the conversion period)
ADC_Cmd (ADC1, the ENABLE) ; // enable ADC1
ADC_ResetCalibration (ADC1); // register reset ADC calibration function
while (ADC_GetResetCalibrationStatus (ADC1)); // wait for the completion of step
ADC_StartCalibration (ADC1); // start specified ADC calibration state
while (ADC_GetCalibrationStatus (ADC1 )); // wait for the completion of the previous step
}
// ************************************** ************************************************** **
void Delay_ms (void)
{
  unsigned char I, J;
  for(i=255;i>0;i--)
     {
      for(j=250;j>0;j--);
     }
}

// **************************** ******************************************
// ****** ************************************************** **********************************
int main (void)
{
  ADC_GPIO_Init (); // ADC configuration IO port
  ADC1_Init (); // ADC configuration parameters and acquisition

  the while (. 1)
   {
   Delay_ms ();
   ADC_SoftwareStartConvCmd (ADC1, the ENABLE); // Software trigger switch _ADC - switch status register is 0
   the while (ADC_GetFlagStatus (ADC1, ADC_FLAG_EOC) == the RESET); // wait for the ADC to complete
   adc_data = ADC_GetConversionValue (ADC1); // ADC data is the data obtained

   dyzh = (adc_data * 3.27) / 4095; // converts the voltage value is acquired
                                                                               // reference voltage 3.27V, 12-bit resolution 4095
                                                                               // This can set breakpoints, see above dyzh value
    }
}

Guess you like

Origin www.cnblogs.com/beiyhs/p/12320695.html