Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
11 years ago.
Timer for interrupts
Hi
Could you tell me what is the best method for measuring time between rise and falling period of interrupts? When I implemented Timer into my code it wasn't working. Below, is my code which increments the counter whenever there is a rise.
Thanks
include the mbed library with this snippet
#include "mbed.h" void pulseLeft(); void pulseRight(); void pulseStop(); int countLeft = 0; int countRight = 0; int main() { ir_rx0.rise(&pulseRight); ir_rx0.fall(&pulseStop); while(true); } void pulseRight() { countRight++; printf("COUNT_RIGHT = : %d\n", countRight); if(countRight == 32) countRight = 0; }
2 Answers
11 years ago.
Which timescale are you looking at? But in principle, just use a timer. If that didn't work post the code that didn't work.
11 years ago.
I am looking at microseconds
here is the bit of code that uses timer. its main purpose is to measure time it to decrement. is this is what I shupossed to do?
include the mbed library with this snippet
void pulseLeft() { int begin; int end; timer.start(); begin = timer.read_us(); countLeft--; end = timer.read_us(); timer.stop(); printf("difference = : %d\n", end - begin); //printf("COUNt_LEFT = : %d\n", countLeft); if(countLeft == 0) { //ticks++ countLeft = 32; } } #include "mbed.h"
Now it just measures how long it takes to decrease countLeft by one (that doesn't take long). In your question you said you wanted to measure time between rising and falling edges of the interrupt, so then you need to start the timer on the rising edge function, and stop it on the falling edge function.
posted by 23 Apr 2013