本帖最后由 834597084 于 2020-3-21 20:21 編輯
之前我們做的一個程序是使用PA2和PA3接口作為usart2來通信,現在接到的板子用的是PD5,PD6,想問問各位大佬這兩種方式有什么區別嗎?應該如何改動代碼呢?
原代碼:
void Usart2_Init(void)//連接端口
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
/* 配置 USART2 Tx (PA.02) 為復用推挽輸出 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* 配置 USART2 Rx (PA.03) 為上拉輸入。否則不接串口程序允許到串口就司機 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
/* 按結構體配置某個串口 */
USART_Init(USART2, &USART_InitStructure);
/*允許接收中斷 */
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
USART_ITConfig(USART2, USART_IT_TXE, DISABLE);
/*串口1使能*/
USART_Cmd(USART2, ENABLE);
}
|