从STM32开发板通过串口向电脑的串口助手发送”hello”,电脑的串口助手向STM32 恢复“hi”
时间: 2023-06-03 09:04:56 浏览: 110
你好!可以通过以下代码实现:
```
#include "stm32f10x.h"
#include <stdio.h>
int main(void)
{
//初始化串口
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO | RCC_APB2Periph_USART1, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_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_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
//发送数据
USART_SendData(USART1, 'h');
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)
;
USART_SendData(USART1, 'e');
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)
;
USART_SendData(USART1, 'l');
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)
;
USART_SendData(USART1, 'l');
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)
;
USART_SendData(USART1, 'o');
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)
;
//等待接收数据
while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET)
;
char data = USART_ReceiveData(USART1);
//回复数据
USART_SendData(USART1, 'h');
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)
;
USART_SendData(USART1, 'i');
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)
;
while (1)
;
return 0;
}
```
通过串口发送数据:
1. 调用 `USART_SendData` 函数,将要发送的字节数据写入USART1寄存器
2. 检查USART发送寄存器是否为空,并且数据已经成功发送出去
通过串口接收数据:
1. 检查USART接收寄存器是否接收到了数据
2. 读取USART接收寄存器的数据
回复数据:
1. 调用 `USART_SendData` 函数,将要发送的字节数据写入USART1寄存器
2. 检查USART发送寄存器是否为空,并且数据已经成功发送出去
希望这个回答能够帮到你!
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)