一般步驟: 1串口時(shí)鐘使能,GPIO時(shí)鐘使能: RCC_APB2PeriphClockCmd(); 2串口復(fù)位: USART_DeInit(); 這一步不是必須的 3GPIO端口模式設(shè)置: GPIO_Init(); 模式設(shè)置為GPIO_Mode_AF_PP 4串口參數(shù)初始化: USART_Init(); 5開啟中斷并且初始化NVIC(如果需要開啟中斷才需要這個(gè)步驟) NVIC_Init(); USART_ITConfig(); ⑥使能串口: USART_Cmd(); ⑦編寫中斷處理函數(shù):USARTx_IRQHandler(); ⑧串口數(shù)據(jù)收發(fā): void USART_SendData();//發(fā)送數(shù)據(jù)到串口,DR uint16_t USART_ReceiveData();//接受數(shù)據(jù),從DR讀取接受到的數(shù)據(jù) ⑨串口傳輸狀態(tài)獲取: FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint16_t USART_FLAG); void USART_ClearITPendingBit(USART_TypeDef* USARTx, uint16_t USART_IT);
|