Frequency Counter Library. Only for mbed LPC1768, mbed LPC1114FN28, Nucleo-F401 and Nucleo-F411. No way to change pin assign.

Dependents:   Frequency_Counter Frequency_wind_speed_measure Frequency_counter_wind_speed

Please refer following page.
http://developer.mbed.org/users/kenjiArai/notebook/simple-frequency-counter/

Revision:
3:61bea8bfe404
Parent:
2:54c05b0a117a
Child:
4:9a726b997366
--- a/freq_counter.cpp	Tue Oct 21 11:19:54 2014 +0000
+++ b/freq_counter.cpp	Wed Oct 22 00:35:23 2014 +0000
@@ -7,7 +7,7 @@
  *  http://mbed.org/users/kenjiArai/
  *      Additional functions and modification
  *      started: October   18th, 2014
- *      Revised: October   21st, 2014
+ *      Revised: October   22nd, 2014
  *
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
@@ -16,13 +16,21 @@
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  */
 //-------------------------------------------------------------------------------------------------
-//  Reference program
+//  Reference program No.1 (see line 104)
 //-------------------------------------------------------------------------------------------------
 //  5MHzOSC
 //      http://developer.mbed.org/users/mio/code/5MHzOSC/
 //  by fuyono sakura
 //      http://developer.mbed.org/users/mio/
 
+//-------------------------------------------------------------------------------------------------
+//  Reference program No.2 (see line 173)
+//-------------------------------------------------------------------------------------------------
+//  fc114
+//      http://developer.mbed.org/users/rutles/code/fc1114/
+//  by Tetsuya Suzuki
+//      http://developer.mbed.org/users/rutles/
+
 #include "mbed.h"
 #include "freq_counter.h"
 
@@ -40,6 +48,15 @@
     LPC_TIM2->CTCR = 1;             // 4)Count on riging edge Cap3[0]
     LPC_TIM2->CCR  = 0;             // 5)Input Capture Disabled
     LPC_TIM2->TCR  = 1;             // 6)Counter Start (bit1<=0,bit0<=1)
+#elif defined(TARGET_LPC1114)
+    LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 9);  //TMR32B0 wakeup
+    LPC_IOCON->PIO1_5 |= (1 << 1);  // Set dp14 (pin20) as CT32B0_CAP0
+    LPC_IOCON->PIO1_5 |= (1 << 5);  // Hysteresis enable
+    LPC_TMR32B0->TCR = 2;           // reset
+    LPC_TMR32B0->CTCR  = 1;         // counter mode
+    LPC_TMR32B0->CCR  = 0;          // Input Capture Disable)
+    LPC_TMR32B0->PR  = 0;           // no prescale
+    LPC_TMR32B0->TCR = 1;           // start
 #elif defined(TARGET_NUCLEO_F401RE) || defined(TARGET_NUCLEO_F411RE)
     // PA0 -> Counter frequency input pin as Timer2 TI1
     GPIOA->AFR[0] &= 0xfffffff0;
@@ -66,17 +83,21 @@
 #if defined(TARGET_LPC1768)
     LPC_TIM2->TCR = 2;             // Reset the counter (bit1<=1,bit0<=0)
     LPC_TIM2->TCR = 1;             // UnReset counter (bit1<=0,bit0<=1)
+    wait(gate_time);               // Gate time for count
+    freq = LPC_TIM2->TC;           // read counter
+#elif defined(TARGET_LPC1114)
+    LPC_TMR32B0->TCR = 2;           // reset
+    LPC_TMR32B0->TCR = 1;           // start
     wait(gate_time);                // Gate time for count
-    freq = LPC_TIM2->TC;
-    return freq;
+    freq = LPC_TMR32B0->TC;         // read counter
 #elif defined(TARGET_NUCLEO_F401RE) || defined(TARGET_NUCLEO_F411RE)
     TIM2->CNT = 0;
     wait(gate_time);                // Gate time for count
-    freq = TIM2->CNT;
-    return freq;
+    freq = TIM2->CNT;               // read counter
 #else
 #error "No support for this CPU"
 #endif
+    return freq;
 }
 
 #if 0
@@ -146,3 +167,67 @@
 }
 
 #endif
+
+#if 0
+
+// fc1114 - Frequency counter with i2c slave.
+// target: LPC1114FN28
+
+#include "mbed.h"
+
+#define I2C_ADRS 0x70
+
+DigitalOut led(dp28);
+I2CSlave slave(dp5, dp27);
+Ticker tick;
+
+volatile uint32_t frq;
+
+void isr_tick()
+{
+    frq = LPC_TMR32B0->TC;
+    LPC_TMR32B0->TC = 0;
+
+    led = !led;
+}
+
+int main()
+{
+    union {
+        char b[4];
+        uint32_t w;
+    } buf;
+    char dummy[4];
+
+    led = 1;
+    tick.attach(&isr_tick, 1);
+
+    LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 9);//TMR32B0 wakeup
+    LPC_IOCON->PIO1_5 |= (1 << 1);// Set PIN14 as CT32B0_CAP0
+    LPC_IOCON->PIO1_5 |= (1 << 5);// Hysteresis enable
+    LPC_TMR32B0->TCR = 2; // reset
+    LPC_TMR32B0->CTCR  = 1; // counter mode
+    LPC_TMR32B0->CCR  = 0; // Input Capture Disable)
+    LPC_TMR32B0->PR  = 0;// no prescale
+    LPC_TMR32B0->TCR = 1; // start
+
+    slave.address(I2C_ADRS << 1);
+
+    while (1) {
+        int i = slave.receive();
+        switch (i) {
+            case I2CSlave::ReadAddressed:
+                buf.w = frq;
+                slave.write(buf.b, 4);
+                break;
+            case I2CSlave::WriteGeneral:
+                slave.read(dummy, 4);
+                break;
+            case I2CSlave::WriteAddressed:
+                slave.read(dummy, 4);
+                break;
+        }
+    }
+}
+
+#endif