ST NUCLEO-C031C6开发板学习笔记06(ADC采样)
<div class='showpostmsg'>## ==9:ADC采样==### 9.1:硬件设计
首先查看具有ADC功能的引脚:
!(https://boreyun.oss-cn-shanghai.aliyuncs.com/image-20240305121254347.png)
!(https://boreyun.oss-cn-shanghai.aliyuncs.com/image-20240305121307975.png)
我们采用PA0引脚来实现ADC采样。
### 9.2:软件设计
ADC的采样方式有很多:
```
(1)单通道数据采集
(2)多通道间断模式轮询采集
(3)多通道中断采集
(4)多通道定时器中断采集
(5)多通道DMA采集
(6)多通道定时器MDA采集
```
这里采用单通道数据采集。
1:CubeMX 设置
!(https://boreyun.oss-cn-shanghai.aliyuncs.com/image-20240305121418623.png)
!(https://boreyun.oss-cn-shanghai.aliyuncs.com/image-20240305121623061.png)
2:代码编程
ADC配置:
```C
ADC_HandleTypeDef hadc1;
/* ADC1 init function */
void MX_ADC1_Init(void)
{
/* USER CODE BEGIN ADC1_Init 0 */
/* USER CODE END ADC1_Init 0 */
ADC_ChannelConfTypeDef sConfig = {0};
/* USER CODE BEGIN ADC1_Init 1 */
/* USER CODE END ADC1_Init 1 */
/** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
*/
hadc1.Instance = ADC1;
hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
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.LowPowerAutoPowerOff = DISABLE;
hadc1.Init.ContinuousConvMode = ENABLE;
hadc1.Init.NbrOfConversion = 1;
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.SamplingTimeCommon1 = ADC_SAMPLETIME_1CYCLE_5;
hadc1.Init.SamplingTimeCommon2 = ADC_SAMPLETIME_1CYCLE_5;
hadc1.Init.OversamplingMode = DISABLE;
hadc1.Init.TriggerFrequencyMode = ADC_TRIGGER_FREQ_HIGH;
if (HAL_ADC_Init(&hadc1) != HAL_OK)
{
Error_Handler();
}
/** Configure Regular Channel
*/
sConfig.Channel = ADC_CHANNEL_0;
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLINGTIME_COMMON_1;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN ADC1_Init 2 */
/* USER CODE END ADC1_Init 2 */
}
```
编写ADC采样任务
```C
uint16_t ADC_Get_Task()
{
uint16_t adcVal=0;
HAL_ADC_Start(&hadc1); //启动ADC转换
HAL_ADC_PollForConversion(&hadc1, 50); //等待转换完成,50为最大等待时间,单位为ms
if(HAL_IS_BIT_SET(HAL_ADC_GetState(&hadc1), HAL_ADC_STATE_REG_EOC))
{
adcVal = HAL_ADC_GetValue(&hadc1);
DEBUG_LOG("adcVal=%d",adcVal);
returnadcVal;//返回ADC的值
}
return 0;
}
```
按照一秒循环依次执行采样任务。
### 9.3:ADC单元测试验证
我们使用一个MQ3传感器来测试:
!(https://boreyun.oss-cn-shanghai.aliyuncs.com/image-20240305130948888.png)
!(https://boreyun.oss-cn-shanghai.aliyuncs.com/image-20240305131035583.png)
从串口调试助手可以看出ADC值在变化。</div><script> var loginstr = '<div class="locked">查看本帖全部内容,请<a href="javascript:;" style="color:#e60000" class="loginf">登录</a>或者<a href="https://bbs.eeworld.com.cn/member.php?mod=register_eeworld.php&action=wechat" style="color:#e60000" target="_blank">注册</a></div>';
if(parseInt(discuz_uid)==0){
} </script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script>
页:
[1]