For the complete documentation index, see llms.txt. This page is also available as Markdown.

GPIO Integration

วัตถุประสงค์

  • Integration: รวมความรู้จาก Lab 1-3 (LED, Button, ADC)

  • Real Application: สร้าง Interactive LED Controller

  • State Machine: จัดการหลาย input/output พร้อมกัน

จะได้เรียนรู้อะไร

  • Multiple Inputs: จัดการ POT + Button พร้อมกัน

  • Value Mapping: แปลงค่า POT เป็น delay time

  • Mode Switching: สลับโหมดด้วย button

  • Non-Blocking Design: ทำหลายอย่างพร้อมกัน


Application Design

┌─────────────────────────────────────────────────────────────────┐
                  Interactive LED Controller                     
├─────────────────────────────────────────────────────────────────┤
                                                                 
   INPUTS:                                                       
   ════════                                                      
   POT (P15_1)     ────►  LED Blink Rate                         │
   └─ 0%   = 1000ms (ช้าสุด)                                       │
   └─ 50%  = 525ms                                               
   └─ 100% = 50ms  (เร็วสุด)                                       │
                                                                 
   Button (USER_BTN1) ────►  Toggle Mode                         │
   └─ Active LOW (กด = 0)                                        │
   └─ Debounce 50ms                                              
                                                                 
   MODES:                                                        
   ═══════                                                       
   MODE_NORMAL:  LED blinks at POT-controlled speed              
   MODE_FAST:    LED blinks at 2x speed (delay / 2)              │
                                                                 
   OUTPUT:                                                       
   ═══════                                                       
   LED (USER_LED1) ────►  Blinks based on calculated delay       │
   Serial Terminal ────►  Real-time ASCII status bar             
                                                                 
└─────────────────────────────────────────────────────────────────┘

Lab 4.1 : Code Implementation

Step 1: Macros Configuration

Step 2: Mode Definition

Step 3: Helper Functions

read_button_with_debounce() - Delay-Based Debounce

หมายเหตุ: PSoC Edge ไม่มี Cy_SysLib_GetTimerTick() จึงใช้ Delay-Based Debounce แทน

map_pot_to_delay() - แปลงค่า POT เป็น Delay

print_status_bar() - ASCII Status Bar

Step 4: Main Function (Non-Blocking Design)


4. Flowchart


5. State Diagram


6. ตัวอย่าง Output


Program Code ทั้งหมด


7. แบบฝึกหัด

Exercise 1: Three Modes (Triple Speed)

Hint

Exercise 2: LED Brightness Pattern

Hint

สร้าง pattern เช่น heartbeat (สั้น-สั้น-ยาว)



Last updated

Was this helpful?