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





/*_____ I N C L U D E S _________________________________*/
#define __XRAM_SFR_H__
#include <SN8F5754.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_Initialization: Timer0 Initial Function
 *	@param		None
 *	@return		None
 */

void TIMER0_Initialization(void)
{
	/*
	 *	(1) Set Timer0 Mode0 (13-bit Timer)	And IHRC As Timer Source
	 *	(2) Set	TL0 Overflow Value
	 *	(3)	Set	TH0 Overflow Value
	 *	  
	 */	
		TMOD |= (mskTMOD_T0_MODE0|mskTMOD_T0CT);			/*	(1)	*/
    TL0 = TL0_VALUE;															/*	(2)	*/
		TH0 = TH0_VALUE; 															/*	(3)	*/           	
		TCON0 |= mskTCON0_T0_DIV128; 									
	  IEN0 |= mskIEN0_ET0;  												
		EAL =	1;																			
		TCON |=	mskTCON_TR0;													

}

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


/**
 *	@Brief		isr_timer0: Timer0 ISR Function
 *	@param		None
 *	@return		None
 */
void ISR_Timer0(void) interrupt ISRTimer0 using 3
{
	/*
	 *	(1)	Timer0 ISR Test Pin	Toggle
	 *	(2) Re-load Timer Counter	
	 *
	 */	
		OB_TEST_PIN ^=  1;															/*	(1)	*/ 
		TCON &=	TIMER_STOP;															
		TL0 = TL0_VALUE; 																/*	(2)	*/
		TH0 = TH0_VALUE;																/*	(2)	*/
		TCON |=	(mskTCON_TR0);													

}
