stm32cubeIDE串口通信:在stm32cubemx中配置串口参数,使用USART1串口,采用中断接收函数HAL_UART_Receive_IT数据,使用DMA方式函数HAL_UART_Transmit_DMA发送数据的具体配置代码
时间: 2023-10-30 16:05:56 浏览: 175
首先,在STM32CubeMX中配置串口参数:
1. 打开STM32CubeMX,选择对应芯片型号。
2. 在Pinout & Configuration中,将USART1的TX和RX引脚设置为GPIO_Alternate_function,并设置对应的引脚号。
3. 在Configuration中,选择USART1,将Mode设置为Asynchronous,Baud Rate设置为波特率,Data Bits设置为8,Parity设置为None,Stop Bits设置为1。
4. 在NVIC Settings中,使能USART1的中断,并设置优先级。
5. 生成代码后,打开main.c文件,在MX_USART1_UART_Init函数后添加以下代码:
```c
HAL_UART_Receive_IT(&huart1, &rx_data, 1); // 开启中断接收数据
```
接下来,使用中断接收函数HAL_UART_Receive_IT读取串口数据:
```c
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if(huart->Instance == USART1)
{
// 处理接收到的数据
}
HAL_UART_Receive_IT(&huart1, &rx_data, 1); // 继续开启中断接收数据
}
```
最后,使用DMA方式函数HAL_UART_Transmit_DMA发送数据:
```c
char tx_data[] = "hello world";
HAL_UART_Transmit_DMA(&huart1, (uint8_t*)tx_data, strlen(tx_data));
```
完整代码如下:
```c
#include "main.h"
#include <string.h>
UART_HandleTypeDef huart1;
char rx_data;
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART1_UART_Init();
HAL_UART_Receive_IT(&huart1, &rx_data, 1); // 开启中断接收数据
char tx_data[] = "hello world";
HAL_UART_Transmit_DMA(&huart1, (uint8_t*)tx_data, strlen(tx_data)); // 使用DMA方式发送数据
while (1)
{
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 8;
RCC_OscInitStruct.PLL.PLLN = 336;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 7;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
{
Error_Handler();
}
}
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();
}
}
static void MX_GPIO_Init(void)
{
__HAL_RCC_GPIOA_CLK_ENABLE();
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_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if(huart->Instance == USART1)
{
// 处理接收到的数据
}
HAL_UART_Receive_IT(&huart1, &rx_data, 1); // 继续开启中断接收数据
}
void Error_Handler(void)
{
}
```
阅读全文