STM32F0_HAL初始化系列:串口DMA输出
阅读原文时间:2023年07月08日阅读:2

static void MX_USART1_UART_Init(void)
{

/* USER CODE BEGIN USART1_Init 0 */

/* USER CODE END USART1_Init 0 */

/* USER CODE BEGIN USART1_Init 1 */

/* USER CODE END USART1_Init 1 */
huart1.Instance = USART1;
huart1.Init.BaudRate = 460800;
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;
huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN USART1_Init 2 */

/* USER CODE END USART1_Init 2 */

}

void HAL_UART_MspInit(UART_HandleTypeDef* huart)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(huart->Instance==USART1)
{
/* USER CODE BEGIN USART1_MspInit 0 */

/* USER CODE END USART1_MspInit 0 */
/* Peripheral clock enable */
__HAL_RCC_USART1_CLK_ENABLE();

\_\_HAL\_RCC\_GPIOA\_CLK\_ENABLE();  
/\*\*USART1 GPIO Configuration  
PA2     ------> USART1\_TX  
PA3     ------> USART1\_RX  
\*/  
GPIO\_InitStruct.Pin = GPIO\_PIN\_2|GPIO\_PIN\_3;  
GPIO\_InitStruct.Mode = GPIO\_MODE\_AF\_PP;  
GPIO\_InitStruct.Pull = GPIO\_NOPULL;  
GPIO\_InitStruct.Speed = GPIO\_SPEED\_FREQ\_HIGH;  
GPIO\_InitStruct.Alternate = GPIO\_AF1\_USART1;  
HAL\_GPIO\_Init(GPIOA, &GPIO\_InitStruct);

/\* USART1 DMA Init \*/  
/\* USART1\_TX Init \*/  
hdma\_usart1\_tx.Instance = DMA1\_Channel2;  
hdma\_usart1\_tx.Init.Direction = DMA\_MEMORY\_TO\_PERIPH;  
hdma\_usart1\_tx.Init.PeriphInc = DMA\_PINC\_DISABLE;  
hdma\_usart1\_tx.Init.MemInc = DMA\_MINC\_ENABLE;  
hdma\_usart1\_tx.Init.PeriphDataAlignment = DMA\_PDATAALIGN\_BYTE;  
hdma\_usart1\_tx.Init.MemDataAlignment = DMA\_MDATAALIGN\_BYTE;  
hdma\_usart1\_tx.Init.Mode = DMA\_NORMAL;  
hdma\_usart1\_tx.Init.Priority = DMA\_PRIORITY\_LOW;  
if (HAL\_DMA\_Init(&hdma\_usart1\_tx) != HAL\_OK)  
{  
  Error\_Handler();  
}

\_\_HAL\_LINKDMA(huart,hdmatx,hdma\_usart1\_tx);

/* USER CODE BEGIN USART1_MspInit 1 */

/* USER CODE END USART1_MspInit 1 */
}

}

//发送
HAL_UART_Transmit_DMA(&huart1, (uint8_t*)_bsp_bluetooth_SendBuff, 19);

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器