stm32cubemx 单通道adc采集 dma版
- 开发环境
- stm32cubemx配置
- 代码实现
开发环境
单片机:stm32f103c8t6
软件:stm32cubemx,keil
其余的时钟等操作与其他一致.
stm32cubemx配置
勾选adc1,随便勾一个引脚,开连续模式,将采样时间拉到最大
选择adc1的dma
循环模式,地址自动增加,字节大小,half word 即每次增16位
勾个串口
工程其余配置一致
代码实现
创建DMA的缓存区
在外设初始化后
进行adc校准,以及开启dma,因为是循环模式,所以会一直采样。
其中因为adc_buf是16位,缓存区位32位,所以采样数为2
HAL_Delay(1000);
HAL_ADCEx_Calibration_Start(&hadc1);
HAL_ADC_Start_DMA(&hadc1, (uint32_t*) adc_buf, 2); //通道数1
只采一次可以在stm32cubemx中配置成normal模式
多次采样需要加缓存区和采样次数
循环中实现数据转换和打印
adc_buf[0]=adc_buf[0]*3300/4096;
adc_buf[1]=adc_buf[1]*3300/4096;
//printf("ADC1 = %d,ADC2 = %d\r\n",adc_buf[0],adc_buf[1]);
//printf("voltage: %f \r\n, (float)adc_buf[0]/4096*3.3");
HAL_Delay(1000);
打印看这点这里
date:2021.5.24
author:dawn_z