进入工程 我们进行串口程序的配置
UartHandle.Instance = USART3;
UartHandle.Init.BaudRate = 9600;
UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
UartHandle.Init.StopBits =UART_STOPBITS_1;
UartHandle.Init.Parity =UART_PARITY_ODD;
UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
UartHandle.Init.Mode =UART_MODE_TX_RX;
UartHandle.Init.OverSampling = UART_OVERSAMPLING_16;
还是96n81的习惯
配置串口中断
void HAL_UART_MspInit(UART_HandleTypeDef*huart)
{
GPIO_InitTypeDef GPIO_InitStruct;
/*##-1- Enable peripherals and GPIO Clocks#################################*/
/*Enable GPIO TX/RX clock */
USARTx_TX_GPIO_CLK_ENABLE();
USARTx_RX_GPIO_CLK_ENABLE();
/*Enable USARTx clock */
USARTx_CLK_ENABLE();
/*##-2- Configure peripheral GPIO ##########################################*/
/*UART TX GPIO pin configura
tion */
GPIO_InitStruct.Pin =USARTx_TX_PIN;
GPIO_InitStruct.Mode =GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull =GPIO_PULLUP;
GPIO_InitStruct.Speed =GPIO_SPEED_HIGH;
GPIO_InitStruct.Alternate = USARTx_TX_AF;
HAL_GPIO_Init(USARTx_TX_GPIO_PORT, &GPIO_InitStruct);
/*UART RX GPIO pin configuration */
GPIO_InitStruct.Pin = USARTx_RX_PIN;
GPIO_InitStruct.Alternate = USARTx_RX_AF;
HAL_GPIO_Init(USARTx_RX_GPIO_PORT, &GPIO_InitStruct);
}
/**
*@brief UART MSP De-Initialization
* This function frees thehardware resources used in this example:
* - Disable thePeripheral's clock
* - Revert GPIO and NVICconfiguration to their default state
*@param huart: UART handle pointer
*@retval None
*/
void HAL_UART_MspDeInit(UART_HandleTypeDef*huart)
{
/*##-1- Reset peripherals##################################################*/
USARTx_FORCE_RESET();
USARTx_RELEASE_RESET();
/*##-2- Disable peripherals and GPIO Clocks#################################*/
/*Configure UART Tx as alternate function */
HAL_GPIO_DeInit(USARTx_TX_GPIO_PORT, USARTx_TX_PIN);
/*Configure UART Rx as alternate function */
HAL_GPIO_DeInit(USARTx_RX_GPIO_PORT, USARTx_RX_PIN);
}
当然我们要是使用printf打印的话必不可少一下程序
/* Private typedef-----------------------------------------------------------*/
/* Private define------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables---------------------------------------------------------*/
/* UART handler declaration */
UART_HandleTypeDef UartHandle;
/* Private function prototypes-----------------------------------------------*/
#ifdef __GNUC__
/* With GCC/RAISONANCE, small printf(option LD Linker->Libraries->Small printf
set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int__io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch,FILE *f)
#endif /* __GNUC__ */
static void SystemClock_Config(void);
static void Error_Handler(void);
OK串口配置工作完成
主函数打印hallo world!