你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

【经验分享】STM32 USART 串口简单使用

[复制链接]
STMCU小助手 发布时间:2022-1-31 15:00
使用查询方式的USART:
设置时钟:
RCC_APB2Periph_AFIO  功能复用IO时钟
" u; l, H7 {$ B* U! WRCC_APB2Periph_GPIOA  GPIOA时钟
RCC_APB2Periph_USART1 USART1时钟
你可以用
8 D. g. j4 E1 d6 i9 T( q( ]" ]//使能串口1,PA,AFIO总线  RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO|RCC_APB2Periph_USART1 ,ENABLE);
或直接
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ALL,ENABLE); //全部APB2外设时钟开启
注意USART2的你开启为 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
设置GPIO:
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  L( K7 O2 B. G1 TGPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
/ x( X8 e, A- \9 IGPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  //推挽输出-TX
9 O1 W0 @( q0 ?& y6 E' OGPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
# [/ q* b6 t/ x1 Y* sGPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入-RX+ x. Z0 L+ [9 F
GPIO_Init(GPIOA, &GPIO_InitStructure);
设置USART:
这里我用的是3.0的库相对于2.0的库来说多了一步,先说2.0
USART_InitTypeDef USART_InitStructure;
USART_StructInit(&USART_InitStructure); //装填默认值
USART_Init(USART1, &USART_InitStructure); //根据USART_InitStruct中指定的参数初始化外设USARTx寄存器
, b+ v7 Z! M. F+ c+ HUSART_Cmd(USART1, ENABLE); //启用
就好了~!
而3.0的库需要
USART_InitTypeDef USART_InitStructure;
USART_ClockInitTypeDef USART_ClockInitStructure;
USART_StructInit(&USART_InitStructure);
5 s4 A* ^* c$ E+ m3 d% t3 @2 OUSART_ClockStructInit (&USART_ClockInitStructure);
8 T" E$ {/ q  E% v+ l& s  v! EUSART_ClockInit(USART1, &USART_ClockInitStructure);
0 R" N( [) Y9 b" C" Y( p% QUSART_Init(USART1, &USART_InitStructure);
3 Q3 f# u4 i/ v* Z) W+ gUSART_Cmd(USART1, ENABLE);
//只是多分出了1个 USART_ClockInitStructure 我也不知为啥要这样??为了同步异步模式?USART_InitStruct中指定的参数内容为:(2.0的)
typedef struct
8 s4 E3 w" a/ d1 a( M! o) a$ I{  _1 ^# t) V, u3 V* N
u32 USART_BaudRate; //USART传输的波特率
- n" Z! s1 B9 j5 S/ E6 mu16 USART_WordLength; //一个帧中传输或者接收到的数据位数通常是82 j+ E$ h6 ]2 q; `5 H# |% r$ E" s
u16 USART_StopBits; //停止位8 ?0 e1 n2 A4 I* \5 ?1 r
u16 USART_Parity; //奇偶校验9 ^8 ~0 v6 G7 m# ^$ B
u16 USART_HardwareFlowControl; //硬件流控制模式使能还是失能0 [3 T$ I  x& u) z( Y9 g" K
u16 USART_Mode; //指定了使能或者失能发送和接收模式
9 }/ ^- ^/ L7 Gu16 USART_Clock; //提示了USART时钟使能还是失能2 Y0 |& H6 I% @6 O" z1 Y
u16 USART_CPOL; //指定了下SLCK引脚上时钟输出的极性! o: h" c  f5 H. o( W
u16 USART_CPHA; //指定了下SLCK引脚上时钟输出的相位
$ S5 R4 s: J1 k; Y6 R/ ru16 USART_LastBit;
//来控制是否在同步模式下,在SCLK引脚上输出最后发送的那个数据字通常用USART_LastBit_Disable' l- }2 T, F% `) {1 O
} USART_InitTypeDef;
我靠~!太细了~!我只知道(9600,8,n,1)这就够了 其他的统统默认~!
USART_StructInit(&USART_InitStructure);) f4 N5 G# D5 @
USART_ClockStructInit (&USART_ClockInitStructure); //2.0不用这句,这样就设好了好了~!自动为您装填了默认参数。默认的参数如下(3.0的库):
void USART_StructInit(USART_InitTypeDef* USART_InitStruct)5 Y. q% P" `" g! y; N* a/ N7 |9 p- I
{, ^4 {- E" U" ^
USART_InitStruct->USART_BaudRate = 9600;
$ a3 x+ C/ W& J1 Z" CUSART_InitStruct->USART_WordLength = USART_WordLength_8b;
8 w1 K1 X; N$ u+ v" {& XUSART_InitStruct->USART_StopBits = USART_StopBits_1;
  r" V) f: Y( Y9 l, OUSART_InitStruct->USART_Parity = USART_Parity_No ;
- R: v; H: m4 C  b7 nUSART_InitStruct->USART_Mode = USART_Mode_Rx | USART_Mode_Tx;- `0 h# v' U1 i
USART_InitStruct->USART_HardwareFlowControl = USART_HardwareFlowControl_None; 6 s  Y) [9 d  ~+ V: i
}
void USART_ClockStructInit(USART_ClockInitTypeDef* USART_ClockInitStruct)
4 p  }4 F! U# q* O0 n/ g{
; w5 U! w; l/ M: O. CUSART_ClockInitStruct->USART_Clock = USART_Clock_Disable;, ^, K, N$ l  U9 o" F
USART_ClockInitStruct->USART_CPOL = USART_CPOL_Low;
8 g0 Y  M' e0 |, T) E3 zUSART_ClockInitStruct->USART_CPHA = USART_CPHA_1Edge;5 x# L, [! v1 s+ @9 i
USART_ClockInitStruct->USART_LastBit = USART_LastBit_Disable;
, {$ e* [% V7 ?  M* H+ A' ~. d% p- @}
/************************************************************************************************/
当然了你也可以自己设参数,比如这样。
void USART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;1 p" c7 d: Q3 h( I9 h7 M" F! v( v
USART_ClockInitTypeDef USART_ClockInitStructure;
USART_InitStructure.USART_BaudRate = 9600;7 }( Z, F9 P$ O% G% s! J+ }
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
( \5 p' k) a! A# rUSART_InitStructure.USART_StopBits = USART_StopBits_1;
0 `- |0 m# \4 P+ i* n1 SUSART_InitStructure.USART_Parity = USART_Parity_No;( n- U. o7 \0 }, T/ K
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;! O" {) s9 D  ?; `8 X+ i) D
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;7 Y/ S9 I" w' m
USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;" y% V+ y" u( D4 g& F5 t
USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;  j( c' Z/ `/ g6 b; [$ u( p
USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;" |) y" R) w" G0 f$ _7 K: N+ Q
USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;
USART_ClockInit(USART1, &USART_ClockInitStructure);
& }8 T8 A. P* D2 r: zUSART_Init(USART1, &USART_InitStructure);
USART_Init(USART1, &USART_InitStructure);
USART_ClockInit(USART1, &USART_ClockInitStructure);
. J+ s/ M# u5 @% g( n. c5 EUSART_Cmd(USART1, ENABLE);
} ////USART_ClockInitStructure.USART_CPHA= USART_CPHA_2Edge;除了这句以外其他的都和默认的参数一样,二者有啥区别我至今也不太清楚但就一般的应用来说两个都可以正常工作。
收发的方法:
1.发送
void USART1_Puts(char * str)3 e& a/ h, L, }, l# d6 x0 Z+ o
{- t5 F# `& Q( ]6 Y) p+ j, g
while(*str)9 o" X* F* J* B7 a9 @* N, X! v
{0 m, f. o8 g# X: j7 u
USART_SendData(USART1, *str++);
$ `2 k) I, J1 d# f9 A* i+ Wwhile(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);  {3 I3 s0 B7 L) d- {- b# _3 F
}
' \. a9 P6 S4 i0 z! A}
USART1_Puts("hello-java~!\r\n"); //这样就发送了hello-java~!
跟C语言的printf不太一样在于\n 并没有另起一行要用个\r这样在终端上好看。
2.接收
u8 uart1_get_data; //存放接受的内容
while(1)
{
if(USART_GetFlagStatus(USART1,USART_IT_RXNE)==SET)
7 y) {/ ]9 p5 ^  Y{
7 Q8 D. w% Z* v& T  P# p1 Y3 xuart1_get_data = USART_ReceiveData(USART1);
6 {0 I- u/ c. H+ K$ ?$ tUSART1_Puts("\r\n获取到串口1数据:");
' V, [0 j1 v1 y* Z' _USART1_Putc(uart1_get_data);- P. r4 D0 _0 W! e
USART1_Puts("\r\n");
6 O" o1 B: T6 P8 A8 E7 X7 ?}
}
查询法的可以看出要不断扫描不是很好,下面介绍中断法。
首先配置时钟:这里我拿USART2说事:
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE); //USART2和USART3都在在APB1上而USART1是在APB2上的
设置GPIO:
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO |ENABLE);
// A2 做T2X
! v7 \! O2 J- b$ M% _GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;( ^8 B3 g- _! a9 }/ t) w
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;1 z% a; t( P4 M$ j, n* }
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
$ w$ s4 W6 \4 s1 ?  qGPIO_Init(GPIOA, &GPIO_InitStructure);
// A3 做R2X% N+ G6 g! g, ?* f0 _! N4 l
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
& _  J) p0 Z; b, ?& i/ uGPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;- f% d' C2 D; ]* p5 f" W, a
GPIO_Init(GPIOA, &GPIO_InitStructure);
配置SUART2:
USART_InitTypeDef USART_InitStructure;0 N* U) p2 f. d' @$ T* U% J
USART_ClockInitTypeDef USART_ClockInitStructure;
USART_StructInit(&USART_InitStructure);; ~. H$ e1 \- y, u1 U
USART_ClockStructInit (&USART_ClockInitStructure);
* ?& F, B( l2 S5 }# i; \USART_ClockInit(USART2, &USART_ClockInitStructure);
& w0 X' D- D6 K) F) bUSART_Init(USART2, &USART_InitStructure);
9 K% }. ~2 W+ I1 l# s6 M* f3 r1 nUSART_Cmd(USART2, ENABLE);

; D$ A% q* \) ?5 e3 L
收藏 评论0 发布时间:2022-1-31 15:00

举报

0个回答

所属标签

相似技术帖

官网相关资源

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
人形机器人运动控制、感知与智能配电
半导体创新技术与应用方向
EE架构与软件定义汽车
12V/48V 汽车智能配电(SPD)
区域控制单元(ZCU)与分区架构
关注我们
st-img 微信公众号
st-img 手机版