EdgeCounter on mbed Counts falling edges on p5. Used as test setup for interfacing a hall sensor to our water/gas sensor. Whenever the hall sensor triggers, edgecount is incremented and the current value is transmitted to the serial port. Any character sent to the serial port returns the current count value

Dependencies:   mbed

Committer:
hollie
Date:
Mon Dec 21 15:38:02 2009 +0000
Revision:
0:8179372e7aaa

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hollie 0:8179372e7aaa 1 // EdgeCounter on mbed
hollie 0:8179372e7aaa 2 //
hollie 0:8179372e7aaa 3 // Counts falling edges on p5.
hollie 0:8179372e7aaa 4 // Used as test setup for interfacing a hall sensor to our water/gas sensor.
hollie 0:8179372e7aaa 5 //
hollie 0:8179372e7aaa 6 // Whenever the hall sensor triggers, edgecount is incremented and the
hollie 0:8179372e7aaa 7 // current value is transmitted to the serial port.
hollie 0:8179372e7aaa 8 //
hollie 0:8179372e7aaa 9 // Any character sent to the serial port returns the current count value
hollie 0:8179372e7aaa 10 //
hollie 0:8179372e7aaa 11 // Written by Lieven Hollevoet
hollie 0:8179372e7aaa 12
hollie 0:8179372e7aaa 13 #include "mbed.h"
hollie 0:8179372e7aaa 14
hollie 0:8179372e7aaa 15 // Create objects
hollie 0:8179372e7aaa 16 Serial pc(USBTX, USBRX);
hollie 0:8179372e7aaa 17 DigitalOut status(LED4);
hollie 0:8179372e7aaa 18 DigitalOut count(LED2);
hollie 0:8179372e7aaa 19 InterruptIn sensor(p5);
hollie 0:8179372e7aaa 20
hollie 0:8179372e7aaa 21 int edgecount;
hollie 0:8179372e7aaa 22
hollie 0:8179372e7aaa 23 void ISR_serial(void) {
hollie 0:8179372e7aaa 24
hollie 0:8179372e7aaa 25 char tmp = pc.getc();
hollie 0:8179372e7aaa 26 status = 1;
hollie 0:8179372e7aaa 27
hollie 0:8179372e7aaa 28 pc.printf("Current edge count is %i\r\n", edgecount);
hollie 0:8179372e7aaa 29 status = 0;
hollie 0:8179372e7aaa 30 }
hollie 0:8179372e7aaa 31
hollie 0:8179372e7aaa 32 void falling_edge(void){
hollie 0:8179372e7aaa 33 edgecount++;
hollie 0:8179372e7aaa 34 count = 1;
hollie 0:8179372e7aaa 35 pc.printf("Falling edge, count is %i!\r\n", edgecount);
hollie 0:8179372e7aaa 36 count = 0;
hollie 0:8179372e7aaa 37 }
hollie 0:8179372e7aaa 38
hollie 0:8179372e7aaa 39 int main() {
hollie 0:8179372e7aaa 40
hollie 0:8179372e7aaa 41 count = 0;
hollie 0:8179372e7aaa 42 edgecount = 0;
hollie 0:8179372e7aaa 43
hollie 0:8179372e7aaa 44 pc.printf("Edgecounter started");
hollie 0:8179372e7aaa 45
hollie 0:8179372e7aaa 46 // Attach interrupt handlers
hollie 0:8179372e7aaa 47 pc.attach(&ISR_serial);
hollie 0:8179372e7aaa 48 sensor.fall(&falling_edge);
hollie 0:8179372e7aaa 49
hollie 0:8179372e7aaa 50 }
hollie 0:8179372e7aaa 51