这个实验向我们演示,不同的串口使用printf函数来输出对应的信息。默认printf()函数使用usart1,剩下usart2~4分别使用USART2_printf(),USART3_printf(),USART4_printf()输出信息。' o. D7 X5 w3 p6 K: R. s+ h
4 Z% x- {& c" a2 g& n* f$ @
新建工程0 F6 K8 b, e6 G' D
1、新建芯片工程"g071c8/g071rb",打开SWD调试接口、HSE( C$ B; G4 ]6 O1 F" K4 F/ Y
) L7 I1 K$ X! o" _: w$ Q
, f3 @# \* t. B" k; B$ f' t5 Y9 \
8 {/ d U9 T6 U8 l( U
3 C+ V% O$ V+ `4 g+ |! F$ J, s; B7 ?0 i6 I" N. y
2、 设置时钟树,HCLK=64MHz- H( N; I1 n9 @, g1 k: E9 B
1 x: v! Q( \3 d% N# Y! ~
5 X: f0 T" l* Q( p+ E/ b _2 l$ v! n) a% C& b0 w+ {# n
3、 查看数据手册,看那些引脚可以用作usart1、usart2、usart3、usart4。把这些引脚设置成TX,RX1 W4 L+ f2 ~# z2 j4 m) {9 Z; g/ ]
T) F! V5 }: h( A5 Q3 T U! a" l4 a
USART1A10,PA9。USART2A2,PA3。USART3B10,PB11。USART4A0,PA1" n$ \4 u0 s2 H* t$ `
# ~5 T9 y+ b1 g) ^) O" z/ _* x
5 z3 u2 D y8 K. ^! r
% V, ~% j2 m# Q, c4 {, L+ V6 `4、启用USART外设,参数按照自己的实际来填。模式设为Asynchronous,115200,8bit,None,1
- A0 D( X! I5 [. Y U+ ?# J: G
! e. }2 W% [% b/ W/ N$ |4 u
6 V# o! Y$ F- X; L# M' V) Z4 {" b
* z% o# Y1 s2 |7 r5、 项目管理填好信息。生成代码"GENERATE CODE"1 u8 t! D- v7 L' f1 s$ k; b
# ^! y7 X4 v+ q: r* `
- Q8 r, ^/ g) W. w0 C& J
9 m" m2 U# M; S
9 O) a8 U b& {
& T4 n3 i2 q* O$ X3 ~$ B6、使用MDK打开工程,debug选项中勾上“Reset and Run”
5 X; ?/ U- O1 v
9 J# S. a( r. w" n) E, ^& ?代码部分
: |- h' b! }% B/ o) n1、usart.h中添加如下代码! y0 ^- {4 v- v6 @
! O' i. S) t9 Q/ P
- #include <stdarg.h>0 [7 X$ E! v( d/ D! |
8 {( B! E* Q) v* Q3 O4 c% t- P- /* USER CODE BEGIN Prototypes */5 p1 ]: E6 `$ m! v( S( q8 |2 l y3 `1 C
( F5 j2 [+ r/ o4 {1 h* A- void USART4_printf(char *fmt,...); //重写usart4的printf函数
+ S! U" s; R, w# m' r9 {6 N - void USART3_printf(char *fmt,...);* u$ I% u) N1 l+ s4 l( A$ _- R
- void USART2_printf(char *fmt,...);2 n1 O, K. v; I
u+ {( o- \, i9 U' o- /* USER CODE END Prototypes */
复制代码
: f) B2 d4 P9 @9 [! x" Q2、usart.c添加如下代码
' b% @& g: I g6 `! q, g) J
, T" u! f' \% ^- /* USER CODE BEGIN 0 */4 e/ F3 k0 [! e: h# l. B, d- O" |
- 6 B5 i. u, ]8 k u4 \) w; t1 X
- #include <stdio.h>4 F7 c6 L: R" l! b @* E
- #include <string.h>
4 z; S. O2 [. s, A' Y/ {' o; \ - #ifdef __GNUC__, s- m/ b% E9 F8 M& |. i9 P/ R
- /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
' d( |$ `! p/ Q9 k3 z - set to 'Yes') calls __io_putchar() */3 ^3 W# g! R1 P; S: D# _$ h5 n
- #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)0 ?3 ?+ X+ ]5 s& q, {: d
- #else
( Z9 S9 f) u) T$ y% J3 C - #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
+ j- P+ e, N% V3 I - #endif /* __GNUC__ */
7 G$ X; a# ~2 u+ i' d& Z - /**+ }% x* Z) j4 E6 W9 R" z
- * @brief Retargets the C library printf function to the USART.6 S# o! ^* Z6 A, F- N& T
- * @param None
, q8 `& {& J5 i - * @retval None
* X& x( u4 s. m3 N: L - */
9 ~& _ B- O1 A - PUTCHAR_PROTOTYPE
* x2 f* W! x5 a$ r - {
5 v" \, D: p$ Q$ f8 I$ T+ r, M - /* Place your implementation of fputc here */
/ l r$ ~& L* i9 z% k - /* e.g. write a character to the EVAL_COM1 and Loop until the end of transmission */
1 R) o! b0 t* h - HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF);
% t: B8 |- _+ } - return ch;
" w: a1 a" D" l - }* z- P5 l1 n. C$ Q% }5 m1 h
- 5 m* a7 W E" K# P" g
- #define USART_TXBUFF_SIZE 256 //定义串口发送缓冲区大小 256字节- W4 V1 ~, I3 X# i
- __align(8) char usart_txBuff[USART_TXBUFF_SIZE]; //字节对齐缓冲区
& V9 Z; k) d2 B/ @9 ^( M' t - + P( r1 r" g+ m% Q6 q+ D4 s6 t7 T" ?
- //usart4的printf()
. _. r% M5 }, y' o+ T# L# ` - void USART4_printf(char *fmt,...)
# X2 t0 e6 v3 u - {* N- n# m0 ^. ~% r ?
- uint32_t i,length;5 E8 `, I y7 w6 a9 u' L
- va_list ap;' ~' u! l, ?2 r, ^' }; `& W, M
- va_start(ap,fmt);
" e' p) S- V- K - vsprintf(usart_txBuff,fmt,ap);
. `! d1 i6 c8 F6 U+ B* B, s - va_end(ap);. }$ t& ^- H+ a: O. t; |
- length=strlen((const char*)usart_txBuff);7 [0 l. l, `2 z1 y6 t
- while((USART4->ISR&0x40)==0);. Y4 {+ a1 w) Q" [
- for(i=0;i<length;i++)
" N/ ?( B9 R8 G& t" R8 p - {
$ k. t2 `7 D; v6 s - USART4->TDR=usart_txBuff<i>;
: d" J+ G2 n/ N: J1 B) H" a, X7 z0 v - </i>while((USART4->ISR&0x40)==0);0 a8 Y; a) X" ^
- }& m5 x! T3 \# t: A4 ~
- }# H& E( L3 `% }8 h O
/ J- a% y4 g# x7 P3 h- //usart3的printf(); C; T8 N/ [& }+ Y
- void USART3_printf(char *fmt,...)
`4 V" K- D! { P# ? - {
+ d8 @: @$ n) C6 h6 c - uint32_t i,length;
! i: C2 _ C' l2 l7 U- z - va_list ap;$ B p+ T S. M2 ?
- va_start(ap,fmt); J: h* }' [4 V0 g5 z: b$ W( K
- vsprintf(usart_txBuff,fmt,ap);
; t5 `% L+ I" k - va_end(ap);3 Z; U6 n* `0 f
- length=strlen((const char*)usart_txBuff);
# U0 B. B% c# d4 B* z, U6 ~: X. p - while((USART3->ISR&0x40)==0);% f" j0 u5 @2 O
- for(i=0;i<length;i++)
2 M) ^1 \, M! H. {% h1 x9 [1 s6 Q - {' m3 ]2 T$ [) D) T6 w6 a
- USART3->TDR=usart_txBuff;, \. F- @; f" E) C3 H5 C( c! A+ X& G' e
- while((USART3->ISR&0x40)==0);
' Y6 n; F b' F0 q/ V - }4 S, q3 s6 c0 a$ ?+ }4 d
- }
7 E7 T }+ c+ w+ J2 ? - ; Y4 [* P' a1 U/ b! _, [; C& P
- //usart2的printf()
# p( l) c( l. N+ e2 t Q! D& m - void USART2_printf(char *fmt,...)
5 \# i( a( d+ \; m# c, a$ Z - {
% b$ h6 J- C1 B& v0 O" r - uint32_t i,length;* z: e; F! _( W9 \
- va_list ap;
& f: I, L; |7 |+ }9 x/ `! h3 Z. ` - va_start(ap,fmt);8 \. `3 M! l# x' ]% k% d1 `
- vsprintf(usart_txBuff,fmt,ap);* v/ P7 `: N( }! ^) x' d
- va_end(ap);0 y3 S. \% H6 z+ p& Y% D" g, g
- length=strlen((const char*)usart_txBuff);& z+ I% |% J+ \, I
- while((USART2->ISR&0x40)==0);5 G u0 {6 v$ |) Z: e) w% @/ o
- for(i=0;i<length;i++)% | f) m) r" _5 h4 B
- {
r& |* W8 N. _: V( i - USART2->TDR=usart_txBuff;
7 q5 m, q# w j - while((USART2->ISR&0x40)==0);
9 P0 A6 p$ f" r9 T% J - }
+ Y4 ? {% P* {" z5 O - }# }9 f0 {4 _' l+ Y4 ~- A
9 g2 n4 [$ [2 L8 d- /* USER CODE END 0 */
复制代码
) v. g3 A& X' S& V6 m8 Y( b" a6 e3、main.c中添加如下代码
& T+ A/ l$ I4 R5 {' `% H
: E7 W, W; w l8 Z. H* n; g/ A1 _1 N/- * USER CODE BEGIN Includes */( s1 V1 O$ Y0 Z% [8 w m0 I* [
- #include <stdio.h>, P& R3 b$ X3 G; ^2 b, B) P7 I
- /* USER CODE END Includes */
3 _' V- r5 I$ Q" Q8 A& ]
1 G2 \ q5 _7 ~# a! B* z$ y- int main(void)
/ B- {! s- c& u* ]& @ - {
: N3 p* T7 y0 i( p - while (1)5 m+ F7 I* o6 X/ z z4 j
- {
2 `' u/ g8 A* P8 V8 y; y) ] - /* USER CODE END WHILE */
3 g# U" S: z U! G8 T - 3 @4 l; L9 ]/ M2 B
- /* USER CODE BEGIN 3 */
0 t/ b% D4 B- N- `$ Z: l% @ - HAL_Delay(1000);+ @! A4 C7 H" |4 X/ H
-
- G+ m5 {9 i- _; t$ _9 c C - USART4_printf("This is the signal from serial port 4.\r\n");
! K4 k2 u/ a! N" {1 v" W - USART3_printf("This is the signal from serial port 3.\r\n");7 Z1 @4 \" b4 t K- K# H" G
- USART2_printf("This is the signal from serial port 2.\r\n");7 C8 q- _1 `/ K" R2 L2 b
- printf("This is the signal from serial port 1.\r\n"); % A8 Z- ~3 G- f- M+ C
- }
! o) j5 m4 a* I/ R - /* USER CODE END 3 */
: Q6 Q! N6 X9 N - }
复制代码 ( P% f9 o3 v1 U9 T8 I! [. g
4、最后编译,烧录固件,串口输出如图
8 f* |/ s, h4 w6 a
+ x( T, i( y* c1 p0 T% w8 e& l& _+ b5 `% m1 g
5 @' {+ r4 k8 w
|
学习学习