I thought this would be simple (average time between 4 switch presses)


Closed Thread
Results 1 to 7 of 7

Hybrid View

  1. #1
    Join Date
    May 2007
    Posts
    604


    Did you find this post helpful? Yes | No

    Default Re: I thought this would be simple (average time between 4 switch presses)

    What if you need more than 2 seconds - say 1 minute? Or more? When designing an application think of terms of events that occur in MCU time (microseconds to perhaps a few miliseconds) and those that happen in human time (from seconds to hundreds of hours).

    Lowering the clock speed is not the best way to accomplish what you want - it would be like running a car with its brakes applied. Its main use is to lower power consumption. For stuff that happens in MCU time, you use timers in their native mode - just like you described. For human time events, set up a 16-bit timer to interrupt say every 100mS (50,000 counts assuming a full-speed 20MHz clock with a PIC16). In the ISR, increment a 32-bit variable. This will allow you generate delays up to 42,949,672 seconds, 11,930 hours, 497 days. Compare the variable to whatever delay you need (you can have multiple delays), when it matches set a flag that will acted upon appropriately in your mainline code.
    Why pay for overpriced toys when you can have
    professional grade tools for FREE!!!

  2. #2
    Join Date
    May 2007
    Posts
    604


    Did you find this post helpful? Yes | No

    Default Re: I thought this would be simple (average time between 4 switch presses)

    Sorry typo, should be:

    set up a 16-bit timer to interrupt say every 10mS
    Why pay for overpriced toys when you can have
    professional grade tools for FREE!!!

  3. #3
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default Re: I thought this would be simple (average time between 4 switch presses)

    All good points well made...but what if the maximum time between switch presses is only ever going to be 1 second ...I've therefore got 100% 'headroom' @ 2 seconds.....and what if this was this simple task was the only purpose of the program/PIC .....hey some power consumption 'win' to boot.

    I guess where I'm heading, before anyone can decide whether a final solution is 'best' or not, then they'd need to know the full requirements.

    I don't agree with the analogy of running a car with the brakes on here ....this is more akin to running the car at very low rpm because that's all that's required! (your family car might be capable of 120mph....but when you go to the corner shop, you're likely going to go there at 30mph, becuase that's 'best' for the situation at hand)

    But as ever, there are many ways to skin a cat....and I always welcome all input from those more learned on here!(just about everybody, lol)

    Your proposed way sounds good....I'll sleep on it!

  4. #4
    Join Date
    Feb 2010
    Location
    USA, New England
    Posts
    164


    Did you find this post helpful? Yes | No

    Default Re: I thought this would be simple (average time between 4 switch presses)

    Hi Hank,

    Late to the game (as usual) and probably haven't read thoroughly enough, but why should that stop me from commenting? 8^)

    You said that the times will always be less than 1 second and that the timer can hold just a little more than half a second. Why not clear the overflow flag at the same time you clear the timer. Then when you read the timer also check the overflow flag. If it is set, add $FFFF to your value. The interrupt flags work whether they are used to trigger an actual interrupt or not.

    Best Regards,
    Paul
    The way to avoid mistakes is to gain experience. The way to gain experience is to make mistakes.

  5. #5
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default Re: I thought this would be simple (average time between 4 switch presses)

    Hi Paul,

    Stonking late entry - many thanks!!

    Hank.

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts