Et eksempel på Debounce av en knapp ved hjelp av interrupts
Dependencies: mbed
main.cpp@0:95812c6efbce, 2015-11-05 (annotated)
- Committer:
- benrammok
- Date:
- Thu Nov 05 13:14:36 2015 +0000
- Revision:
- 0:95812c6efbce
Debounce Program
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
benrammok | 0:95812c6efbce | 1 | #include "mbed.h" |
benrammok | 0:95812c6efbce | 2 | |
benrammok | 0:95812c6efbce | 3 | //Definerer inn/utganger |
benrammok | 0:95812c6efbce | 4 | DigitalOut led(LED1); |
benrammok | 0:95812c6efbce | 5 | InterruptIn button(USER_BUTTON); |
benrammok | 0:95812c6efbce | 6 | Timeout tOut; |
benrammok | 0:95812c6efbce | 7 | |
benrammok | 0:95812c6efbce | 8 | |
benrammok | 0:95812c6efbce | 9 | //Funksjonsprototyper |
benrammok | 0:95812c6efbce | 10 | void btnClick(void); |
benrammok | 0:95812c6efbce | 11 | void enableIRQ(void); |
benrammok | 0:95812c6efbce | 12 | void toggleLed(void); |
benrammok | 0:95812c6efbce | 13 | |
benrammok | 0:95812c6efbce | 14 | |
benrammok | 0:95812c6efbce | 15 | int main() { |
benrammok | 0:95812c6efbce | 16 | //Setter pull-up og fester en funksjon til fall på button. |
benrammok | 0:95812c6efbce | 17 | button.mode(PullUp); |
benrammok | 0:95812c6efbce | 18 | button.fall(&btnClick); |
benrammok | 0:95812c6efbce | 19 | |
benrammok | 0:95812c6efbce | 20 | |
benrammok | 0:95812c6efbce | 21 | |
benrammok | 0:95812c6efbce | 22 | while(1) { |
benrammok | 0:95812c6efbce | 23 | |
benrammok | 0:95812c6efbce | 24 | wait(0.2); // 200 ms |
benrammok | 0:95812c6efbce | 25 | } |
benrammok | 0:95812c6efbce | 26 | } |
benrammok | 0:95812c6efbce | 27 | |
benrammok | 0:95812c6efbce | 28 | //Toggler Led fra av/på -> på/av |
benrammok | 0:95812c6efbce | 29 | void toggleLed(){ |
benrammok | 0:95812c6efbce | 30 | led = !led; //Toggler LED |
benrammok | 0:95812c6efbce | 31 | } |
benrammok | 0:95812c6efbce | 32 | |
benrammok | 0:95812c6efbce | 33 | |
benrammok | 0:95812c6efbce | 34 | //Skrur på interrupts |
benrammok | 0:95812c6efbce | 35 | void enableIRQ(){ |
benrammok | 0:95812c6efbce | 36 | button.enable_irq(); |
benrammok | 0:95812c6efbce | 37 | } |
benrammok | 0:95812c6efbce | 38 | |
benrammok | 0:95812c6efbce | 39 | |
benrammok | 0:95812c6efbce | 40 | //Kjører når knappen blir trykket |
benrammok | 0:95812c6efbce | 41 | void btnClick(){ |
benrammok | 0:95812c6efbce | 42 | button.disable_irq(); |
benrammok | 0:95812c6efbce | 43 | tOut.attach_us(enableIRQ, 10); |
benrammok | 0:95812c6efbce | 44 | toggleLed(); |
benrammok | 0:95812c6efbce | 45 | } |