11 years, 6 months ago.

Timer and USB compatibility

Hi to everyone.

I'm trying timer code with a terminal console but I don´t be able to start timer with a key pull from the Pc keyboard. This is my code:

#include "mbed.h"

Timer timer;
DigitalOut led(LED1);
Serial pc(USBTX, USBRX);
int begin;

int main() {
    
    
    while(1)
    {
    char c = pc.getc();
    if (c == 's')
    {
    led = 1;
    timer.start();
        begin = timer.read_ms();
        wait(1);
        printf("Timer started at = %d ms \r\n", begin);
            if (begin == 500)
                 {
                 led = 0;
                printf("Timer stopped at = %d ms \r\n", begin);
                timer.stop();
                 }
    }
    }   
}

Is there any incompatibility between timer and usb pins?

Thanks for all in advance Francisco.

2 Answers

11 years, 6 months ago.

The USB pins you are now using are just a serial connection connected to a serial <> USB bridge, so no there are no compatibility issues there.

The code probably doesn't do what you expect it to do, begin will never be equal to 500 since the first run it will be equal to zero (you measure directly after starting it), and the next run it will be larger than 1000 (since you wait 1000). The timer works if you put timer.stop(); in front of timer.start();.

Why to do that? I have no clue at all. The timer function seems to be kinda bugged, unless I am really missing something some dev should have a look at it. If I place a wait(0.4) between line 17 and 18, it will always read 400ms. If I place timer.stop in front of timer.read_ms(), and timer.start afterwards, it always increases its measured time by 400ms.

Accepted Answer
11 years, 6 months ago.

Hi Erik.

Thanks a lot for your answer. Actually you're right. About the timer, I don't understand what you want to say when you suggest to put timer.stop() in front of timer.start(). In the same line?

Just add a line in front of it, where you write timer.stop();, and on the next line you got your current timer.start();

posted by Erik - 18 Dec 2012