Shop OBEX P1 Docs P2 Docs Learn Events
microsecond counter in C — Parallax Forums

microsecond counter in C

ccb214ccb214 Posts: 2
edited 2009-06-12 15:48 in Propeller 1
Hi im new in the parallax family, and well im trying to do a microsecond counter in C, but the only way that ive seen that they use the timer is with a given time
i was hoping some one could help me with any ideas on how to take the counter to start and stop when i tell it to, the first idea i had was to use the CLCKFREQ and use it to add up a counter... yet its still kinda fuzzy.

thank you
SincerelyCcb

Comments

  • jazzedjazzed Posts: 11,803
    edited 2009-06-12 05:03
    Hi Ccb. Welcome to the forum.

    The CNT global register is free running. The alternative solution is to use the CNTA, FRQA, and PHSA registers (or CNTB ...). CNTA can be used to enable/disable the counter. The FRQA register can be set to toggle the "A pin" at end of the FRQA count. You should look at the latest Propeller Datasheet and/or the Counter App-note on the Parallax Propeller web-site download page. Maybe someone else can describe this in better detail for you. Your purpose is a little vague to me ... you might give an example of use.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    --Steve


    Propalyzer: Propeller PC Logic Analyzer
    http://forums.parallax.com/showthread.php?p=788230
  • Mike GreenMike Green Posts: 23,101
    edited 2009-06-12 05:05
    Download the application note on the built-in counters (AN001 - www.parallax.com/tabid/442/Default.aspx).

    These counters can be started and stopped either under program control or an I/O pin.

    The other option is to save the system clock value at the beginning of the time period and save another value at the end of the time period. By subtracting the two values, you get the elapsed time in system clock ticks (12.5ns at 80MHz). Since the counters are 32 bits, the maximum time period before wrapping around is around 50 seconds.
  • ccb214ccb214 Posts: 2
    edited 2009-06-12 06:58
    Thank you very much, i think ill go with taking to readings from the internal clock and then substracting [noparse]:)[/noparse]
    Thanks for your help [noparse]:D[/noparse]
  • RaymanRayman Posts: 14,833
    edited 2009-06-12 09:22
    Microsecond timing was my main motiviation!· Look here:

    http://www.rayslogic.com/propeller/PulseGen.htm

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    My Prop Info&Apps: ·http://www.rayslogic.com/propeller/propeller.htm
  • jazzedjazzed Posts: 11,803
    edited 2009-06-12 15:48
    If all you care about is generating a 1us delay with ICC, all you have to do is #include <propeller.h> in your module and use WAIT(count) where count is an int. The count for would be CLKFREQ/1000000 or 80 for an 80MHz clock. I have no idea how to do it in Catalina. The WAIT macro is included below for reference.

    /**
     * Pause program execution for number of variable count clock ticks.
     * @note This macro gives higher resolution than the wait function.
     * @note Using a count under 20 will make the program hang.
     * @note If you need a wait with a constant, use the wait function.
     * @param count is number of clock ticks to wait and must be a variable.
     */
    #define WAIT(count)     \
    do { \
        asm("mov     TEMP0,%" #count); \
        asm("add     TEMP0,cnt"); \
        asm("waitcnt TEMP0,%" #count " WZ WC NR"); \
    } while(0)
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    --Steve


    Propalyzer: Propeller PC Logic Analyzer
    http://forums.parallax.com/showthread.php?p=788230
Sign In or Register to comment.