Frequency counter with i2c slave.

Dependencies:   mbed

Revision:
0:1913ea9baf30
Child:
1:20b452641613
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Jun 12 05:34:52 2014 +0000
@@ -0,0 +1,58 @@
+// fc1114 - Frequency counter with i2c slave output
+// target: LPC1114FN28
+
+#include "mbed.h"
+
+#define I2C_ADRS 0x70
+
+DigitalOut led(dp28);
+I2CSlave slave(dp5, dp27);
+Ticker tick;
+
+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];
+
+    tick.attach(&isr_tick, 1);
+
+    led = 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;
+        }
+    }
+}
\ No newline at end of file