Time measurement in seconds

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderator: RoccoMarco

Post Reply
kab
Posts: 70
Joined: Sun Sep 16, 2012 6:18 pm

Time measurement in seconds

Post by kab »

Hello!

I'm totally miss how to measure time in seconds.
Task: store time for each event and send (upon request) time in seconds for each event - how old it happen.

Code:

Code: Select all

//! time variable
static uint32_t last_time_in_seconds=0;

//! update timer function
static void UpdateTime(void)
{
   static systime_t last_time_in_ticks=0;
   systime_t ticks;

   //! Update our time
   ticks = chVTGetSystemTimeX();

   if (ticks > last_time_in_ticks)
   {
      //! no overflow, just add delta
      last_time_in_seconds += ST2S(ticks - last_time_in_ticks);
   }
   else
   {
      //! overflow happen
      last_time_in_seconds += ST2S(0xffffffff - last_time_in_ticks + ticks);
   }

   last_time_in_ticks = ticks;
}



This code is called for each happen event (to update time) and then global var last_time_in_sec stored in structure for each event.

Code: Select all

UpdateTIme();
event->time=last_time_in_seconds;


Problem - last_time_in_seconds are very large.

Code: Select all

chprintf(chp, "Time: %d\r\n", last_time_in_seconds - (event->time));


Few seconds after system start i got values more than 1000000.
What i do wrong?
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Time measurement in seconds

Post by Giovanni »

Not sure about that code but you could just increase a variable every second using a virtual timer or a thread.

Giovanni
kab
Posts: 70
Joined: Sun Sep 16, 2012 6:18 pm

Re: Time measurement in seconds

Post by kab »

Separate thread with inc to variable and 1 second delay working fine and pretty accurate.
Thanks!
Post Reply