一、打开上一节的工程,在上一节的基础上进行修改
# s: L$ ?. a* }( |& I二、配置DMA
9 j, w* L) d5 e4 l/ \在cubemx界面上点击DMA- h! W# @2 ?& N8 I
+ R( a, ]3 h {0 ]% n& o5 i1 D
2 i& s% F5 Z0 L( x2 T6 e
+ }* n" t' s' l t A9 U* n因为串口1是挂在DMA2上的,所以点击DMA2,如图中标记的2,随后点击ADD
7 m. s, F* z- f. Z: z$ j# f* e- u0 V6 ^
! @+ B! b1 p, o' l" C* K1 Z' }2 I. g' y
选择USART1_RX,继续点击ADD,将USART1_TX也添加进去,随后配置DMA如下图所示
8 S! [! Z* H: T: x* b/ p5 h0 Z" E# a @# L/ t8 [) b
) x# y! N `6 s
. T' b: I7 |8 S# x6 W. Z
; G8 d1 b6 O* e7 b( q/ L
其中mode设置可以选择Normal表单次传输,传输一次后终止传输,Circular表示循环传输,传输完成后又重新开始继续传输,不断循环永不停止。此处选择单次传输。
$ `2 ]! e) }6 `+ `" w5 |7 _0 I& J X/ U) Y$ A- `1 e
Increment Address表示地址指针递增。串口发送数据是将数据不断存进串口的发送数据寄存器(USARTx_TDR)。所以外接的地址是不递增。而内存储器存储的是要发送的数据,所以地址指针要递增才能将所以的数据发送出去。
+ y6 M- o# J) q+ h: g0 W. E5 e& q+ S+ G1 s
串口数据发送寄存器只能存储8bit,每次发送一个字节,所以数据长度选择Byte。; { ?6 D/ s7 D& e8 l* h2 ^
/ Z& b2 K: _- p7 T2 b8 ~' [& }% L
( G+ d c3 N) c z; u
三、生成代码
& L1 D9 g0 z- T! A在主函数中添加代码,添加完成如下所示/ G# r) \& B; ?8 T
- <font face="Tahoma"><font size="3">int main(void)</font></font>: |& Z) ~/ _5 V0 T
- <font face="Tahoma"><font size="3">{</font></font>
$ C& z/ V& D% y: g |9 p& r0 A+ I - <font face="Tahoma"><font size="3"> /* USER CODE BEGIN 1 */</font></font>0 o1 e$ A+ H' y+ Y N
- <font face="Tahoma"><font size="3"> uint8_t *data="uart dma test\r\n";</font></font>7 l- O8 X. p; |" @( n* }
- <font face="Tahoma"><font size="3"> /* USER CODE END 1 */</font></font>! q0 B7 Q+ B" I6 U6 \9 f
- <font face="Tahoma"><font size="3">
1 K3 T+ Q5 j2 v/ o' \ - </font></font>
8 T, g( m' A9 ] - <font face="Tahoma"><font size="3"> /* Enable I-Cache---------------------------------------------------------*/</font></font>) `8 Q4 {" e! l0 Q
- <font face="Tahoma"><font size="3"> SCB_EnableICache();</font></font>
' c- N$ }7 f7 U. ~2 p5 m - <font face="Tahoma"><font size="3">( |7 w+ Z' {" P5 g+ N
- </font></font>
, [) y) S* T! F4 O7 G - <font face="Tahoma"><font size="3"> /* Enable D-Cache---------------------------------------------------------*/</font></font>' G4 E3 m. n" I# _
- <font face="Tahoma"><font size="3"> SCB_EnableDCache();</font></font> |6 O/ ^# i4 m2 t- w* x: y
- <font face="Tahoma"><font size="3">$ m1 ]. L# B2 B9 e4 C$ [
- </font></font>
1 H2 j! L9 ?5 ]. W - <font face="Tahoma"><font size="3"> /* MCU Configuration--------------------------------------------------------*/</font></font>
- L! c" s6 g6 J' A3 U" z - <font face="Tahoma"><font size="3">
* q: N; `4 G( B1 N+ b - </font></font>- e, ~: d3 N* l& u7 M) P, H! M/ c
- <font face="Tahoma"><font size="3"> /* Reset of all peripherals, Initializes the Flash interface and the Systick. */</font></font>
g6 `/ G) p# U0 r- L# c$ u - <font face="Tahoma"><font size="3"> HAL_Init();</font></font>
+ {0 }, i+ F# v/ R4 @! f - <font face="Tahoma"><font size="3">6 a: n2 E% l+ y( Y4 S
- </font></font>9 C( G0 `5 ]- K! S" d9 t
- <font face="Tahoma"><font size="3"> /* USER CODE BEGIN Init */</font></font>
% Q; s4 ?! U/ L6 N: e+ B1 d - <font face="Tahoma"><font size="3">
0 Z7 [+ ^ N- Q/ Q2 l - </font></font>
& G v; m. y4 o( @1 ?* H; R% } - <font face="Tahoma"><font size="3"> /* USER CODE END Init */</font></font>
) }7 V: L2 D/ b - <font face="Tahoma"><font size="3">. L- Z2 I: ^1 u- f; {
- </font></font>
# v) b- Z/ T2 i - <font face="Tahoma"><font size="3"> /* Configure the system clock */</font></font>
& s L; M9 B8 m1 x' ] - <font face="Tahoma"><font size="3"> SystemClock_Config();</font></font>
. F/ w/ J* L7 f0 j - <font face="Tahoma"><font size="3">
# X$ M) u+ Z% l, ^* [8 c l - </font></font>: k5 P$ T3 b% [# t+ i/ i
- <font face="Tahoma"><font size="3"> /* USER CODE BEGIN SysInit */</font></font>
# e$ h0 H9 S$ s& p; Z# n3 Q% N( ? - <font face="Tahoma"><font size="3">5 V( ~' [/ V8 n8 q) _" s; E
- </font></font>
: P+ O9 }. C# _1 o- J - <font face="Tahoma"><font size="3"> /* USER CODE END SysInit */</font></font>) d+ p5 C8 z9 l' T# M$ ` Y
- <font face="Tahoma"><font size="3">8 }0 _7 S& e6 m! d" t3 s
- </font></font>
1 C; _ t- U! w: n - <font face="Tahoma"><font size="3"> /* Initialize all configured peripherals */</font></font>
7 }0 f/ e# y6 O, A - <font face="Tahoma"><font size="3"> MX_GPIO_Init();</font></font>
( ?6 ?6 J. q3 a+ p - <font face="Tahoma"><font size="3"> MX_DMA_Init();</font></font>
; P1 X7 |/ D( J7 w& @% S, w( F/ S - <font face="Tahoma"><font size="3"> MX_USART1_UART_Init();</font></font>
& O8 [ b" S+ Q9 j4 U* N: U - <font face="Tahoma"><font size="3"> /* USER CODE BEGIN 2 */</font></font>
2 d% k- _4 Q D& a4 l5 m - <font face="Tahoma"><font size="3"> HAL_UART_Receive_IT(&huart1,aRecBuff,1);</font></font># s! Y6 m, Y1 H+ v" f- U
- <font face="Tahoma"><font size="3"> printf("uart test is run!!!\r\n");</font></font>
. ]/ w8 V2 d- @6 y' j - <font face="Tahoma"><font size="3"> </font></font>/ \, s, M! E. {+ p9 F: A0 u x
- <font face="Tahoma"><font size="3"> /* USER CODE END 2 */</font></font>
- a- |% L. Q# G& z - <font face="Tahoma"><font size="3">0 ]& U; y" x& O5 e
- </font></font> M1 W" e6 b& i9 q) O7 z
- <font face="Tahoma"><font size="3"> /* Infinite loop */</font></font>- k4 E: ?) j9 E; V
- <font face="Tahoma"><font size="3"> /* USER CODE BEGIN WHILE */</font></font>
/ L! ? R) g/ z- y6 ?$ z - <font face="Tahoma"><font size="3"> while (1)</font></font>/ @+ I5 O6 b# h0 H# k- g
- <font face="Tahoma"><font size="3"> {</font></font>
2 Z& W% m% \% } - <font face="Tahoma"><font size="3"> /* USER CODE END WHILE */</font></font> i2 q0 O1 B* c4 }$ I3 k
- <font face="Tahoma"><font size="3">, h0 l, b7 {2 l% t W& z% \
- </font></font>! D; W6 U* z2 k4 ^0 f
- <font face="Tahoma"><font size="3"> /* USER CODE BEGIN 3 */</font></font># G( l/ U' Z/ G0 A A' D5 f8 p
- <font face="Tahoma"><font size="3"> HAL_UART_Transmit_DMA(&huart1,data,strlen((char*)data));</font></font>
( |8 @6 y: M/ m( k - <font face="Tahoma"><font size="3"> HAL_Delay(1000);</font></font># {5 V3 m: n" Y- a! y& s" I s
- <font face="Tahoma"><font size="3"> }</font></font>2 ~% s. m: [' ?4 j' A. {" B. w5 G: O
- <font face="Tahoma"><font size="3"> /* USER CODE END 3 */</font></font>3 {. U+ s$ P: C
- <font face="Tahoma"><font size="3">}</font></font>
复制代码 9 j8 j$ Q' j4 K$ H. Y0 S! E2 Z
四、打开串口调试助手,观察现象,串口每隔1S输出一次数据
; E; G: ~) l( C7 n6 M+ f" x3 I" q5 F% E8 d
/ i* c2 _0 u' X8 M7 f% R0 u& M$ a, i6 A- _! H% e) r5 x- b% I" ?
4 j" a5 d, A& c" i: \! X2 y+ j4 X. \ |