找回密码
 注册
关于网站域名变更的通知
查看: 29|回复: 0
打印 上一主题 下一主题

STM32 UART 怎么初始化

[复制链接]

该用户从未签到

跳转到指定楼层
1#
 楼主| 发表于 2024-7-3 11:23 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

EDA365欢迎您登录!

您需要 登录 才可以下载或查看,没有帐号?注册

x
因为想用串口3,但配置了很长时间还是不行,为什么UART1,2行,UART3就不行的,最后原因是:使能GPIOB,端口时钟  USART3时钟,我只使能了UART3时钟,没有使能UART3所在端口GPIOB的时钟,所以导致无法正常启动串口3。

$ ~5 V% q% }6 {; k+ D0 ~8 M
下面具体写下串口配置过程:& \  s, P* x( x9 u
  1:系统时钟初始化,包括系统时钟和要开放的IO口和串口的时钟配置。0 z8 b" k! e" h6 T
  2:IO口初始化,包括引脚,速率,输入输出模式等。0 }# B* u% G+ ?; z
  3:配置USART的波特率,数据位等。
) p1 U. c2 f( H9 _
9 m& }' ]3 S& R
对应的3个函数,相当有条理
/--------------——————---------------------------------------------------------------------/
void  RCC_Configuration(void)
{
       RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB , ENABLE); //使能UART3所在GPIOB的时钟% a8 I! r' I, a4 |
       RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE); //串口时钟配置
}
& ?3 e5 y( {) R2 k/ M! ?6 p
void GPIO_Configuration(void)
7 }( [$ ~; R6 {1 Q- r/ @; U$ V{% A" @) ~6 T' c! I
      GPIO_InitTypeDef GPIO_InitStructure;
      // Configure USART3 Tx (PB.10) as alternate function push-pull ! N( A" p* f, c& T
      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;) p8 t) y1 i) H" M( ]
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;1 R. D& K  ^* B% d
      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;' [6 [, X/ A$ P) K
      GPIO_Init(GPIOB,   &GPIO_InitStructure);
      // Configure USART3 Rx (PB.11) as input floating ' L& r9 q  [8 k# _  a
      GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_11;
3 m! y" w; Z/ U1 U0 q  c, e      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;         
: ~2 `8 L7 V" P9 t+ C, [1 n  I2 a& R      GPIO_Init(GPIOB, &GPIO_InitStructure);
}

7 I0 X. m  L  p' E: J
void USART_Configuration(void)
{
      USART_InitStructure.USART_BaudRate = 38400;9 Y# ^& E+ A4 U5 f- T
      USART_InitStructure.USART_WordLength = USART_WordLength_8b;
4 @% n* Z" G. X      USART_InitStructure.USART_StopBits = USART_StopBits_1;
5 p, T% |  Z( z0 S      USART_InitStructure.USART_Parity = USART_Parity_No;9 D" X2 b7 |8 L6 G" o
      USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;% ]; z6 [; s! Q5 g; a; X8 n
      USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
      USART_Init(USART3, &USART_InitStructure);- a) E/ z1 a# s3 H2 ^
      // 使能 USART3
/ L( L: }1 Q/ O/ P( U      USART_Cmd(USART3, ENABLE);
}
9 d/ P5 b7 `* `  s! t% v
. g! a" [5 O. N# \& h# J
3 {4 A7 G( y0 ], ]' b
您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

推荐内容上一条 /1 下一条

EDA365公众号

关于我们|手机版|EDA365电子论坛网 ( 粤ICP备18020198号-1 )

GMT+8, 2025-7-18 17:56 , Processed in 0.109375 second(s), 23 queries , Gzip On.

深圳市墨知创新科技有限公司

地址:深圳市南山区科技生态园2栋A座805 电话:19926409050

快速回复 返回顶部 返回列表