初级会员

- 积分
- 83
- 金钱
- 83
- 注册时间
- 2016-5-14
- 在线时间
- 14 小时
|

楼主 |
发表于 2016-5-14 13:21:29
|
显示全部楼层
[mw_shl_code=c,true]/**
* @brief USART串口GPIO初始化
* @param 无
* @retval 无
**/
static void GPIO_USARTConfig(void)
{
GPIO_InitTypeDef GPIO_InitStructure;/*GPIO时钟初始化*/
/*对GPIO引脚的时钟进行初始化,此处为AHB1时钟总线*/
/*USART1 GPIO初始化*/
#ifdef USART1_RX_EN //USART1接收模式
RCC_AHB1PeriphClockCmd(USART1_RX_GPIO_Clk ,ENABLE);
#endif
#ifdef USART1_TX_EN //USART1发送模式
RCC_AHB1PeriphClockCmd(USART1_TX_GPIO_Clk,ENABLE);
#endif
#ifdef USART1_EN //USART1接收发送模式
RCC_AHB1PeriphClockCmd(USART1_RX_GPIO_Clk |
USART1_TX_GPIO_Clk ,ENABLE);
#endif
/*USART2 GPIO初始化*/
#ifdef USART2_RX_EN //USART2接收模式
RCC_AHB1PeriphClockCmd(USART2_RX_GPIO_Clk,ENABLE);
#endif
#ifdef USART2_TX_EN //USART2发送模式
RCC_AHB1PeriphClockCmd(USART2_TX_GPIO_Clk,ENABLE);
#endif
#ifdef USART2_EN //USART2发送模式
RCC_AHB1PeriphClockCmd(USART2_RX_GPIO_Clk |
USART2_TX_GPIO_Clk ,ENABLE);
#endif
/*配置GPIO*/
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //引脚复用模式
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽模式
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉电阻
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //输出速度为50MHz
#if defined (USART1_RX_EN) || defined (USART1_TX_EN) || defined(USART1_EN)
/*配置USART1 Rx GPIO*/
GPIO_InitStructure.GPIO_Pin = USART1_RX_GPIO_Pin; //配置RX
GPIO_Init(USART1_RX_GPIO_Port,&GPIO_InitStructure);
/*配置USART1 Tx GPIO*/
GPIO_InitStructure.GPIO_Pin = USART1_TX_GPIO_Pin;
GPIO_Init(USART1_TX_GPIO_Port,&GPIO_InitStructure);
/*引脚关联*/
GPIO_PinAFConfig(USART1_RX_GPIO_Port,USART1_RX_GPIO_Source,
GPIO_AF_USART1); //Rx引脚关联到USART2_Rx上
GPIO_PinAFConfig(USART1_TX_GPIO_Port,USART1_TX_GPIO_Source,
GPIO_AF_USART1); //Tx引脚关联到USART2_Tx上
#endif
#if defined (USART2_RX_EN) || defined (USART2_TX_EN) || defined(USART2_EN)
/*配置USART2 Rx GPIO*/
GPIO_InitStructure.GPIO_Pin = USART2_RX_GPIO_Pin; //配置RX
GPIO_Init(USART2_RX_GPIO_Port,&GPIO_InitStructure);
/*配置USART2 Tx GPIO*/
GPIO_InitStructure.GPIO_Pin = USART2_TX_GPIO_Pin;
GPIO_Init(USART2_TX_GPIO_Port,&GPIO_InitStructure);
/*引脚关联*/
GPIO_PinAFConfig(USART2_RX_GPIO_Port,USART2_RX_GPIO_Source,
GPIO_AF_USART2); //Rx引脚关联到USART2_Rx上
GPIO_PinAFConfig(USART2_TX_GPIO_Port,USART2_TX_GPIO_Source,
GPIO_AF_USART2); //Tx引脚关联到USART2_Tx上
#endif
}[/mw_shl_code] |
|