本帖最后由 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);
}
|