News:

;) This forum is the property of Proton software developers

Main Menu

Simple elapsed timer in hours, minutes and seconds

Started by RGV250, Sep 23, 2025, 07:38 PM

Previous topic - Next topic

RGV250

Hi,
I needed to see how long the program had been running, normally I have had a series of counters if > 59 then etc for hours minutes and seconds. It worked but always seemed messy. I think I have come up with a really neat solution now.

    Include "Amicus18.inc"
       
    Dim SecondsCount As Dword
    SecondsCount = 0
   
    Dim Hours As Word
    Dim Minutes As Byte
    Dim Seconds As Byte

       
Do
    HMS(SecondsCount)  ' Extract Hours, Minutes & Seconds
' Display total seconds, Hours, Minutes & Seconds.   
    HSerOut ["Elapsed time : ",Dec SecondsCount, " - ",Dec Hours,":",Dec Minutes, ":",Dec Seconds, 13] 
    Inc SecondsCount

    DelayMS 1000

Loop

' Procedure to convert seconds into running time, Hours, Minutes & seconds.
Proc HMS(pParam As Dword)
    Dim Temp As Dword
    Hours = pParam / 3600    ' Calculate the hours
    Temp = pParam // 3600    ' Calculate the remainder
    Minutes = Temp / 60      ' Calculate the minutes
    Seconds = Temp // 60     ' Calculate the remainder for the seconds
EndProc     


OK, it will not be that accuare if there is a lot going on but for my purposes it is ideal.

Bob

JackB

Hello, and thank's for posting this code,
I don't have Amicus, but I guess to use it with a PIC12F683 will be to remove the  Include "Amicus18.inc" line
add my config, and it should run easy?



RGV250

Hi Jack,
It has only been 5 months and I cannot remember why I needed it in the first place.
You are correct, it will run on anything, if you know how long your code takes you could modify the 1000ms delay or use an interrupt to be more precise.

Regards,
Bob

Fanie

If that is all the pic does than you can probably trim the 1000ms delay to give you a more accurate delay using a stopwatch.  50Hz off the mains is still used even today (despite all the power failures off late) and is supposed to be a fairly accurate reference.  If the mains fail (and you run off alternative power) you can still count using your 978ms (trimmed from 1000ms) delay so the timer is not stopping.

Alternative, if you use an external 1 sec external timer, or a RTC chip.  Some have a 1 sec output.
This free the pic up to do other things like driving a display etc without buggering the timing up or complicating the time accuracy.  I used RTC's before and I wasn't impressed with their accuracy, required weekly correction.

If you want to use it for a clock then it's fine, but sometimes there can be other time factors, depending on the application.  For instance if you want to use it for watering plants, then the seasons can make a big difference.  Instead of using time, it is easier then to use a light sensor which adjusts your time to day and night effect.  Since it is not critical, cloudy days can delay your timer's time but will correct itself again.  This timer method auto adjust according to weather conditions.
I have timers on watering my plants, in summer the sun is still shining when they open the water, and in winter it is long after sun down when they water.