标题: stm32 GPIO常用脚配置 [打印本页]

作者: 鹿先生    时间: 2017-11-1 16:22
标题: stm32 GPIO常用脚配置


GPIO  常用配置( MCU:STM32F103C8T6                ,固件库: 1.0 ):
AD:
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
   //AD  配置为模拟输入
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
   GPIO_Init(GPIOC, &GPIO_InitStructure);
CAN :
   /* Configure CAN pin: RX */
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
   //接收脚配置为上拉输入
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
   GPIO_Init(GPIOA, &GPIO_InitStructure);
   /* Configure CAN pin: TX */
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
   //发送脚配置为复用推拉输出
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
   GPIO_Init(GPIOA, &GPIO_InitStructure);
EXTI:
   /* Configure PB.09 as input floating (EXTI Line 9) */
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
   //作为外部中断时配置为浮空输入
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOA             TING;
   GPIO_Init(GPIOB, &GPIO_InitStructure);
I2C :
   /* Configure I2C1 pins: SCL and SDA     ----------------------------------------*/
   GPIO_InitStructure.GPIO_Pin =       GPIO_Pin_6 | GPIO_Pin_7;
   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
   //SCL 和SDA  都配置为复用开漏输出
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
   GPIO_Init(GPIOB, &GPIO_InitStructure);
SPI:
   /* Configure SPI1 pins: SCK, MISO and MOSI ---------------------------------*/
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
   //配置为复用推拉输出
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
   GPIO_Init(GPIOA, &GPIO_InitStructure);
USART:
  /* Configure USART2 RTS (PD.04) and USART2 Tx (PD.05) as alternate function push-pull */
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5;
   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  //发送脚配置为推拉输出
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
   GPIO_Init(GPIOD, &GPIO_InitStructure);
  /* Configure USART2 CTS (PD.03) and USART2 Rx (PD.06) as input floating */
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_6;
  //接收脚配置为浮空输入
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOA     TING;
   GPIO_Init(GPIOD, &GPIO_InitStructure);
总结:普通 IO        口作为输入时配置为浮空输入(  GPIO_Mode_IN_FLOA  TING  ),作为输出如
果不需要从本口获取数据时配置为推拉输出(                         GPIO_Mode_Out_PP  ),需要读取数据时配置
为开漏输出( GPIO_Mode_Out_OD           )。
                                                                        王伟






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