STM32F103VET6——ADC single channel interrupt reading experiment explanation

Publisher:乘风翻浪Latest update time:2019-01-29 Source: eefocusKeywords:STM32F103VET6  ADC Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Programming Tips

Define

GPIO

ADC_InitTypeDef

ADC Clock

Channel conversion order and sampling time

Enable ADC conversion complete interrupt

Enable ADC conversion start

Calibrating the ADC

Select ADC conversion trigger source

Configuring interrupt priority

Writing an interrupt service function

main function

/*-----GPIO-----*/

static void ADC_GPIO_Config(void)

{

    GPIO_InitTypeDef GPIO_InitStructure;

    ADC_GPIO_APBxClock_FUN ( ADC_GPIO_CLK, ENABLE );


    GPIO_InitStructure.GPIO_Pin = ADC_PIN; //Must be analog input

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;

    GPIO_Init(ADC_PORT, &GPIO_InitStructure);               

}



/*-----ADC_InitTypeDef-----*/

static void ADCx_Mode_Config(void)

{

    ADC_InitTypeDef ADC_InitStructure;  


    // Turn on the ADC clock

    ADC_APBxClock_FUN ( ADC_CLK, ENABLE );


    //ADC mode configuration

    // Only one ADC is used, which is independent mode

    ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;


    // Disable scanning mode, only required for multi-channel, not required for single channel

    ADC_InitStructure.ADC_ScanConvMode = DISABLE ; 


    // Continuous conversion mode

    ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;


    // No external trigger conversion required, just start the software

    ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;


    //Conversion result is right aligned

    ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;


    //Convert channel 1

    ADC_InitStructure.ADC_NbrOfChannel = 1; 


    // Initialize ADC

    ADC_Init(ADCx, &ADC_InitStructure);


    // Configure the ADC clock to be divided by 8 of PCLK2, i.e. 9MHz

    RCC_ADCCLKConfig(RCC_PCLK2_Div8); 


    //Configure ADC channel conversion order and sampling time

    ADC_RegularChannelConfig(ADCx, ADC_CHANNEL, 1, 

                             ADC_SampleTime_55Cycles5);


    // ADC conversion ends and generates an interrupt. Read the conversion value in the interrupt service routine

    ADC_ITConfig(ADCx, ADC_IT_EOC, ENABLE);


    // Turn on ADC and start conversion

    ADC_Cmd(ADCx, ENABLE);


    // Initialize ADC calibration register  

    ADC_ResetCalibration(ADCx);

    // Wait for calibration register initialization to complete

    while(ADC_GetResetCalibrationStatus(ADCx));


    //ADC starts calibration

    ADC_StartCalibration(ADCx);

    // Wait for calibration to complete

    while(ADC_GetCalibrationStatus(ADCx));


    // Since no external trigger is used, the ADC conversion is triggered by software 

    ADC_SoftwareStartConvCmd(ADCx, ENABLE);

}



/*-----NVIC-----*/

static void ADC_NVIC_Config(void)

{

  NVIC_InitTypeDef NVIC_InitStructure;

  // Priority grouping

  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);


  //Configure interrupt priority

  NVIC_InitStructure.NVIC_IRQChannel = ADC_IRQ;

  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;

  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;

  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

  NVIC_Init(&NVIC_InitStructure);

}


/*-----Heat-----*/

void ADCx_Init(void)

{

    ADCx_GPIO_Config();

    ADCx_Mode_Config();

    ADC_NVIC_Config();

}



/*-----Interrupt service subfunction-----*/

void ADC_IRQHandler(void)

{   

    if (ADC_GetITStatus(ADCx,ADC_IT_EOC)==SET) 

    {

        // Read the conversion value of ADC

        ADC_ConvertedValue = ADC_GetConversionValue(ADCx);

    }

    ADC_ClearITPendingBit(ADCx,ADC_IT_EOC);

}


Keywords:STM32F103VET6  ADC Reference address:STM32F103VET6——ADC single channel interrupt reading experiment explanation

Previous article:STM32 basic design (1) --- Light up the LED
Next article:Voltage display based on STM32F103

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号