标题: stm32f103vet6单片机将PD5,PD6配置为USART2 [打印本页]

作者: 834597084    时间: 2020-3-21 20:20
标题: stm32f103vet6单片机将PD5,PD6配置为USART2
本帖最后由 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);
}


作者: csmyldl    时间: 2020-3-21 20:57
改以下三个地方(实际4个地方)
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_Init(GPIOD, &GPIO_InitStructure);   //这一句有2个地方
作者: 834597084    时间: 2020-3-21 22:31
好像需要remap...
作者: 834597084    时间: 2020-3-21 23:04
csmyldl 发表于 2020-3-21 20:57
改以下三个地方(实际4个地方)
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Pi ...

请问还需要运用remap函数对USART2进行重新映射吗?之前没改过的话,USART2默认映射的端口是PA2和PA3吗?之前学校实验用的板子都是用的默认端口,现在给我了一个跟默认的引出脚不同的板子。。。
作者: 834597084    时间: 2020-3-21 23:06
csmyldl 发表于 2020-3-21 20:57
改以下三个地方(实际4个地方)
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Pi ...

但也没在库里找到remap这个函数。。。




欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1