Freq calculations using counters and interrupt

Dependencies:   FastPWM PwmIn USBDevice mbed

Revision:
0:6d4f91decc4f
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Nov 13 13:26:08 2014 +0000
@@ -0,0 +1,122 @@
+#include "FastPWM.h"
+#include "mbed.h"
+#include "PwmIn.h"
+#include "USBSerial.h"
+
+// using fastpwm signal as a clock signal to be used to trigger the counter whilst the signal I want to measure is high. 
+// to get this to work properly I believe I will need to use nested interrupts along with setting interupt priorities. This is so that
+// when the signal I want to read  (led) causes an interrupt on a rising edge whilst this value is high I want the clock trigger to interrupt a seperate pin 
+// which will continuoulsy count until the (led) signal causes another interrupt by going low.
+
+// clkin is currently set as a DigitalIn but this likely needs to become an interrupt.
+
+USBSerial serial;
+
+FastPWM clkoutput(p20);
+DigitalIn clkin(p8);
+InterruptIn led(p7);
+Ticker display;
+
+int count;
+int count_2;
+int t_rise;
+int t_fall;
+int t_period = 0;
+
+
+void clock_pulse() {
+    
+      clkoutput.period(8e-7); //around 1.2 MHz signal
+    clkoutput.write(0.5f);   // 50% duty cycle
+    }
+    
+    
+void rise() { // rising edge interrupt routine 
+  
+   while (led && clkin) {           // whilst the signal I want to measure is high (led) and the clk signal (clkin) is high increment the counter
+           //  if (clkin) {
+                 count++;
+                 }
+  //  if (led && clkoutput) {
+            
+        //    count++;
+   //     while (led) {
+     //        if (clkin) {
+     //            count++;
+      //           }
+             t_fall = count_2;   
+            // count_2 = 0;
+             }
+  //  }
+    
+  void fall() { // falling edge interrupt routine
+      
+      while (!led && clkin) {  // whilst the signal I want to measure is low and the clk signal is high increment counter 2
+          count_2++;
+          }
+          t_rise = count;
+         // count = 0;
+       
+      }
+  
+// }
+
+/*void period() {
+    t_period = count;
+    count = 0;
+    }  
+*/
+
+void disp() {
+    
+    serial.printf("count is %d \n\r" , t_rise);
+    serial.printf("count_2 is %d \n\r" , t_fall);   
+    }
+
+
+int main()
+{
+    clock_pulse();
+    
+    led.rise(&rise); // set rising edge interrupt and call rise function
+    led.fall(&fall); // set falling edge interrupt and call fall function       
+             //count = 0;
+           //  }
+   
+    display.attach(&disp, 2); // ticker function to display results every 2 seconds
+
+   while (1) {
+         
+      //   while (led) {
+          //   if (clkin) {
+          //       count++;
+            //     }
+            // count = 0;
+             //}
+         
+        // if (led == 1) && (clkoutput == 1) {
+            
+          //  count++;
+         
+           /*
+  
+  while (led) {
+      
+      if (clkin) {
+          
+          count++;
+                   }        
+          
+  */
+         
+         
+            }
+
+        //serial.printf("Pulsewidth is %.2f \n\r" , pa.pulsewidth());
+                
+
+
+
+
+}
+