stm32f103vet6 hal库编写串口通信中断接收DMA发送
时间: 2024-01-25 11:03:39 浏览: 115
STM32串口中断接收(HAL库)
以下是使用HAL库编写串口通信中断接收DMA发送的示例代码:
```c
#include "stm32f1xx_hal.h"
UART_HandleTypeDef huart1;
DMA_HandleTypeDef hdma_usart1_tx;
DMA_HandleTypeDef hdma_usart1_rx;
uint8_t tx_buffer[50];
uint8_t rx_buffer[50];
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_DMA_Init(void);
static void MX_USART1_UART_Init(void);
/* USART1 init function */
static void MX_USART1_UART_Init(void)
{
huart1.Instance = USART1;
huart1.Init.BaudRate = 115200;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
Error_Handler();
}
}
/* DMA init function */
static void MX_DMA_Init(void)
{
/* DMA controller clock enable */
__HAL_RCC_DMA1_CLK_ENABLE();
/* DMA interrupt init */
/* DMA1_Channel4_IRQn interrupt configuration */
HAL_NVIC_SetPriority(DMA1_Channel4_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(DMA1_Channel4_IRQn);
/* DMA1_Channel5_IRQn interrupt configuration */
HAL_NVIC_SetPriority(DMA1_Channel5_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(DMA1_Channel5_IRQn);
}
/* GPIO init function */
static void MX_GPIO_Init(void)
{
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
}
/* System Clock Configuration */
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;
/**Initializes the CPU, AHB and APB busses clocks
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/**Initializes the CPU, AHB and APB busses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
Error_Handler();
}
}
/* USART1 DMA Transmit Complete callback */
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{
/* DMA transmit complete callback */
}
/* USART1 DMA Receive Complete callback */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
/* DMA receive complete callback */
}
/* USART1 Error callback */
void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
{
/* UART error callback */
}
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_DMA_Init();
MX_USART1_UART_Init();
/* Enable USART1 DMA TX and RX interrupts */
__HAL_UART_ENABLE_IT(&huart1, UART_IT_TC);
__HAL_UART_ENABLE_IT(&huart1, UART_IT_RXNE);
/* Start USART1 reception */
HAL_UART_Receive_DMA(&huart1, rx_buffer, 50);
while (1)
{
/* Wait for USART1 reception */
if (HAL_UART_GetState(&huart1) == HAL_UART_STATE_READY)
{
/* Copy received data to transmit buffer */
memcpy(tx_buffer, rx_buffer, sizeof(rx_buffer));
/* Start USART1 transmission */
HAL_UART_Transmit_DMA(&huart1, tx_buffer, sizeof(tx_buffer));
}
}
}
/* DMA1_Channel4_IRQn interrupt handler */
void DMA1_Channel4_IRQHandler(void)
{
HAL_DMA_IRQHandler(&hdma_usart1_tx);
}
/* DMA1_Channel5_IRQn interrupt handler */
void DMA1_Channel5_IRQHandler(void)
{
HAL_DMA_IRQHandler(&hdma_usart1_rx);
}
```
在该示例代码中,使用HAL库初始化了USART1、GPIO和DMA模块,并启用USART1的DMA传输中断。在main函数中,使用HAL库启用了USART1接收DMA传输,并在while循环中等待接收完成。一旦接收完成,将接收缓冲区中的数据复制到发送缓冲区,并启动USART1的DMA传输发送数据。在USART1的DMA传输完成后,将调用相应的回调函数进行处理。
阅读全文