
/*-*------------------------------------------------------------------

   COPYRIGHT
   ---------

   This code is associated with the book:

   EMBEDDED C by Michael J. Pont 
   [Pearson Education, 2002: ISBN: 0-201-79523-X].

   This code is copyright (c) 2001 by Michael J. Pont.
 
   See book for copyright details and other information.

-*------------------------------------------------------------------*/


void sEOS_Init_Timer0(const tByte TICK_MS)
   {
   tLong Inc;
   tWord Reload_16;
  


   // Using Timer 0, 16-bit *** manual reload ***
   TMOD &= 0xF0; // Clear all T0 bits (T1 left unchanged)
   TMOD |= 0x01; // Set required T0 bits (T1 left unchanged) 

   // Number of timer increments required (max 65536)
   Inc = ((tLong)TICK_MS * (OSC_FREQ/1000)) / (tLong)OSC_PER_INST;   

   // 16-bit reload value
   Reload_16 = (tWord) (65536UL - Inc);
   

   // 8-bit reload values (High & Low)
   Reload_08H = (tByte)(Reload_16 / 256);
   Reload_08L = (tByte)(Reload_16 % 256);

   // Used for manually checking timing (in simulator)
   //P2 = Reload_08H;
   //P3 = Reload_08L;

   TL0  = Reload_08L; 
   TH0  = Reload_08H;

   // Timer 0 interrupt is enabled, and ISR will be called 
   // whenever the timer overflows.
   ET0 = 1;

   // Start Timer 0 running
   TR0 = 1;     

   // Globally enable interrupts
   EA = 1;            
   }
