本帖最后由 dvacos 于 2024-9-6 20:17 编辑
在论坛里看见有的小伙伴使用B-G431B-ESC1开发板的ADC采样旋钮的电压一直都是0V,于是我挑战一下。
一 、硬件确认
3D PCB 确认引脚
经过万用表测量,确认电压会随着旋钮的变化而变化,由0V-3.3V之间变化。
二 、软件初始化
Cube Mx配置
配置PB12 为ADC,默认分配为ADC1 11通道
ADC只有一根线,所以配置为单端
ADC配置
以上Cube Mx配置ADC初始化完毕
代码配置
由于初始化的时候选择的是软件触发,所以需要添加一些代码,才能开始采样。开始采样与获取采样值API如下
HAL_ADC_Start(&hadc1);
HAL_ADC_GetValue(&hadc1);
三、 实验
四 、完整ADC代码
/**
* [url=home.php?mod=space&uid=159083]@brief[/url] ADC1 Initialization Function
* @param None
* @retval None
*/
static void MX_ADC1_Init(void)
{
/* USER CODE BEGIN ADC1_Init 0 */
/* USER CODE END ADC1_Init 0 */
ADC_MultiModeTypeDef multimode = {0};
ADC_ChannelConfTypeDef sConfig = {0};
/* USER CODE BEGIN ADC1_Init 1 */
/* USER CODE END ADC1_Init 1 */
/** Common config
*/
hadc1.Instance = ADC1;
hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
hadc1.Init.Resolution = ADC_RESOLUTION_12B;
hadc1.Init.DataAlign = ADC_DATAALIGN_LEFT;
hadc1.Init.GainCompensation = 0;
hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
hadc1.Init.LowPowerAutoWait = DISABLE;
hadc1.Init.ContinuousConvMode = DISABLE;
hadc1.Init.NbrOfConversion = 1;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc1.Init.DMAContinuousRequests = DISABLE;
hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED;
hadc1.Init.OversamplingMode = DISABLE;
if (HAL_ADC_Init(&hadc1) != HAL_OK)
{
Error_Handler();
}
/** Configure the ADC multi-mode
*/
multimode.Mode = ADC_MODE_INDEPENDENT;
if (HAL_ADCEx_MultiModeConfigChannel(&hadc1, &multimode) != HAL_OK)
{
Error_Handler();
}
/** Configure Regular Channel
*/
sConfig.Channel = ADC_CHANNEL_11;
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLETIME_640CYCLES_5;
sConfig.SingleDiff = ADC_SINGLE_ENDED;
sConfig.OffsetNumber = ADC_OFFSET_NONE;
sConfig.Offset = 0;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN ADC1_Init 2 */
/* USER CODE END ADC1_Init 2 */
}
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_ADC1_Init();
MX_USART2_UART_Init();
/* Initialize interrupts */
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
char printf_data[20];
uint16_t get_adc=0;
double voltage=0;
HAL_ADC_Start(&hadc1);
get_adc=HAL_ADC_GetValue(&hadc1);
voltage = get_adc*(3.3/65535);
sprintf(printf_data,"Get_Adc:%d Voltage:%.2f \r\n",get_adc,voltage);
HAL_UART_Transmit(&huart2,(uint8_t *)printf_data,strlen(printf_data),50);
HAL_Delay(100);
}
/* USER CODE END 3 */
}