Sensor sampling library

Revision:
0:eea8d19a7f6b
Child:
1:f9f0b92a9d7c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Sensors.h	Mon Feb 23 18:03:29 2015 +0000
@@ -0,0 +1,39 @@
+#ifndef SENSORS_H
+#define SENSORS_H
+ 
+#define CHANNEL_0 0x00
+#define CHANNEL_1 0x01
+#define CHANNEL_2 0x02
+#define CHANNEL_3 0x03
+ 
+#define CPU_CLOCK 48000000
+ 
+/**
+*   Pulses sets how many edges to detect before returning a time, max of 2^16, affectively refers to sample time
+*   Glitch filter recognizes change on input pin after 2^n rising clock edges
+*   Setting glitch filter to zero disables glitch filter
+*   TODO: DETERMINE EXACT FUNCTIONALITY AND USAFULNESS OF FUNCTION
+*/
+extern "C" void setup_counter(int glitch_filter);
+ 
+/**
+*   Measure the period observed on the sensor indicated by "sensor"
+*   TO VERIFY (KL25Z):
+*       PTC1 -> Pulse Counter Input 0 (00)
+*       PTC2 -> Pulse Counter Input 1 (01)
+*       PTC3 -> Pulse Counter Input 2 (10)
+*       PTC4 -> Pulse Counter Input 3 (11)
+*/
+extern "C" int measure_clock_cycles(char sensor, int samples);
+ 
+float measure_frequency(char sensor, int samples)
+{
+    // Divide the number of cpu clock cycles by the number of measured periods of the measured waveform to
+    // get the number of clock cycles per period
+    int clock_cycles = measure_clock_cycles(sensor, samples) / samples;
+    
+    // Divide CPU_CLOCK by the number of clock cycles to get the period
+    return (CPU_CLOCK / (float)clock_cycles);  
+}
+ 
+#endif
\ No newline at end of file