当前位置:网站首页>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 !
边栏推荐
- AWK空行过滤
- ATT&CK初步了解
- 二进制的原码、反码、补码
- Integrating database Ecology: using eventbridge to build CDC applications
- Bluetooth smart Bracelet system based on STM32 MCU
- STM32 - external interrupt application (exti) (use cubemx to configure interrupts)
- winServer运维技术栈
- STM32 - interrupt overview (interrupt priority)
- Detection and tracking evaluation index
- Solve Jupiter: the term 'Jupiter' is not recognized as the name of a cmdlet, function, script file
猜你喜欢

npm ERR code ETIMEDOUT npm ERR syscall connect npm ERR errno ETIMEDOUT npm ERR network reques...
![[CVPR 2021] cylinder3d: cylindrical asymmetric 3D convolution network for LIDAR point cloud segmentation](/img/3d/3def78ec88419712e14cf437e21447.png)
[CVPR 2021] cylinder3d: cylindrical asymmetric 3D convolution network for LIDAR point cloud segmentation

Bluetooth smart Bracelet system based on STM32 MCU

STM32 -- program startup process

How about the actual use effect of common source oscilloscope

STM32 - reset and clock control (cubemx for clock configuration)

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

STM32 - Communication

96. Different binary search trees (medium binary search tree dynamic planning)

Can the MySQL create statement be used to create a table structure and append new records
随机推荐
STM32 - Communication
Sword finger offer II 058. schedule (medium design segment tree treemap ordered set)
Bluetooth smart Bracelet system based on STM32 MCU
Command line agent: proxychains configuration
imx6q gpio复用
mysql8.0无法给用户授权或提示You are not allowed to create a user with GRANT的问题
SSH password free login
Solve Jupiter: the term 'Jupiter' is not recognized as the name of a cmdlet, function, script file
使用PCL批量显示PCD点云数据流
redis相关
[connect your mobile phone wirelessly] - debug your mobile device wirelessly via LAN
Imx6q GPIO multiplexing
Att & CK preliminary understanding
Chrome encountered a problem when debugging the code. After modifying and saving the code in vscode, chrome did not update after refreshing
Kali source solution software cannot be installed correctly
20-09-27项目迁移到阿里折腾记录(网卡顺序导致服务无法通过haproxy连接到db)
CMD common commands
Target segmentation learning
[CVPR 2021] cylinder3d: cylindrical asymmetric 3D convolution network for LIDAR point cloud segmentation
(翻译)图技术简明历史