...

Dependencies:   mbed EthernetInterface mbed-rtos

Fork of FOTA_K64F by Erik -

Revision:
0:9396d3376435
Child:
1:782a3ddc329e
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri Aug 22 15:14:52 2014 +0000
@@ -0,0 +1,58 @@
+#include "mbed.h"
+#include "FastPWM.h"
+#include "FastIO.h"
+
+#define TIME_REG    LPC_TIM2->TC
+FastPWM output(p21);
+FastIn<p22> input;
+void initTimer2();
+
+ 
+int main()
+{
+    //Ticks to be measured
+    unsigned int tick_width = 100;
+    
+    unsigned int measured_timed[3];
+    
+    initTimer2(); 
+ 
+    while (true) {
+        //Setup PWM
+        output.period_ticks(tick_width);
+        output.pulsewidth_ticks(tick_width/2);
+        wait_us(10);
+        
+        //Start measuring at first rising edge
+        while(!input);
+        while(input);
+        measured_timed[0] = TIME_REG;
+        while(!input);
+        measured_timed[1] = TIME_REG;
+        while(input);
+        measured_timed[2] = TIME_REG;
+        
+        printf("\r\nPulsewidth = %d ticks, which is %2.1fMHz\r\n", tick_width, (float)SystemCoreClock / (float)tick_width / 1000000.0f);
+        printf("Measured period = %d ticks, pulsewidth = %d ticks\r\n", measured_timed[2]-measured_timed[0], measured_timed[1]-measured_timed[0]);
+        
+        //If more than 20% too large, consider it a fail
+        if ((measured_timed[2] - measured_timed[0]) > 1.2 * tick_width) {
+            printf("Period setting failed\r\n");
+            while(1);
+        }
+        
+        //Setup next tick
+        tick_width--;
+ 
+    }
+}
+
+//Semi copy-pasted from: https://mbed.org/users/garyr/code/Counter/file/e619b6823668/Timer2.cpp
+//We init timer at full clock speed, don't do anything else
+void initTimer2() {
+    LPC_SC->PCONP |= (1<<22);           // Power on the Timer2
+    LPC_SC->PCLKSEL1 &= ~(3<<12);
+    LPC_SC->PCLKSEL1 |= (1<<12);        // Select  CCLK for Timer2
+    LPC_TIM2->TCR = 2;
+    LPC_TIM2->TCR = 1;                  // Enable Timer2
+}
\ No newline at end of file