Elapsed Timer Demo - Page 3


Closed Thread
Page 3 of 3 FirstFirst 123
Results 81 to 112 of 112
  1. #81
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by hackableFM View Post
    I intend to built a control unit based around a 16F877 which will include menu's to control output pins on the PIC, Obviously within the menu there will likely be instructions which will keep the processor busy for a few Ms at a time. My project ideas originally were to include an external clock device such as the DS1307 or the DS1603 to keep track of time whilst the rest of the program (Menu's etc etc) is running and keeping the processor busy.
    I was advised on this forum to look at this thread and if this is what I think it is then it will be truly fantastic for my project. ...
    It can do the things you are suggesting.

    But it's not a replacement for a battery backed-up RTC chip.
    Any RESET will lose the accumulated time.

    I have used it as a clock running on-chip, but on power-up it reads the time from an RTC. That way it doesn't have to continuously read the time via I2C and it's always available from the internal time clock.
    <br>
    DT

  2. #82
    Join Date
    Feb 2009
    Posts
    7


    Did you find this post helpful? Yes | No

    Default

    Thank you, This sounds good so I'll have a bit more of a read up on it, I'll try to understand more about how it works before I go building anything.

    Battery backed is not an issue, this will be connected to a permanent 12v system within a caravan and therefore never likely to loose the accumulated time.

    hackableFM...

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


    Did you find this post helpful? Yes | No

    Default Re: Elapsed Timer Demo

    Hi Darrel,

    I'm trying to create a timer - a run time timer that will turn off an output after 30 minutes. Would this code work for such a task?

    Thanks

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


    Did you find this post helpful? Yes | No

    Default Re: Elapsed Timer Demo

    Sure, it'll do that really easy.

    Code:
    IF MinutesChanged THEN
        MinutesChanged = 0
        IF Minutes = 30 THEN
            ; turn off whatever it is here
            GOSUB ResetTime
        ENDIF
    ENDIF
    DT

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


    Did you find this post helpful? Yes | No

    Default Re: Elapsed Timer Demo

    Excellent!

    Can it do incremental too? As in can it be started and stopped during the 30 minute period?

    Also I need it to run when an output is on, but if the output is only on say 20 minutes then off for say an hour, I dont want it to only run for 10 minutes if the output is turned on again. What this timer is actually doing is stopping high power LED's getting hot due to excessive run time.

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


    Did you find this post helpful? Yes | No

    Default Re: Elapsed Timer Demo

    The Elapsed Timer can be Stopped and Started, like a stopwatch.
    Code:
     GOSUB StopTimer
     GOSUB ResetTime
     GOSUB StartTimer
    You can time things any which way you need to.
    DT

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


    Did you find this post helpful? Yes | No

    Default Re: Elapsed Timer Demo

    Last question!! I swear!! Can I have 2 instances running at once? 1 counting the on time, and the other counting the off time?

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


    Did you find this post helpful? Yes | No

    Default Re: Elapsed Timer Demo

    No, the Elapsed Timer Demo can only have one timer at a time.
    The same concept can be used to create multiple timers, but this demo doesn't do that.

    Since the ON and OFF periods can't happen at the same time...
    When one period is finished, Reset the timer and measure the next period.
    You only need one timer.

    You don't have to stop and start the timer, just GOSUB ResetTime, and the time will revert to 0d-00:00:00.0 and keep counting.
    Use it like a Stopwatch.

    The interactive LCD on this page shows how it works. (Click on the "GOSUBs")
    http://www.darreltaylor.com/DT_INTS-14/elapsed.html
    But that page is for the DT_INTS version of the Elapsed Timer.
    DT

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


    Did you find this post helpful? Yes | No

    Default Re: Elapsed Timer Demo

    Well I've had an implementation of this working great for months now. Its counts down 20 minutes, turns off the output, flashes an LED using the pause command, then sits there waiting for the input to start again.
    I need to add a feature now though. As the time reaches 15 minutes, I need it to flash an LED at 2Hz while still counting down and turning off at 20 minutes.

    Currently the code is like this:

    Code:
    
    IF MinutesChanged THEN
        MinutesChanged = 0
        IF Minutes = 20 THEN
        HPWM 1, 0 , 30000
        POWER = 0
        LEDS = 0
        CURRENT = 0
    If I change it to this will it do what I need it to do? Second thing is if it will, how do I flash an LED while still running the main program? I am using HPWM already.

    Code:
    IF MinutesChanged THEN
        MinutesChanged = 0
        IF Minutes = 15 THEN
        GOSUB Flashyflashy
    ENDIF
    
    
    IF MinutesChanged THEN
        MinutesChanged = 0
        IF Minutes = 20 THEN
        HPWM 1, 0 , 30000
        POWER = 0
        LEDS = 0
        CURRENT = 0
    ENDIF

  10. #90
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default Re: Elapsed Timer Demo

    Hi,
    I'm not that familiar with the code but the way you suggest will only call FlashyFlashy when minutes is 15 - not more, not less. If you want FlashyFlashy to keep executing all the way to 0 you need IF MINUTES <= 15 THEN You could also streamline it a bit, there's no real need to check and reset MinutesChanged twice, perhaps something like
    Code:
    IF MinutesChanged THEN
        MinutesChanged = 0
    
        IF Minutes <= 15 THEN
        GOSUB Flashyflashy
    
        IF Minutes = 20 THEN
        HPWM 1, 0 , 30000
        POWER = 0
        LEDS = 0
        CURRENT = 0
    ENDIF

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


    Did you find this post helpful? Yes | No

    Default Re: Elapsed Timer Demo

    Jim,

    I think that'll just flash it once at the 15 minute point.

    This should work.
    Code:
     Flashing VAR BIT
    
       IF MinutesChanged THEN
            MinutesChanged = 0
            IF Minutes = 15 THEN Flashing = 1
            IF Minutes = 20 THEN
                Flashing = 0
                HPWM 1, 0 , 30000
                POWER = 0
                LEDS = 0
                CURRENT = 0
            ENDIF
        ENDIF
    
        IF Flashing THEN
            SELECT CASE Ticks
              CASE IS <25 : HIGH LED
              CASE IS <50 : LOW  LED
              CASE IS <75 : HIGH LED
              CASE ELSE   : LOW  LED
            END SELECT
        ENDIF
    
    Last edited by Darrel Taylor; - 26th April 2012 at 22:08.
    DT

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


    Did you find this post helpful? Yes | No

    Default Re: Elapsed Timer Demo

    Darrel, can this be used as a countdown timer with the time left updated each second and displayed on a LCD?

    Thanks.

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


    Did you find this post helpful? Yes | No

    Default Re: Elapsed Timer Demo

    It Can ...

    But you would need to create the count down routine.
    The demo only counts up.
    DT

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


    Did you find this post helpful? Yes | No

    Default Re: Elapsed Timer Demo

    OK thanks. I will play around with it and see what mess I can create.

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


    Did you find this post helpful? Yes | No

    Default Re: Elapsed Timer Demo

    Ticks = Ticks + 1
    if Ticks = 100 then
    Ticks = Ticks-100
    Seconds = Seconds - 1
    SecondsChanged = 1
    if Seconds = 0 then
    Minutes = Minutes - 1
    MinutesChanged = 1 Seconds = 59
    endif

    if Minutes = 0 then
    Hours = Hours - 1
    HoursChanged = 1 Minutes = 59
    endif

    if Hours = 0 then
    Days = Days - 1
    DaysChanged = 1 Hours = 23
    endif
    endif

    To my untrained eyes, this would count down the time at every 100 ticks. I guess the issue, if this works, is how to preloaded the timer with the required start time and how to get it to stop at 0.

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


    Did you find this post helpful? Yes | No

    Default Re: Elapsed Timer Demo

    Seem to be having an issue I cant resolve. I've only use the timer software on a 18F2550 and it worked perfectly. I'm now using a 18F1320 and I've ran into an issue when compiling.

    ERROR Line 23: Redefinition of VAR. (DT_INTS-18.bas)
    ERROR Line 24: Redefinition of VAR. (DT_INTS-18.bas)
    ERROR Line 25: Redefinition of VAR. (DT_INTS-18.bas)

    Which are these lines in the code:

    Code:
    wsave       var byte  BANKA  SYSTEM   ' location for WREG
    ssave       var byte  BANK0  SYSTEM   ' location for STATUS register
    bsave       var byte  BANK0  SYSTEM   ' location for BSR register
    I am unsure as to what the problem is and where to start.

    I'm using PBP 2.60A and MCS 3.0.0.5

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


    Did you find this post helpful? Yes | No

    Default Re: Elapsed Timer Demo

    Fixed the above issue.

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


    Did you find this post helpful? Yes | No

    Default Re: Elapsed Timer Demo

    Getting frustrated! :-) I've tried a bunch of things to get the counter to count down correctly.

    There is a problem at rollover. Let's say the timer is set to 1 hour 1 minute zero seconds, it should start there and one second later change to 1:00:59. It doesn't, it stays at 1:01:00 for 1 second and then counts down from 1:01:59. Then when it gets to 1:00:00 it rolls over to 0:00:00 for 1 minute then the next second its at 0:59:59.
    Not really sure what to try. What about a "first run through the interrupt" flag that automatically makes minutes = minutes - 1 and starts the seconds at 59?
    Last edited by jmgelba; - 25th October 2012 at 01:12.

  19. #99
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default Re: Elapsed Timer Demo

    Hi,
    Without thinking too much, how about this:
    Code:
    Hours VAR BYTE
    Minutes VAR BYTE
    Seconds VAR BYTE
    
    Hours = 1
    Minutes = 1
    Seconds = 0
    Ticks = 100
    
    ISR:
      Ticks = Ticks - 1
      If Ticks = 0 THEN              ' Ticks rolled over, one second has passed
        Ticks = 100                    ' Reset ticks (10ms ticks)
    
        Seconds = Seconds - 1     ' Count down a second.
    
        If Seconds = 255 THEN      ' Seconds rolled over
          Seconds = 59                 ' Reset seconds
          Minutes = Minutes - 1      ' Count down a minute
    
            If Minutes = 255 THEN  ' Minutes has rolled over
              Minutes = 59              ' Reset minutes
              Hour = Hour - 1         ' Count down an hour
            ENDIF
        ENDIF
      ENDIF
    /Henrik.

  20. #100
    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.

  21. #101
    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.

  22. #102
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    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 18:50.

  23. #103
    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 19:14.

  24. #104
    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

  25. #105
    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.

  26. #106
    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

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


    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.

  28. #108
    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

  29. #109
    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.

  30. #110
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default Re: Elapsed Timer Demo

    Here's one idea
    Code:
    Temp VAR BYTE
    
    If TimerRunning = 1 THEN
      If (PortA.2 = 1) OR (PortA.3 = 1) THEN
        Temp = PortA & %11111100
        PortA = Temp + DIM
      ENDIF
    ENDIF
    /Henrik.

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


    Did you find this post helpful? Yes | No

    Default Re: Elapsed Timer Demo

    Henrik I see what you are doing but wouldnt this create an issue with the higher bits? For instance if I am understanding correctly, the higher bits are always at 1, which would indicate a high, which in the program would stop and reset the timer. If I were to make them 0 in the loop, they would never change to 1 when the switch was closed. Switches are on bits 4, 5, 6 and 7.

  32. #112
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default Re: Elapsed Timer Demo

    Hi,
    No, the high bits are not always 'at 1'.
    When doing a bitwise AND both "inputs" must be true for the "output" to be true. Thus Temp = PortA & %11111100 will do a 'copy' of PortA except the two LSB's will be forced to zero. All the other bits will be left as they are when PortA is read.

    Besides, it doesn't really matter what you write to the port/pins which are configured as inputs. If, for example, PortA.5 is an input and you write '1' to it it will still read the actual state that whatever is connected to it drives it to - which isn't neccesarily '1'. If you look at the schematic for the I/O pins you'll see that the output driver for the pin is disabled when TRIS is set (ie. pin is made an input). If it wasn't it would short out whatever drives the pin to either Vdd or Vss.

    /Henrik.

Similar Threads

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

Members who have read this thread : 4

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