0Pricing
Arduino & IoT Academy · درس

كتابة ISR آمن

أبقِ معالجات المقاطعة قصيرة واستخدم volatile.

كتابة ISR آمن درس مجاني في Arduino & IoT Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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 آمن» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Arduino & IoT Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Arduino & IoT Academy 4 دروس في المجموع.

ماذا ستتعلم في «كتابة ISR آمن»؟

أبقِ معالجات المقاطعة قصيرة واستخدم volatile. تتمرن على Arduino & IoT Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Arduino & IoT Academy؟

لا تُشترط خبرة سابقة. Arduino & IoT Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.

كم من الوقت يستغرق درس «كتابة ISR آمن»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Arduino & IoT Academy هذا؟

نعم. كل درس في Arduino & IoT Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. Polling مقابل Interrupts
  2. استخدام attachInterrupt على منفذ
  3. كتابة ISR آمن
  4. عدّ النبضات من Encoder
← العودة إلى Arduino & IoT Academy