ဒီ Project မှာ Button Debounce အကြောင်းကို လေ့လာမှာ ဖြစ်ပြီး Firmware Development မှာ အရေးကြီး တဲ့ Interrupt အကြောင်းကိုလဲ ပြောပြမှာပါ။
Circuit Diagram
Pin 12 နှင့် Pin 13 ကို LED အတွက် သုံးထားပါတယ်။
Pin 2 (Interrupt 0 Pin) ကို တော့ Button အတွက်သုံးထားပါတယ်။
Debouncing ဆိုတာ ဘာလဲ?
အင်္ဂလိပ်လိုတော့ အောက်ပါအတိုင်း ရှင်းထားပါတယ်။
Bouncing is the tendency of any two metal contacts in an electronic device to generate multiple signals as the contacts close or open; debouncing is any kind of hardware device or software that ensures that only a single signal will be acted upon for a single opening or closing of a contact.
When you press a key on your computer keyboard , you expect a single contact to be recorded by your computer. In fact, however, there is an initial contact, a slight bounce or lightening up of the contact, then another contact as the bounce ends, yet another bounce back, and so forth. A similar effect takes place when a switch made using a metal contact is opened. The usual solution is a debouncing device or software that ensures that only one digital signal can be registered within the space of a given time (usually milliseconds).
အဓိပ္ဗါယ်ပြောရရင်တော့ ခလုပ်နှိပ်တဲ့ အခါ ခလုပ်က Signal တစ်ခုထဲ ထွက်ရမယ့် အစား Noise ပါတဲ့ Signal တွေထွက်လာတယ်။ အဲဒိအခါ Microprocessor က Signal High/Low ကို သေသေချာချာမသိတော့ပါဘူး။ ဒီ Project အတွက်တော့ Button နှိပ်တာက Signal High ပါ။ Button နှိပ်ရာကနေ လွှတ်လိုက်တဲ့ အခါ ချက်ချင်း LOW ဖြစ်ရမယ့် အစား အောက်ပါပုံ အတိုုင်း Debounce Signal ထွက်လာရင် Microprocessor က Button ကို နှိပ်လိုက် လွှတ်လိုက်လုပ်နေတယ်လို့ ထင်မှာပါ။ ဒီပြဿနာက Program ကို တော်တော် ဒုက္ခပေးနိုင်တဲ့ အတွက် အစောကြီးထဲက ဒီလို ပြဿနာမျိုးကို ရှင်းထားရင် အကောင်းဆုံးပါပဲ။
Interrupt ဆိုတာဘာလဲ?
Interrupt ဆိုသည်မှာ Hardware (သို့) Software တစ်ခုခုမှ အရေးတကြီး Signal တစ်ခုပိုလွှတ်တဲ့အခါ ပုံမှန်လုပ်နေကြအလုပ်ကို ခဏရပ်စေပြီး အခြား ပိုအရေးကြီးသော အလုပ်တစ်ခုကို လုပ်ဆောင်စေခြင်းဖြစ်သည်။
အလုပ်လုပ်ပုံ
ဒီ Project အတွက် attachInterrupt(interrupt, ISR, mode) အကြောင်းကို တော့ သိထားဖို့လိုအပ်ပါတယ်။ attachInterrupt Function မှာ
interrupt: သုံးလိုသော Interrupt
ISR: လုပ်စေချင်တဲ့ Function
Mode: Mode မှာ ၄ မျိုးရှိတယ်။
- LOW,
- CHANGE,
- RISING,
- FALLING
LOW Mode: Pin Signal LOW ဖြစ်သွားရင် Interrupt Trigger ပေးပါမယ်။
CHANGE Mode: Pin Signal LOW to HIGH ဖြစ်ဖြစ်၊ HIGH to LOW ဖြစ်ဖြစ် Interrupt Trigger ပေးပါမယ်။
RISING Mode: Pin Signal LOW to HIGH ဖြစ်ရင် Interrupt Trigger ပေးပါမယ်။
FALLING Mode: Pin Signal HIGH to LOW ဖြစ်ရင် Interrupt Trigger ပေးပါမယ်။
ဒီ Project မှာ Interrupt “0” ကိုသုံးထားပါတယ်။
ခုလုပ်နှိပ်လိုက်တာနဲ့ Interrupt Trigger စလုပ်ပါတယ်။ ဒါပေမယ့် Pin Signal က Debounce မဖြစ်ပဲ ငြိမ်နေတယ်ဆိုုတာ သေချာအောင် ၁၀ မီလီ စက္ကန့်စောင့်ကြည့်ပါမယ်။ Debounce Signal မတွေ့ရဘူးဆိုမှ လုပ်ချင်တဲ့ အလုပ်ကို ဆက်လုပ်ပါ့မယ်။ Coding အကြောင်းကို ဆက်လေ့လာလိုက်ကြရအောင်။
Coding
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
const int buttonInterrupt = 0; // interrupt ၀ const int buttonLedPin = 13; // Button နှိပ်၊ မနှိပ်ပြတဲ့ LED const int switchLedPin = 12; // PIN Signal High to Low အခြေအနေပြ LED volatile int buttonState = LOW; //Button မနှိပ်ထားရင် LOW ပါ။ volatile boolean switchState = false; volatile long lastDebounceTime = 0; // နောက်ဆုံး Interrupt Trigger ရတဲ့ အချိန် long debounceDelay = 10; // the debounce time; ခလုပ်ကိုု ချက်ချင်းနှိပ်ပြီး ပြန်လွတ်ရင် Debounce Signal ထင်နိုင်ပါတယ် void setup() { pinMode(buttonLedPin, OUTPUT); pinMode(switchLedPin, OUTPUT); digitalWrite(buttonLedPin, LOW);//Pin Signal LOW to HIGH ဖြစ်ရင် Interrupt Trigger ပေးပါမယ်။ attachInterrupt(buttonInterrupt, Rise, RISING); } void loop() { } //RISING Mode Trigger ရပြီးဆိုလျှင် Rise Function ကို အလုပ်လုပ်စေပါတယ်။ void Rise() { long currentTime = millis(); //နောက်ဆုံး Debounce Time နဲ့ လက်ရှိအချိန်က debouncedelay ထက်ပိုကြာသွားပြီဆိုုလျှင် if ((currentTime - lastDebounceTime) > debounceDelay) { lastDebounceTime = currentTime; if (buttonState == LOW) { buttonState = HIGH; attachInterrupt(buttonInterrupt, Fall, FALLING); } digitalWrite(buttonLedPin, buttonState); Switch(buttonState); } } void Fall() { long currentTime = millis(); if ((currentTime - lastDebounceTime) > debounceDelay) { lastDebounceTime = currentTime; if (buttonState == HIGH) { buttonState = LOW; attachInterrupt(buttonInterrupt, Rise, RISING); } digitalWrite(buttonLedPin, buttonState); ;Switch(buttonState); } } void Switch(int state) { if (state == HIGH) { switchState = !switchState; } if (switchState) { digitalWrite(switchLedPin, HIGH); } else { digitalWrite(switchLedPin, LOW); } } |