用keil 5写一个两个串口互相接收发送的程序
时间: 2023-09-18 07:09:16 浏览: 115
串口接收发送程序
以下是基于Keil 5的两个串口互相接收发送的程序示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "stm32f4xx.h"
#define BUF_SIZE 64
char str1[BUF_SIZE] = {0};
char str2[BUF_SIZE] = {0};
void USART1_Init(void);
void USART2_Init(void);
void USART1_Send(char *str);
void USART2_Send(char *str);
int main(void)
{
USART1_Init();
USART2_Init();
while (1)
{
// 从串口1接收数据
if (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == SET)
{
char ch = USART_ReceiveData(USART1);
if (ch == '\r' || ch == '\n')
{
USART_SendData(USART2, '\r');
USART_SendData(USART2, '\n');
USART2_Send(str1);
memset(str1, 0, BUF_SIZE);
}
else
{
strncat(str1, &ch, 1);
}
}
// 从串口2接收数据
if (USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == SET)
{
char ch = USART_ReceiveData(USART2);
if (ch == '\r' || ch == '\n')
{
USART_SendData(USART1, '\r');
USART_SendData(USART1, '\n');
USART1_Send(str2);
memset(str2, 0, BUF_SIZE);
}
else
{
strncat(str2, &ch, 1);
}
}
}
}
void USART1_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
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);
}
void USART2_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
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(USART2, &USART_InitStructure);
USART_Cmd(USART2, ENABLE);
}
void USART1_Send(char *str)
{
while (*str)
{
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
USART_SendData(USART1, *str++);
}
}
void USART2_Send(char *str)
{
while (*str)
{
while (USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);
USART_SendData(USART2, *str++);
}
}
```
这个程序使用了两个串口:USART1 和 USART2。在主循环中,程序不断轮询两个串口的接收标志位,如果有数据接收,则将数据存储在缓冲区中。当接收到回车或换行符时,将缓冲区中的数据发送到另一个串口,并清空缓冲区。
注意:在使用 Keil 5 编写 STM32 代码时,需要根据具体的芯片型号选择正确的库文件。本示例代码中使用的是 STM32F407VG 的库文件。如果使用其他型号的芯片,需要修改库文件路径和包含的头文件。
阅读全文