STM32F4工程--串口--配置一个发送函数

it2025-04-27  20

STM32F4工程--串口--配置一个发送函数

  ----芯片:STM32F429IGT6


/************************************************************************** **作者: 小丁工程 **时间: 2020.10.19 ***************************************************************************/ #include "sys.h" //---------------------主函数------------------------------------------------- main.c int main(void) { u8 p[]="test"; HAL_Init();//========================初始化HAL库 Stm32_Clock_Init(360,25,2,8);//======设置时钟,180Mhz delay_init(180);//===================初始化延时函数 uart1_init(); while(1) { HAL_UART_Transmit(&usrt1_handler,p,sizeof(p),1000 ); delay_ms(300); } } //---------------------end----------------------------------------------------- usart.c #include "usart.h" UART_HandleTypeDef usrt1_handler; void uart1_init(void)//串口1初始化 { usrt1_handler.Instance=USART1; usrt1_handler.Init.BaudRate=115200; usrt1_handler.Init.WordLength=UART_WORDLENGTH_8B; usrt1_handler.Init.StopBits=UART_STOPBITS_1; usrt1_handler.Init.HwFlowCtl=UART_HWCONTROL_NONE; usrt1_handler.Init.Mode=UART_MODE_TX_RX; usrt1_handler.Init.Parity=UART_PARITY_NONE; HAL_UART_Init(&usrt1_handler); } void HAL_UART_MspInit(UART_HandleTypeDef *huart) { GPIO_InitTypeDef GPIO_Initure; if(huart->Instance==USART1) { __HAL_RCC_GPIOA_CLK_ENABLE(); //使能GPIOA时钟 __HAL_RCC_USART1_CLK_ENABLE(); //使能USART1时钟 GPIO_Initure.Pin=GPIO_PIN_9; //PA9 GPIO_Initure.Mode=GPIO_MODE_AF_PP; //复用推挽输出 GPIO_Initure.Pull=GPIO_PULLUP; //上拉 GPIO_Initure.Speed=GPIO_SPEED_HIGH; //高速 GPIO_Initure.Alternate=GPIO_AF7_USART1; //复用为USART1 HAL_GPIO_Init(GPIOA,&GPIO_Initure); //初始化PA9 GPIO_Initure.Pin=GPIO_PIN_10; //PA10 HAL_GPIO_Init(GPIOA,&GPIO_Initure); //初始化PA10 } } usart.h #ifndef _USART_H #define _USART_H #include "sys.h" #include "stdio.h" extern UART_HandleTypeDef usrt1_handler; void uart1_init(void); #endif

 

最新回复(0)