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 )。
王偉
|