0Pricing
Arduino & IoT Academy · บทเรียน

เขียน ISR ที่ปลอดภัย

เขียนตัวจัดการอินเทอร์รัปต์ให้สั้นและใช้ volatile

เขียน ISR ที่ปลอดภัย เป็นบทเรียน Arduino & IoT Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Arduino & IoT Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Arduino & IoT Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

A Special Function

An ISR, or interrupt service routine, is the function that runs when an interrupt fires. It has rules that ordinary functions do not.

Keep It Short

The golden rule: make your ISR as short as possible. The main program is paused while it runs, so finish fast and get out.

Do the Heavy Work Later

Inside the ISR, just set a flag or bump a counter. Let the loop do the slow work like printing or math once it sees the flag.

void myISR() {
  pressed = true;
}

Shared Variables

An ISR and your loop both touch the same variable. The compiler may not expect it to change behind the loop's back, which causes subtle bugs. 🐞

The volatile Keyword

Mark any variable shared with an ISR as volatile. It tells the compiler the value can change at any moment, so always reread it.

volatile bool pressed = false;

No delay Inside

Never call delay in an ISR. The timing that delay needs is itself paused during an interrupt, so it simply will not work.

Serial Is Risky Too

Avoid Serial.print inside an ISR. It relies on interrupts that are blocked while your handler runs, so output can stall or garble.

millis Freezes

Inside an ISR, millis stops advancing because its background counting is paused. Read elapsed time in the loop, not the handler.

Handle the Flag in Loop

Back in the loop, check the flag, act on it, then clear it. This is where the real response safely takes place.

if (pressed) {
  pressed = false;
  handlePress();
}

Reading Multi-Byte Values

When the loop reads a multi-byte volatile value an ISR updates, briefly disable interrupts so you get a clean, unsplit number.

noInterrupts();
long c = count;
interrupts();

Why These Rules Matter

Follow these rules and your ISR stays fast and predictable. Break them and you get freezes, lost data, or values that never seem to update.

Quick Check

Why must a variable shared between an ISR and the loop be marked volatile?

Recap

You learned to keep an ISR short, mark shared data volatile, skip delay and Serial, and do the real work back in the loop. 🎉

คำถามที่พบบ่อย

บทเรียน “เขียน ISR ที่ปลอดภัย” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “เขียน ISR ที่ปลอดภัย” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Arduino & IoT Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Arduino & IoT Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “เขียน ISR ที่ปลอดภัย”

เขียนตัวจัดการอินเทอร์รัปต์ให้สั้นและใช้ volatile คุณปฏิบัติ Arduino & IoT Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Arduino & IoT Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Arduino & IoT Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “เขียน ISR ที่ปลอดภัย” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Arduino & IoT Academy นี้ได้ไหม

ได้ บทเรียน Arduino & IoT Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การตรวจสอบสถานะเทียบกับอินเทอร์รัปต์
  2. ใช้ attachInterrupt กับขา
  3. เขียน ISR ที่ปลอดภัย
  4. นับพัลส์จากเอ็นโคเดอร์
← กลับไปที่ Arduino & IoT Academy