adc 接收cube_STM32CubeMX系列教程7:模数转换(ADC)

/* USER CODE BEGIN WHILE */

while (1)

{

/* USER CODE END WHILE */

/* USER CODE BEGIN 3 */

/*##-1- Start the conversion process #######################################*/

HAL_ADC_Start(&hadc1);

/*##-2- Wait for the end of conversion #####################################*/

/* Before starting a new conversion, you need to check the current state of

the peripheral; if it’s busy you need to wait for the end of current

conversion before starting a new one.

For simplicity reasons, this example is just waiting till the end of the

conversion, but application may perform other tasks while conversion

operation is ongoing. */

HAL_ADC_PollForConversion(&hadc1, 50);

/* Check if the continous conversion of regular channel is finished */

if(HAL_IS_BIT_SET(HAL_ADC_GetState(&hadc1), HAL_ADC_STATE_REG_EOC))

{

/*##-3- Get the converted value of regular channel ######################*/

AD_Value = HAL_ADC_GetValue(&hadc1);

printf("MCU Temperature : %.1f¡æ\r\n",((AD_Value*3300/4096-760)/2.5+25));

}

HAL_Delay(1000);

}

/* USER CODE END 3 */

HAL_ADC_Start(&hadc1)为启动ADC装换,

HAL_ADC_PollForConversion(&hadc1, 50);表示等待转换完成,第二个参数表示超时时间,单位ms.

HAL_ADC_GetState(&hadc1)为换取ADC状态,HAL_ADC_STATE_REG_EOC表示转换完成标志位,转换数据可用。

HAL_IS_BIT_SET(HAL_ADC_GetState(&hadc1), HAL_ADC_STATE_REG_EOC)就是判断转换完成标志位是否设置。

HAL_ADC_GetValue(&hadc1);读取ADC转换数据,数据为12位。查看数据手册可知,寄存器为16位存储转换数据,数据右对齐,则转换的数据范围为0~2^12-1,即0~4095.

17914476d29520fd1bf2d603c16b7d2e.png

AD_Value*3300/4096为将转换后的数据转化为电压,单位为mV,参考电压为3.3V。查询数据手册可以电压和温度的关系。经过计算公式装换后等到MCU内部温度值。

d6ead36c259c0c41459798924da9e7dc.png44969b4b27128d063886202fadb7f270.png

编译程序并下载到开发板。打开串口调试助手。设置波特率为115200。串口助手上会显示MCU温度。

2.ADC_DMA

前面介绍了通过ADC轮询的方式采集单通道的数据。现在介绍一下通过DMA方式采集多通道的数据。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值