STM32F10x GPIO -2


GPIO的功能8種模式 ,可以通過程式設計選擇:
 1. 浮空輸入(Input floating)
 2. 輸入上拉(Input pull-up)
3. 輸入下拉(Input-pull-down)
4.  類比輸入(Analog Input)
5. 開漏輸出(Output open-drain)
6. 推挽輸出(Output push-pull)
7. 複用功能的推挽輸出(Alternate function push-pull)
8. 複用功能的開漏輸出(Alternate function open-drain)


在網路上找到一些會GPIO的功能用途的說明如下:

1. 輸入浮接(Input floating),可以做為鍵盤掃描使用,電壓準位由外部訊號來決定,沒有訊號驅動,就會呈現高阻抗狀態。
2. 輸入上拉(Input pull-up),啟用內部上拉電阻(20~40K),若使用在按鈕輸入,外部就可以少接上一顆電阻節省外部零件。
3. 輸入下拉(Input-pull-down),啟用內部下拉電阻(20~40K),若使用在按鈕輸入,外部就可以少接上一顆電阻節省外部零件。
4. 類比輸入(Analog),ADC訊號輸入。
5. 開漏輸出(Output open-drain),輸出0時,IO = GND,輸出1時 IO = 浮接,所以需要在外部加上上拉電阻,此模式可以當成雙向IO使用(與8051相同)。
6. 推挽輸出(Output push-pull),輸出0時,IO = GND,輸出1時 IO = VCC。
7. 推挽式交替功能(Alternate function push-pull),啟動I2C,SDA、SCL等…
8. 開漏式交替功能(Alternate function open-drain),啟動TX、MOSI、MISO、SCK等…


由 ST 提供的範例來看,可能會比較好

1. USART 的GPIO規劃
void GPIO_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;

  /* Configure USART1 Tx (PA.09) as alternate function push-pull */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  /* Configure USART1 Rx (PA.10) as input floating */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
}


2. I2C1 的GPIO規劃
Connect I2C1 SCL pin (PB.06) to I2C EEPROM SCL (pin6)
   - Connect I2C1 SDA pin (PB.07) to I2C EEPROM SDA (pin5)

void GPIO_Configuration(void)
{
  GPIO_InitTypeDef  GPIO_InitStructure;

  /* Configure I2C1 pins: SCL and SDA */
  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_6 | GPIO_Pin_7;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
  GPIO_Init(GPIOB, &GPIO_InitStructure);
}
3. SPI  的GPIO規劃
void GPIO_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;

  /* Configure SPI1 pins: NSS, SCK, MISO and MOSI ----------------------------*/
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | 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);

  /* Configure SPI2 pins: NSS, SCK, MISO and MOSI ----------------------------*/
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
  GPIO_Init(GPIOB, &GPIO_InitStructure);
}



由這些內容對GPIO 規劃作一些總結:


1. Configure USART Rx as input floating   GPIO_Mode_IN_FLOATING。或外部訊號決定 High/Low
2. 輪詢KEY : GPIO_Mode_IPU,輸入上拉(Input pull-up)
3.按鍵中斷:帶下拉輸入GPIO_Mode_IPD
4.  ADC  GPIO_Mode_AIN;

5.蜂鳴器 、I2C (軟體模擬)開漏輸出 GPIO_Mode_OUT_OD
6. LED推挽式輸出GPIO_Mode_OUT_PP。
 7. Configure USART Tx  、SPI  NSS, SCK, MISO and MOSI ,  GPIO_Mode_AF_PP;複用功能的推挽 輸出_AF_PP。
8.. STM32讀寫EEPROM 24C08 (I2C) : GPIO_Mode_AF_OD

留言

這個網誌中的熱門文章

STM32 I2C-EEPROM 的讀寫

如何提高STM32 ADC的精度

ENC28J60+Uip TCP Server/Client