这个实验向我们演示,不同的串口使用printf函数来输出对应的信息。默认printf()函数使用usart1,剩下usart2~4分别使用USART2_printf(),USART3_printf(),USART4_printf()输出信息。
' `7 O$ `9 N; e& s2 ?8 o
7 [0 M F$ i" H6 y1 a新建工程
5 b- N1 Q. U4 z' }# o1、新建芯片工程"g071c8/g071rb",打开SWD调试接口、HSE/ c2 r' t; Y2 ]; x& V! ]6 T: G
5 P7 @# h7 {5 X# S! Z; }, I
9 O2 g8 L! P( G- V* r. N2 L3 D
% V. S+ d* h9 _
e% }+ n1 B( t! k- X( [" {( V% ?2 C. | C( Q
2、 设置时钟树,HCLK=64MHz
7 j) C+ i+ R# k( C& [3 {7 m* P+ V4 L5 Y, t* o, H
( p) n% y" o# s; h8 S
1 P. p/ o$ H0 R1 F3、 查看数据手册,看那些引脚可以用作usart1、usart2、usart3、usart4。把这些引脚设置成TX,RX- K0 X. M! i; v# }1 }
: ^; p& N/ h" B* h" bUSART1A10,PA9。USART2A2,PA3。USART3B10,PB11。USART4A0,PA1 N: s- l2 U n. o% N
3 h( j* d J1 N! L- ^
5 u' A1 _# y# p* d' E7 P
) F+ G) n& L7 b# M: K5 ~3 c. O2 @
4、启用USART外设,参数按照自己的实际来填。模式设为Asynchronous,115200,8bit,None,1
2 C0 n0 q U1 E$ Z! Y% r
5 G0 G4 W s8 w5 s4 L* N/ u( @# V; \& y, T9 C' d- v; H
% V* f" u7 y- b; z
5、 项目管理填好信息。生成代码"GENERATE CODE"3 K1 p' Y+ ]2 p; ~
* K! \- W2 X6 \( D
6 J+ |# {; j7 i1 L
$ U- ^1 y7 Z+ B: E% x' m6 L
8 x& o- }" i% M; \& f+ N4 R/ J5 J% X* M
( g! G- _1 |* [) Q; C6、使用MDK打开工程,debug选项中勾上“Reset and Run”
8 F& W8 U: G9 q- a3 u0 A
: e* ~2 K) J9 o% D7 o+ w+ r/ E7 D代码部分3 v7 _. s# \! G/ S
1、usart.h中添加如下代码
. H, a# G. W- A% |! F, W R' u# B8 }, |
- #include <stdarg.h>
$ ~- Z/ _; N: t8 K5 p, l - $ I* y$ N& J9 G& D+ P) Q
- /* USER CODE BEGIN Prototypes */. W& s$ E. Y& m8 r4 Q' h2 O
- 9 q" e; P% b2 T6 r% A# Q% q* T7 `
- void USART4_printf(char *fmt,...); //重写usart4的printf函数3 |/ V H9 ~& |7 s
- void USART3_printf(char *fmt,...);
) |% S$ w8 |2 N9 p" w6 @ - void USART2_printf(char *fmt,...);
' Y" C4 O4 N. ^ - $ H+ z& z q* z) }* V
- /* USER CODE END Prototypes */
复制代码 6 ~5 R7 i) I, _! A
2、usart.c添加如下代码
5 o h7 L7 n: P* G! z& N& n4 q
, S' B0 B# k9 j( O' z* a2 y4 V- /* USER CODE BEGIN 0 *// y" l2 Q2 d3 l$ ~& e) k% q8 ^* @
$ I3 Y- I1 q& g" S# R+ {+ g! e- #include <stdio.h>8 X8 r8 P+ C7 B2 w$ m
- #include <string.h>
* V' D) r3 u+ x* \: q2 l$ O7 a4 ?/ x. v* ~ - #ifdef __GNUC__
2 y& Z1 @" N$ W0 f1 k; N - /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf9 g+ }! G0 e- R$ }, g% h5 v$ {
- set to 'Yes') calls __io_putchar() */. Z6 J1 r1 x; A7 f6 q1 s7 T
- #define PUTCHAR_PROTOTYPE int __io_putchar(int ch) }8 \5 V- f+ ^2 T2 _
- #else
3 r& K- q6 L9 |, O& i; L2 k - #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
0 M- Z! y2 r* Y# u( ` - #endif /* __GNUC__ */* k8 ]/ P3 v* ^) _, s; J; ~
- /**
/ [. w" B* O* I - * @brief Retargets the C library printf function to the USART.$ }3 L/ U2 W4 s* D% i
- * @param None" `! E' ~1 Z5 C+ D. C; ?4 I
- * @retval None4 ^. L* e9 ?- y: A! P
- */. G2 ^/ H1 Q, j6 p2 X' l; ~- R
- PUTCHAR_PROTOTYPE2 @6 {% d0 e: l7 G3 O
- {
# F, n% d0 p) u - /* Place your implementation of fputc here */
4 H' @# z6 O/ K/ j" ?. h - /* e.g. write a character to the EVAL_COM1 and Loop until the end of transmission */
( q3 o+ P" E3 C; p; ^& @ - HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF);7 ?1 a6 |) \" O! [# K @
- return ch;+ u; g; u( q* H. o2 p
- }" k9 j" t$ {. t0 {" n
8 W$ \/ O9 o+ p1 R- #define USART_TXBUFF_SIZE 256 //定义串口发送缓冲区大小 256字节
/ r \; V& T% n9 O6 i - __align(8) char usart_txBuff[USART_TXBUFF_SIZE]; //字节对齐缓冲区! R% \, @' m8 w) V( ^, e1 Z' ^5 g7 \
- ! S# ~% I- l: r, ^( Y2 M
- //usart4的printf()" u4 O; u$ o( {4 A8 x
- void USART4_printf(char *fmt,...)
1 V* i, ]2 h1 h5 a$ k7 | - {
7 `% T4 {/ y4 E7 |5 ^ - uint32_t i,length;
# }) @+ B7 m! q) D+ L5 R6 l - va_list ap;
0 G+ O/ ]5 q* G- e) \8 {5 y* |+ f - va_start(ap,fmt);4 i8 J1 k% w7 F% e0 [
- vsprintf(usart_txBuff,fmt,ap);
( C( e& F9 g, d* k* \+ Z* f* m4 \4 u - va_end(ap);
, y# m2 |, [. k) U9 A6 h [4 A - length=strlen((const char*)usart_txBuff);0 t/ N6 d: `: S3 G6 X- Y* Z
- while((USART4->ISR&0x40)==0);+ _. G9 C0 n v) Q
- for(i=0;i<length;i++)( H2 z) `5 Y. j& l! N
- {8 ^+ m% ~0 X* N" E, [: l
- USART4->TDR=usart_txBuff<i>;
4 }' {/ Y6 U$ `8 V - </i>while((USART4->ISR&0x40)==0);
/ r& O# l. {, v! D - }- |, q* g6 Y" F0 ^
- }; e, C4 U! E0 Z2 J
# L5 S. ~( O6 Z7 Y, q- //usart3的printf()
8 U$ m5 Y2 B( Q3 @ - void USART3_printf(char *fmt,...)
% j: O5 d9 E& M1 U* L - {
, ]& l e- O0 Y( S - uint32_t i,length;
& U- n& T9 j: ~& Z0 ^" ^ - va_list ap;
( u: o- M2 `: B- h9 w$ t - va_start(ap,fmt);
( g* l+ m9 ?# U - vsprintf(usart_txBuff,fmt,ap);/ Q# e4 ]: p# c2 S' v1 z2 i, q
- va_end(ap);. g) ~* \* K# n3 `% F9 A
- length=strlen((const char*)usart_txBuff);2 F2 u1 q- Q/ }! l
- while((USART3->ISR&0x40)==0);
" \' D/ ]+ S$ P- v' a0 Z: @- h - for(i=0;i<length;i++)
! v' F X% n" g+ ~' X - {8 Y7 F" R) ]: e& c) ~. i
- USART3->TDR=usart_txBuff;
* x. c* c8 [$ ~ - while((USART3->ISR&0x40)==0);
! s: M( v, c- L. Q4 c - }
* ]9 }" H2 k7 n2 @; g/ { - }& o5 I+ I3 O# T4 o/ n: f: ^
- 1 I/ k! D( [; A. O) Z
- //usart2的printf() }; c; g; L( R) j" g1 P4 D
- void USART2_printf(char *fmt,...)
% \- j1 T1 P! |& y( }: Q. C - {
8 `* E. c& j. }5 @ - uint32_t i,length;
/ X0 ~) Z) J7 j/ a( } - va_list ap;* U. g0 u4 J4 q8 T4 M
- va_start(ap,fmt);
$ Z/ O7 G3 p3 f) b - vsprintf(usart_txBuff,fmt,ap); D3 ], G* S7 I9 n! \/ G9 |3 j
- va_end(ap);
% P( Y3 n7 |3 {9 L - length=strlen((const char*)usart_txBuff);/ o3 y% _) s+ A' h
- while((USART2->ISR&0x40)==0);
: G+ T4 y7 ^0 X s, R2 V" H - for(i=0;i<length;i++)
2 b/ ?) M8 V6 \5 W0 O% k3 U8 e - {
* y, |- ]$ ~3 a6 n8 ] - USART2->TDR=usart_txBuff;- `' C& C: }, B& c+ y) d
- while((USART2->ISR&0x40)==0);
| r& h2 @& t. v - }0 N2 r. D+ ?! \5 _' Y3 f6 Q& o) s7 F
- }6 e0 p. [; d! y/ [) x4 y" K
& C R6 z; p: p% z4 Q8 \- /* USER CODE END 0 */
复制代码
! n9 b& O/ x2 \* K" A' {3、main.c中添加如下代码+ c3 S3 u N5 @4 {; x
) r0 [; a$ R7 v/- * USER CODE BEGIN Includes */1 ]- f4 S0 [" \% @9 k
- #include <stdio.h>
! j3 U. y- N7 T3 o8 ]* q/ H - /* USER CODE END Includes */
! l o3 A' _7 l$ z; C3 i; w - ; [2 O# e" i* m* O
- int main(void)
/ ^3 P$ x# o5 B2 G2 {0 h; h! R - {. B5 {/ U7 e. o5 T( W8 ^
- while (1)/ {4 ~" c. q0 s1 J
- {
4 z4 _& O% b# v: I5 X; o7 e - /* USER CODE END WHILE */3 Y3 e# y( D u8 e* g
# B( q: i9 L* m; l0 v/ ~- /* USER CODE BEGIN 3 */
" c9 Y0 q& i% X+ D - HAL_Delay(1000);+ n. K9 l% O) \5 }6 I. G
- " }3 C! t) L% E. r7 I2 R; G; m
- USART4_printf("This is the signal from serial port 4.\r\n");6 {! F$ j2 G7 B1 }& \& k
- USART3_printf("This is the signal from serial port 3.\r\n"); ]! Q& {. Y& a4 l
- USART2_printf("This is the signal from serial port 2.\r\n");
+ h' I8 ?4 G1 m+ f: e - printf("This is the signal from serial port 1.\r\n");
8 j( U6 s, G) O' C- p; J' Y% q1 \ - }
1 m! s$ h0 H; e, W, ] - /* USER CODE END 3 */
W+ C5 w, o8 K0 F" R4 V1 o/ F7 ~ - }
复制代码
6 M. b. ]2 m, C9 v4、最后编译,烧录固件,串口输出如图( O1 ^; ^" Z. d( ]
5 h( r8 w! @, J9 Z2 w6 X4 I* O4 l
2 e/ A% m3 Q/ U+ u) W1 l( g0 L
1 r# P' w" w4 x: S1 | P
|
学习学习