Shop OBEX P1 Docs P2 Docs Learn Events
Yet another counter question...... — Parallax Forums

Yet another counter question......

I believe the answer will be "no" but just double-checking:

Whilst counting pulses, if I kill a high-order bit in the counter's register, to avoid rollover, would that in any way interrupt the pulse count? IOW, could I lose pulses?

Comments

  • Ah, wait, seems that any write to the register, zeroes it.
  • AribaAriba Posts: 2,690
    If you clear a bit in the PHSx register you will miss the edges that happen in between the read and the write of the register. With a single read-modify-write instruction, this window takes only a few clock cycles, so you will maybe not notice it if the input frequency is not in the Megahertz.

    You can write every value into the register, not sure why you think it gets cleared.

    For the rollover you want avoid, we need a bit more info what you want to do. Normally you can just read the PHSx register and subtract it from the previous read value to get no rollovers.

    Andy

  • BeanBean Posts: 8,129
    What Andy said. As long as it doesn't rollover more than once between reads.

    Bean
  • jmgjmg Posts: 15,173
    edited 2015-12-07 19:26
    Mickster wrote: »
    IOW, could I lose pulses?
    Yes.

    You could measure this aperture, by running two timers, one where you clear upper bits (RMW) and one free-running, and then compare the LSBs.


  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2015-12-07 22:09
    You can't do read-modify-writes to the phsx registers while the counter is running.

    -Phil
  • Yeah, it will be in the Megahertz. I intend to feed PHSA with upcount pulses and PHSB with downcount pulses, subtract B from A and sum the difference with "Actual_Count" variable.
    In my case, the difference will never be greater than, say, 1,000,000 between the two registers so I was thinking that I could watch for bit 22 (or greater) being high on both registers and simply reset them. This, apparently is not a good idea because I don't want to risk losing pulses at all. So I guess I need to handle rollover some way.
  • No no no - never reset a variable like this, rollover is handled for you if you are subtracting,
    the difference will simply be correct at all times.
  • Mark_T wrote: »
    rollover is handled for you if you are subtracting,
    the difference will simply be correct at all times.

    LOL, that simple fact didn't REGISTER with me, seemed so COUNTER intuitive....thanks!
Sign In or Register to comment.