Elapsed Timer Demo


Closed Thread
Results 1 to 40 of 112

Hybrid View

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


    Did you find this post helpful? Yes | No

    Default

    Is there a reason you subtract 100 instead of zeroing Ticks?
    Not really. The original program used ON INTERRUPT, and there was a possibility that Ticks might be higher than 100 by the time it got around to calculating the time. But when I switched it over to ASM interrupts, it wasn't needed anymore. Just never changed it.

    And for the ms100 routine, I can't figure out what you're trying to do there.

    If tmr = 80 then (ms100 - tmr >= 50) will be True when ms100 is between 0-79. So the output will be toggled on every loop. Then for 80-99 the (ms100 < tmr) will never evaluate True since it's always >= 80.

    Maybe if you explained what you're trying to do, we can come up with something that'll work.
    <br>
    DT

  2. #2
    Bob_W's Avatar
    Bob_W Guest


    Did you find this post helpful? Yes | No

    Default

    Basically, I want to turn something on - like an LED (later it will actually be a 7 HP siren with a longer on and off time) for 500 ms. I go through all the decision process to decide it should be turned on. Then....

    I save the current ticks value - tmr - # of ticks at start, which is 16 for this grab.

    I turn on the port - for the test program, I'm just toggling an LED at 500 ms intervals.

    I come out of the main program loop every pass to see if I have expired my timer. Get current ticks - ms100 - we'll say I get 65 for this grab.

    Sooo... IF ms100 - tmr >= 50 THEN 65 - 16 = 49.... almost there..

    Where you run into the problem is if you grab, say 80 for tmr.

    For 500 ms to elapse, you'd have to roll over the 100 mark. So, if you try to do the test IF ms100 - tmr >= 50 THEN with tmr = 80, and ms100 = 10 it evaluates true because, under pic math 10 - 80 = 250, or 65391 if you look at all 16 bits. So I figured out that you have to test to see if ms100 (the current tick value) is less than tmr (the tick value when you started timing) and if so, add 100 to the ms100 before subtracting tmr to see if it's 50 or better elapsed.

    Sure you do timing with pause, but you can't look at change of state while you wait for pause to expire. Your timing routine makes it possible watch inputs, and manipulate and time multiple outputs at the same time.

    I used to run into this when doing timing in vbdos. Seconds was easy - if you wound up with a negative, just add 86400.

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


    Did you find this post helpful? Yes | No

    Default

    Well, I was hoping for more of what the program needs to do, instead of why it doesn't work the way you have it. But if I make a few assumptions I guess we can go from there.

    Assumption 1 - The only thing the elapsed timer will be used for, is timing the 500ms periods.

    Assumption 2 - The on/off cycle times are the same. 500ms ON, 500ms OFF. And repeats continuously untill it's determined that it needs to stop.

    I believe the key here is in Starting the Elapsed Timer at the beginning of the alarm cycle so that it always starts at 0. That way you don't have to subtract the starting Ticks count from the current Ticks count. Al you'll need to do is test if it's less than 50.

    Something like this...
    Code:
    Clear
    
    Include "Elapsed.pbp"
    
    AlarmOUT VAR PORTB.0
    AlarmON  VAR BIT
    
    ;----------------------------
    MainLoop:
    ; Somewhere in here determine when to turn on/off alarm
    ; and Gosub to either StartAlarm or StopAlarm
    
        if AlarmON and (Ticks < 50) then
           HIGH AlarmOUT       ' Alarm ON  
        else
           LOW  ALARMOUT       ' Alarm OFF
        endif
    Goto Mainloop
    
    ;----------------------------
    StartAlarm:
        Gosub ResetTime    ' Reset Time to  0d-00:00:00.00    
        Gosub StartTimer   ' Start the Elapsed Timer
        AlarmON = 1
    Return
    
    ;----------------------------
    StopAlarm:
        Gosub StopTimer
        AlarmON = 0
    Return
    HTH
    <br>
    DT

  4. #4
    Bob_W's Avatar
    Bob_W Guest


    Did you find this post helpful? Yes | No

    Default

    The 500 ms on, 500 ms off is just an LED for testing your code. The beauty of your timer routine is that you can time multiple things at the same time......

    Example...
    Siren is running in first of four blasts - timing the on and off interval... WHILE...

    Timing to see it someone is holding down the stop button more than 2 seconds.... WHILE timing to see how long it's been since activation, so we refuse to activate again if it's been less than, say , 5 minutes, so if the tones are sent over the air again, we don't drive the neighbors nuts.... ETC..


    And, that's just this project.


    Thanks.....

  5. #5
    jessey's Avatar
    jessey Guest


    Did you find this post helpful? Yes | No

    Default

    Hi Darrel

    I am trying to use your Elapsed Timer in a thermostat program using a DS1820 chip. My thermostat code compares a user set value to the temperature reading which turns a relay on/off accordingly. I'd like to be able to use your timer as a stop watch to record the total time that the heater is on for. I was really surprised and also delighted to see that your elapsed timer code only uses 139 words! My code by itself seems to work flawlessly but when I include your elapsed code then the temperature isn't consistent, as in, every so often it toggles the heater relay off then back on again ever so briefly.

    I'm thinking that the interrupt is giving me a false temperature reading by corrupting the read. Could you suggest anything I could try to alleviate this? Also, I noticed that you use DEC2 in your LCDOUT command for your timer code, what does the DEC2 command relate to? I've seen that being used in some code before but I don't understand what it's used for. Could you or someone else here explain why and when you'd use the DEC2 or 3 or 4 commands? I've included my thermostat code so you can see how I have your timer set up.

    Thanks
    jessey
    Attached Files Attached Files
    Last edited by jessey; - 4th November 2005 at 11:25.

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


    Did you find this post helpful? Yes | No

    Default

    Hiya Jessey,

    That's the only bad part about interrupts. They Interrupt things.

    And when you have something with very strict timing requirement such as 1-Wire devices, they just don't go together very good.

    Probably the easiest way to remedy the situation is to just turn-off the interrupts when reading the sensor. This may cause a small amount of error to accumulate in the Timer routine, but if you keep the Temp readings to a minimum, it shouldn't even be noticible, maybe add a second a month or so.

    Other than that, using a syncronous device like I2C or SPI will eliminate the problem all together.

    HTH,
    &nbsp;&nbsp;Darrel
    Last edited by Darrel Taylor; - 4th November 2005 at 21:11.

  7. #7
    jessey's Avatar
    jessey Guest


    Did you find this post helpful? Yes | No

    Default Having trouble turning the asm interrupt off/on

    Hi Darrel,

    Well I figured out what the DEC2 command is for, it adds an extra 0 to the display which disappears when the variable is > 9, so I'm guessing it's just added to give the Lcd that digital clock look, which is cool. Now if I could just figure out how to turn off/on your interrupts? I thought it would be as simple as adding PIE1.0 = 0:INTCON.6 = 0:INTCON.7 = 0, for overflow, peripheral and global interrupts just before reading the DS1820 then after the read setting them back to 1 but with no success. When I do that it takes about 10 seconds for the clock to increment?

    Then I implemented a counter in the mainloop and added an IF...THEN to only read the temperature about once every second or so as you suggested and that just wrecks havoc with the temperature reading, making it fluctuate wildly. I tried to make sense of the asm interrupts by reading the manual and searching the archives but to no avail. This experience really has me scratching my head. One thing that's for sure, is that using interrupts can be a scary thing and are not for the faint of heart! How would I go about turning the interrupts off and then back on again? Any further help would be greatly appreciated.

    Thanks
    jessey

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