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

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
12/19/04 11:01
Modified:
  12/19/04 11:05

Read: times


 
#83450 - Code for accurate 1 sec
Responding to: ???'s previous message
Hi Shahzad,

I am sure by now you would have seen the code samples for using Timer2 in the Keil examples. Getting the Timer 2 interrupt going is no big deal if you initialize it properly and remember to clear the TF2 flag inside of that ISR when running in the 16bit auto reload mode!

Anyway you were worried about accuracy of the interrupt - yes that IS a problem and is called the interrupt latency - the actual time that elapses between the interrupt request and the MCU vectoring to the ISR.

This forum has had many such discussions - search with the term Interrupt Latency. Just to kick start, I am producing below a code that had been written by a member of this Forum - Mr Peter Danneger. ( The website link : /~danni/appl/soft/c51/timer/t0_int2.c51: returns "Not Found" error. Hence pasting the code from my hard drive )
/************************************************************************/
/*                                                                      */
/*			High Precise Time Base				*/
/*                                                                      */
/*              Author: Peter Dannegger                                 */
/*                      danni@specs.de                                  */
/*                                                                      */
/************************************************************************/
#pragma cd pl(30000)


#define uchar	unsigned char
#define uint	unsigned int

#include <reg51.h>

/************************************************************************/
/*									*/
/*	Insert your crystal frequency and you get exact 1 second	*/
/*	without need understanding why :				*/
/*									*/
/**/	#define XTAL		12345701L			      /**/
/*									*/
/************************************************************************/

#define T0_RELOAD	((uint) (XTAL / 12.0 / 256.0 + 0.5))

#define T0_RELOAD_1S	((uint) (XTAL / 12.0 - 255.0 * T0_RELOAD + 0.5))

#define T0_STOP_CYCLES	16			// calculated with list file

bit F_1second = 0;

union bw{
  int w;
  struct{
    char h;
    char l;
  }b;
};


void t0_int( void ) interrupt 1
{
  static uchar prescaler = 0;
  union bw i;
  if( --prescaler == 0 ){
    EA = 0;
    TR0 = 0;
    i.b.l = TL0;
    i.b.h = TH0;
    i.w += -T0_RELOAD_1S + T0_STOP_CYCLES;	//   1 * every second
    TL0 = i.b.l;
    TH0 = i.b.h;
    EA = 1;
    TR0 = 1;
    F_1second = 1;
  }else{
    EA = 0;
    TR0 = 0;
    i.b.l = TL0;
    i.b.h = TH0;
    i.w += -T0_RELOAD + T0_STOP_CYCLES;		// 255 * every second
    TL0 = i.b.l;
    TH0 = i.b.h;
    EA = 1;
    TR0 = 1;
  }
}



I personally have not used this code - but have just gone through it. As is common with any such posted code, PLEASE understand it fully before putting it to use.

Hope that helps.

Raghu



List of 8 messages in thread
TopicAuthorDate
Timer interrupt in KEIL            01/01/70 00:00      
   RTFM!            01/01/70 00:00      
      ... and the Tutorials            01/01/70 00:00      
   two ways            01/01/70 00:00      
      A Third Way            01/01/70 00:00      
   Code for accurate 1 sec            01/01/70 00:00      
      Re Raghunathan            01/01/70 00:00      
         Thank Peter...            01/01/70 00:00      

Back to Subject List