原创 A simple example on STM32 Pin Re-mapping

2013-2-14 23:46 1438 1 1 分类: MCU/ 嵌入式

Pin Re-mapping is a simple but important skills in programming STM32.

Here shows a simple example to re-map USART1_TX and USART1_RX from PA9, PA10 (not Five-volt tolerant)
to PB6, PB7 (Five-volt tolerant) for STM32F103RB.

Comparisons between the steps in re-mapping and not re-mapping the pins

When PA9, PA10 are used for USART1
i. Turn on Peripheral Clock for GPIOA
ii. Config PA9 as Alternate Function Push-pull output, and PA10 as Input Floating
iii. Turn on Peripheral Clock for USART1
iv. Config USART1 baud rate and properties...


When PB6, PB7 are used for USART1 (Re-mapping)
i. Turn on Peripheral Clock for GPIOB
ii. Config PA6 as Alternate Function Push-pull output, and PA7 as Input Floating
iii. Turn on Peripheral Clock for USART1
iv. Config USART1 baud rate and properties...
v. Enable Alternate Function Clock

vi. Enable USART1 re-mapping

From above, we can see that only two more steps is needed... It is so simple!

Sample Coding for re-mapping the USART1:
----------------------------------------------------
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;


/* Turn on periph clock for both GPIOB and USART */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_USART1, ENABLE);

/* Config GPIO */
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_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);

/* Config USART1 */   
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);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
USART_ClearFlag(USART1,USART_FLAG_TC);
USART_Cmd(USART1, ENABLE);

/* Re-mapping Config */

RCC_APB2PeriphClockCmd( RCC_APB2Periph_AFIO , ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_USART1, ENABLE);

----------------------------------------------------
The peripheral clock command of AFIO can be combined with that of GPIOB and USART1,
i.e. the re-mapping only requires only 1 more line of code. 笑脸

广告

文章评论 0条评论)

登录后参与讨论
相关推荐阅读
mariohk321_586285434 2014-09-10 23:58
我的手机
苹果发布了iPhone6​​,再次掀起了一鼓追「疯」潮...没有留意苹果产品的我,也听到了不少消息...虽然我是一名电子爱好者,但是到2009年尾才拥有第一部智能手机。那是一部的Windows M...
mariohk321_586285434 2014-09-01 00:53
Updates from Me
Hi, long time no see! This September will be a different one for me, since I am not a student an...
mariohk321_586285434 2014-01-25 18:44
Reusing Card Reader from a Canon inkjet Printer
Inspired from Charlieb000: http://icrontic.com/discussion/93568/reusing-card-reader-from-mp180-can...
mariohk321_586285434 2013-12-30 14:22
Power? Energy? Electronics?
Electricity has brought numerous benefits to our daily life. But we should be aware of the energy ...
mariohk321_586285434 2013-11-29 20:19
Device is not powered on? Replacing the capacitors may help!
Hi! Long time no see! Life has been busy over the past few months... Students' lives are busy... ...
mariohk321_586285434 2013-08-23 10:14
STM32F1 with 1.5inch CSTN Color LCD
First of all, happy new year and happy Valentine's day! Life has been very busy over the past fe...
广告
我要评论
0
1
广告
关闭 热点推荐上一条 /2 下一条