当前位置:网站首页>STM32CUBEIDE(10)----ADC在DMA模式下扫描多个通道
STM32CUBEIDE(10)----ADC在DMA模式下扫描多个通道
2022-07-28 20:51:00 【记帖】
概述
本章STM32CUBEMX配置STM32F103的ADC在DMA模式扫描多个通道,通过串口进行打印。
最近在弄ST和GD的课程,需要样片的可以加群申请:615061293。
视频教学
csdn课程
课程更加详细。
https://download.csdn.net/course/detail/35611
生成例程
使用STM32CUBEMX生成例程,这里使用NUCLEO-F103RB开发板
查看原理图,PA2和PA3设置为开发板的串口。
配置串口。

开启中断。
查看原理图,Arduino的接口A0-A5都是AD口。
ADC通道配置
| ADC1 | IN0(PA0) | IN1(PA1) | IN4(PA4) |
|---|
ADC1配置。

- ADCs_Common_Settings:
- Mode:Independent mod 独立 ADC 模式,当使用一个 ADC 时是独立模式,使用两个 ADC 时是双模式,在双模式下还有很多细分模式可选,具体配置 ADC_CR1:DUALMOD 位。
- ADC_Settings:
- Data Alignment:
- Right alignment 转换结果数据右对齐,一般我们选择右对齐模式。
- Left alignment 转换结果数据左对齐。
- Scan Conversion Mode:
- Disabled 禁止扫描模式。如果是单通道 AD 转换使用 DISABLE。
- Enabled 开启扫描模式。如果是多通道 AD 转换使用 ENABLE。
- Continuous Conversion Mode:
- Disabled 单次转换。转换一次后停止需要手动控制才重新启动转换。
- Enabled 自动连续转换。
- DiscontinuousConvMode:
- Disabled 禁止间断模式。这个在需要考虑功耗问题的产品中很有必要,也就是在某个事件触发下,开启转换。
- Enabled 开启间断模式。
- Data Alignment:
- ADC_Regular_ConversionMode:
- Enable Regular Conversions 是否使能规则转换。
- Number Of Conversion ADC转换通道数目,有几个写几个就行。
- External Trigger Conversion Source 外部触发选择。这个有多个选择,一般采用软件触发方式。
- Rank:
- Channel ADC转换通道
- Sampling Time 采样周期选择,采样周期越短,ADC 转换数据输出周期就越短但数据精度也越低,采样周期越长,ADC 转换数据输出周期就越长同时数据精度越高。
- ADC_Injected_ConversionMode:
- Enable Injected Conversions 是否使能注入转换。注入通道只有在规则通道存在时才会出现。
- WatchDog:
- Enable Analog WatchDog Mode 是否使能模拟看门狗中断。当被 ADC 转换的模拟电压低于低阈值或者高于高阈值时,就会产生中断。
DMA开启。
生成独立的文件。
STM32CUBEIDE配置

若需要打印浮点型,需要勾选下面的选项。
串口重定向
在main.c中,添加头文件,若不添加会出现 identifier “FILE” is undefined报错。
/* USER CODE BEGIN Includes */
#include "stdio.h"
/* USER CODE END Includes */
函数声明和串口重定向:
/* USER CODE BEGIN PFP */
#ifdef __GNUC__ //串口重定向
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif
PUTCHAR_PROTOTYPE
{
HAL_UART_Transmit(&huart2 , (uint8_t *)&ch, 1, 0xFFFF);
return ch;
}
/* USER CODE END PFP */
代码
定义变量,存放采集到的数据。
/* USER CODE BEGIN 0 */
uint32_t ADC1_1, ADC1_2,ADC1_3;//采集的三个通道的ADC
uint32_t ADC1_Value[30];//DMA存放数组
uint8_t i;
uint8_t ADC1_Flag;//dma采集完毕中断
/* USER CODE END 0 */
使能ADC传输。
/* USER CODE BEGIN 2 */
HAL_ADC_Start_DMA(&hadc1,(uint32_t*)&ADC1_Value,30); //使用DMA传输
/* USER CODE END 2 */
主循环。
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
if(ADC1_Flag==1)
{
ADC1_Flag=0;
ADC1_1=0;
ADC1_2=0;
ADC1_3=0;
for(i=0;i<30;)
{
ADC1_1+=ADC1_Value[i++];
ADC1_2+=ADC1_Value[i++];
ADC1_3+=ADC1_Value[i++];
}
printf("\n");
printf("adc1_IN0(PA0)=%4.0d,ADC_IN0=%1.4f\r\n",ADC1_1/10,ADC1_1/10*3.3f/4096);
printf("adc1_IN1(PA1)=%4.0d,ADC_IN1=%1.4f\r\n",ADC1_2/10,ADC1_2/10*3.3f/4096);
printf("adc1_IN4(PA4)=%4.0d,ADC_IN2=%1.4f\r\n",ADC1_3/10,ADC1_3/10*3.3f/4096);
HAL_ADC_Start_DMA(&hadc1,(uint32_t*)&ADC1_Value,30); //使用DMA传输
}
HAL_Delay(1000);
}
/* USER CODE END 3 */
ADC回调函数。
DMA传输的时候如果读取内存片段,会有仲裁器的问题,加了一句关闭DMA的语句HAL_ADC_Stop_DMA(&hadc1);
/* USER CODE BEGIN 4 */
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
{
if(hadc->Instance == ADC1){
ADC1_Flag=1;
/* * DMA传输的时候如果读取内存片段,会有仲裁器的问题,加了一句关闭DMA的语 */
HAL_ADC_Stop_DMA(&hadc1);
}
}
/* USER CODE END 4 */
测试结果
输入固定电压进行测试。
| ADC1 | IN0(PA0) | IN1(PA1) | IN4(PA4) |
|---|---|---|---|
| 输入电压 | VCC | 2.0V | GND |
Normal下测试结果如下。
若不试用关闭DMA的语句HAL_ADC_Stop_DMA(&hadc1);
会造成数据错乱。
Circular可以下可以一直进行采集,不需要HAL_ADC_Stop_DMA(&hadc1)都可。
最后
以上的代码会在Q群里分享。QQ群:615061293。
或者关注微信公众号『记帖』,持续更新文章和学习资料,可加作者的微信交流学习!
边栏推荐
- How to install WiFi correctly
- Leetcode integer exercises integer inversion
- Jmeter 安装第三方插件 Plugins Manager
- STM32 - reset and clock control (cubemx for clock configuration)
- 6K6w5LiA5qyh5pS75Ye75YiG5p6Q
- Analysis notes on let (const) temporary dead zone in JS
- imx6q gpio复用
- Winserver operation and maintenance technology stack
- Sword finger offer II 053. Medium order successor in binary search tree (medium binary search tree DFS)
- PaddleNLP基于ERNIR3.0文本分类:WOS数据集为例(层次分类)
猜你喜欢

Imx6q GPIO multiplexing

SQL injection less38 (Stack Injection)

If you want to grow rapidly, you must first experience a major blow!

CMD common commands

Static route and default route experiment

(翻译)图技术简明历史

Paddlenlp text classification based on ernir3.0: take wos dataset as an example (hierarchical classification)
![Paddlenlp is based on ernir3.0 text classification. Take the crime prediction task of cail2018-small dataset as an example [multiple tags]](/img/87/d943cc1e8169bb670414fbf7a322c5.jpg)
Paddlenlp is based on ernir3.0 text classification. Take the crime prediction task of cail2018-small dataset as an example [multiple tags]

LVS+KeepAlived高可用部署实战应用

XXX port is already in use
随机推荐
Baidu map usage
winServer运维技术栈
Ruiji takeout - background login function development
Log4j vulnerability elk platform processing method (logstah5.5.1)
php二维数组如何删除去除第一行元素
internet的基本服务中文件传输命令是哪个
What is time complexity
[leetcode] maximum depth of binary tree
JS array merging, de duplication, dimensionality reduction (es6: extended operator, set)
Overall introduction of Ruiji takeout project
XXX port is already in use
Sword finger offer II 056. Sum of two nodes in a binary search tree (simple binary search tree DFS hash table double pointer iterator)
Establishment of Ruiji takeout development environment
Less than a year after its establishment! MIT derivative quantum computing company completed financing of US $9million
Use webworker to perform background tasks
20-09-27 the project is migrated to Alibaba toss record (the network card order makes the service unable to connect to DB through haproxy)
flask之蓝图 补充openpyxl
ES6, deep copy, shallow copy
6K6w5LiA5qyh5pS75Ye75YiG5p6Q
JS implementation generates a random key of specified length