標(biāo)題: STM32F303--ADC簡單應(yīng)用電壓檢測 [打印本頁]

作者: wu317417    時間: 2019-1-22 19:34
標(biāo)題: STM32F303--ADC簡單應(yīng)用電壓檢測
本帖最后由 wu317417 于 2019-1-22 19:44 編輯

ADC 作為模擬量檢測有很多應(yīng)用場合,最近用來做一個電壓檢測。

比較麻煩的是網(wǎng)上好像沒有STM32F303VE這方面的詳細資料。只好自己研究做個簡單的應(yīng)用。

PC4,PC5作為輸入檢測腳。對應(yīng)的ADC通道是PC4----ADC2_IN5,PC5----ADC2_IN11

直接來代碼吧,有空再整理下:
void ADC_Initpc4_pc5(void){
    ADC_InitTypeDef         ADC_InitStructure;
    ADC_CommonInitTypeDef   ADC_CommonInitSt;
    GPIO_InitTypeDef        GPIO_InitStructure;

    //初始化始終和    GPIO
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_ADC12|RCC_AHBPeriph_GPIOC, ENABLE);
    // RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOC, &GPIO_InitStructure);

    /* ADC 時鐘配置為 72M/8 */
    RCC_ADCCLKConfig(RCC_ADC12PLLCLK_Div8);

    ADC_StructInit(&ADC_InitStructure);

    /* Calibration procedure */
    ADC_VoltageRegulatorCmd(ADC2, ENABLE);
    ADC_SelectCalibrationMode(ADC2, ADC_CalibrationMode_Single);
    ADC_StartCalibration(ADC2);

    while(ADC_GetCalibrationStatus(ADC2) != RESET);
    ADC_GetCalibrationValue(ADC2);

    ADC_CommonInitSt.ADC_Mode = ADC_Mode_Independent;
    ADC_CommonInitSt.ADC_Clock = ADC_Clock_AsynClkMode;
    ADC_CommonInitSt.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
    ADC_CommonInitSt.ADC_DMAMode = ADC_DMAMode_OneShot;
    ADC_CommonInitSt.ADC_TwoSamplingDelay = 0;

    ADC_CommonInit(ADC2, &ADC_CommonInitSt);

    ADC_InitStructure.ADC_ContinuousConvMode = ADC_ContinuousConvMode_Disable;
    ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
    ADC_InitStructure.ADC_ExternalTrigConvEvent = ADC_ExternalTrigConvEvent_0;
    ADC_InitStructure.ADC_ExternalTrigEventEdge = ADC_ExternalTrigEventEdge_None;
    ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
    ADC_InitStructure.ADC_OverrunMode = ADC_OverrunMode_Disable;
    ADC_InitStructure.ADC_AutoInjMode = ADC_AutoInjec_Disable;
    ADC_InitStructure.ADC_NbrOfRegChannel = 1;

    ADC_Init(ADC2, &ADC_InitStructure);

    ADC_Cmd(ADC2, ENABLE);
    /* wait for ADRDY */
    while(!ADC_GetFlagStatus(ADC2, ADC_FLAG_RDY));
}



















歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1