基于stm32f103的USART3的rs485通信代码
时间: 2023-10-10 19:10:27 浏览: 59
以下是基于STM32F103的USART3的RS485通信代码,代码中包含了发送和接收数据的函数:
```c
#include "stm32f10x.h"
#include <stdio.h>
void USART3_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
//USART3_TX PB10
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//USART3_RX PB11
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//USART3_RE PB12
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
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(USART3, &USART_InitStructure);
USART_Cmd(USART3, ENABLE);
}
void USART3_SendData(uint8_t *pucBuffer, uint16_t uiLength)
{
GPIO_SetBits(GPIOB, GPIO_Pin_12);
while (uiLength--)
{
USART_SendData(USART3, *pucBuffer++);
while (USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET)
{
}
}
while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET)
{
}
GPIO_ResetBits(GPIOB, GPIO_Pin_12);
}
void USART3_ReceiveData(uint8_t *pucBuffer, uint16_t uiLength)
{
while (uiLength--)
{
while (USART_GetFlagStatus(USART3, USART_FLAG_RXNE) == RESET)
{
}
*pucBuffer++ = USART_ReceiveData(USART3);
}
}
```
在主函数中可以调用上述函数实现RS485通信:
```c
int main(void)
{
uint8_t send_buffer[] = "Hello World!";
uint8_t receive_buffer[32];
USART3_Configuration();
while (1)
{
USART3_SendData(send_buffer, sizeof(send_buffer));
USART3_ReceiveData(receive_buffer, sizeof(receive_buffer));
}
}
```
在上述代码中,发送数据时需要先将RE置为高电平,然后通过USART3_SendData函数发送数据。接收数据时需要等待接收完成,然后通过USART3_ReceiveData函数将接收到的数据存储到缓冲区中。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![](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)