Pushbutton demo using interrupts with switch contact bounce

Dependencies:   mbed

Committer:
4180_1
Date:
Thu Oct 06 00:18:44 2011 +0000
Revision:
0:7db73d1dc65f
Child:
1:6582c095e8d1

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
4180_1 0:7db73d1dc65f 1 #include "mbed.h"
4180_1 0:7db73d1dc65f 2
4180_1 0:7db73d1dc65f 3 DigitalOut myled(LED1);
4180_1 0:7db73d1dc65f 4 DigitalOut myled2(LED2);
4180_1 0:7db73d1dc65f 5 DigitalOut myled3(LED3);
4180_1 0:7db73d1dc65f 6 DigitalOut myled4(LED4);
4180_1 0:7db73d1dc65f 7
4180_1 0:7db73d1dc65f 8 InterruptIn pb(p8);
4180_1 0:7db73d1dc65f 9 // SPST Pushbutton count demo using interrupts
4180_1 0:7db73d1dc65f 10 // no external PullUp resistor needed
4180_1 0:7db73d1dc65f 11 // Pushbutton from P8 to GND.
4180_1 0:7db73d1dc65f 12 // A pb hit generates an interrupt and activates the interrupt routine
4180_1 0:7db73d1dc65f 13 // after the switch is debounced
4180_1 0:7db73d1dc65f 14
4180_1 0:7db73d1dc65f 15 // Global count variable
4180_1 0:7db73d1dc65f 16 int volatile count=0;
4180_1 0:7db73d1dc65f 17
4180_1 0:7db73d1dc65f 18 // pb Interrupt routine - is interrupt activated by a falling edge of pb input
4180_1 0:7db73d1dc65f 19 void pb_hit_interrupt (void) {
4180_1 0:7db73d1dc65f 20 count++;
4180_1 0:7db73d1dc65f 21 myled4 = count & 0x01;
4180_1 0:7db73d1dc65f 22 myled3 = (count & 0x02)>>1;
4180_1 0:7db73d1dc65f 23 myled2 = (count & 0x04)>>2;
4180_1 0:7db73d1dc65f 24 }
4180_1 0:7db73d1dc65f 25
4180_1 0:7db73d1dc65f 26 int main() {
4180_1 0:7db73d1dc65f 27 // Use internal pullup for pushbutton
4180_1 0:7db73d1dc65f 28 pb.mode(PullUp);
4180_1 0:7db73d1dc65f 29 // Delay for initial pullup to take effect
4180_1 0:7db73d1dc65f 30 wait(.01);
4180_1 0:7db73d1dc65f 31 // Attach the address of the interrupt handler routine for pushbutton
4180_1 0:7db73d1dc65f 32 pb.fall(&pb_hit_interrupt);
4180_1 0:7db73d1dc65f 33 // Blink myled in main routine forever while responding to pb changes
4180_1 0:7db73d1dc65f 34 // via interrupts that activate pb_hit_interrupt routine
4180_1 0:7db73d1dc65f 35 while (1) {
4180_1 0:7db73d1dc65f 36 myled = !myled;
4180_1 0:7db73d1dc65f 37 wait(.5);
4180_1 0:7db73d1dc65f 38 }
4180_1 0:7db73d1dc65f 39 }