当前位置:网站首页>Stm32subeide (10) -- ADC scans multiple channels in DMA mode
Stm32subeide (10) -- ADC scans multiple channels in DMA mode
2022-07-28 22:42:00 【Post】
STM32CUBEIDE.10----ADC stay DMA Scan multiple channels in mode
summary
This chapter STM32CUBEMX To configure STM32F103 Of ADC stay DMA Mode scan multiple channels , Print through serial port .
Recently ST and GD Curriculum , If you need samples, you can add groups to apply :615061293.
Video teaching
csdn Course
The course is more detailed .
https://download.csdn.net/course/detail/35611
Generate routine
Use STM32CUBEMX Generate routine , Use here NUCLEO-F103RB Development board 
Look at the schematic ,PA2 and PA3 Set as the serial port of the development board .
Configure serial port .

Open interrupt .
Look at the schematic ,Arduino The interface of A0-A5 All are AD mouth .
ADC Channel configuration
| ADC1 | IN0(PA0) | IN1(PA1) | IN4(PA4) |
|---|
ADC1 To configure .

- ADCs_Common_Settings:
- Mode:Independent mod Independent ADC Pattern , When using a ADC It's an independent mode , Use two ADC It's dual mode , In dual mode, there are many subdivision modes available , Specific configuration ADC_CR1:DUALMOD position .
- ADC_Settings:
- Data Alignment:
- Right alignment Right align the conversion result data , Generally, we choose right alignment mode .
- Left alignment The conversion result data is left aligned .
- Scan Conversion Mode:
- Disabled Disable scan mode . If it's a single channel AD Conversion uses DISABLE.
- Enabled Turn on scan mode . If it's multichannel AD Conversion uses ENABLE.
- Continuous Conversion Mode:
- Disabled Single conversion . To stop the conversion after one time, manual control is needed to restart the conversion .
- Enabled Automatic continuous conversion .
- DiscontinuousConvMode:
- Disabled Disable interrupt mode . This is necessary in products that need to consider power consumption , That is, when an event triggers , Turn on the switch .
- Enabled Turn on break mode .
- Data Alignment:
- ADC_Regular_ConversionMode:
- Enable Regular Conversions Whether to enable rule conversion .
- Number Of Conversion ADC Number of conversion channels , Just write a few of them .
- External Trigger Conversion Source External trigger selection . There are many options for this , Generally, software trigger mode is adopted .
- Rank:
- Channel ADC Switching channels
- Sampling Time Sampling period selection , The shorter the sampling period ,ADC The shorter the output period of the converted data, the lower the accuracy of the data , The longer the sampling period ,ADC The longer the output period of the converted data, the higher the accuracy of the data .
- ADC_Injected_ConversionMode:
- Enable Injected Conversions Whether to enable injection conversion . Injection channels only appear when regular channels exist .
- WatchDog:
- Enable Analog WatchDog Mode Whether the watchdog interrupt can be simulated . When ADC When the converted analog voltage is below the low threshold or above the high threshold , There will be interruptions .
DMA Turn on .
Generate a separate file .
STM32CUBEIDE To configure

If you need to print floating point type , You need to check the following options .
Serial port redirection
stay main.c in , Add header file , If you don't add it, it will appear identifier “FILE” is undefined Report errors .
/* USER CODE BEGIN Includes */
#include "stdio.h"
/* USER CODE END Includes */
Function declaration and serial port redirection :
/* USER CODE BEGIN PFP */
#ifdef __GNUC__ // Serial port redirection
#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 */
Code
Defining variables , Store the collected data .
/* USER CODE BEGIN 0 */
uint32_t ADC1_1, ADC1_2,ADC1_3;// Three channels of acquisition ADC
uint32_t ADC1_Value[30];//DMA Store array
uint8_t i;
uint8_t ADC1_Flag;//dma Interruption after collection
/* USER CODE END 0 */
Can make ADC transmission .
/* USER CODE BEGIN 2 */
HAL_ADC_Start_DMA(&hadc1,(uint32_t*)&ADC1_Value,30); // Use DMA transmission
/* USER CODE END 2 */
Main circulation .
/* 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); // Use DMA transmission
}
HAL_Delay(1000);
}
/* USER CODE END 3 */
ADC Callback function .
DMA If you read memory segments during transmission , There will be problems with the arbiter , Added a sentence close DMA The sentence of HAL_ADC_Stop_DMA(&hadc1);
/* USER CODE BEGIN 4 */
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
{
if(hadc->Instance == ADC1){
ADC1_Flag=1;
/* * DMA If you read memory segments during transmission , There will be problems with the arbiter , Added a sentence close DMA The language of */
HAL_ADC_Stop_DMA(&hadc1);
}
}
/* USER CODE END 4 */
test result
Input a fixed voltage for testing .
| ADC1 | IN0(PA0) | IN1(PA1) | IN4(PA4) |
|---|---|---|---|
| Input voltage | VCC | 2.0V | GND |
Normal The test results are as follows .
If you don't try, close DMA The sentence of HAL_ADC_Stop_DMA(&hadc1);
Will cause data confusion .
Circular It can be collected all the time , Unwanted HAL_ADC_Stop_DMA(&hadc1) All right .
Last
The above code will be in Q Share in the group .QQ Group :615061293.
Or WeChat official account 『 Note 』, Continuously update articles and learning materials , You can add the author's wechat communication and learning !
边栏推荐
- STM32 board level support package for keys
- STM32 -- program startup process
- Container configuration starts redis cluster single machine 6 nodes 3 Master 3 slave
- JS convert numbers to letters
- 微信小程序里button点击的时候会边框有黑线
- 删除容器镜像报错解决image is referenced in multiple repositories
- Less than a year after its establishment! MIT derivative quantum computing company completed financing of US $9million
- npm ERR code ETIMEDOUT npm ERR syscall connect npm ERR errno ETIMEDOUT npm ERR network reques...
- Winserver operation and maintenance technology stack
- 二进制的原码、反码、补码
猜你喜欢

(翻译)图技术简明历史

Win11 how to open software notification

842. 排列数字
![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]

STM32 - external interrupt application (exti) (use cubemx to configure interrupts)

2022年一级建造师考试什么时候才能报名?

JVM——自定义类加载器

PaddleNLP基于ERNIR3.0文本分类以CAIL2018-SMALL数据集罪名预测任务为例【多标签】

c语言实现字符串逆序排列

Can the MySQL create statement be used to create a table structure and append new records
随机推荐
AWK空行过滤
[connect set-top box] - use ADB command line to connect ec6108v9 Huawei Yuehe box wirelessly
CMD common commands
静态成员static详解
c语言实现字符串逆序排列
Analysis notes on let (const) temporary dead zone in JS
Baidu map usage
Sword finger offer II 065. The shortest word code (medium dictionary tree string array)
Summary of common error types in JS
How do we do full link grayscale on the database?
How about the actual use effect of common source oscilloscope
Solve the problem that TS node xxx.ts executes TS code and reports errors
Att & CK preliminary understanding
Paddlenlp is based on ernir3.0 text classification. Take the crime prediction task of cail2018-small dataset as an example [multiple tags]
Win11 how to open software notification
(翻译)图技术简明历史
Use webworker to perform background tasks
Command line agent: proxychains configuration
winServer运维技术栈
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)