https://gitee.com/xushicheng1202/stm32-cube-mx_LL.git
这一章我们在前一章GPIO的工程修改。复制GPIO的工程,修改文件夹名。
1、USART & NVIC 配置,没有配置 USART3,引脚会显示黄色警告
2、?点击?USART3,设置模式为?Asynchronous,无硬件流控制,参数为:115200,8 Bits,None,1;(配置完 USART3 后,相关引脚会显示绿色,提示引脚配置正常)
3、点击?NVIC,使能?USART3?中断,优先级设置为 2,0
?4、勾选?Generate IRQ handler?生成中断服务函数,不勾选?Call HAL handler;(由于默认不开启中断源,勾选了也没有作用)
5、Clock 配置
6、生成代码点击?GENERATE CODE?生成代码。
?7、代码测试
7.1、main.c?文件?main ?函数中,有?MX_USART1_UART_Init() ?的回调函数,查看其函数源码(保持stm32cubemx生成的不变,值得注意的是:生成代码中开启中断,但默认不使能中断源。所以我们需要在初始化代码后配置使能一下 USART3 的中断源。)
void MX_USART3_UART_Init(void)
{
/* USER CODE BEGIN USART3_Init 0 */
/* USER CODE END USART3_Init 0 */
LL_USART_InitTypeDef USART_InitStruct = {0};
LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
LL_RCC_SetUSARTClockSource(LL_RCC_USART3_CLKSOURCE_PCLK1);
/* Peripheral clock enable */
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_USART3);
LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOB);
/**USART3 GPIO Configuration
PB10 ------> USART3_TX
PB11 ------> USART3_RX
*/
GPIO_InitStruct.Pin = LL_GPIO_PIN_10|LL_GPIO_PIN_11;
GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
GPIO_InitStruct.Alternate = LL_GPIO_AF_7;
LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* USART3 interrupt Init */
NVIC_SetPriority(USART3_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),0, 0));
NVIC_EnableIRQ(USART3_IRQn);
/* USER CODE BEGIN USART3_Init 1 */
/* USER CODE END USART3_Init 1 */
USART_InitStruct.BaudRate = 115200;
USART_InitStruct.DataWidth = LL_USART_DATAWIDTH_8B;
USART_InitStruct.StopBits = LL_USART_STOPBITS_1;
USART_InitStruct.Parity = LL_USART_PARITY_NONE;
USART_InitStruct.TransferDirection = LL_USART_DIRECTION_TX_RX;
USART_InitStruct.HardwareFlowControl = LL_USART_HWCONTROL_NONE;
USART_InitStruct.OverSampling = LL_USART_OVERSAMPLING_16;
LL_USART_Init(USART3, &USART_InitStruct);
LL_USART_ConfigAsyncMode(USART3);
LL_USART_Enable(USART3);
/* USER CODE BEGIN USART3_Init 2 */
/* USER CODE END USART3_Init 2 */
}
?7.2、在初始化代码后配置使能一下 USART3 的中断源。
/* USER CODE BEGIN 2 */
LL_USART_EnableIT_RXNE(USART3); // ENABLE USART3 RXNE Interrupt
LL_USART_EnableIT_IDLE(USART3); // ENABLE USART3 IDLE Interrupt
/* USER CODE END 2 */
7.3、在?stm32l4xx_it.c?文件的? void?USART3_IRQHandler(void)? 用户代码区域加入中断处理代码(注意:各芯片对应的寄存器可能不一致,需要正确修改)
void USART3_IRQHandler(void)
{
/* USER CODE BEGIN USART3_IRQn 0 */
/* USER CODE END USART3_IRQn 0 */
/* USER CODE BEGIN USART3_IRQn 1 */
if (LL_USART_IsActiveFlag_RXNE(USART3) != 0)
{
USART3_RX_BUF[USART3_RX_CNT++] = USART3->RDR;
}
else if (LL_USART_IsActiveFlag_IDLE(USART3) != 0)
{
LL_USART_ClearFlag_IDLE(USART3);
USART3_RX_BUF[0] = USART3_RX_CNT - 1;
USART3_RX_CNT = 1;
// memset(USART3_RX_BUF, 0, USART3_RX_BUF[0] + 1);
USART3_TX_STA = 1;
}
/* USER CODE END USART3_IRQn 1 */
}
7.4、在 main.c文件的while ?循环中加入测试代码
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
if (USART3_TX_STA == 1)
{
for (int i = 1; i < USART3_RX_BUF[0] + 1; i++)
{
while (LL_USART_IsActiveFlag_TC(USART3) == RESET)
{
};
LL_USART_TransmitData8(USART3, USART3_RX_BUF[i]);
}
while (LL_USART_IsActiveFlag_TC(USART3) == RESET)
{
};
memset(USART3_RX_BUF, 0, USART3_RX_BUF[0] + 1);
USART3_TX_STA = 0;
}
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
8、测试结果
?
|