用stm32f103的USART3串口来实现rs485通信代码
时间: 2023-09-02 12:05:35 浏览: 238
以下是基于STM32F103的USART3串口实现RS485通信的示例代码:
```
#include "stm32f10x.h"
#define USART3_DR_Base ((uint32_t)0x40004804)
// 定义USART3的GPIO引脚
#define USART3_TX_GPIO_PORT GPIOB
#define USART3_TX_GPIO_CLK RCC_APB2Periph_GPIOB
#define USART3_TX_PIN GPIO_Pin_10
#define USART3_RX_GPIO_PORT GPIOB
#define USART3_RX_GPIO_CLK RCC_APB2Periph_GPIOB
#define USART3_RX_PIN GPIO_Pin_11
// 定义RS485的控制引脚
#define RS485_CTRL_GPIO_PORT GPIOB
#define RS485_CTRL_GPIO_CLK RCC_APB2Periph_GPIOB
#define RS485_CTRL_PIN GPIO_Pin_12
// 定义RS485的发送和接收状态
#define RS485_RX_EN() (RS485_CTRL_GPIO_PORT->BRR = RS485_CTRL_PIN)
#define RS485_TX_EN() (RS485_CTRL_GPIO_PORT->BSRR = RS485_CTRL_PIN)
// 定义USART3的波特率
#define USART3_BAUDRATE 9600
void USART3_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
// 使能GPIO和USART的时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | USART3_TX_GPIO_CLK | USART3_RX_GPIO_CLK, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
// 配置USART3的GPIO引脚
GPIO_InitStructure.GPIO_Pin = USART3_TX_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(USART3_TX_GPIO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = USART3_RX_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(USART3_RX_GPIO_PORT, &GPIO_InitStructure);
// 配置RS485控制引脚
GPIO_InitStructure.GPIO_Pin = RS485_CTRL_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(RS485_CTRL_GPIO_PORT, &GPIO_InitStructure);
// 配置USART3的参数
USART_InitStructure.USART_BaudRate = USART3_BAUDRATE;
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(USART3, &USART_InitStructure);
// 使能USART3
USART_Cmd(USART3, ENABLE);
}
void USART3_SendByte(uint8_t byte)
{
// 等待发送缓冲区为空
while (USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET);
// 发送数据
USART_SendData(USART3, byte);
}
uint8_t USART3_ReceiveByte(void)
{
// 等待接收到数据
while (USART_GetFlagStatus(USART3, USART_FLAG_RXNE) == RESET);
// 读取数据
return USART_ReceiveData(USART3);
}
void RS485_SendData(uint8_t *data, uint16_t len)
{
// 切换为发送状态
RS485_TX_EN();
// 逐个发送字节
for (int i = 0; i < len; i++)
{
USART3_SendByte(data[i]);
}
// 等待数据发送完成
while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET);
// 切换为接收状态
RS485_RX_EN();
}
void RS485_ReceiveData(uint8_t *data, uint16_t len)
{
// 切换为接收状态
RS485_RX_EN();
// 逐个接收字节
for (int i = 0; i < len; i++)
{
data[i] = USART3_ReceiveByte();
}
}
int main(void)
{
USART3_Config();
// 发送数据
uint8_t tx_data[] = {0x01, 0x02, 0x03, 0x04};
RS485_SendData(tx_data, sizeof(tx_data));
// 接收数据
uint8_t rx_data[4];
RS485_ReceiveData(rx_data, sizeof(rx_data));
while (1);
}
```
在这个示例代码中,我们首先配置了USART3的GPIO引脚和参数,然后定义了RS485的控制引脚和发送、接收状态的函数。在主函数中,我们先发送了一些数据,然后接收了一些数据。需要注意的是,在发送数据之前需要先切换为发送状态,在接收数据之前需要先切换为接收状态。
阅读全文