Jovica D.
/
FeuerwehrStoppuhr0805
stoppuhr s
Fork of FeuerwehrStoppuhr by
main.cpp@0:48f4880c730b, 2013-04-19 (annotated)
- Committer:
- fox46
- Date:
- Fri Apr 19 18:36:29 2013 +0000
- Revision:
- 0:48f4880c730b
- Child:
- 1:3ed42298abc3
Timer mit Interrupt Tasteneingabe
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
fox46 | 0:48f4880c730b | 1 | #include "mbed.h" |
fox46 | 0:48f4880c730b | 2 | #include "PinDetect.h" |
fox46 | 0:48f4880c730b | 3 | #include "TextLCD.h" |
fox46 | 0:48f4880c730b | 4 | // must import Cookbook PinDetct library into project |
fox46 | 0:48f4880c730b | 5 | // URL: http://mbed.org/users/AjK/libraries/PinDetect/lkyxpw |
fox46 | 0:48f4880c730b | 6 | |
fox46 | 0:48f4880c730b | 7 | DigitalOut myled(LED1); |
fox46 | 0:48f4880c730b | 8 | DigitalOut myled2(LED2); |
fox46 | 0:48f4880c730b | 9 | DigitalOut myled3(LED3); |
fox46 | 0:48f4880c730b | 10 | DigitalOut myled4(LED4); |
fox46 | 0:48f4880c730b | 11 | Timer t; |
fox46 | 0:48f4880c730b | 12 | TextLCD lcd(p36, p34, p9, p10, p15, p16); // rs, e, d0-d3 |
fox46 | 0:48f4880c730b | 13 | |
fox46 | 0:48f4880c730b | 14 | PinDetect pb1(p18); |
fox46 | 0:48f4880c730b | 15 | PinDetect pb2(p19); |
fox46 | 0:48f4880c730b | 16 | // SPST Pushbutton debounced count demo using interrupts and callback |
fox46 | 0:48f4880c730b | 17 | // no external PullUp resistor needed |
fox46 | 0:48f4880c730b | 18 | // Pushbutton from P8 to GND. |
fox46 | 0:48f4880c730b | 19 | // Second Pushbutton from P7 to GND. |
fox46 | 0:48f4880c730b | 20 | // A pb hit generates an interrupt and activates the callback function |
fox46 | 0:48f4880c730b | 21 | // after the switch is debounced |
fox46 | 0:48f4880c730b | 22 | |
fox46 | 0:48f4880c730b | 23 | // Global count variable |
fox46 | 0:48f4880c730b | 24 | int volatile count=0; |
fox46 | 0:48f4880c730b | 25 | int volatile resetcnt=0; |
fox46 | 0:48f4880c730b | 26 | |
fox46 | 0:48f4880c730b | 27 | // Callback routine is interrupt activated by a debounced pb1 hit |
fox46 | 0:48f4880c730b | 28 | void pb1_hit_callback (void) { |
fox46 | 0:48f4880c730b | 29 | count++; |
fox46 | 0:48f4880c730b | 30 | myled4 = count & 0x01; |
fox46 | 0:48f4880c730b | 31 | myled3 = (count & 0x02)>>1; |
fox46 | 0:48f4880c730b | 32 | myled2 = (count & 0x04)>>2; |
fox46 | 0:48f4880c730b | 33 | t.start(); |
fox46 | 0:48f4880c730b | 34 | if (resetcnt==1) { |
fox46 | 0:48f4880c730b | 35 | t.reset(); |
fox46 | 0:48f4880c730b | 36 | resetcnt=0; |
fox46 | 0:48f4880c730b | 37 | } |
fox46 | 0:48f4880c730b | 38 | } |
fox46 | 0:48f4880c730b | 39 | // Callback routine is interrupt activated by a debounced pb2 hit |
fox46 | 0:48f4880c730b | 40 | void pb2_hit_callback (void) { |
fox46 | 0:48f4880c730b | 41 | count--; |
fox46 | 0:48f4880c730b | 42 | myled4 = count & 0x01; |
fox46 | 0:48f4880c730b | 43 | myled3 = (count & 0x02)>>1; |
fox46 | 0:48f4880c730b | 44 | myled2 = (count & 0x04)>>2; |
fox46 | 0:48f4880c730b | 45 | t.stop(); |
fox46 | 0:48f4880c730b | 46 | resetcnt=resetcnt++; |
fox46 | 0:48f4880c730b | 47 | } |
fox46 | 0:48f4880c730b | 48 | int main() { |
fox46 | 0:48f4880c730b | 49 | |
fox46 | 0:48f4880c730b | 50 | // Use internal pullups for pushbutton |
fox46 | 0:48f4880c730b | 51 | //pb1.mode(PullUp); |
fox46 | 0:48f4880c730b | 52 | //pb2.mode(PullUp); |
fox46 | 0:48f4880c730b | 53 | // Delay for initial pullup to take effect |
fox46 | 0:48f4880c730b | 54 | //wait(.01); |
fox46 | 0:48f4880c730b | 55 | // Setup Interrupt callback functions for a pb hit |
fox46 | 0:48f4880c730b | 56 | pb1.attach_deasserted(&pb1_hit_callback); |
fox46 | 0:48f4880c730b | 57 | pb2.attach_deasserted(&pb2_hit_callback); |
fox46 | 0:48f4880c730b | 58 | // Start sampling pb inputs using interrupts |
fox46 | 0:48f4880c730b | 59 | pb1.setSampleFrequency(); |
fox46 | 0:48f4880c730b | 60 | pb2.setSampleFrequency(); |
fox46 | 0:48f4880c730b | 61 | //Blink myled in main routine forever while responding to pb changes |
fox46 | 0:48f4880c730b | 62 | // via interrupts that activate the callback counter function |
fox46 | 0:48f4880c730b | 63 | while (1) { |
fox46 | 0:48f4880c730b | 64 | myled = !myled; |
fox46 | 0:48f4880c730b | 65 | |
fox46 | 0:48f4880c730b | 66 | lcd.printf("Timer\n"); |
fox46 | 0:48f4880c730b | 67 | lcd.printf(" %4.2f sec\n", t.read()); |
fox46 | 0:48f4880c730b | 68 | |
fox46 | 0:48f4880c730b | 69 | |
fox46 | 0:48f4880c730b | 70 | wait(.01); |
fox46 | 0:48f4880c730b | 71 | lcd.cls(); |
fox46 | 0:48f4880c730b | 72 | resetcnt=1; |
fox46 | 0:48f4880c730b | 73 | |
fox46 | 0:48f4880c730b | 74 | } |
fox46 | 0:48f4880c730b | 75 | |
fox46 | 0:48f4880c730b | 76 | } |