首页 > 其他 > 详细

Uart_STM32F4

时间:2021-03-28 11:11:47      阅读:30      评论:0      收藏:0      [点我收藏+]
技术分享图片
1 #ifndef __BSP_UART_H
2 #define __BSP_UART_H
3 #include "main.h"
4 
5 void User_Uart1_Init(void);
6 
7 #endif
bsp_uart.h
技术分享图片
 1 #include "bsp_uart.h"
 2 
 3 UART_HandleTypeDef huart1;
 4 
 5 void User_Uart1_Init(void)
 6 {
 7     GPIO_InitTypeDef GPIO_Instructure;
 8     
 9     __HAL_RCC_GPIOA_CLK_ENABLE();
10     __HAL_RCC_USART1_CLK_ENABLE();
11     
12     GPIO_Instructure.Pin       = GPIO_PIN_9; //TX
13     GPIO_Instructure.Pull      = GPIO_PULLUP;
14     GPIO_Instructure.Speed     = GPIO_SPEED_FREQ_VERY_HIGH; //50-200MHz
15     GPIO_Instructure.Mode      = GPIO_MODE_AF_PP;
16     GPIO_Instructure.Alternate = GPIO_AF7_USART1;
17     HAL_GPIO_Init(GPIOA,&GPIO_Instructure);
18     
19     GPIO_Instructure.Pin       = GPIO_PIN_10; //RX
20     GPIO_Instructure.Pull      = GPIO_NOPULL;
21     GPIO_Instructure.Speed     = GPIO_SPEED_FREQ_VERY_HIGH; //50-200MHz
22     GPIO_Instructure.Mode      = GPIO_MODE_INPUT;
23     GPIO_Instructure.Alternate = GPIO_AF7_USART1;
24     HAL_GPIO_Init(GPIOA,&GPIO_Instructure);
25     
26     huart1.Instance          = USART1;
27     huart1.Init.BaudRate     = 115200;
28     huart1.Init.HwFlowCtl    = UART_HWCONTROL_NONE;
29     huart1.Init.Mode         = UART_MODE_TX_RX;
30     huart1.Init.OverSampling = UART_OVERSAMPLING_16;
31     huart1.Init.Parity       = UART_PARITY_NONE;
32     huart1.Init.StopBits     = UART_STOPBITS_1;
33     huart1.Init.WordLength   = UART_WORDLENGTH_8B;
34     HAL_UART_Init(&huart1);
35     
36     HAL_NVIC_SetPriority(USART1_IRQn,0,0);
37     HAL_NVIC_EnableIRQ(USART1_IRQn);
38 }
39 int fputc(int ch,FILE *f)
40 {
41     HAL_UART_Transmit(&huart1,(uint8_t *)&ch,1,0xFFFF);
42     while(__HAL_UART_GET_FLAG(&huart1,UART_FLAG_TXE) != SET);
43     return ch;
44 }
bsp_uart.c

 

Uart_STM32F4

原文:https://www.cnblogs.com/pathfinder-world/p/14587323.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!