两块STM32串口通信传输字符
时间: 2023-07-16 09:12:10 浏览: 121
以下是两块STM32串口通信传输字符的示例代码:
主控板代码:
```
#include "stm32f10x.h"
#include "stdio.h"
#define USART1_DR_Base 0x40013804
#define USART2_DR_Base 0x40004404
void USART1_Init(void);
void USART1_SendData(char data);
void USART1_SendString(char* str);
void USART2_Init(void);
void USART2_SendData(char data);
void USART2_SendString(char* str);
int main()
{
// 初始化USART1和USART2
USART1_Init();
USART2_Init();
while (1)
{
char data;
// 从USART1读取数据并发送到USART2
if (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == SET)
{
data = USART_ReceiveData(USART1);
USART2_SendData(data);
}
// 从USART2读取数据并发送到USART1
if (USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == SET)
{
data = USART_ReceiveData(USART2);
USART1_SendData(data);
}
}
}
void USART1_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
// 使能GPIOA和USART1的时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1, ENABLE);
// 配置USART1的Tx引脚
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);
// 配置USART1的Rx引脚
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// 配置USART1的波特率、字长等
USART_InitStructure.USART_BaudRate = 9600;
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);
// 使能USART1
USART_Cmd(USART1, ENABLE);
}
void USART1_SendData(char data)
{
// 等待发送缓冲区为空
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
// 发送数据
USART_SendData(USART1, data);
}
void USART1_SendString(char* str)
{
// 循环发送字符串
while (*str)
{
USART1_SendData(*str++);
}
}
void USART2_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
// 使能GPIOA和USART2的时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB1Periph_USART2, ENABLE);
// 配置USART2的Tx引脚
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// 配置USART2的Rx引脚
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// 配置USART2的波特率、字长等
USART_InitStructure.USART_BaudRate = 9600;
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);
// 使能USART2
USART_Cmd(USART2, ENABLE);
}
void USART2_SendData(char data)
{
// 等待发送缓冲区为空
while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
// 发送数据
USART_SendData(USART2, data);
}
void USART2_SendString(char* str)
{
// 循环发送字符串
while (*str)
{
USART2_SendData(*str++);
}
}
```
从板代码:
```
#include "stm32f10x.h"
#include "stdio.h"
#define USART1_DR_Base 0x40013804
#define USART2_DR_Base 0x40004404
void USART1_Init(void);
void USART1_SendData(char data);
void USART1_SendString(char* str);
void USART2_Init(void);
void USART2_SendData(char data);
void USART2_SendString(char* str);
int main()
{
// 初始化USART1和USART2
USART1_Init();
USART2_Init();
while (1)
{
char data;
// 从USART2读取数据并发送到USART1
if (USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == SET)
{
data = USART_ReceiveData(USART2);
USART1_SendData(data);
}
// 从USART1读取数据并发送到USART2
if (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == SET)
{
data = USART_ReceiveData(USART1);
USART2_SendData(data);
}
}
}
void USART1_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
// 使能GPIOA和USART1的时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1, ENABLE);
// 配置USART1的Tx引脚
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);
// 配置USART1的Rx引脚
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// 配置USART1的波特率、字长等
USART_InitStructure.USART_BaudRate = 9600;
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);
// 使能USART1
USART_Cmd(USART1, ENABLE);
}
void USART1_SendData(char data)
{
// 等待发送缓冲区为空
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
// 发送数据
USART_SendData(USART1, data);
}
void USART1_SendString(char* str)
{
// 循环发送字符串
while (*str)
{
USART1_SendData(*str++);
}
}
void USART2_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
// 使能GPIOA和USART2的时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB1Periph_USART2, ENABLE);
// 配置USART2的Tx引脚
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// 配置USART2的Rx引脚
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// 配置USART2的波特率、字长等
USART_InitStructure.USART_BaudRate = 9600;
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);
// 使能USART2
USART_Cmd(USART2, ENABLE);
}
void USART2_SendData(char data)
{
// 等待发送缓冲区为空
while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
// 发送数据
USART_SendData(USART2, data);
}
void USART2_SendString(char* str)
{
// 循环发送字符串
while (*str)
{
USART2_SendData(*str++);
}
}
```
阅读全文