STM32F407基于官方库串口通信代码
时间: 2024-03-08 15:51:27 浏览: 27
以下是基于官方库实现的 STM32F407 的串口通信代码,以 USART1 为例:
```c
#include "stm32f4xx.h"
#include <stdio.h>
#define USARTx USART1
#define USARTx_CLK RCC_APB2Periph_USART1
#define USARTx_CLK_INIT RCC_APB2PeriphClockCmd
#define USARTx_IRQn USART1_IRQn
#define USARTx_IRQHandler USART1_IRQHandler
#define USARTx_TX_PIN GPIO_Pin_9
#define USARTx_TX_GPIO_PORT GPIOA
#define USARTx_TX_GPIO_CLK RCC_AHB1Periph_GPIOA
#define USARTx_TX_SOURCE GPIO_PinSource9
#define USARTx_TX_AF GPIO_AF_USART1
#define USARTx_RX_PIN GPIO_Pin_10
#define USARTx_RX_GPIO_PORT GPIOA
#define USARTx_RX_GPIO_CLK RCC_AHB1Periph_GPIOA
#define USARTx_RX_SOURCE GPIO_PinSource10
#define USARTx_RX_AF GPIO_AF_USART1
GPIO_InitTypeDef GPIO_InitStruct;
USART_InitTypeDef USART_InitStruct;
NVIC_InitTypeDef NVIC_InitStruct;
void USARTx_Config(void)
{
USARTx_CLK_INIT(USARTx_CLK, ENABLE);
GPIO_InitStruct.GPIO_Pin = USARTx_TX_PIN;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(USARTx_TX_GPIO_PORT, &GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin = USARTx_RX_PIN;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(USARTx_RX_GPIO_PORT, &GPIO_InitStruct);
GPIO_PinAFConfig(USARTx_TX_GPIO_PORT, USARTx_TX_SOURCE, USARTx_TX_AF);
GPIO_PinAFConfig(USARTx_RX_GPIO_PORT, USARTx_RX_SOURCE, USARTx_RX_AF);
USART_InitStruct.USART_BaudRate = 115200;
USART_InitStruct.USART_WordLength = USART_WordLength_8b;
USART_InitStruct.USART_StopBits = USART_StopBits_1;
USART_InitStruct.USART_Parity = USART_Parity_No;
USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USARTx, &USART_InitStruct);
USART_Cmd(USARTx, ENABLE);
NVIC_InitStruct.NVIC_IRQChannel = USARTx_IRQn;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStruct);
USART_ITConfig(USARTx, USART_IT_RXNE, ENABLE);
}
void USARTx_IRQHandler(void)
{
if (USART_GetITStatus(USARTx, USART_IT_RXNE) != RESET)
{
char data = USART_ReceiveData(USARTx);
USART_SendData(USARTx, data);
while (USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET);
}
}
int main(void)
{
USARTx_Config();
while (1)
{
}
}
```
这段代码首先定义了一些宏和变量,然后定义了 USARTx_Config 函数,该函数用于初始化 USART1 的相关配置,包括 GPIO 和 USART 的配置。在中断处理函数 USARTx_IRQHandler 中,当 USART1 接收到数据时,将该数据发送回去,以实现回显功能。在主函数中,只是一个空循环。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)