stm32串口通用怎麽加功能
1.使能串口時鐘+使能GPIO時鐘
2.設置引腳的復位映射
3.配置GPIO引腳初始化,GPIO引腳的模式改為復用模式
4.配置串口的參數
5.中斷優先級+配置NVIC初始化
6.使能串口
7.編寫中斷服務函數
---------------------------------
1.硬件圖 (day04/串口.png)
2.
a.使能串口時鐘 RCC_APB2PeriphClockCmd
void RCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph, FunctionalState NewState)
參數1:uint32_t RCC_APB2Periph 串口號
RCC_APB2Periph_USART1
參數2:FunctionalState NewState
ENABLE 開啟使能 DISABLE 關閉使能
b.設置引腳的復位映射 GPIO_PinAFConfig()
void GPIO_PinAFConfig(GPIO_TypeDef* GPIOx, uint16_t GPIO_PinSource, uint8_t GPIO_AF)
參數1:GPIO_TypeDef* GPIOx 組別
GPIOA ---GPIOG
參數2:uint16_t GPIO_PinSource 引腳號
GPIO_PinSourcex where x can be (0..15).
參數3:uint8_t GPIO_AF
@arg GPIO_AF_USART1: Connect USART1 pins to AF7
@arg GPIO_AF_USART2: Connect USART2 pins to AF7
@arg GPIO_AF_USART3: Connect USART3 pins to AF7
@arg GPIO_AF_UART4: Connect UART4 pins to AF8
@arg GPIO_AF_UART5: Connect UART5 pins to AF8
@arg GPIO_AF_USART6: Connect USART6 pins to AF8
@arg GPIO_AF_UART7: Connect UART7 pins to AF8
@arg GPIO_AF_UART8: Connect UART8 pins to AF8
等下配置復用功能 GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF;//復用模式
c.配置串口的參數 USART_Init
void USART_Init(USART_TypeDef* USARTx, USART_InitTypeDef* USART_InitStruct)
參數1:USART_TypeDef* USARTx 串口號
USARTx: where x can be 1, 2, 3, 4, 5, 6, 7 or 8
參數2:USART_InitTypeDef* USART_InitStruct
typedef struct
{
uint32_t USART_BaudRate; /*!< 波特率 */
uint16_t USART_WordLength; /*!< 字節長度 */
uint16_t USART_StopBits; /*!< 停止位 */
uint16_t USART_Parity; /*!< 奇偶校驗位 */
uint16_t USART_Mode; /*!< 模式配置*/
uint16_t USART_HardwareFlowControl; /*!< 硬件控制流 */
} USART_InitTypeDef;
1.常用的波特率 是9600 115200
2.
/** @defgroup USART_Word_Length
* @{
*/
#define USART_WordLength_8b ((uint16_t)0x0000)//8位
#define USART_WordLength_9b ((uint16_t)0x1000)//9位
3.
/** @defgroup USART_Stop_Bits
* @{
*/
#define USART_StopBits_1 ((uint16_t)0x0000)
#define USART_StopBits_0_5 ((uint16_t)0x1000)
#define USART_StopBits_2 ((uint16_t)0x2000)
#define USART_StopBits_1_5 ((uint16_t)0x3000)
4./** @defgroup USART_Parity
* @{
*/
#define USART_Parity_No ((uint16_t)0x0000)//常用
#define USART_Parity_Even((uint16_t)0x0400)//奇校驗
#define USART_Parity_Odd ((uint16_t)0x0600)//偶校驗
5.
/** @defgroup USART_Mode
* @{
*/
#define USART_Mode_Rx ((uint16_t)0x0004)//接收
#define USART_Mode_Tx ((uint16_t)0x0008)//發送
6.
/** @defgroup USART_Hardware_Flow_Control
* @{
*/
#define USART_HardwareFlowControl_None ((uint16_t)0x0000)//無流控
#define USART_HardwareFlowControl_RTS ((uint16_t)0x0100)
#define USART_HardwareFlowControl_CTS ((uint16_t)0x0200)
#define USART_HardwareFlowControl_RTS_CTS ((uint16_t)0x0300)
d.中斷優先級+配置NVIC初始化
void NVIC_PriorityGroupConfig(uint32_t NVIC_PriorityGroup)
參數:uint32_t NVIC_PriorityGroup
@arg NVIC_PriorityGroup_0: 0 bits for pre-emption priority
4 bits for subpriority
@arg NVIC_PriorityGroup_1: 1 bits for pre-emption priority
3 bits for subpriority
@arg NVIC_PriorityGroup_2: 2 bits for pre-emption priority
2 bits for subpriority
@arg NVIC_PriorityGroup_3: 3 bits for pre-emption priority
1 bits for subpriority
@arg NVIC_PriorityGroup_4: 4 bits for pre-emption priority
0 bits for subpriority
配置中斷分組 NVIC_Init()
void NVIC_Init(NVIC_InitTypeDef* NVIC_InitStruct)
typedef struct
{
uint8_t NVIC_IRQChannel; /*!<中斷線 */
uint8_t NVIC_IRQChannelPreemptionPriority; /*!< 搶占優先級 */
uint8_t NVIC_IRQChannelSubPriority; /*!< 響應優先級*/
FunctionalState NVIC_IRQChannelCmd; /*!<權限*/
} NVIC_InitTypeDef;
1.
USART1_IRQn = 37, /*!< USART1 global Interrupt */
USART2_IRQn = 38, /*!< USART2 global Interrupt */
USART3_IRQn = 39, /*!< USART3 global Interrupt