Elapsed Timer Demo


Closed Thread
Results 1 to 40 of 112

Hybrid View

  1. #1
    Join Date
    Feb 2004
    Location
    Michigan, USA
    Posts
    305


    Did you find this post helpful? Yes | No

    Default Re: Elapsed Timer Demo

    Henrik, thank you for your reply. It is very logical.

    I came up with this before you posted your reply last night:

    Code:
    if seconds => 59 then
    minutes = minutes - 1
    minuteschanged = 1
    endif
    if seconds > 59 then
    seconds = 59
    endif
    
    
    if Minutes => 59 and seconds => 59 then
    hours = hours - 1
    HoursChanged = 1
    endif
    IF Minutes > 59 then
    Minutes = 59
    endif
    
    if hours = 0 then
    Days = Days - 1 
    DaysChanged = 1 
    Hours = 0 
    endif
    
    IF HOURS > 23 then
    HOURS = 23
    ENDIF
     
    endif
    ENDIF
    It works and has been running all night. Yours is cleaner though! My issue was I was trying to change numbers at 0 not 0 - 1. Once I realized this is took 2 minutes to fix it.

  2. #2
    Join Date
    Feb 2004
    Location
    Michigan, USA
    Posts
    305


    Did you find this post helpful? Yes | No

    Default Re: Elapsed Timer Demo

    This interrupt based timer runs in the background. Does that mean a pause in the main program will stop it or will it continue to run in the background? I am trying to de-bounce a switch.

  3. #3
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,624


    Did you find this post helpful? Yes | No

    Default Re: Elapsed Timer Demo

    Hi,
    A Pause (or any other command) will not interfere with the interrupt - it'll keep running in the background.
    However, if you're interrupting at a high rate the context saving, interrupt code and context restore will take cycles from the main PBP program without it knowing so your pause may be a bit longer than what you specify. For non critical timings it's not a problem.

    /Henrik.

    EDIT: Obviosuly interrupting at ANY rate will take cycles away from the main program. What I mean is that the faster you interrupt the more cycles will "vanish" and any software timed routines like Pause, SERIN, Pulsin that you have in the main program will be affected.
    Last edited by HenrikOlsson; - 25th October 2012 at 19:50.

  4. #4
    Join Date
    Feb 2004
    Location
    Michigan, USA
    Posts
    305


    Did you find this post helpful? Yes | No

    Default Re: Elapsed Timer Demo

    No the pause length isnt critical. I'm trying to get one switch to start and stop the elapsed timer. So it needs a debounce and a wait period so that it doesnt immediately stop the timer again.

    Here's what I have but it doesnt work to well:

    Code:
    loop1:
    
    For A = 0 to 1000
         IF PORTB.7 = 1 and TimerRunning = 0 then 
         gosub StartTimer
       endif
    next A
    
    if SecondsChanged = 1 then
               LCDout $FE,2, dec2 Hours,":",dec2 Minutes,":",dec2 Seconds
               SecondsChanged = 0
            endif
            
    For B = 0 to 1000
         If PORTB.7 = 1 and timerrunning = 1 then 
         gosub StopTimer
       endif
    next B
    goto loop1
    This means the user has to press and hold the button for 1 second before the timer will start. However, if they press it for 1.5 seconds, then they only need to press it again for half a second and it stops. If they press it for 2.5 seconds, then timer has started, stopped and is half way to starting again.

    I was wondering id something like this would work?

    Code:
    
    'If PORTB.7 = 1 then
    'gosub StartTimer
    'TimerRunning = 1
    'pause 150
    'endif
    
    If SecondsChanged = 1 then
               LCDout $FE,2, dec2 Hours,":",dec2 Minutes,":",dec2 Seconds
               SecondsChanged = 0
            endif
    
    If PORTB.7 = 1 AND TimerRunning = 1 then
    'gosub StopTimer
    'TimerRunning = 0
    'pause 150
    'endif
    I think the problem with both of them is that there is no time between the on if then and the off if then, and there needs to be some sort of logic that says that if the timer is running and the button is still pressed, do not stop the timer until the button has been released and pressed again.
    Last edited by jmgelba; - 25th October 2012 at 20:14.

  5. #5
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default Re: Elapsed Timer Demo

    It keeps running in the background.
    The interrupts will not wait for PBP statements to finish.

    Ok fine, ... for many years I've resisted making a count down elapsed timer for humanitarian reasons.
    But if you guys are going to do it anyhow, I might as well make a new version of the Elapsed Timer.
    I can only hope that if somebody uses it for nefarious purposes, they end up blowing themselves up.

    Here's the test circuit.



    Here's the test program ...
    Code:
    ' Define LCD connections
    DEFINE LCD_DREG PORTC   ' Set LCD Data port
    DEFINE LCD_DBIT 4       ' Set starting Data bit (0 or 4) if 4-bit bus
    DEFINE LCD_RSREG PORTC  ' Set LCD Register Select port
    DEFINE LCD_RSBIT 2      ' Set LCD Register Select bit
    DEFINE LCD_EREG PORTC   ' Set LCD Enable port
    DEFINE LCD_EBIT 3       ' Set LCD Enable bit
    DEFINE LCD_BITS 4       ' Set LCD bus size (4 or 8 bits)
    DEFINE LCD_LINES 2      ' Set number of lines on LCD
    DEFINE LCD_COMMANDUS 2000 'Command delay time in us
    DEFINE LCD_DATAUS 50 'Data delay time in us
    
    DEFINE  OSC 4
    
    INCLUDE "Elapsed_DN.bas"  ; Elapsed Timer Routines
    
    ZERO_LED   VAR PORTB.3
    
    ANSEL = 0                 ' All Digital
    ANSELH = 0
    LOW ZERO_LED              ' start with LED OFF
    OPTION_REG.7 = 0          ' enable PORTB pull-ups
    PAUSE 250                 
    LCDOUT $FE,1              ' Initialize LCD
    PAUSE  250
    
    Days = 1                  ' set initial time
    Hours = 1
    Minutes = 3
    Seconds = 10
    
    GOSUB StartTimer          ' Start the Elapsed Timer
    
    Main:
    CountDown = !PORTB.0
      IF SecondsChanged = 1 THEN  
         SecondsChanged = 0
         LCDOUT $FE,2, DEC Days,"d-",DEC2 Hours,":",DEC2 Minutes,":",DEC2 Seconds
      ENDIF
      IF ZERO_LED != ZERO THEN ZERO_LED = ZERO
    GOTO Main
    If the button on PORTB.0 is pressed it counts down.
    If it is not pressed, it counts up.
    The LED comes on when it reaches 0.

    If you are using the countdown for a movie set, it has to stop at 1 second.
    Put this in the main loop.
    Code:
      IF (CountDown=1) AND (Days=0) AND (Hours=0) AND (Minutes=0) and (Seconds=1) _
          THEN GOSUB StopTimer
    You'll need the ASM_INTS include from the original Elapsed Demo.
    Attached Files Attached Files
    DT

  6. #6
    Join Date
    Feb 2004
    Location
    Michigan, USA
    Posts
    305


    Did you find this post helpful? Yes | No

    Default Re: Elapsed Timer Demo

    Question then. Can I place the LCDOUT routine in the interrupt so that the screen always updates, even if the main program needed to pause for a total length over 1 second? The reason is to keep the display updating correctly on the second, every second.

  7. #7
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default Re: Elapsed Timer Demo

    NO!

    You could in the Elapsed_INT version, but not in this standalone version.
    DT

  8. #8
    Join Date
    May 2006
    Location
    Del Rio, TX, USA
    Posts
    343


    1 out of 1 members found this post helpful. Did you find this post helpful? Yes | No

    Default Re: Elapsed Timer Demo

    Darrell,
    You should have put the stop at 1 sec in the code, and make people pull it out otherwise. Just a little step towards keeping the knuckle-heads from causing trouble.

  9. #9
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default Re: Elapsed Timer Demo

    LOL Steve, I like that idea.

    But as it is, they have to be holding the button down for it to count down.
    If they're still holding it when it reaches 0 ... well ...

    I know it can be bypassed, but don't tell them how
    DT

  10. #10
    Join Date
    Feb 2004
    Location
    Michigan, USA
    Posts
    305


    Did you find this post helpful? Yes | No

    Default Re: Elapsed Timer Demo

    Code:
    IF PORTA.2 = 1 OR PORTA.3 = 1 AND DIM = 1 AND timerrunning = 1 THEN
    PORTA.0 = 1
    ENDIF
    IF PORTA.2 = 1 OR PORTA.3 = 1 AND DIM = 2 AND timerrunning = 1 THEN
    PORTA.1 = 1
    ENDIF
    IF PORTA.2 = 1 OR PORTA.3 = 1 AND DIM = 3 AND timerrunning = 1 THEN
    PORTA.0 = 1
    PORTA.1 = 1
    ENDIF
    Looking for a more efficient way of doing this. I need to monitor 2 inputs and turn on the correct outputs only when the timer is running. This does compile but I havent tested it to see if it actually works. This snippet will go in the main loop of the program that runs while the timer counts down.

Similar Threads

  1. Instant Interrupts - Revisited
    By Darrel Taylor in forum Code Examples
    Replies: 772
    Last Post: - 17th February 2016, 23:14
  2. Get elapsed time while TIMER samples pulses
    By RodSTAR in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 27th March 2009, 17:27
  3. Elapsed Timer Demo in a PIC12F675
    By Leonardo in forum mel PIC BASIC Pro
    Replies: 16
    Last Post: - 21st November 2008, 01:01
  4. Totally Baffled with Elapsed Timer
    By CocaColaKid in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 11th June 2008, 22:01
  5. Darrel Taylor Elapsed Timer
    By rwskinner in forum mel PIC BASIC Pro
    Replies: 14
    Last Post: - 13th March 2008, 02:22

Members who have read this thread : 5

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