
/******************** (C) COPYRIGHT 2021 SONiX *******************************
* COMPANY:	SONiX
* DATE:		2021/12/29
* IC:			SN8F5762
*____________________________________________________________________________
* REVISION		Date				Author		Description
* 1.0					2021/12/29	Roger			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 TIMER0_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;
#define    IO_PORT3_OUTPUT_LOW        P3M |= 0xFF;    P3 &= 0x00;

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

void main(void)
{
	/*
   *	System initial:
	 *	(1) GPIO
	 *	(2) Timer0
	 *  
	 */
		GPIO_Initialization();        																	/*	(1)	*/
		TIMER0_Initialization();        																/*	(2)	*/


		while(1)
		{
			WDTR = 0x5A;																									/*	Clear Watchdog Timer	*/
		}
}

/**
 *	@Brief		TIMER0_ExecuteTimerInit: Timer0 Demo Function
 *	@param		None
 *	@return		None
 */


void TIMER0_Initialization(void)
{
	/*
	 *	(1) Set Timer0 Mode3 (2-set of 8-bit timer) And IHRC As Timer0 Source
	 *	(2) Set	TL0 Overflow Value
	 *	(3)	Set	TH0 Overflow Value
	 *	  
	 */		
		TMOD |= (mskTMOD_T0_MODE3|mskTMOD_T0CT);												/*	(1)	*/
		TH0 = TH0_VALUE;            																		/*	(2)	*/
    TL0 = TH0_VALUE;            																		/*	(3)	*/	
		TCON0 |=(mskTCON0_T0_DIV128|mskTCON0_T1_DIV128);  								
	  IEN0 |= (mskIEN0_ET0|mskIEN0_ET1);  														
		EAL =	1;																												
		TCON |=	(mskTCON_TR0|mskTCON_TR1);															

}

/**
 *  @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_Timer0: Timer0 ISR Function
 *	@param		None
 *	@return		None
 */
void ISR_Timer0(void) interrupt ISRTimer0	 using 2
{
	/*
	 *	(1)	Timer0 ISR Test Pin	Toggle
	 *	(2) Re-load Timer Counter	
	 *
	 */	
		OB_T0TEST_PIN ^=  1;																							/*	(1)	*/ 
		TCON &=	TIMER0_STOP;																							
		TL0 = TL0_VALUE;																									/*	(2)	*/
		TCON |=	(mskTCON_TR0);																					
}

/**
 *	@Brief		Timer1_ISR: Timer1 ISR Function
 *	@param		None
 *	@return		None
 */

void ISR_Timer1(void) interrupt ISRTimer1	using 3
{
	/*
	 *	(1)	Timer1 ISR Test Pin	Toggle
	 *	(2) Re-load Timer Counter	

	 *
	 */		
		OB_T1TEST_PIN	^=	1;																							/*	(1)	*/ 
		TCON &=	TIMER1_STOP;																						
		TH0 = TH0_VALUE;																									/*	(2)	*/
		TCON |=	(mskTCON_TR1);																					
}