I used the code to read pulses from the Ambient weather air flow meter.

Dependencies:   mbed

Committer:
Neel
Date:
Sun Feb 03 21:06:02 2013 +0000
Revision:
0:c5bcf643a8b1
Can read pulses on the pin and convert them into frequency count

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Neel 0:c5bcf643a8b1 1 #include "mbed.h"
Neel 0:c5bcf643a8b1 2
Neel 0:c5bcf643a8b1 3 Serial pc(USBTX, USBRX);
Neel 0:c5bcf643a8b1 4 InterruptIn in(p8);
Neel 0:c5bcf643a8b1 5 DigitalOut led(LED1);
Neel 0:c5bcf643a8b1 6 DigitalOut led2(LED2);
Neel 0:c5bcf643a8b1 7 Timer t1;
Neel 0:c5bcf643a8b1 8
Neel 0:c5bcf643a8b1 9 float t_period = 0; // This is the period between interrupts in microseconds
Neel 0:c5bcf643a8b1 10 float t_freq = 0;
Neel 0:c5bcf643a8b1 11
Neel 0:c5bcf643a8b1 12 void flip(void)
Neel 0:c5bcf643a8b1 13 {
Neel 0:c5bcf643a8b1 14 led=!led;
Neel 0:c5bcf643a8b1 15 t_period = t1.read_us(); // Get time since last interrupt
Neel 0:c5bcf643a8b1 16 t_freq = (1/t_period)*1000000; // Convert period (in us) to frequency (Hz)
Neel 0:c5bcf643a8b1 17 t1.reset(); // Reset timer and wait for next interrupt
Neel 0:c5bcf643a8b1 18 }
Neel 0:c5bcf643a8b1 19
Neel 0:c5bcf643a8b1 20 int main() {
Neel 0:c5bcf643a8b1 21 pc.printf("\rStarting frequency counter\n");
Neel 0:c5bcf643a8b1 22 in.mode(PullDown); // Set the pin to Pull Down mode.
Neel 0:c5bcf643a8b1 23 in.rise(&flip); // Set up the interrupt for rising edge
Neel 0:c5bcf643a8b1 24 t1.start(); // start the timer
Neel 0:c5bcf643a8b1 25
Neel 0:c5bcf643a8b1 26 while (1) {
Neel 0:c5bcf643a8b1 27 wait_ms(100);
Neel 0:c5bcf643a8b1 28 led2=!led2;
Neel 0:c5bcf643a8b1 29 pc.printf("\rfrq is %d Hz\n", (int)t_freq);
Neel 0:c5bcf643a8b1 30 }
Neel 0:c5bcf643a8b1 31
Neel 0:c5bcf643a8b1 32 }