新聞中心

STM32 UART 重映射

作者: 時(shí)間:2016-11-10 來(lái)源:網(wǎng)絡(luò) 收藏
在進(jìn)行原理圖設(shè)計(jì)的時(shí)候發(fā)現(xiàn)管腳的分配之間有沖突,需要對(duì)管腳進(jìn)行重映射,在手冊(cè)中了解到STM32 上有很多I/O口,也有很多的內(nèi)置外設(shè)像:I2C,ADC,ISP,USART等 ,為了節(jié)省引出管腳,這些內(nèi)置外設(shè)基本上是與I/O口共用管腳的,也就是I/O管腳的復(fù)用功能。但是STM32還有一特別之處就是:很多復(fù)用內(nèi)置的外設(shè)的 I/O引腳可以通過(guò)重映射功能,從不同的I/O管腳引出,即復(fù)用功能的引腳是可通過(guò)程序改變的。

第一次這么干感覺(jué)心里沒(méi)底,所以針對(duì)USART1在STM32F103RBT6的板子上實(shí)現(xiàn)了一把,以下是相關(guān)的測(cè)試代碼:

本文引用地址:http://butianyuan.cn/article/201611/317500.htm

/*****************************************************************************

//函數(shù)名:void Uart1_Init(void)

//功能:串口(USART1)重映射初始化配置函數(shù),由TX PA9~PB6 RX PA10~~PB7

*****************************************************************************/

void Uart1_Init(void)

{

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO,ENABLE);//開(kāi)啟端口B和復(fù)用功能時(shí)鐘

GPIO_PinRemapConfig(GPIO_Remap_USART1,ENABLE);//使能端口重映射

GPIO_InitTypeDef GPIO_InitStructure;

//uart 的GPIO重映射管腳初始化 PB6 usart1_TX PB7 USART_RX

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);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);

USART_InitTypeDef USART_InitStructure;

//串口參數(shù)配置:9600,8,1,無(wú)奇偶校驗(yàn),無(wú)硬流量控制 ,使能發(fā)送和接收

USART_InitStructure.USART_BaudRate = 9600;

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_Cmd(USART1, ENABLE);

}

簡(jiǎn)要分析重映射步驟為:

1.打開(kāi)重映射時(shí)鐘和USART重映射后的I/O口引腳時(shí)鐘,

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB |RC C_APB2Periph_AFIO,ENABLE);

2.I/O口重映射開(kāi)啟.

GPIO_PinRemapConfig(GPIO_Remap_USART1,ENABLE);

3.配制重映射引腳, 這里只需配置重映射后的I/O,原來(lái)的不需要去配置.

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);

只需要簡(jiǎn)單的以上三步就能輕松搞定。



關(guān)鍵詞: STM32UART重映

評(píng)論


技術(shù)專區(qū)

關(guān)閉