
/******************** (C) COPYRIGHT 2021 SONiX *******************************
* COMPANY:	SONiX
* DATE:		2021/12/29
* IC:			SN8F5762
*____________________________________________________________________________
* REVISION		Date				Author		Description
* 1.0					2021/12/29	SONiX			1. First release
*____________________________________________________________________________
*****************************************************************************/

/*_____ I N C L U D E S _________________________________*/
#include <SN8F5762.h>
#include "timer.h"
/*_____ F U N C T I O N S _______________________________*/
void TIMER2_Initialization(void);
void GPIO_Initialization(void);

/*_____ D E F I N I T I O N S ___________________________*/


/*_____ D E C L A R A T I O N S _________________________*/



/*_____ M A C R O S _____________________________________*/

#define    IO_PORT0_OUTPUT_LOW        P0M |= 0xFF;    P0 &= 0x00;
#define    IO_PORT1_OUTPUT_LOW        P1M |= 0xFF;    P1 &= 0x00;
#define    IO_PORT2_OUTPUT_LOW        P2M |= 0xFF;    P2 &= 0x00;

/**
 *	@Brief		main: Timer2 Demo Function
 *	@param		None
 *	@return		None
 */

void main(void)
{
	/*
   *	System initial:
	 *	(1) GPIO
	 *	(2) Timer2
	 *  
	 */	
		GPIO_Initialization(); 												/*	(1)	*/ 
    TIMER2_Initialization(); 											/*	(2)	*/
		while(1)
		{
			WDTR = 0x5A;																/*	Clear Watchdog Timer	*/	
			CPTM |= mskCPTST_START;

		}
}

/**
 *	@Brief		TIMER1_ExecuteTimerInit: Timer1 Initial Function
 *	@param		None
 *	@return		None
 */

void TIMER2_Initialization(void)
{  
	/* Timer 2 _Initial */
	 T2CH = 0x00;
   T2CL = 0x00;

	
	/*	Low Speed Capture Mode Initial	*/
	 Capture_Timer_Input;
	 CPTS |= mskCPTS_T2CC0;                                             //Enable T2CC0/P0.3
   CPTM |= mskCPTST_CYCLE;                                        
	 
	 T2M |= mskT2CKS_FHOSC | mskT2RATE_1 | mskT2CKS_T2COV ;             //Enable T2 Timer, Fhosc/n, clear T2COV	
	 
	/*	Interrupt	*/
	 IRCON2 &= mskT2_TF2;                                               //Clear Timer 2 interrupt request 
	 EAL =	1;
 	 IEN2 |= mskIEN2_ET2;                                               //Enable Timer2 interrupt

	/*	Start to count	*/
	 CPTM |= mskCPTEN_EN;
	 T2M |= mskT2EN; 
}

/**
 *  @Brief    initial GPIO status
 *  @param    None
 *  @return   None
 */
void GPIO_Initialization(void)
{
    IO_PORT0_OUTPUT_LOW
    IO_PORT1_OUTPUT_LOW
    IO_PORT2_OUTPUT_LOW      
}

/**
 *	@Brief		ISR_Timer1: Timer1 ISR Function
 *	@param		None
 *	@return		None
 */
void ISR_Timer2(void) interrupt ISRTimer2  
{	
	T2CH = 0x00;
  T2CL = 0x00;
//  P00 = 1;	//TEST IO	
//  P00 = 0;	//TEST IO	
  IRCON2 &= mskT2_TF2; 	
}
