Pushbutton demo using interrupts with switch contact bounce

Dependencies:   mbed

Committer:
4180_1
Date:
Thu Oct 06 00:55:26 2011 +0000
Revision:
1:6582c095e8d1
Parent:
0:7db73d1dc65f

        

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
4180_1 0:7db73d1dc65f 14 // Global count variable
4180_1 0:7db73d1dc65f 15 int volatile count=0;
4180_1 0:7db73d1dc65f 16
4180_1 0:7db73d1dc65f 17 // pb Interrupt routine - is interrupt activated by a falling edge of pb input
4180_1 0:7db73d1dc65f 18 void pb_hit_interrupt (void) {
4180_1 0:7db73d1dc65f 19 count++;
4180_1 0:7db73d1dc65f 20 myled4 = count & 0x01;
4180_1 0:7db73d1dc65f 21 myled3 = (count & 0x02)>>1;
4180_1 0:7db73d1dc65f 22 myled2 = (count & 0x04)>>2;
4180_1 0:7db73d1dc65f 23 }
4180_1 0:7db73d1dc65f 24
4180_1 0:7db73d1dc65f 25 int main() {
4180_1 0:7db73d1dc65f 26 // Use internal pullup for pushbutton
4180_1 0:7db73d1dc65f 27 pb.mode(PullUp);
4180_1 0:7db73d1dc65f 28 // Delay for initial pullup to take effect
4180_1 0:7db73d1dc65f 29 wait(.01);
4180_1 0:7db73d1dc65f 30 // Attach the address of the interrupt handler routine for pushbutton
4180_1 0:7db73d1dc65f 31 pb.fall(&pb_hit_interrupt);
4180_1 0:7db73d1dc65f 32 // Blink myled in main routine forever while responding to pb changes
4180_1 0:7db73d1dc65f 33 // via interrupts that activate pb_hit_interrupt routine
4180_1 0:7db73d1dc65f 34 while (1) {
4180_1 0:7db73d1dc65f 35 myled = !myled;
4180_1 0:7db73d1dc65f 36 wait(.5);
4180_1 0:7db73d1dc65f 37 }
4180_1 0:7db73d1dc65f 38 }