ARTICLE

Interrupt และ Timer คืออะไร? พื้นฐาน Embedded ที่สำคัญกว่า Delay Loop.

อธิบาย Interrupt, Timer, Polling และ Event-driven design สำหรับ ESP32/Arduino แบบใช้งานจริง ตั้งแต่ปุ่มกด Pulse Counter ไปจนถึง Sensor sampling พร้อมข้อควรระวังเรื่อง ISR และ Debounce

IoTProgrammingAutomationโดย Thospaakเผยแพร่ 10 ต.ค. 2569อัปเดต 10 ต.ค. 2569

ถ้าเขียน Firmware แบบเริ่มต้น เรามักเห็นโครงสร้าง:

อ่าน input
รอ
อ่าน input
รอ
ทำซ้ำ

วิธีนี้เรียกว่า polling แบบง่าย แต่เมื่อระบบต้องจับ pulse, อ่าน Sensor หลายตัว, คุม timing และสื่อสารพร้อมกัน Interrupt และ Timer จะเริ่มสำคัญ

Polling คืออะไร

Polling คือการถามสถานะซ้ำ ๆ ใน loop

ตัวอย่างแนวคิด:

loop:
  read button
  read sensor
  check network
  repeat

ข้อดีคือเข้าใจง่ายและ debug ง่าย

ข้อเสียคือถ้า loop ถูก block ด้วยงานอื่น อาจพลาด event หรือ timing ไม่สม่ำเสมอ

Interrupt คืออะไร

Interrupt คือกลไกที่ทำให้ MCU หยุด flow ปกติชั่วคราวเพื่อรับมือ event ที่กำหนด

ตัวอย่าง event:

  • GPIO edge;
  • timer event;
  • UART receive;
  • ADC/DMA completion;
  • peripheral condition

ภาพรวม:

Main code
  ↓
[event เกิด]
  ↓
Interrupt handler
  ↓
กลับไป main code

ISR คืออะไร

ISR หรือ Interrupt Service Routine คือ code ที่ทำงานเมื่อ interrupt เกิด

หลักสำคัญคือ ควรสั้นและ predictable

งานที่ไม่ควรยัดไว้ใน ISR โดยไม่จำเป็น เช่น:

  • network request;
  • long delay;
  • filesystem operation;
  • heavy calculation;
  • logging ยาว ๆ

รูปแบบที่ดีกว่าคือให้ ISR บันทึก event/flag/counter แล้วให้ main task ไปทำงานหนักต่อ

Timer คืออะไร

Timer เป็น peripheral ที่นับเวลา/clock เพื่อสร้าง event หรือวัดช่วงเวลา

ใช้กับ:

  • periodic sampling;
  • timeout;
  • PWM generation;
  • pulse measurement;
  • scheduler tick;
  • watchdog-related logic

Timer ทำให้ timing ไม่ต้องพึ่ง delay() ทุกอย่าง

Delay มีปัญหาอะไร

ถ้าใช้ blocking delay:

ทำงาน A
wait 5 seconds
ทำงาน B

ระหว่าง wait งานอื่นอาจไม่ได้รับการดูแลตามต้องการ

ระบบจริงจึงมักใช้:

  • non-blocking state machine;
  • timer/event;
  • RTOS task/timer;
  • timestamp comparison

ขึ้นกับ framework

Interrupt เหมาะกับงานอะไร

Pulse Counter

เช่น flow meter หรือ energy pulse:

Pulse edge
  ↓
Interrupt / hardware counter
  ↓
increment count

ถ้า polling ช้าเกินไปอาจพลาด pulse

Button / Contact

Interrupt ใช้ได้ แต่ต้องจัดการ bounce

Alarm Input

event สำคัญอาจต้องตอบสนองเร็ว แต่ต้องคิด fail-safe และ noise ด้วย

Debounce คืออะไร

Mechanical contact ไม่ได้เปลี่ยนจาก OFF → ON แบบคมสมบูรณ์ อาจเด้งหลายครั้งในไม่กี่ ms

Firmware อาจเห็น:

ON OFF ON OFF ON

ทั้งที่กดครั้งเดียว

วิธีแก้มีทั้ง:

  • hardware debounce;
  • software time filter;
  • state machine

อย่าคิดว่า interrupt = นับ event ได้ถูกทันที

Timer กับ Sampling Sensor

ถ้าต้องการอ่าน Sensor ทุก 1 วินาที ไม่ควรเขียนว่า:

read
sleep/delay 1000

ในระบบที่มี network/control หลายงาน

ควรแยก schedule จาก task execution เช่น:

Timer / timestamp
  ↓
set sample_due
  ↓
Sensor task reads data

Watchdog เกี่ยวอะไร

Watchdog ใช้ตรวจว่าระบบค้างหรือ task ไม่ตอบสนองตามที่คาด

แต่ watchdog reset ไม่ใช่คำตอบแทนการแก้ deadlock/blocking design

ควร log reset cause และออกแบบ recovery state

ESP32 มี RTOS แล้ว Interrupt ยังสำคัญไหม

ยังสำคัญ

RTOS ช่วยจัด task/scheduling แต่ hardware event ยังต้องเข้าสู่ software ผ่าน interrupt/peripheral mechanism หลายกรณี

สิ่งสำคัญคือรู้ว่าอะไรควรอยู่:

  • ISR;
  • high-priority task;
  • normal task;
  • network/background task

Timer/Interrupt กับ Automation Safety

อย่าเอา timing software เพียงชั้นเดียวไปแทน protection

ตัวอย่าง Pump:

MCU timer says stop after 10 min

ไม่ได้แทน:

  • overload protection;
  • dry-run protection ถ้าจำเป็น;
  • pressure/mechanical protection;
  • contactor/interlock;
  • emergency stop

Software timeout เป็น control feature ไม่ใช่ safety layer อัตโนมัติ

8051 มี Timer/Interrupt เหมือน ESP32 ไหม

แนวคิดพื้นฐานมีมานานแล้วใน MCU รุ่นเก่า รวมถึง 8051/MCS-51

สิ่งที่เปลี่ยนคือ peripheral, abstraction, RTOS และ development ecosystem แต่แนวคิด event/timing ยังเหมือนเดิมในระดับระบบ

อ่านต่อ: 8051 / MCS-51 คืออะไร

สรุป

Polling เหมาะกับระบบง่าย แต่ Timer และ Interrupt ช่วยให้ MCU จัดการ event และ timing ได้แม่นขึ้น

สิ่งสำคัญไม่ใช่ “ใช้ interrupt ให้มากที่สุด” แต่คือแยกว่า event ไหนต้องตอบสนองทันที งานไหนทำภายหลังได้ และอย่าให้ ISR/Timer logic กลายเป็นจุดที่ระบบค้างหรือไม่สามารถ recover ได้