前段时间做了一下IO的测试,至少证明环境搭建是没有问题的,今天来测试一下我们LPUART,本来是打算用例程然后外加串口来进行测试的,但是现在少了一个外接的串口,我查了一下原理图,发现是接的是PA2和PA3,这个正好也是LPUART1的复用的引脚。
只要硬件支持了,就支持修改映射就行了,在此之前,我们需要修改Hall的配置,开启LPUART和LPTIM的使能
完成了之后我们就自己建立工程,
然后就是LPUART的初始化程序
具体的代码如下:
#include "LP_UART.h"
UART_HandleTypeDef LP_UART1;
void LP_UART_Init(void)
{
__HAL_RCC_LPUART1_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
//--设置复用---
GPIO_InitTypeDef gpio_init_structure;
/* Configure USART Tx as alternate function */
gpio_init_structure.Pin = GPIO_PIN_2;
gpio_init_structure.Mode = GPIO_MODE_AF_PP;
gpio_init_structure.Speed = GPIO_SPEED_FREQ_HIGH;
gpio_init_structure.Pull = GPIO_PULLUP;
gpio_init_structure.Alternate = GPIO_AF8_LPUART1;
HAL_GPIO_Init(GPIOA, &gpio_init_structure);
/* Configure USART Rx as alternate function */
gpio_init_structure.Pin = GPIO_PIN_3;
gpio_init_structure.Mode = GPIO_MODE_AF_PP;
gpio_init_structure.Alternate = GPIO_AF8_LPUART1;
HAL_GPIO_Init(GPIOA, &gpio_init_structure);
LP_UART1.Instance = LPUART1;
LP_UART1.Init.BaudRate = 115200;
LP_UART1.Init.WordLength = UART_WORDLENGTH_8B;
LP_UART1.Init.StopBits = UART_STOPBITS_1;
LP_UART1.Init.Parity = UART_PARITY_NONE;
LP_UART1.Init.Mode = UART_MODE_TX_RX;
LP_UART1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
LP_UART1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
LP_UART1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
LP_UART1.FifoMode = UART_FIFOMODE_DISABLE;
if (HAL_UART_Init(&LP_UART1) != HAL_OK)
{
}
if (HAL_UARTEx_SetTxFifoThreshold(&LP_UART1, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
{
}
if (HAL_UARTEx_SetRxFifoThreshold(&LP_UART1, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
{
}
if (HAL_UARTEx_DisableFifoMode(&LP_UART1) != HAL_OK)
{
}
}
void LP_UART_SendData(unsigned char *Data,unsigned int Len)
{
HAL_UART_Transmit(&LP_UART1,Data,Len,200);
}
这个代码写好之后木就是Main函数了,其实很简单,闪个灯,发个消息就行了。
int main(void)
{
HAL_Init();
SystemClock_Config();
__HAL_RCC_GPIOA_CLK_ENABLE();
/* -2- Configure IO in output push-pull mode to drive external LEDs */
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Pin = GPIO_PIN_5;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
LP_UART_Init();
while (1)
{
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
HAL_Delay(500);
LP_UART_SendData("This is LPUART Demo!\r\n",22);
}
/* USER CODE END 3 */<img src="https://bbs.21ic.com/forum.php?mod=image&aid=2246468&size=300x300&key=65d009f6634f4b5c&nocache=yes&type=fixnone" border="0" aid="attachimg_2246468" alt="">
}
好了,我们来看看实际效果
这次的测试就到这里,LPUART的代码欢迎大家验证指教。
|