仔细阅读L496的Datasheet会发现,其共有3个12bit的ADC,5Msps, ADC1能达到19个通道,资源还是挺丰富的。在参考手册的ADC main feature中说的很清楚。板子上没有电位器,找了个电位器按常规套路连接到板子上,就可以测试AD了。 引脚参考图1和datasheet的图2.
然后就是程序的初始化,初始化引脚: - GPIO_InitStruct.Pin = GPIO_PIN_0;
- GPIO_InitStruct.Mode = GPIO_MODE_ANALOG_ADC_CONTROL;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
复制代码
初始化AD1: - hadc1.Instance = ADC1;
- hadc1.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
- hadc1.Init.Resolution = ADC_RESOLUTION_12B;
- hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
- hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
- hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
- hadc1.Init.LowPowerAutoWait = DISABLE;
- hadc1.Init.ContinuousConvMode = ENABLE;
- hadc1.Init.NbrOfConversion = 1;
- hadc1.Init.DiscontinuousConvMode = DISABLE;
- hadc1.Init.NbrOfDiscConversion = 1;
- hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
- hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
- hadc1.Init.DMAContinuousRequests = ENABLE;
- hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED;
- hadc1.Init.OversamplingMode = DISABLE;
- if (HAL_ADC_Init(&hadc1) != HAL_OK)
- {
- Error_Handler();
- }
复制代码
使能时钟: - __HAL_RCC_GPIOC_CLK_ENABLE();
- __HAL_RCC_ADC_CLK_ENABLE();
复制代码
下载到板子中,通过仿真watch1窗口,转动电位器,观察数据的变化。如图3和图4所示:
这只是简单的ADC测试,通过这个外设功能,还可以实现其他扩展功能。
|