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

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

[复制链接]
STMCU小助手 发布时间:2022-1-31 15:00
使用查询方式的USART:
设置时钟:
RCC_APB2Periph_AFIO  功能复用IO时钟
3 W) D$ A. x$ h: E. g! o- WRCC_APB2Periph_GPIOA  GPIOA时钟
RCC_APB2Periph_USART1 USART1时钟
你可以用
& h% v6 ?+ m8 d) z4 A//使能串口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;1 t. F0 E) u% i) F7 C
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;, D, B: Y; Q8 r
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  //推挽输出-TX* l: C# H. E6 G4 n9 X* _- E$ L& m
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;) t6 U) L) U# M  H# a
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入-RX$ J& s$ @9 d# {
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寄存器
5 Z% b  e& ^& E1 [; OUSART_Cmd(USART1, ENABLE); //启用
就好了~!
而3.0的库需要
USART_InitTypeDef USART_InitStructure;
USART_ClockInitTypeDef USART_ClockInitStructure;
USART_StructInit(&USART_InitStructure);6 e7 i/ j# f/ W& \' k
USART_ClockStructInit (&USART_ClockInitStructure);! ?. @" b9 E5 _5 X! N4 q) [( z4 I5 e
USART_ClockInit(USART1, &USART_ClockInitStructure);: d* [' H" U/ {3 U4 l; c/ l
USART_Init(USART1, &USART_InitStructure);
1 M8 j6 K* H9 D& {9 J4 U3 |# }USART_Cmd(USART1, ENABLE);
//只是多分出了1个 USART_ClockInitStructure 我也不知为啥要这样??为了同步异步模式?USART_InitStruct中指定的参数内容为:(2.0的)
typedef struct3 V2 i: f' s6 L% m! k/ G
{' l3 M0 v/ I( Z) S" P
u32 USART_BaudRate; //USART传输的波特率
9 E; `' x0 V3 c: d* O. H2 c& g# ]" A7 du16 USART_WordLength; //一个帧中传输或者接收到的数据位数通常是8
* {( M: f4 Q6 Du16 USART_StopBits; //停止位
/ h7 k  G4 D0 Q# i( Mu16 USART_Parity; //奇偶校验
8 s4 k6 [& t5 \" o5 i" Cu16 USART_HardwareFlowControl; //硬件流控制模式使能还是失能% n7 @+ C) l. a2 F  d+ c
u16 USART_Mode; //指定了使能或者失能发送和接收模式
% I; Q) Z' k" x+ e, l% |u16 USART_Clock; //提示了USART时钟使能还是失能
( o$ D2 N- U7 X5 \: o% Y0 bu16 USART_CPOL; //指定了下SLCK引脚上时钟输出的极性
/ s2 r* V7 @- R& Du16 USART_CPHA; //指定了下SLCK引脚上时钟输出的相位
) f- z3 P7 b9 x* F, Y5 e$ s. \% z$ Qu16 USART_LastBit;
//来控制是否在同步模式下,在SCLK引脚上输出最后发送的那个数据字通常用USART_LastBit_Disable
! Y9 V0 s4 |# e+ f1 B: A9 @} USART_InitTypeDef;
我靠~!太细了~!我只知道(9600,8,n,1)这就够了 其他的统统默认~!
USART_StructInit(&USART_InitStructure);. S7 C' A4 q* Z# {/ d; X2 P. i  l+ j
USART_ClockStructInit (&USART_ClockInitStructure); //2.0不用这句,这样就设好了好了~!自动为您装填了默认参数。默认的参数如下(3.0的库):
void USART_StructInit(USART_InitTypeDef* USART_InitStruct)4 c2 n6 t3 u- G3 `, D
{
1 m) W- H0 ?/ U, P( g* ^2 W5 c  {! iUSART_InitStruct->USART_BaudRate = 9600;
- ~5 ^4 Y: a! c0 WUSART_InitStruct->USART_WordLength = USART_WordLength_8b;
$ J6 Y! ]  C. j$ o4 j) |0 RUSART_InitStruct->USART_StopBits = USART_StopBits_1;9 |, T6 b8 g  i6 t. ]
USART_InitStruct->USART_Parity = USART_Parity_No ;
8 ?, }  V' D& T! h. r7 t1 O0 H* f; G: oUSART_InitStruct->USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
( M' p% H  O( I- _& M' sUSART_InitStruct->USART_HardwareFlowControl = USART_HardwareFlowControl_None;   J+ d! p6 T" C+ c; }. K4 T' y
}
void USART_ClockStructInit(USART_ClockInitTypeDef* USART_ClockInitStruct)4 V3 F7 s+ L2 d. A+ @1 V! {4 I
{
& G+ f* _. H1 U: |1 ?4 EUSART_ClockInitStruct->USART_Clock = USART_Clock_Disable;! Z( K5 F, G* p9 W
USART_ClockInitStruct->USART_CPOL = USART_CPOL_Low;
. a/ s, M7 p3 PUSART_ClockInitStruct->USART_CPHA = USART_CPHA_1Edge;
/ k8 z7 p8 g5 E3 WUSART_ClockInitStruct->USART_LastBit = USART_LastBit_Disable;
/ j* m9 k2 L7 ]7 Z9 F) T}
/************************************************************************************************/
当然了你也可以自己设参数,比如这样。
void USART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;7 Z6 ]6 y# P( c' i7 {5 u$ C
USART_ClockInitTypeDef USART_ClockInitStructure;
USART_InitStructure.USART_BaudRate = 9600;
2 r! _. y: ?$ Z/ [4 eUSART_InitStructure.USART_WordLength = USART_WordLength_8b;4 y6 ?5 c4 M8 Q( f
USART_InitStructure.USART_StopBits = USART_StopBits_1;% r" L" \3 A$ K. |8 P# |
USART_InitStructure.USART_Parity = USART_Parity_No;
, ^$ l6 n" y" f+ rUSART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
+ w% q& a+ h9 L3 d  B, K+ I2 \% zUSART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;) a: g6 j/ A: u
USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;+ i( }# l/ o& |
USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;4 S& [. O7 }; O$ G  }: \
USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
: |; i- d- K! ~0 K4 EUSART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;
USART_ClockInit(USART1, &USART_ClockInitStructure);/ B: n, v- G* h- h' n
USART_Init(USART1, &USART_InitStructure);
USART_Init(USART1, &USART_InitStructure);
USART_ClockInit(USART1, &USART_ClockInitStructure);
. M/ G9 b" t2 o* {4 z6 x6 I' qUSART_Cmd(USART1, ENABLE);
} ////USART_ClockInitStructure.USART_CPHA= USART_CPHA_2Edge;除了这句以外其他的都和默认的参数一样,二者有啥区别我至今也不太清楚但就一般的应用来说两个都可以正常工作。
收发的方法:
1.发送
void USART1_Puts(char * str)
/ F8 \; f( }( k3 Z" B! J9 n{- F; Z( c% V. A# D
while(*str)
# b0 U% B: G2 h/ D, w7 b  ~$ d$ p{" |5 C6 ~# e* ~  h
USART_SendData(USART1, *str++);
! [; c% \  o$ V# ^, L' _0 cwhile(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
' t2 `7 s; {# E8 N}/ G3 a7 B+ o; q. I  J: o' ~8 ~
}
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)) h- X& W" V1 _1 G
{ , {) |. Y9 c8 j' G3 N( @3 w
uart1_get_data = USART_ReceiveData(USART1);
4 F8 m/ y5 x' N" H, G# n# bUSART1_Puts("\r\n获取到串口1数据:");
5 ?1 n! R& P/ a& {" [! ~8 {USART1_Putc(uart1_get_data);5 }% I/ J% x, m% t4 p
USART1_Puts("\r\n");
% l, b2 X# u: F  o9 W: I}
}
查询法的可以看出要不断扫描不是很好,下面介绍中断法。
首先配置时钟:这里我拿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" }2 u# b* g; \: R, C; M
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;: ~" L3 v" e+ l3 Z$ h
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
. v* G" A4 d3 Y# B6 ]9 Z6 h2 l3 \GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
2 ?' j; K+ D5 `GPIO_Init(GPIOA, &GPIO_InitStructure);
// A3 做R2X
- X: ]' O- t5 U2 ~) Z& EGPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
  ~( z8 q, s8 r9 v! _% `  BGPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;2 [% y1 e+ I0 V8 c# I
GPIO_Init(GPIOA, &GPIO_InitStructure);
配置SUART2:
USART_InitTypeDef USART_InitStructure;
, N- [1 w4 `" a$ @1 g# I; }! JUSART_ClockInitTypeDef USART_ClockInitStructure;
USART_StructInit(&USART_InitStructure);
* {( ?% b% i3 p- o2 z- yUSART_ClockStructInit (&USART_ClockInitStructure);* c& x3 ?, m% s7 z& K* w+ f/ g: s
USART_ClockInit(USART2, &USART_ClockInitStructure);2 ]3 b: A9 B7 {6 J! T
USART_Init(USART2, &USART_InitStructure);
# x9 k/ x$ z  s8 }USART_Cmd(USART2, ENABLE);
. J9 n: a0 P1 J# M! j( h& j
赞 收藏 评论0 发布时间:2022-1-31 15:00

举报

0个回答

所属标签

相似技术帖

官网相关资源

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