Thomas Stundner / Mbed 2 deprecated pineingabe
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "PinDetect.h"
00003 #include "TextLCD.h"
00004 // must import Cookbook PinDetct library into project
00005 // URL: http://mbed.org/users/AjK/libraries/PinDetect/lkyxpw
00006  
00007 DigitalOut myled(LED1);
00008 DigitalOut myled2(LED2);
00009 DigitalOut myled3(LED3);
00010 DigitalOut myled4(LED4);
00011 Timer t;
00012 TextLCD lcd(p36, p34, p9, p10, p15, p16); // rs, e, d0-d3
00013  
00014 PinDetect pb1(p18);
00015 PinDetect pb2(p19);
00016 // SPST Pushbutton debounced count demo using interrupts and callback
00017 // no external PullUp resistor needed
00018 // Pushbutton from P8 to GND.
00019 // Second Pushbutton from P7 to GND.
00020 // A pb hit generates an interrupt and activates the callback function
00021 // after the switch is debounced
00022  
00023 // Global count variable
00024 int volatile count=0;
00025 int volatile resetcnt=0;
00026  
00027 // Callback routine is interrupt activated by a debounced pb1 hit
00028 void pb1_hit_callback (void) {
00029     count++;
00030     myled4 = count & 0x01;
00031     myled3 = (count & 0x02)>>1;
00032     myled2 = (count & 0x04)>>2;
00033     t.start();
00034     if (resetcnt==1) {
00035         t.reset();
00036         resetcnt=0;
00037        } 
00038 }
00039 // Callback routine is interrupt activated by a debounced pb2 hit
00040 void pb2_hit_callback (void) {
00041     count--;
00042     myled4 = count & 0x01;
00043     myled3 = (count & 0x02)>>1;
00044     myled2 = (count & 0x04)>>2;
00045     t.stop();
00046     resetcnt=resetcnt++;
00047 }
00048 int main() {
00049  
00050     // Use internal pullups for pushbutton
00051     //pb1.mode(PullUp);    
00052     //pb2.mode(PullUp);
00053     // Delay for initial pullup to take effect
00054     //wait(.01);
00055     // Setup Interrupt callback functions for a pb hit
00056     pb1.attach_deasserted(&pb1_hit_callback);
00057     pb2.attach_deasserted(&pb2_hit_callback);
00058     // Start sampling pb inputs using interrupts
00059     pb1.setSampleFrequency();
00060     pb2.setSampleFrequency();
00061     //Blink myled in main routine forever while responding to pb changes
00062     // via interrupts that activate the callback counter function
00063     while (1) {
00064         myled = !myled;
00065         
00066                 lcd.printf("Timer\n");
00067                 lcd.printf(" %4.2f sec\n", t.read());
00068                 
00069                 
00070         wait(.01);
00071         lcd.cls();
00072         resetcnt=1;
00073         
00074     }
00075  
00076 }