GPIO STM32F1

Started by tha, September 08, 2020, 08:42:55 AM

Previous topic - Next topic

tha

เดี๋ยวจะมาแปลให้ดูนะ แต่จะไม่เอาทั้งหมดนะ อันไหนที่ยังไม่ได้ใช้ก็จะไม่แปล เดี๋ยวจะเยอะเกินไป เยอะมาก ก็ลองหัดแปลกัน คำศัพย์มีไม่กี่ตัว

9.1.7 Input configuration(การกำหนดค่าอินพุท)
เมื่อ I/O Port ถูกโปรแกรมเป็น Input:
  • The Output Buffer ถูกปิดการใช้งาน
  • The Schmitt Trigger Input ถูกทำให้ทำงาน
  • The weak pull-up and pull-down resistors ถูกทำให้ทำงานหรือไม่ขึ้นอยู่กับ input configuration (pull-up, pull-down or floating):
  • The data ที่แสดงบน  I/O pin จะถูกสุ่มเข้าไปใน Input Data Register ทุกๆ APB2 clock cycle
  • A read เข้าถึง the Input Data Register จะได้ the I/O State.


tha

ดู Reference Manual ตามไปด้วยนะ


9.1.8 Output configuration
เมื่อ the I/O Port ถูกโปรแกรมเป็น Output:
  • The Output Buffer ถูกเปิดการใช้งาน:
        – Open Drain Mode: A "0" ใน the Output register ทำให้ the N-MOS ทำงาน ขณะที่ "1" ใน the Output register
           ทำให้ the port อยู่ในสถานะไฮอิมพิแด๊นต์ (Hi-Z )(the P-MOS จะไม่ถูกทำให้ทำงานเลย)
          ( Open Drain นี้จะต้องมี power supply ภายนอกมาต่อกัย load แล้วอีกขาหนึ่งของ load ก็มาต่อที่ output pin อีกที คือจะลง
          กราวน์ภายในตัว mcu )
       – Push-Pull Mode: A "0" in the Output register ทำให้ the N-MOS ทำงาน ขณะที่ "1" ใน the Output register ทำให้
          P-MOS ทำงาน
        ( mcu จะเป็นตัวจ่ายไฟให้ load ภายนอก)
  • The Schmitt Trigger Input ถูกทำให้ทำงาน
  • The weak pull-up and pull-down resistors ถูกปิดการใช้งาน.
  • The data ที่แสดงบน  I/O pin จะถูกสุ่มเข้าไปใน Input Data Register ทุกๆ APB2 clock cycle
  • A read เข้าถึง the Input Data Register จะได้ the I/O state ใน open drain mode
  • A read เข้าถึง the Output Data register จะได้ค่าที่เขียนเป็นครั้งสุดท้ายใน Push-Pull mode


tha





ถ้าเราเลือกให้เป็น input mode (MODE[1:0]=00 เราสามารถเลือกให้เป็น input แบบไหนก็กำหนดใน bit config CNFy[1:0]: ตามนี้ได้เลย
   00: Analog mode
   01: Floating input (reset state)
   10: Input with pull-up / pull-down
ถ้าเลือก Input with pull-up / pull-down จะให้เป็นแบบ Input  pull-up ก็กำหนดให้ Port Output Data Register bit นั้นให้เป็น '1' หรือจะให้เป็นแบบ Input  pull-down ก็กำหนดให้ Port Output Data Register bit นั้นให้เป็น '0' ดูตาม Table 20 นะครับ

ถ้าเราเลือกเป็น output mode (MODE[1:0] > 00 เราสามารถเลือกให้เป็น output แบบไหนก็กำหนดใน bit config CNFy[1:0]: ตามนี้ได้เลย
   00: General purpose output push-pull
   01: General purpose output Open-drain
   10: Alternate function output Push-pull
   11: Alternate function output Open-drain

เราจะเลือก mode เป็นแบบไหนก็กำหนดได้ที่ bit mode MODEy[1:0]: ตามนี้ได้เลย
   00: Input mode (reset state)
   01: Output mode, max speed 10 MHz.
   10: Output mode, max speed 2 MHz.
   11: Output mode, max speed 50 MHz.


tha

อย่างเช่นถ้าเราต้องการให้บิต PA0 เป็น input แบบ Floating ก็กำหนดดังนี้

   GPIOA_CRL = 0x00000004; 

ลองไล่บิตดูนะว่าถูกต้องไหม
ถ้าเราต้องการให้บิต PA4 เป็น output แบบ General purpose output push-pull และ Output mode, max speed 50 MHz. อีกด้วย ก็กำหนดเพิ่มมาดังนี้

   GPIOA_CRL = 0x00030004;

ปล. ผิดพลาดอย่างไร ท่านใดทราบ กรุณาแนะนำด้วยนะครับ

tha

ถ้าใช้ HAL ก็จะเขียนได้เป็นแบบนี้

static void MX_GPIO_Init(void)
   {

     GPIO_InitTypeDef GPIO_InitStruct;

     /* GPIO Ports Clock Enable */
     __HAL_RCC_GPIOA_CLK_ENABLE();

          /*Configure GPIO pin : PA0 */
         GPIO_InitStruct.Pin = GPIO_PIN_0;
         GPIO_InitStruct.Mode = GPIO_MODE_INPUT; // digital Input
         GPIO_InitStruct.Pull = GPIO_NOPULL;
         HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
   
     /*Configure GPIO pins : PA4 */
     GPIO_InitStruct.Pin = GPIO_PIN_4;
     GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
     HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

   }

ก็เอาตัวอย่างมาจากนี้ครับ
https://simonmartin.ch/resources/stm32/dl/STM32%20Tutorial%2001%20-%20GPIO%20Operations%20using%20HAL%20(and%20FreeRTOS).pdf

ปล. ผิดถูกอย่างไร แนะนำกันมาเลย ผิดก็แจ้งมานะครับจะได้แก้ไข รับทราบที่ถูกต้องกันทุกฝ่ายนะครับ

tha

GPIO_InitTypeDef GPIO_InitStruct;  // มันหมายถึงอะไร ท่านใดทราบก็แนะนำกันด้วยครับ เสริมมาเลย ไม่ต้องเกรงใจ จะได้แน่นๆ

dec

Quote from: tha on September 08, 2020, 04:10:57 PM
GPIO_InitTypeDef GPIO_InitStruct;  // มันหมายถึงอะไร ท่านใดทราบก็แนะนำกันด้วยครับ เสริมมาเลย ไม่ต้องเกรงใจ จะได้แน่นๆ

มันคือตัวแปร Structure ที่รวม config parameter ต่างๆ ของ GPIO ครับ มีไว้เพื่อใช้ Init GPIO โดยเฉพาะครับ

Style ของ ST เค้าชอบออกแบบ Function ให้การ Init มันจบใน Function เดียว อย่าง HAL_GPIO_Init
ถ้า implement ให้ส่งค่า config เป็น parameter ทั้งหมด parameter มันจะดูยืดยาวและรกมาก

HAL_GPIO_Init(GPIOA, GPIO_PIN_4, GPIO_MODE_OUTPUT_PP, GPIO_PULLUP, GPIO_SPEED_FREQ_HIGH, 0x00);

ST เลยนิยาม Structure มาตัวหนึ่งมารวม config ต่างๆ ที่เป็นไว้ ให้เซ็ตค่า
แล้วก็ส่ง Pointer to Structure ตัวนี้เป็น Parameter ไปแทน

HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

tha

ขอบคุณครับ คุณ dec
มาดู Port input data register กันต่อ
bits เหล่านี้อ่านได้เพียงอย่างเดียวและสามารถเข้าถึงได้เป็นแบบ Word mode เพียงอย่างเดียว. register บรรจุค่าอินพุทของ the corresponding(ที่ตรงกัน) I/O port.



เราจะกำหนดแบบนี้ได้ไหม

   unsigned long A;
   A = GPIOA_IDR;

หรือตามตัวอย่างเขา

   /* read PA0 */
   if(GPIOA -> IDR & GPIO_PIN_0)
   {
       /* user code*/
    }

หรืจะใช้ HAL library function

   /* read PCA0 */
   if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0))
   {
       /* user code */
   }