11 years, 1 month ago.

Counting pulses on input

Please can you help. I need to count 15 pulses on an input pin and then give one output puls every 15 input pulses.

Thank you.

3 Answers

11 years, 1 month ago.

Hi, Chris, try this code. It try to count 15 pulse in pin p5 and then do a pulse of 10 us in pin p6.

#include "mbed.h"

DigitalOut myPulse(p6);
InterruptIn pulseInterrupt(p5);

int pulseCount;

void highPulseDetected() {
    pulseCount++;
}

int main() {
    pulseCount=0;  
    pulseInterrupt.rise(&highPulseDetected);
      
    while(1) {        
        if(pulseCount==15) {
            myPulse=1;
            wait_ms(10);
            myPulse=0;
            
            pulseCount=0;
        }                
    }
}

I hope that this code help you, I cant probe the code right now because i dont have my mbed close

Greetings

Accepted Answer
11 years, 1 month ago.

Create an interrupt on the pin you are counting the pulses on, and create an ISR that updates a counter varable. somewhere in your program, check that counter for a value of 15 and when it reaches that value, set an output pin high and reset your counter to 0. This isnt the most efficient way to do this, but it should work.

11 years, 1 month ago.

Thanks for your help, just off to work so will try it out when I get back.