本帖最后由 gsy幸运 于 2023-8-31 06:30 编辑
单片机:STM32L031G6U6
UART1:PA2 TX PA3 RX
电路:TX直接输出
问题:TX无输出,测试输出高低电平正常
代码:
1、时钟初始化,系统时钟使用MSI,UART使用HSIvoid SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
__HAL_RCC_LSE_CONFIG(RCC_LSE_OFF);
/**Configure the main internal regulator output voltage
*/
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/**Initializes the CPU, AHB and APB busses clocks
*/
//RCC_MSIRANGE_4MSI 时钟被用作系统时钟。从复位重启后,MSI 频率被置位其默认值 4 MHz
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_MSI;
RCC_OscInitStruct.HSIState = RCC_HSI_DIV4;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.MSIState = RCC_MSI_ON;// **MSI时钟(100KHz-48MHz多速内部RC振荡器时钟)
RCC_OscInitStruct.MSICalibrationValue = 0;
RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_4;////配置为 MSI = 1.048 MHz
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
/**Initializes the CPU, AHB and APB busses clocks
*/
//需要配置的时钟HCLK(APB1/APB2总线时钟源)、SYSCLK(系统时钟源)、PCLK1(APB1上外设时钟源)、PCLK2(APB2上外设时钟源)
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_MSI;//配置系统时钟为MSI输入
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;//// AHB时钟为系统时钟1分频,1.048 MHz/1 =1.048 MHz
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;//APB1时钟为系统时钟1分频,1.048 MHz/1 =1.048 MHz
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;//APB2时钟为系统时钟1分频,1.048 MHz/1 =1.048 MHz
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
/**Configure the Systick interrupt time
*/
/// 配置系统定时器中断时间,配置为HCLK的千分频
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
/**Configure the Systick
*/
// 配置系统定时器中断时间,配置为HCLK的千分频
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
// 配置系统定时器中断时间,配置为HCLK的千分频
/* SysTick_IRQn interrupt configuration */
HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_LPUART1;
PeriphClkInit.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_HSI;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
{
Error_Handler();
}
}
2、GPIO初始化:
#define UartTx_Pin GPIO_PIN_2 //
#define UartTx_GPIO_Port GPIOA
#define UartRx_Pin GPIO_PIN_3 //
#define UartRx_GPIO_Port GPIOA
GPIO_InitStruct.Pin = UartTx_Pin; //uart TX
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(UartTx_GPIO_Port, &GPIO_InitStruct);
3、uart初始化
static void MX_LPUART1_UART_Init(void)
{
/* USER CODE BEGIN LPUART1_Init 0 */
/* USER CODE END LPUART1_Init 0 */
/* USER CODE BEGIN LPUART1_Init 1 */
/* USER CODE END LPUART1_Init 1 */
hlpuart1.Instance = LPUART1;
hlpuart1.Init.BaudRate = 2400;
hlpuart1.Init.WordLength = UART_WORDLENGTH_8B;
hlpuart1.Init.StopBits = UART_STOPBITS_1;
hlpuart1.Init.Parity = UART_PARITY_NONE;
hlpuart1.Init.Mode = UART_MODE_TX_RX;
hlpuart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
hlpuart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
hlpuart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_TXINVERT_INIT|UART_ADVFEATURE_RXINVERT_INIT;
hlpuart1.AdvancedInit.TxPinLevelInvert = UART_ADVFEATURE_TXINV_ENABLE;
hlpuart1.AdvancedInit.RxPinLevelInvert = UART_ADVFEATURE_RXINV_ENABLE;
if (HAL_UART_Init(&hlpuart1) != HAL_OK)
{
Error_Handler();
}
}
4、main函数调用初始化代码
SystemClock_Config();
MX_GPIO_Init();
MX_LPUART1_UART_Init();
5、主程序1S输出一次
if(HAL_GetTick()%1000 == 0)
{
//printf("hello world");
uint8_t TxData[10]= "01234abcde";
HAL_UART_Transmit(&hlpuart1,TxData,10,0xffff);
//HAL_UART_Transmit_IT(&hlpuart1,TxData,10);
}
结果,无输出,示波器接上也没有反应,想了好几天了,也没想明白
|