基于stm32f103的rs485收发代码
时间: 2023-10-20 19:06:45 浏览: 86
以下是基于STM32F103的RS485收发代码,其中使用了HAL库和DMA传输方式:
```c
#include "stm32f1xx_hal.h"
#define RS485_UART USART1
#define RS485_BAUDRATE 9600
#define RS485_ADDRESS 0x01
/* RS485发送数据缓冲区长度 */
#define RS485_TX_BUF_LEN 256
/* RS485接收数据缓冲区长度 */
#define RS485_RX_BUF_LEN 256
/* RS485发送DMA传输完成标志 */
static volatile uint8_t rs485_tx_dma_complete = 1;
/* RS485发送数据缓冲区 */
static uint8_t rs485_tx_buf[RS485_TX_BUF_LEN];
/* RS485接收数据缓冲区 */
static uint8_t rs485_rx_buf[RS485_RX_BUF_LEN];
/* RS485发送DMA */
static DMA_HandleTypeDef rs485_tx_dma_handle;
/* RS485接收DMA */
static DMA_HandleTypeDef rs485_rx_dma_handle;
/* RS485初始化函数 */
void rs485_init(void)
{
/* 使能USART1时钟 */
__HAL_RCC_USART1_CLK_ENABLE();
/* 使能DMA1时钟 */
__HAL_RCC_DMA1_CLK_ENABLE();
/* 配置USART1 GPIO */
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = GPIO_PIN_9 | GPIO_PIN_10;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* 配置RS485发送使能引脚 GPIO */
GPIO_InitStruct.Pin = GPIO_PIN_8;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* 配置USART1 */
RS485_UART->CR1 = 0;
RS485_UART->CR2 = USART_CR2_STOP_1; /* 1个停止位 */
RS485_UART->CR3 = USART_CR3_DMAT | USART_CR3_DMAR; /* 使能DMA发送和接收 */
RS485_UART->BRR = HAL_RCC_GetHCLKFreq() / RS485_BAUDRATE;
RS485_UART->CR1 = USART_CR1_TE | USART_CR1_RE | USART_CR1_UE; /* 使能发送和接收 */
/* 配置RS485发送DMA */
rs485_tx_dma_handle.Instance = DMA1_Channel4;
rs485_tx_dma_handle.Init.Direction = DMA_MEMORY_TO_PERIPH;
rs485_tx_dma_handle.Init.PeriphInc = DMA_PINC_DISABLE;
rs485_tx_dma_handle.Init.MemInc = DMA_MINC_ENABLE;
rs485_tx_dma_handle.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
rs485_tx_dma_handle.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
rs485_tx_dma_handle.Init.Mode = DMA_NORMAL;
rs485_tx_dma_handle.Init.Priority = DMA_PRIORITY_LOW;
HAL_DMA_Init(&rs485_tx_dma_handle);
/* 配置RS485接收DMA */
rs485_rx_dma_handle.Instance = DMA1_Channel5;
rs485_rx_dma_handle.Init.Direction = DMA_PERIPH_TO_MEMORY;
rs485_rx_dma_handle.Init.PeriphInc = DMA_PINC_DISABLE;
rs485_rx_dma_handle.Init.MemInc = DMA_MINC_ENABLE;
rs485_rx_dma_handle.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
rs485_rx_dma_handle.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
rs485_rx_dma_handle.Init.Mode = DMA_NORMAL;
rs485_rx_dma_handle.Init.Priority = DMA_PRIORITY_LOW;
HAL_DMA_Init(&rs485_rx_dma_handle);
/* 使能USART1接收中断 */
HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(USART1_IRQn);
}
/* RS485发送函数 */
void rs485_send(const uint8_t *data, uint16_t len)
{
/* 等待上次发送DMA传输完成 */
while (!rs485_tx_dma_complete) {}
/* 复制数据到发送缓冲区 */
len = len > RS485_TX_BUF_LEN ? RS485_TX_BUF_LEN : len;
memcpy(rs485_tx_buf, data, len);
/* 配置DMA传输 */
rs485_tx_dma_handle.Instance->CCR &= ~DMA_CCR_EN;
rs485_tx_dma_handle.Init.PeriphAddress = (uint32_t)&RS485_UART->DR;
rs485_tx_dma_handle.Init.MemAddress = (uint32_t)rs485_tx_buf;
rs485_tx_dma_handle.Init.Direction = DMA_MEMORY_TO_PERIPH;
rs485_tx_dma_handle.Init.BufferSize = len;
HAL_DMA_Init(&rs485_tx_dma_handle);
/* 使能RS485发送 */
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_SET);
/* 启动DMA传输 */
rs485_tx_dma_complete = 0;
HAL_DMA_Start_IT(&rs485_tx_dma_handle, (uint32_t)rs485_tx_buf, (uint32_t)&RS485_UART->DR, len);
}
/* RS485接收函数 */
uint16_t rs485_receive(uint8_t *data, uint16_t len, uint32_t timeout)
{
uint16_t rx_len = 0;
/* 启动DMA传输 */
HAL_DMA_Start(&rs485_rx_dma_handle, (uint32_t)&RS485_UART->DR, (uint32_t)rs485_rx_buf, RS485_RX_BUF_LEN);
/* 等待接收完成或超时 */
uint32_t start_ms = HAL_GetTick();
while (HAL_DMA_GetState(&rs485_rx_dma_handle) != HAL_DMA_STATE_READY) {
if (HAL_GetTick() - start_ms >= timeout) {
HAL_DMA_Abort(&rs485_rx_dma_handle);
break;
}
}
/* 计算接收数据长度 */
rx_len = RS485_RX_BUF_LEN - __HAL_DMA_GET_COUNTER(&rs485_rx_dma_handle);
rx_len = rx_len > len ? len : rx_len;
/* 复制接收数据到用户缓冲区 */
memcpy(data, rs485_rx_buf, rx_len);
return rx_len;
}
/* USART1中断处理函数 */
void USART1_IRQHandler(void)
{
HAL_UART_IRQHandler(&huart1);
}
/* RS485发送DMA传输完成回调函数 */
void HAL_DMA_TxCpltCallback(DMA_HandleTypeDef *hdma)
{
/* 禁用RS485发送 */
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET);
/* 标记DMA传输完成 */
rs485_tx_dma_complete = 1;
}
```
注意事项:
1. 在初始化函数中,需要配置USART1的GPIO、波特率、发送和接收使能等参数。
2. 在发送函数中,需要等待上次发送DMA传输完成后再进行下一次发送。
3. 在接收函数中,使用DMA方式接收数据,并计算接收数据长度和复制数据到用户缓冲区。
4. 在发送DMA传输完成回调函数中,需要禁用RS485发送并标记DMA传输完成。
阅读全文