# Button "Bounce" Principles

การเชื่อมต่อ push button switch เพื่อเป็นอินพุตไปยังไมโครคอนโทรลเลอร์เป็นเรื่องที่ไม่ยาก แต่ก็จะเกิดปัญหาในบางครั้ง ซึ่ง ปัญหาหลักคือสวิตช์จะมีการกระเด้ง (Bouncing) ซึ่งหมายถึง เมื่อมีการกด (หรือปล่อย) push button switch มันจะมักเปลี่ยนระดับสองครั้งก่อนที่จะคงที่ที่ระดับใหม่ ดังนั้นถ้าเชื่อมต่อสวิตช์กับขาที่เปิดใช้งาน interrupt ภายนอก เมื่อมีการกดปุ่มครั้งเดียว มีโอกาสที่จะได้รับสัญญาณ interrupts หลายครั้งซ้อนกัน ทำให้โปรแกรมอาจจะเข้าใจผิดว่ามีการกดเกิดขึ้นหลายครั้ง โดยทั่วไปประมาณ 10 ถึง 100 ครั้งในช่วงเวลาประมาณ 1 มิลลิวินาที *("The Art of electronics", Horowitz & Hill, Second edition, p. 506)* ดังแสดงในรูป

<figure><img src="/files/DTOOeOuaB5MqGIMb7MHt" alt=""><figcaption></figcaption></figure>

หนึ่งในเทคนิคที่ถูกนำใช้เพื่อจัดการกับปัญหานี้คือ ให้มีการตรวจสอบ (check/poll) ค่าสถานะของ push button switch เป็นระยะๆ และตัดสินใจว่า push button switch ได้ถูกกดอยู่ ถ้าหากมันอยู่ในสถานะที่ถูกกดเป็นครั้งที่สอง

ดังนั้นเมื่อมีการเชื่อมต่อ push button switch กับขาอินพุตบนไมโครคอนโทรเลอร์ เราจำเป็นต้องมีการกำหนดสถานะที่ชัดเจนทั้งในกรณีที่สวิตช์เปิดและเมื่อสวิตช์ปิด สามารถทำได้ด้วยตัวต้านทานที่ดึงขาพอร์ตในทิศทางหนึ่งเมื่อปุ่มเปิด เราสามารถเลือกการตั้งค่าที่มีสถานะสูงหรือสถานะต่ำ

<figure><img src="/files/Zo7CxA8aba9pfME2EUjz" alt=""><figcaption></figcaption></figure>

เนื่องจากไมโครคอนโทรเลอร์ มีตัวต้านทาน pull-up ภายใน เรามักจะเลือกการตั้งค่าที่มีสถานะต่ำที่แสดงอยู่ทางซ้ายในภาพ เราสามารถข้ามตัวต้านทานภายนอกและเปิดใช้งานตัวต้านทาน pull-up ภายในแทน


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.aic-eec.com/biil_psoc6/basic-mcu-interfacing/psoc-tm-6s2-peripherals-interfacing-gpio/button-bounce-principles.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
