Shop OBEX P1 Docs P2 Docs Learn Events
Why use a counter and not CNT? — Parallax Forums

Why use a counter and not CNT?

AkkarinAkkarin Posts: 43
edited 2012-08-22 03:50 in Propeller 1
Hi,

I'm curious what the advantages of using a dedicated counter are instead of using the CNT (register,variable?) for example to measure pulsewidth? I mean, either way you would use waitpeq and/or waitpne to trigger start and stop of measurement, right? If you account for the overhead as it says in the SPIN help (width := -cnt, SOME CODE,..., width += cnt - 544, where 544 was determined before) the measurements should be quite the same as with a counter and the handling is much easier.
Please don't say now: because it's more accurate. If it is more accurate, please explain why. Same with speed of execution or something like that.

Thank you.

I'd really like to understand this. It's not that I don't believe that using the counter is sometimes (or always) better.


- Akkarin

Comments

  • kuronekokuroneko Posts: 3,623
    edited 2012-08-22 01:17
    Let's assume positive pulse width measurement. One would use a counter in POS mode and use waitpxx to get an idea about start/end of measurement. When done the result is present in phsx with a granularity of one system clock.

    Now let's do the same with just waitpxx and manual cnt sampling. No matter how you do it, the SPIN interpreter reads the insn to sample cnt (hub access), gets the value from the cnt register and then does further processing. This value is therefore always aligned to the hub window (16n system cycles) which effectively lowers your accuracy.

    Similar issues apply between waitpxx at PASM level exiting and the SPIN interpreter getting the next insn.

    Edit: That being said, doing the waitpxx/cnt sampling in PASM will give you equivalent accuracy to the counter approach and may well be easier as you can do it on the fly without sacrificing a counter.
  • AkkarinAkkarin Posts: 43
    edited 2012-08-22 02:21
    kuroneko,

    thank you. I think I get the point.

    I knew I should have asked this before I write my routines in SPIN using CNT ;)
    Now I have to rewrite the whole thing :) And for that I have to go deeper into PASM programming since I never used it before.

    - Akkarin
  • kuronekokuroneko Posts: 3,623
    edited 2012-08-22 02:36
    Akkarin wrote: »
    Now I have to rewrite the whole thing :) And for that I have to go deeper into PASM programming since I never used it before.
    What's stopping you from simply setting up a counter in SPIN (or are you using them both already)? OTOH, if you program in PASM both solutions would have worked.
  • AkkarinAkkarin Posts: 43
    edited 2012-08-22 03:50
    You are right of course I don't have to redo everything from scratch.
    I'm currently reading Application Note AN001 to make myself familiar with the counter setup.
Sign In or Register to comment.