Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
12/27/04 16:44
Read: times


 
#83945 - Accuracy, thats the question
Responding to: ???'s previous message
At first you need to decide the needed accuracy !

Often a timer interrupt must not be very accurate, because no human can detect a deviation of only several microseconds.

And for further counting of real time it is easy to correct the deviation by fractional arithmetic.

Then you can only reload the high byte and thus you must not correct the time to enter the interrupt handler.

You must only watch, that no other interrupt of the same or higher priority can delay it for more than 256 cycle and thus TH0 was already increased prior it was reloaded.

The example was calculated with a 12MHz crystal.
For more accuracy further correction every minute may be added.

Following the example:
#pragma cd pl(9999)
#include <reg51.h>

typedef unsigned char u8;

//#define	XTAL	12e6
#define	XTAL	11.0592e6

#define TH0_RELOAD	(XTAL / 12 / 256 * 50e-3)
#define TH0_REMAIN	((TH0_RELOAD - (u8)TH0_RELOAD) * 20 + 0.5)

bit one_second;
u8 count_50ms;


void t0_init( void )
{
  TMOD = 1;	// T0: Mode 1 (16 Bit)
  TR0 = 1;
  ET0 = 1;
}


void t0_int( void ) interrupt INT_T0
{
  TH0 = -TH0_RELOAD;

  if( count_50ms <= (u8)TH0_REMAIN )	// handle remainder
    TH0--;

  if( --count_50ms )
    return;

  count_50ms = 20;		// 1sec = 20 * 50msec
  one_second = 1;
}


void main( void )
{
  t0_init();
  EA = 1;
  for(;;){
    if( one_second ){
      one_second = 0;
      P2++;
    }
  }
}


#if 0

at 12MHz:

~50msec: 49.92 msec
~1sec:   0.999936 sec (5sec/day)

#endif


Peter


List of 11 messages in thread
TopicAuthorDate
Interrupt latency calculation            01/01/70 00:00      
   Constant = variable            01/01/70 00:00      
      Yes that's correct            01/01/70 00:00      
         16-bit auto-reload            01/01/70 00:00      
   Performance Analyzer            01/01/70 00:00      
   Accuracy, thats the question            01/01/70 00:00      
      accuracy            01/01/70 00:00      
   A bad order            01/01/70 00:00      
   another alternative            01/01/70 00:00      
      correction Timer2 not Timer1            01/01/70 00:00      
         Timer2            01/01/70 00:00      

Back to Subject List