STM32串口的部分映射与完全映射

觉得不错分享给大家,学习!

转载地址:http://www.eeworld.com.cn/mcu/article_2017091234363.html

                http://blog.csdn.net/liucheng_34/article/details/48175505


以stm32的USART1来举例


默认使用的是PA9为TX,PA10为RX,当需要开启复用功能时,需要注意修改以下几个地方的代码

      1 时钟的开启


     当使用复用功能后,就需要开启复用时钟RCC_APB2Periph_AFIO


 2 初始化引脚的不同,原来的是PA9,PA10,但是现在要初始化PB6,PB7,这一部分就不贴代码了。

 3 调用GPIO_PinRemapConfig()函数

      


  1. GPIO_PinRemapConfig(GPIO_Remap_USART1,ENABLE);  

    USART1的重映射开启




注意:GPIO_PinRemapConfig()函数的参数是不能随便写的,在《stm32固件库使用手册》里面给出了可用参数的列表

GPIO_PinRemapConfig(GPIO_FullRemap_USART3, ENABLE); //USART3的复用功能完全重映射

       GPIO_PinRemapConfig(GPIO_PartialRemap_USART3, ENABLE); //USART3的部分重映射

部分重映射和完全重映射有什么区别那?在网上我也没找到确定的答案,但是建议开启完全重映射,因为网上有资料说部分重映射在发送消息时候不正常(我自己没有做实验)

还需要注意,在开启重映射功能之前一定要先查看一下自己的单片机是否支持重映射功能。


附:初始化串口的程序


  1. void USART1_Config(void)  

  2. {  

  3.         GPIO_InitTypeDef GPIO_InitStructure;  

  4.         USART_InitTypeDef USART_InitStructure;  

  5.           

  6.         /* config USART1 clock */  

  7.       

  8.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO, ENABLE);  

  9.           

  10.         /* USART1 GPIO config */  

  11.           

  12.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;  

  13.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  

  14.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  

  15.         GPIO_Init(GPIOB, &GPIO_InitStructure);  

  16.       

  17.           

  18.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;  

  19.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;  

  20.         GPIO_Init(GPIOB, &GPIO_InitStructure);  

  21.           

  22.       

  23.             GPIO_PinRemapConfig(GPIO_Remap_USART1,ENABLE);  

  24.         //GPIO_AFIODeInit();  

  25.         /* USART1 mode config */  

  26.         USART_InitStructure.USART_BaudRate = 115200;  

  27.         USART_InitStructure.USART_WordLength = USART_WordLength_8b;  

  28.         USART_InitStructure.USART_StopBits = USART_StopBits_1;  

  29.         USART_InitStructure.USART_Parity = USART_Parity_No ;  

  30.         USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;  

  31.         USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;  

  32.         USART_Init(USART1, &USART_InitStructure);   

  33.         USART_Cmd(USART1, ENABLE);  

  34. }  

重映射步骤为:

1.打开重映射时钟和USART重映射后的I/O口引脚时钟, 

           RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO,ENABLE);

2.I/O口重映射开启.

                    GPIO_PinRemapConfig(GPIO_Remap_USART1,ENABLE);

3.配制重映射引脚, 这里只需配置重映射后的I/O,原来的不需要去配置.

                              GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
                              GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
                              GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
                              GPIO_Init(GPIOB, &GPIO_InitStructure);


                               GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
                               GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
                               GPIO_Init(GPIOB, &GPIO_InitStructure);

只需要这三步

简单的说 STM32的 io 有3个功能 一个是默认的 一个是复用 一个是重映射功能(这个其实也属于复用)

如果配置成复用 则将使用第2个功能 如果配置成复用 同时 相应的重映射配置了 则将使用第3个功能

通常一个口的 复用+重映射有好多 不止两个 这时候就看你使能哪个设备了 

开复用 + 使能设备+ 是否重映射 就可以决定这个io口 到底使用哪个功能



http://fly-top.blog.163.com/blog/static/172755112201271724947243/

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是STM32串口映射的示例代码: 首先,在main函数前面添加如下代码,定义重映射的UART端口: ``` #define USART1_REMAP ``` 然后,在main函数内部添加如下代码,根据定义的重映射端口来初始化对应的UART端口: ``` #ifdef USART1_REMAP /* Enable USART1 clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); /* Enable GPIOA clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); /* Configure USART1 Tx (PA9) as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); /* Configure USART1 Rx (PA10) as input floating */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); /* USART1 configuration */ 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(USART1, &USART_InitStructure); /* Enable USART1 */ USART_Cmd(USART1, ENABLE); #endif ``` 需要注意的是,以上代码中的USART1_REMAP是根据具体情况来定义的,其他重映射端口可以按照类似的方式实现。 最后,在main函数中添加如下代码,以发送和接收数据: ``` /* Send data using USART1 */ USART_SendData(USART1, data); /* Wait for transmission to complete */ while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); /* Receive data using USART1 */ data = USART_ReceiveData(USART1); ``` 需要注意的是,以上代码中的data是一个变量,其类型可以根据具体情况来定义。同时,发送和接收数据的函数可以根据具体情况来修改,例如使用DMA方式进行数据传输。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值