This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

real time clock calculation

Hai,
i am using lpc2368 microcontroller in my embeddd project.
i am displaying the clock in lcd also using RTC calculation.
i am stuck with a section in which we have to find the clock duration between
end time and running time.

the end time is obtained by transmitting it serially.
the running time is the current rtc time and i am getting it

so please help me in this section
i am attaching the running time section program

unsigned char hh[2],mm[2],ss[2];
hh[0]=(RTC_HOUR)/10+0x30;
hh[1]=(RTC_HOUR)%10+0x30;
mm[0]=(RTC_MIN)/10+0x30;
mm[1]=(RTC_MIN)%10+0x30;
ss[0]=(RTC_SEC)/10+0x30;
ss[1]=(RTC_SEC)%10+0x30;


the following code of substraction of running time from end time will work numerically

        hh[0]=(bus_time_msg[0]-hh[0])+0x30;
        hh[1]=(bus_time_msg[1]-hh[1])+0x30;
        mm[0]=(bus_time_msg[3]-mm[0])+0x30;
        mm[1]=(bus_time_msg[4]-mm[1])+0x30;
        ss[0]=(bus_time_msg[6]-ss[0])+0x30;
        ss[1]=(bus_time_msg[7]-ss[1])+0x30;


where the bus_time_msg[] is obtained serially

so please help me in this section

  • Depending on what runtime library you make use of, you could convert the two times to unix epoch, i.e. number of seconds since 1970-01-01 00:00. But you need extra code in case the times are in local time and you live in a time zone with daylight savings. On the other hand, you don't show any date information, but haven't told us the full information - what the maximum difference is expected to be between the two times. If the span can represent times from different days - or if the span can be more than 24 hours.

    Your code for subtracting the individual digits are too simple - it doesn't take care of underflow. It doesn't matter which of the two times that is larger. The individual components of the time can still sometimes be larger and sometimes smaller. Another thing is that your code only seems to care about a clock value. What if there have been one or more clock turnarounds, so the two times doesn't represent the same day?

    Anyway - if all you need is differences smaller than 24 hours, then it is trivial to convert the two times into seconds. If the "newer" time happens to be smaller, because of overflow, then just add 24*3600 to it, before subtracting the other time.

    So in the end - exactly where are you stuck?

  • RTC calculates dates and time also.
    Run time is from the RTC and End time time is obtained serially.
    for eg:if run time is 06/08/2012 10:10:10 and end time is 07/08/2012 04:10:10
    around 18 hrs.
    so i want to calculate this duration in my application

  • So, didn't my answer help? The only thing it didn't cover was what to do if you have dates and are using local times with daylight savings.

    But you can you also calculate dates, when your source code so clearly only contains hours, minutes and seconds?