stm32 ADC 非DMA方式
C++ Code
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | voidadcGPIOConfig(void) { GPIO_InitTypeDefgpioInitStruct; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//使能PortA時鐘 gpioInitStruct.GPIO_Speed=GPIO_Speed_2MHz; gpioInitStruct.GPIO_Pin=GPIO_Pin_4; gpioInitStruct.GPIO_Mode=GPIO_Mode_AIN; GPIO_Init(GPIOA,&gpioInitStruct);//GPIO口別弄錯 } voidadcModeConfig(void) { ADC_InitTypeDefadcInitStruct; adcInitStruct.ADC_Mode=ADC_Mode_Independent; adcInitStruct.ADC_ScanConvMode=DISABLE; adcInitStruct.ADC_ContinuousConvMode=DISABLE;// adcInitStruct.ADC_ExternalTrigConv=ADC_ExternalTrigConv_None; adcInitStruct.ADC_DataAlign=ADC_DataAlign_Right; adcInitStruct.ADC_NbrOfChannel=1; ADC_Init(ADC1,&adcInitStruct); RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);//使能ADC1時鐘 /*EnableADC1*/ ADC_Cmd(ADC1,ENABLE); /*復(fù)位校準(zhǔn)寄存器*/ ADC_ResetCalibration(ADC1); /*等待校準(zhǔn)寄存器復(fù)位完成*/ while(ADC_GetResetCalibrationStatus(ADC1)); /*ADC校準(zhǔn)*/ ADC_StartCalibration(ADC1); /*等待校準(zhǔn)完成*/ while(ADC_GetCalibrationStatus(ADC1)); ADC_TempSensorVrefintCmd(ENABLE); /*由于沒有采用外部觸發(fā),所以使用軟件觸發(fā)ADC轉(zhuǎn)換*/ ADC_SoftwareStartConvCmd(ADC1,ENABLE); } voidadcInit(void) { adcGPIOConfig(); adcModeConfig(); } floatgetPowerAD(void) { uint16_tADC_ConvertedValue; floatADC_ConvertedValueLocal; ADC_Cmd(ADC1,ENABLE); ADC_SoftwareStartConvCmd(ADC1,ENABLE);//使能指定的ADC1的軟件轉(zhuǎn)換啟動功能 ADC_RegularChannelConfig(ADC1,ADC_Channel_4,1,ADC_SampleTime_239Cycles5); while(!ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC));//等待轉(zhuǎn)換結(jié)束 ADC_ConvertedValue=ADC_GetConversionValue(ADC1);//返回最近一次ADC1規(guī)則組的轉(zhuǎn)換結(jié)果 ADC_ConvertedValueLocal=(float)ADC_ConvertedValue/4096*3.3;//讀取轉(zhuǎn)換的AD值 ADC_ClearFlag(ADC1,ADC_FLAG_EOC);//清除轉(zhuǎn)化結(jié)束標(biāo)志位 returnADC_ConvertedValueLocal; } |
評論