使用CubeMX工具,对DMA进行配置。
/ K" Q3 Z, `7 y, q: i5 @& m% X6 F3 F+ y9 i: ?( n1 c1 c2 V
dma相关的配置比较简单,主要是adc配置相关的修改:
: k& q0 G1 Z( S, a数据要设置成DMA循环模式ADC_CONVERSIONDATA_DMA_CIRCULAR;
2 ~: `+ Y* _. ]7 f& V溢出操作要设置为覆盖ADC_OVR_DATA_OVERWRITTEN;, W6 W% O: G8 O; u$ }, \
主函数逻辑代码如下:
4 ?+ C& x# s2 o& m0 i3 O+ k' o' M- #include "sys.h"6 f4 t# x; t% A7 `$ A0 H. v
- #include "delay.h"
3 b( n c; z& w7 d4 p$ b - #include "usart.h"
, ^8 I" k9 s& r - #include "adc.h". j q. r( `/ I& _# U
- #include "dma.h": T" C( X5 f5 O8 O; C( L* `+ o: b4 P1 w
- * @8 {/ S7 q2 _& |% X& j
- u16 buffer[128];
" `/ |6 l! C6 j4 s: g+ k - int main(void)
. @; p5 \2 A+ l% }4 k+ X6 F - {
! f1 M5 g) P* B" S( y! w8 G - u16 adcx;
# e' L1 o8 P) q' b- i$ n - //Cache_Enable(); //打开L1-Cache4 M" N/ }- r1 |/ d0 o
- HAL_Init(); //初始化HAL库
: r: w7 ~' F) y2 T- l - Stm32_Clock_Init(200, 2, 2, 2); //设置时钟,400Mhz
6 m& s* J# M( r* B) R6 o$ a - delay_init(400); //延时初始化4 M7 T8 T/ p3 A* j
- uart_init(115200); //串口初始化0 B7 ], Z2 s" N: q8 P5 Y& Y+ p8 }
- printf("hello\r\n");
7 h6 b- b" @% V' y/ x1 j) b* g - MX_DMA_Init();
b+ |7 M, T% G, U7 f - MX_ADC1_Init();
" Q+ J- H: c/ A Y - HAL_ADC_Start_DMA(&hadc1, (uint32_t *)buffer, 16);
( l. x# Q" L( J. n - while (1)
B& z7 g3 r/ |4 s4 j - {3 d& g" P7 c* B9 g$ S
- printf("\r\nvalue:");
( C$ `; k0 r3 E' w - for (int i = 0; i < 32; i++)
1 p( ^% q4 `" x3 g3 d - {/ s; D8 m; v* s' g
- if (i % 8 == 0) printf("\r\n");5 ]0 t: m% N+ D6 F+ l* q
- printf("%d\t", buffer[i]); //返回最近一次ADC1规则组的转换结果
( u6 c+ n# J3 |/ N/ z5 N - }0 v- Y2 J! q a7 `/ i
- delay_ms(500);
0 P* c; }3 I/ A. e6 P2 G% n - }
( }. l2 D8 T7 ?" a" Y - }
# v3 |$ g( G) @# K$ s - + U2 P" @2 d1 i1 L8 b$ V
- void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc)3 z1 F9 d) i; z9 q. C
- {+ q! U* d& t. c
- printf("DMA transfer complete\r\n");* a9 E# J. z4 D1 @- k7 N9 a( q; F
- }, f$ w3 {2 J5 K0 s, e ~7 F* k: i
- void HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef *hadc)5 ^# ~+ x% Y% v% {& u+ M5 H" C
- {
1 v1 }! Y' K0 D# d3 \) ?+ D - printf("DMA Half transfer complete\r\n");
0 V& [2 g; z6 ]/ b. ~2 ? O - }
- B8 Q! k/ L9 m6 R - ) ~2 |3 O4 V, D! l# N% u* e
- void HAL_ADC_ErrorCallback(ADC_HandleTypeDef *hadc), i* `' F) _' n5 e0 S
- {
+ P/ s% I' u6 U3 ~. c' r: v+ E8 ` - printf("DMA transfer error\r\n");: L8 j; ~6 }& x6 G7 J0 w1 M
- }
a3 b ]1 ^6 G! n - 3 R3 [8 c$ B/ v3 | S
- ( \+ G" X5 g; B6 n9 u0 Y$ t) ?/ r
' @1 }% c9 O7 x6 X, l9 p8 c- void Error_Handler(void)' q0 t! ~1 [4 `3 T
- {. k+ ]& Q! ^; K1 C; }* W
- /* USER CODE BEGIN Error_Handler_Debug */- T/ K y: ]* g
- /* User can add his own implementation to report the HAL error return state */& I: p+ l4 t3 f2 t: s# o
1 g4 N3 _; u" n- /* USER CODE END Error_Handler_Debug */# ~1 f9 o2 u3 N& D
- }
$ B- k' n( |& U; `" j" f
- Q) J% }$ d% ]7 S0 P: _
复制代码 . G9 {6 C' P% b) a% H
单次转换完成的效果:
0 S, U2 d6 m/ e" b. \8 l6 P6 p' H( _: ?1 O% L7 t
/ ?, W) y$ ~4 S3 ^6 S6 N2 O! u6 k- F* v7 I# O
————————————————8 u* y/ r1 Q6 o ?9 K5 Z+ N
版权声明:小盼你最萌哒
& X& O' X7 w7 v1 S/ W6 @9 M$ o f
5 O4 C5 U* d1 i |