1.配置STM32CUBEMX
(1)使用的是PB0和PB1,對應的通道是IN8和IN9。
(2)ADC的配置
2.代碼部分
(1)定義存儲變量
uint32_t ADC_Value[2];//ADC采集存儲 uint8_t i; float ad1,ad2;存儲轉化為電壓的變量
(2)主函數while內代碼
for(i=0;i<2;i++) { HAL_ADC_Start(&hadc1); HAL_ADC_PollForConversion(&hadc1,0xffff); ADC_Value[i]=HAL_ADC_GetValue(&hadc1); } HAL_ADC_Stop(&hadc1); ad1 = (float)(ADC_Value[0]&0xFFF)*3.3/4096; ad2 = (float)(ADC_Value[1]&0xFFF)*3.3/4096;
轉化后的AD電壓值可用串口或者顯示屏查看。