stm32f103c8t6初始化USART3
时间: 2023-07-26 18:03:51 浏览: 88
要初始化`USART3`,需要按照以下步骤进行:
1. 使能`USART3`的时钟,这可以通过对应的`RCC_APB1PeriphClockCmd`或者`RCC_APB2PeriphClockCmd`函数来实现;
2. 配置`USART3`的引脚,将其设置为复用推挽输出模式;
3. 配置`USART3`的工作参数,包括波特率、数据位、停止位、校验位等;
4. 使能`USART3`。
以下是一个示例代码,可以初始化`USART3`:
```c
#include "stm32f10x.h"
int main(void) {
// 使能USART3的时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
// 配置USART3的引脚
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
// 配置USART3的工作参数
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Tx;
USART_Init(USART3, &USART_InitStructure);
// 使能USART3
USART_Cmd(USART3, ENABLE);
while (1) {
}
}
```
在上面的代码中,我们首先通过`RCC_APB1PeriphClockCmd`函数使能了`USART3`的时钟。然后,我们配置了`USART3`的引脚,将其设置为`GPIO_Mode_AF_PP`复用推挽输出模式。接着,我们配置了`USART3`的工作参数,包括波特率、数据位、停止位、校验位等。最后,我们使能了`USART3`。
阅读全文
相关推荐















