Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
RT-Thread一般讨论
RTT下ADC+DMA裸机程序移植成功经验分享
发布于 2023-06-14 16:05:08 浏览:790
订阅该版
[tocm] # 1、实验环境 RT-Thread 4.03 STM32F407VET6 STM3F4库版本 0.2.2 在SDK资源管理器中可以查看版本信息如下图。(小编遇见过因版本不兼容出现的编译错误问题) ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20230614/3a07d52fd09332d18853b5817ffedf5b.png.webp) # 2、创建RT-Thread项目 ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20230614/05f78593f2d75113d9bb593676aabeea.png.webp) 此时编译结果没有错误 ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20230614/2440ed1594a64f6089f45fb1476929ab.png.webp) 打开CubeMX软件(不使用CubeMX Setting)step1:配置时钟。step2、配置ADC+DMA。 ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20230614/5de18d802e7904bb75dc31ddf3b49cd3.png.webp) 根据自己板子晶振确定时钟 ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20230614/25763aa52ce94304cd55596293af410e.png.webp) 根据ADC引脚配置 ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20230614/c2f346ba80beabea74758a859729c63b.png) 配置ADC1通道 ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20230614/b924c473f3f97c99bf476e28116517fb.png) 配置DMA ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20230614/4d434c6ad21b8b1e066ada5380419f01.png.webp) ADC参数设置 ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20230614/5b18166922fc8173078db3e706efdac4.png.webp) 生成项目 ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20230614/fdb2bc937f495ea75f22dd6fc442dcbd.png.webp) ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20230614/89f07cb984ba92ea0374501dfd92857d.png.webp) 在RTT中如图文件夹下新建源文件 ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20230614/81e2ebd5deb3bd2f36c10ba2ebb21d12.png.webp) ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20230614/8d1f0ec7c4f42899b6afce17325acd57.png) 从CubeMX生成的工程中复制如下图函数到RTT文件中;具体代码有所修改,可参考代码部分 ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20230614/1b246bba4e237ba91dc36f0e65382993.png.webp) 参考代码 ```c #include "stm32f4xx_hal.h" ADC_HandleTypeDef hadc1; DMA_HandleTypeDef hdma_adc1; static void MX_DMA_Init(void) { /* DMA controller clock enable */ __HAL_RCC_DMA2_CLK_ENABLE(); /* DMA interrupt init */ /* DMA2_Stream0_IRQn interrupt configuration */ HAL_NVIC_SetPriority(DMA2_Stream0_IRQn, 0, 0); HAL_NVIC_EnableIRQ(DMA2_Stream0_IRQn); } void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc) { GPIO_InitTypeDef GPIO_InitStruct = {0}; MX_DMA_Init(); if(hadc->Instance==ADC1) { /* USER CODE BEGIN ADC1_MspInit 0 */ /* USER CODE END ADC1_MspInit 0 */ /* Peripheral clock enable */ __HAL_RCC_ADC1_CLK_ENABLE(); __HAL_RCC_GPIOC_CLK_ENABLE(); __HAL_RCC_GPIOA_CLK_ENABLE(); /**ADC1 GPIO Configuration PC0 ------> ADC1_IN10 PC1 ------> ADC1_IN11 PC2 ------> ADC1_IN12 PC3 ------> ADC1_IN13 PA0-WKUP ------> ADC1_IN0 PA1 ------> ADC1_IN1 */ GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3; GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1; GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /* ADC1 DMA Init */ /* ADC1 Init */ hdma_adc1.Instance = DMA2_Stream0; hdma_adc1.Init.Channel = DMA_CHANNEL_0; hdma_adc1.Init.Direction = DMA_PERIPH_TO_MEMORY; hdma_adc1.Init.PeriphInc = DMA_PINC_DISABLE; hdma_adc1.Init.MemInc = DMA_MINC_ENABLE; hdma_adc1.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD; hdma_adc1.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD; hdma_adc1.Init.Mode = DMA_CIRCULAR; hdma_adc1.Init.Priority = DMA_PRIORITY_LOW; hdma_adc1.Init.FIFOMode = DMA_FIFOMODE_DISABLE; HAL_DMA_Init(&hdma_adc1); __HAL_LINKDMA(hadc,DMA_Handle,hdma_adc1); /* USER CODE BEGIN ADC1_MspInit 1 */ /* USER CODE END ADC1_MspInit 1 */ } } void MX_ADC1_Init(void) { /* USER CODE BEGIN ADC1_Init 0 */ /* USER CODE END ADC1_Init 0 */ ADC_ChannelConfTypeDef sConfig = {0}; /* USER CODE BEGIN ADC1_Init 1 */ /* USER CODE END ADC1_Init 1 */ /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion) */ hadc1.Instance = ADC1; hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV8; hadc1.Init.Resolution = ADC_RESOLUTION_12B; hadc1.Init.ScanConvMode = ENABLE; hadc1.Init.ContinuousConvMode = ENABLE; hadc1.Init.DiscontinuousConvMode = DISABLE; hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START; hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT; hadc1.Init.NbrOfConversion = 6; hadc1.Init.DMAContinuousRequests = ENABLE; hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV; HAL_ADC_Init(&hadc1); /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. */ sConfig.Channel = ADC_CHANNEL_0; sConfig.Rank = 1; sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES; HAL_ADC_ConfigChannel(&hadc1, &sConfig); /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. */ sConfig.Channel = ADC_CHANNEL_1; sConfig.Rank = 2; HAL_ADC_ConfigChannel(&hadc1, &sConfig); /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. */ sConfig.Channel = ADC_CHANNEL_13; sConfig.Rank = 3; HAL_ADC_ConfigChannel(&hadc1, &sConfig); /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. */ sConfig.Channel = ADC_CHANNEL_12; sConfig.Rank = 4; HAL_ADC_ConfigChannel(&hadc1, &sConfig); /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. */ sConfig.Channel = ADC_CHANNEL_11; sConfig.Rank = 5; HAL_ADC_ConfigChannel(&hadc1, &sConfig); /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. */ sConfig.Channel = ADC_CHANNEL_10; sConfig.Rank = 6; HAL_ADC_ConfigChannel(&hadc1, &sConfig); /* USER CODE BEGIN ADC1_Init 2 */ /* USER CODE END ADC1_Init 2 */ } void DMA2_Stream0_IRQHandler(void) { /* USER CODE BEGIN DMA2_Stream0_IRQn 0 */ /* USER CODE END DMA2_Stream0_IRQn 0 */ HAL_DMA_IRQHandler(&hdma_adc1); // rt_kprintf("11111111 \n"); /* USER CODE BEGIN DMA2_Stream0_IRQn 1 */ /* USER CODE END DMA2_Stream0_IRQn 1 */ } ``` 配置RT-Thread Setting ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20230614/7a8e06b4c504a01338581d75e773e46e.png.webp) ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20230614/932ba86a4e66cd57b160fac44d018af0.png.webp) ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20230614/47f0409fa471e9b1482e4a4b7cef433f.png.webp) 编译通过,结果如下图 ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20230614/5f6cda5066913d042efc919f6d9e3890.png.webp) 修改drv_adc.c文件 ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20230614/b4aba9047ea57cdefffac0006d3f0432.png.webp) 编译通过,结果如下图 ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20230614/205571b77d41f5aa6d24d9c97dc6262e.png.webp) ### 3、添加业务测试代码 新建user_adc.c源文件添加业务测试代码如下 ```c #include
#include
#include
rt_thread_t Adc_thread1= RT_NULL; #define ADC_MAX_SAMPLE 12 #define ADC_MAX_CHANLE 6 extern ADC_HandleTypeDef hadc1; extern rt_uint16_t value[ADC_MAX_SAMPLE][ADC_MAX_CHANLE]; rt_uint16_t ADC_ConvertedValue[ADC_MAX_SAMPLE][ADC_MAX_CHANLE]; rt_uint16_t ADC_AverageValue[ADC_MAX_CHANLE]; extern ADC_HandleTypeDef hadc1; static void CalADC_Average(void) { unsigned short i, j; unsigned short uIntMax,uIntMin,i_Max,i_Min; unsigned int sum; for(i=0;i<(ADC_MAX_CHANLE);i++) { uIntMax = ADC_ConvertedValue[0][i]; uIntMin = ADC_ConvertedValue[1][i]; i_Max = 0; i_Min = 1; for(j=0;j
ADC_ConvertedValue[j][i]){ uIntMin=ADC_ConvertedValue[j][i]; i_Min = j; } } ADC_ConvertedValue[i_Max][i]=0; ADC_ConvertedValue[i_Min][i]=0; sum = 0; for(j=0; j
2
条评论
默认排序
按发布时间排序
登录
注册新账号
关于作者
VHMK_9546
这家伙很懒,什么也没写!
文章
1
回答
0
被采纳
0
关注TA
发私信
相关文章
1
有关动态模块加载的一篇论文
2
最近的调程序总结
3
晕掉了,这么久都不见layer2的踪影啊
4
继续K9ii的历程
5
[GUI相关] FreeType 2
6
[GUI相关]嵌入式系统中文输入法的设计
7
20081101 RT-Thread开发者聚会总结
8
嵌入式系统基础
9
linux2.4.19在at91rm9200 上的寄存器设置
10
[转]基于嵌入式Linux的通用触摸屏校准程序
推荐文章
1
RT-Thread应用项目汇总
2
玩转RT-Thread系列教程
3
国产MCU移植系列教程汇总,欢迎查看!
4
机器人操作系统 (ROS2) 和 RT-Thread 通信
5
五分钟玩转RT-Thread新社区
6
【技术三千问】之《玩转ART-Pi》,看这篇就够了!干货汇总
7
关于STM32H7开发板上使用SDIO接口驱动SD卡挂载文件系统的问题总结
8
STM32的“GPU”——DMA2D实例详解
9
RT-Thread隐藏的宝藏之completion
10
【ART-PI】RT-Thread 开启RTC 与 Alarm组件
热门标签
RT-Thread Studio
串口
Env
LWIP
SPI
AT
Bootloader
Hardfault
CAN总线
FinSH
ART-Pi
USB
DMA
文件系统
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
RTC
FAL
rt-smart
I2C_IIC
ESP8266
UART
WIZnet_W5500
ota在线升级
PWM
cubemx
flash
freemodbus
BSP
packages_软件包
潘多拉开发板_Pandora
定时器
ADC
flashDB
GD32
socket
编译报错
中断
Debug
rt_mq_消息队列_msg_queue
SFUD
msh
keil_MDK
ulog
C++_cpp
MicroPython
本月问答贡献
xusiwei1236
8
个答案
2
次被采纳
踩姑娘的小蘑菇
1
个答案
2
次被采纳
用户名由3_15位
7
个答案
1
次被采纳
bernard
4
个答案
1
次被采纳
RTT_逍遥
3
个答案
1
次被采纳
本月文章贡献
聚散无由
2
篇文章
15
次点赞
catcatbing
2
篇文章
5
次点赞
Wade
2
篇文章
2
次点赞
Ghost_Girls
1
篇文章
6
次点赞
YZRD
1
篇文章
2
次点赞
回到
顶部
发布
问题
投诉
建议
回到
底部