生成一段通过stm32f103rct6串口助手给串口1发送数据,并且在串口助手上显示串口接收的数据的代码
时间: 2023-11-09 07:13:22 浏览: 113
基于HAL库的stm32f103单片机实现DMA+串口的接收、发送数据功能代码
以下是一段通过stm32f103rct6串口助手给串口1发送数据,并且在串口助手上显示串口接收的数据的代码:
```c
#include "stm32f10x.h"
#include <stdio.h>
void USART1_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
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);
}
int main(void)
{
char data[100];
USART1_Init();
while (1)
{
if (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == SET)
{
char c = USART_ReceiveData(USART1);
sprintf(data, "Received: %c\n", c);
USART_SendData(USART1, c);
USART_SendData(USART1, '\n');
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)
;
printf(data);
}
}
}
```
希望对您有帮助!
阅读全文