/*
******************************************************************************************************
 @Module        Timer 
 @Filename     Timer.c
 @Project        SEM3000
-----------------------------------------------------------------------------------------------------
 @Controller     

 @Compiler       

 @ Frequency   Using  clock 24.5MHz

 @Codegenerator 0.1

 @Description   T

------------------------------------------------------------------------------------------------------
 @Date          2013-03-15 15:15:00

******************************************************************************************************
*/


#define  _TIMER_C_


#include "PROJECT_INCLUDES.H"

/*
********************************************************************************************************
									 @Project Includes
********************************************************************************************************
*/

//#include "PROJECT_INCLUDES.H"

/*
*********************************************************************************************************
 									@Macros
*********************************************************************************************************
*/


/*
*********************************************************************************************************
*                                                             TIMER_0_PRESCALE
*
* 	Description:    //Set 8Bit TIMER0 Count PRESCALE /  When CPU clock 16MHz
*                          These bits control the Timer 0/1 Clock Prescaler:

*	  PRESCALE               PRESCALE / CPU Clock x 256
*
*	PRESCALE 4: 		 	//maxms 1.60   uS
*	PRESCALE 8:			//maxms 128    uS
*	PRESCALE 12:			//maxms 1.024 mS
*	PRESCALE 48:		//maxms 4.096 mS
*
*********************************************************************************************************
*/


// There are SYSCLK/TIMER_PRESCALER timer ticks per second, so
// SYSCLK/TIMER_PRESCALER/1000 timer ticks per millisecond.

#define TIMER_TICKS_PER_MS  SYSCLK/TIMER_0_PRESCALE/1000  //1  1ms


#define AUX1     TIMER_TICKS_PER_MS
#define AUX2     -AUX1
#define AUX3     AUX2&0x00FF
#define AUX4     ((AUX2&0xFF00)>>8)

#define TIMER0_RELOAD_HIGH       AUX4  // Reload value for Timer0 high byte
#define TIMER0_RELOAD_LOW        AUX3  // Reload value for Timer0 low byte

#if   TIMER_0_PRESCALE==4
		#define TIMER_0_PRESCALE_VALUE	0x01
	
#elif TIMER_0_PRESCALE==8
		#define TIMER_0_PRESCALE_VALUE	0x03
		
#elif TIMER_0_PRESCALE==12
		#define TIMER_0_PRESCALE_VALUE	0x00
		
#elif TIMER_0_PRESCALE==48
		#define TIMER_0_PRESCALE_VALUE	0x02
#endif


#if   TIMER_3_PRESCALE==1
	#define TIMER_3_PRESCALE_VALUE	0x01	
#elif  TIMER_3_PRESCALE==8
	#define TIMER_3_PRESCALE_VALUE	0x02	
#elif  TIMER_3_PRESCALE==64
	#define TIMER_3_PRESCALE_VALUE	0x03	
#elif  TIMER_3_PRESCALE==256
	#define TIMER_3_PRESCALE_VALUE	0x04
#elif  TIMER_3_PRESCALE==1024
	#define TIMER_3_PRESCALE_VALUE	0x05	
#endif


/*
*********************************************************************************************************
									@Global Variables
*********************************************************************************************************
*/

static UBYTE Time0_Cnt_Value;	//1 Timer Overflow 발생 이후 타이머를 제 설정 하기  Time 값을 보관 하는 변수 

/*
*********************************************************************************************************
 									@TIMER Intertupt_Service Function
*********************************************************************************************************
*/

void  (*pTimer0_Intertupt_Service)(void);	//Timer0 interrupt service function


/*
*********************************************************************************************************
*                                                             INITIALIZE THE TIMER0
*
* Description:    TIMER0 Init
*
* Arguments  :  Uart_Channel,  Baud_Rate,  CPU_Clock
*
* Returns      :   NONE

*********************************************************************************************************
*/


void Timer0_Init( void (*a_pInterrupt_Service)() )  //,  U16  Interrupt_Event_Ratio_mS )
{
	pTimer0_Intertupt_Service = a_pInterrupt_Service;  //1 Set Timer0 interrupt service function

	TMOD |= 0x01;						//1 Timer0 in two 8-bit mode
										/*
										Bit 		7 	6	       5  4       3      2           1 0
										Name GATE1 C/T1 T1M[1:0] GATE0 C/T0 T0M[1:0]	
										
										Bit 	Name 
										7 	GATE1 Timer 1 Gate Control.
												0: Timer 1 enabled when TR1 = 1 irrespective of INT1 logic level.
												1: Timer 1 enabled only when TR1 = 1 AND INT1 is active as defined by bit IN1PL in register IT01CF (see SFR Definition 12.7).
										6 	C/T1 Counter/Timer 1 Select.
												0: Timer: Timer 1 incremented by clock defined by T1M bit in register CKCON.
												1: Counter: Timer 1 incremented by high-to-low transitions on external pin (T1).
										5:4 	T1M[1:0] Timer 1 Mode Select.
												These bits select the Timer 1 operation mode.
												00: Mode 0, 13-bit Counter/Timer
												01: Mode 1, 16-bit Counter/Timer
												10: Mode 2, 8-bit Counter/Timer with Auto-Reload
												11: Mode 3, Timer 1 Inactive
										3 	GATE0 Timer 0 Gate Control.
												0: Timer 0 enabled when TR0 = 1 irrespective of INT0 logic level.
												1: Timer 0 enabled only when TR0 = 1 AND INT0 is active as defined by bit IN0PL in register IT01CF (see SFR Definition 12.7).
										2   C/T0 Counter/Timer 0 Select.
										              0: Timer: Timer 0 incremented by clock defined by T0M bit in register CKCON.
												1: Counter: Timer 0 incremented by high-to-low transitions on external pin (T0).
										1:0 T0M[1:0] Timer 0 Mode Select.
												These bits select the Timer 0 operation mode.
											00: Mode 0, 13-bit Counter/Timer
											01: Mode 1, 16-bit Counter/Timer
											10: Mode 2, 8-bit Counter/Timer with Auto-Reload
											11: Mode 3, Two 8-bit Counter/Timers
										*/
										
	
	TH0 = TIMER0_RELOAD_HIGH;			//1 Init Timer0 High register
	TL0 = TIMER0_RELOAD_LOW ;			//1 Init Timer0 Low register

	CKCON |= TIMER_0_PRESCALE_VALUE;		//1 Timer0 uses a 1:48 prescaler
										/*
										Bit 	Name Function
										7 	T3MH Timer 3 High Byte Clock Select.
												Selects the clock supplied to the Timer 3 high byte (split 8-bit timer mode only).
												0: Timer 3 high byte uses the clock defined by the T3XCLK bit in TMR3CN.
												1: Timer 3 high byte uses the system clock.
										6 	T3ML Timer 3 Low Byte Clock Select.
												Selects the clock supplied to Timer 3. Selects the clock supplied to the lower 8-bit timer
												in split 8-bit timer mode.
												0: Timer 3 low byte uses the clock defined by the T3XCLK bit in TMR3CN.
												1: Timer 3 low byte uses the system clock.

												
										5 	T2MH Timer 2 High Byte Clock Select.
												Selects the clock supplied to the Timer 2 high byte (split 8-bit timer mode only).
												0: Timer 2 high byte uses the clock defined by the T2XCLK bit in TMR2CN.
												1: Timer 2 high byte uses the system clock.
												
										4 T2ML Timer 2 Low Byte Clock Select.
												Selects the clock supplied to Timer 2. If Timer 2 is configured in split 8-bit timer mode,
												this bit selects the clock supplied to the lower 8-bit timer.
												0: Timer 2 low byte uses the clock defined by the T2XCLK bit in TMR2CN.
												1: Timer 2 low byte uses the system clock.
												
										3 T1M Timer 1 Clock Select.										
												Selects the clock source supplied to Timer 1. Ignored when C/T1 is set to 1.
												0: Timer 1 uses the clock defined by the prescale bits SCA[1:0].
												1: Timer 1 uses the system clock.

												
										2 T0M Timer 0 Clock Select.
												Selects the clock source supplied to Timer 0. Ignored when C/T0 is set to 1.
												0: Counter/Timer 0 uses the clock defined by the prescale bits SCA[1:0].
												1: Counter/Timer 0 uses the system clock.
										1:0 SCA[1:0] Timer 0/1 Prescale Bits.
												These bits control the Timer 0/1 Clock Prescaler:
												00: System clock divided by 12
												01: System clock divided by 4
												10: System clock divided by 48
												11: External clock divided by 8 (synchronized with the system clock) */                                       
			
	
	ET0=1;								//1 Timer0 interrupt enabled
	TCON |= 0x10;						//1 Timer0 ON
	
}


//-----------------------------------------------------------------------------
// Timer2_Init
//-----------------------------------------------------------------------------
//
// Return Value:  None
// Parameters:    None
//
// Configure Timer2 to 16-bit auto-reload and generate an interrupt at 390uS 
// intervals. A Timer 2 overflow automatically triggers an ADC0 conversion.
// 
//-----------------------------------------------------------------------------
#define SAMPLERATE  		9500               // 1500  ADC word rate in Hz  9500
void Timer2_Init (void)
{   

   TMR2CN  = 0x00;                     // Stop Timer2; Clear TF2;
                                       // Use SYSCLK as timebase, 16-bit 
                                       //  auto-reload
   CKCON  |= 0x10;                     // Select SYSCLK for timer 2 source
   TMR2RL  = 65535 - (SYSCLK / 2560);  // Init reload value 
   TMR2    = 0xffff;                   // Set to reload immediately
   TR2     = 1;                        // Start Timer2
}

void Timer2_Run(void)
{

//	TR2     = 1;                              // start Timer2
}


void Timer2_Stop(void) 
{
//	TR2     = 0;                              // start Timer2
}




//-----------------------------------------------------------------------------
// Interrupt Service Routines
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// Timer0_ISR
//-----------------------------------------------------------------------------

INTERRUPT (TIMER0_ISR, INTERRUPT_TIMER0)  //1  1ms Timer
{
   
 //  BEEP_OUT=!BEEP_OUT;

   	(*pTimer0_Intertupt_Service)();

	TH0 = TIMER0_RELOAD_HIGH;			// Init Timer0 High register  1ms
	TL0 = TIMER0_RELOAD_LOW ;			// Init Timer0 Low register
}

//-----------------------------------------------------------------------------
// Timer1_ISR
//-----------------------------------------------------------------------------
//
// Here we process the Timer1 interrupt and toggle the Red LED
// Note: In this mode the Timer0 is effectively using the Timer1 interrupt
// vector. For more info check the Timers chapter of the datasheet.
//-----------------------------------------------------------------------------

INTERRUPT (TIMER1_ISR, INTERRUPT_TIMER1)
{

}



/*
ISR(TIMER3_COMPA_vect)
{
	 (*pTimer3_Intertupt_Service)();
}


*/




