Shop OBEX P1 Docs P2 Docs Learn Events
Need a micro-second timer — Parallax Forums

Need a micro-second timer

LoopyBytelooseLoopyByteloose Posts: 12,537
edited 2014-05-06 20:23 in Propeller 1
Hi all,
I know that that SimpleIDE provides mstimer.h for millisecond delays, but in porting Arduino code over to the Propeller I have run into problems as the code uses the Arduino library that include a microsecond timer.

I have created a good timer for micro second delays in pfth Forth that worked very linearly down to about 25 microseconds. Under that, system call overhead begain to distort the timing. I have looked at the mstimer.h library and the c code, but I am not finding much to indicate how to create my own micro-second timer library.

Any suggestions before I plod along on my own?

Comments

  • ersmithersmith Posts: 5,900
    edited 2014-05-06 10:24
    Hi all,
    I know that that SimpleIDE provides mstimer.h for millisecond delays, but in porting Arduino code over to the Propeller I have run into problems as the code uses the Arduino library that include a microsecond timer.

    I have created a good timer for micro second delays in pfth Forth that worked very linearly down to about 25 microseconds. Under that, system call overhead begain to distort the timing. I have looked at the mstimer.h library and the c code, but I am not finding much to indicate how to create my own micro-second timer library.

    Any suggestions before I plod along on my own?

    The PropGCC C library already has a usleep() function that sleeps for a given number of microseconds. The prototype is in <unistd.h>, if I remember correctly.
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2014-05-06 10:49
    Thanks, I will look for that.

    I am not sure that a delay, a sleep, and a timer are the same thing.

    Sleep mode in a BasicStamp actually puts the microcontroller into a low power suspended state.
    Delay mode in anything is generally a delay
    Timer mode could be a lot of things and more complex.

    I have to study both the Propeller GCC and the Arduino libraries to sort out whether they are trying to do the same thing or something completely different or similar things in different ways.

    Propelleruino library is attempting to mimic Arduino libraries, but it is only partally completed at this point.
  • Heater.Heater. Posts: 21,230
    edited 2014-05-06 10:59
    Does it matter?

    If the function is "doSomeKindOfSleep(T);" where T is seconds, milliseconds, microseconds then do you really care if the processor goes into some low power mode of not? As far as you program is concerned that function returns after whatever time and you continue on your merry way.

    Timers generally look a bit different, perhaps "setTimeout(someFunction, 1000);" Which returns immediately but "someFunction" will be called a thousand whatever units later.
  • mindrobotsmindrobots Posts: 6,506
    edited 2014-05-06 11:50
    It looks like the Arduino timers are used to do the sleepy/delay stuff and generate PWM and also to support this strange feature:

    attachInterrupt(function, period)
    Calls a function at the specified interval in microseconds. Be careful about trying to execute too complicated of an interrupt at too high of a frequency, or the CPU may never enter the main loop and your program will 'lock up'. Note that you can optionally set the period with this function if you include a value in microseconds as the last parameter when you call it.

    You surely wouldn't be needing an INTERRUPT????? :lol:
  • Dave HeinDave Hein Posts: 6,347
    edited 2014-05-06 14:13
    The source for usleep is as follows:
    #include <time.h>
    #include <sys/thread.h>
    #include <unistd.h>
    #include "cog.h"
    
    int
    usleep(unsigned int n)
    {
      unsigned waitcycles;
      unsigned usecond = _clkfreq/1000000;
    
      waitcycles = _CNT + n*usecond;
      __napuntil(waitcycles);
    
      return 0;
    }
    
    I couldn't locate the source for __napuntil, but it may have some overhead associated with pthread's. If you want a wait routine with low overhead you should probably just write your own using waitcnt. Or you could write it as a macro to eliminate the calling overhead.
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2014-05-06 20:23
    Yep, the Arduino library often has features that are clumped together in a different way than things are done with the Propeller.

    Discussing it here does help. Why so?

    Well, I look at 'timers' and think of just the Propeller context. The code may be an Arduino context and trying to do a PWM or a sleep funciton. These things have to be sorted out in different ways or just eliminated.

    I do learn a lot by the comparison and contrast processes. But there is a bit of confusion along the way. Thanks to all.

    At this point, I am trying to eliminate all sleep functions and watchdog timer calls... they are distracting.
  • JDJD Posts: 570
    I'm guessing this might have already been answered or forgotten by now, but I've found the need to accurately count microseconds in a project. There is a easy solution and it's a timer object from Jeff Martin that works great, and it comes with the Propeller Tool. It's called Clock.spin and it allows the use of pauses in a program using spin, so no need to learn ASM just for this one thing. Although super fast, ASM is not as easy as spin to use. With clock.spin you can use seconds, milliseconds, and microseconds in a routine, and it takes execution time into count so you can get solid timing. There are also some functions like Mark a reference time for synchronized-delay, Sync to start of next microsecond-based time window, and more that could be used for a whole host of applications.

    Hope this helps
Sign In or Register to comment.