//-----------------------------------------------------------------------------
// TITLE	:timer related code
// REVISION	:00.01
// AUTHOR	:Chengfj
// COMPANY	:Radioway Co.,Ltd..
// DATE		:2016/05/23
// CHIP		:FM8PE531

//Include files Segment-----------------------------------------
#include	<8PE531A.H>
#include    "typesdefine.h"
#include    "timer.h"

//Global or Other define segment--------------------------------
unsigned int	TMR1 @ TMR1LB_BASE;

//Global or Other define segment---------------------------------
//unsigned char	RG_TM_1SCNT=0x00;
//unsigned int	RG_TMRCOUNT=0x0000;
  signed int    gTimer[MAX_TIMER];
byte rf_reset_cnt;
byte rf_sleep_cnt;
	
//Vaule segment--------------------------------------------------
//#define	EQ_TMR_DL1mS	0xFE0B  //4M/8=500K, (0xffff-0xfe0b)/500K=1ms
#define	EQ_TMR_DL1mS	0xFC17  //4M/8=500K, (0xffff-0xfc17)/500K=2ms
void interrupt HW_isr(void) @ HWINT_BASE
{
	T1CONbits.T1EN=0;
	TMR1=EQ_TMR_DL1mS;			//Reload timer count value
	T1CONbits.T1EN=1;

	//RG_TMRCOUNT++;
	//if(RG_TMRCOUNT==0x03EB)
	//{
	//	RG_TM_1SCNT++;
	//	RG_TMRCOUNT=0x0000;
	//}    
 	 if(rf_reset_cnt) rf_reset_cnt--;
	 if(rf_sleep_cnt) rf_sleep_cnt--;  	
	if(gTimer[0] > 0)
	    gTimer[0]--;									
//	if(gTimer[1] > 0)
//	    gTimer[1]--;
									
/*
	if(gTimer[2] > 0)
	    gTimer[2]--;									
	if(gTimer[3] > 0)
	    gTimer[3]--;									
*/
	INTFLAG=0xFE;				//Clear T1IF Flag(Not recommed INTFLAGbits.T1IF=0;)
}


void timer1init(void)
{
	T1CON=0x18;				    //Prescal=1:8, Clock source=Instruction clock
	TMR1=EQ_TMR_DL1mS;			//Set timer count 1ms

	gTimer[0] = -1;
	gTimer[1] = -1;
	//gTimer[2] = -1;
	//gTimer[3] = -1;

}
//*********************************************************************
//* SetUserTimer
//*********************************************************************/
/*
void SetUserTimer(void)
{
	U8 i;
	for(i=0; i<MAX_TIMER; i++)								//timer 1~3 dount down
	{
		if(gTimer[i] > 0)
			gTimer[i]--;									
	}
}*/

//
//Function: 		void	SetTimer(Uint8 TimeCnt,Uint16 TimerN)
//Description:		Set timer0,timer1,timer2 count value	
//Input:			1.TimeCnt :time count value(Delay time=1ms*TimeCnt)
//					2.TimerN :timer0,time1 or time2(=0,1,2)					
/*
void	SetTimer0(U16 TimeCnt)
{
	gTimer[TimerN] = TimeCnt;									//set timer count value
}

void	SetTimer1(U16 TimeCnt)
{
	gTimer[TimerN] = TimeCnt;									//set timer count value
}
*/
//
//Function: 			Uint8	CheckTimer(Uint8 TimerN)
//Description:		check timer0,timer1,timer2 time out or not
//Input:					TimerN :timer0,time1 or time2(=0,1,2)					
//Output:			
//Return:				1 :time out, 0 :time ont out					
//
U8 CheckTimer0(void)
{
	if(gTimer[0] == 0)
	{
		gTimer[0] = -1;
		return 1;
	}
	return 0;
}

U8 CheckTimer1(void)
{
	if(gTimer[1] == 0)
	{
		gTimer[1] = -1;
		return 1;
	}
	return 0;
}

//
//Function: 			void StopTimer(Uint8 TimerN)
//Description:		stop timer0,timer1,timer2 
//Input:         TimerN :timer0,time1 or time2(=0,1,2)
//
/*
void StopTimer(U8 TimerN)
{
	gTimer[TimerN] = -1;
}
*/

