An Example Using A Master Roll Over Timer To Get Time Since Last Event


//----- DO IN 1mS HEARTBEAT TIMER -----
main_1ms_clock_timer_irq++;


//----- USING THE TIMER -----
unsigned long main_1ms_clock_timer_irq;
unsigned long main_1ms_clock_timer;
unsigned long process_last_time;
unsigned long process_1ms_time_since_last;


  //Create a local copy of the timer (which won't change in the interrupt when being used - if no interrupt then no need to use)
  main_1ms_clock_timer = main_1ms_clock_timer_irq;
  while (main_1ms_clock_timer != main_1ms_clock_timer_irq)    //Use sizeof(main_1ms_clock_timer) != the processor native bit length
    main_1ms_clock_timer = main_1ms_clock_timer_irq;
  

  //Start timing
  process_last_time = main_1ms_clock_timer;


  //Time since start
  process_1ms_time_since_last = (main_1ms_clock_timer - process_last_time);    //Time in mS since last time point with roll over of the timer inherantly compensated for

 

 

USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.