This is a very simple way to count DigitalIn rising by using Ticker Interrupts.

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
MBE13170
Date:
Thu Dec 23 07:48:27 2010 +0000
Commit message:

Changed in this revision

Pswitch.cpp Show annotated file Show diff for this revision Revisions of this file
Pswitch.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Pswitch.cpp	Thu Dec 23 07:48:27 2010 +0000
@@ -0,0 +1,52 @@
+
+
+#include "Pswitch.h"
+ #include "mbed.h"
+ 
+ /*
+  * Constructor
+  */
+ Pswitch::Pswitch(PinName in) 
+     : _in(in) {    
+         
+     // reset all the flags and counters    
+     _samples = 0;
+     _output = 0;
+     _output_last = 0;
+     _rising_flag = 0;
+     
+     // Attach ticker
+     _ticker.attach(this, &Pswitch::_sample, 0.005);     
+ }
+   
+ void Pswitch::_sample() {
+ 
+
+	_output_last = _output;
+	_output = _in;
+	if (!_output && _output_last)
+	{
+		_rising_flag++;
+	}
+
+}
+
+ 
+ 
+ 
+ // return number of rising edges
+ int Pswitch::count(void) {
+     int return_value = _rising_flag; 
+     _rising_flag = 0;
+     return(return_value);
+ }
+ 
+ // return the debounced status
+ int Pswitch::read(void) {
+     return(_in);
+ }
+ 
+ // shorthand for read()
+ Pswitch::operator int() {
+     return read();
+ }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Pswitch.h	Thu Dec 23 07:48:27 2010 +0000
@@ -0,0 +1,26 @@
+#include "mbed.h"
+
+     class Pswitch {
+         public:      
+              Pswitch(PinName in);
+ 
+              int read (void);
+              int count (void);
+              operator int();
+               
+         private :    
+                // objects
+                DigitalIn _in;    
+                Ticker _ticker;
+ 
+                // function to take a sample, and update flags
+                void _sample(void);
+ 
+                // counters and flags
+                int _samples;
+                int _output;
+                int _output_last;
+                int _rising_flag;
+ 
+     };
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Dec 23 07:48:27 2010 +0000
@@ -0,0 +1,15 @@
+#include "mbed.h"
+#include "Pswitch.h"
+
+DigitalOut led(LED1);
+Pswitch button(p21);
+
+int main() {
+
+    while(1) {
+        if (button.count() > 0){
+           led = !led;
+        }
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Thu Dec 23 07:48:27 2010 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/20a79241b4a0