Freq calculations using counters and interrupt

Dependencies:   FastPWM PwmIn USBDevice mbed

Files at this revision

API Documentation at this revision

Comitter:
Ellor1
Date:
Thu Nov 13 13:26:08 2014 +0000
Commit message:
Freq calculation of signal using counters and interrupt routines

Changed in this revision

FastPWM.lib Show annotated file Show diff for this revision Revisions of this file
PwmIn.lib Show annotated file Show diff for this revision Revisions of this file
USBDevice.lib 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/FastPWM.lib	Thu Nov 13 13:26:08 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/Sissors/code/FastPWM/#ba7a5bf634b3
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PwmIn.lib	Thu Nov 13 13:26:08 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/simon/code/PwmIn/#6d68eb9b6bbb
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBDevice.lib	Thu Nov 13 13:26:08 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/USBDevice/#4d3e7f3d5211
--- /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());
+                
+
+
+
+
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Thu Nov 13 13:26:08 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/031413cf7a89
\ No newline at end of file