
/******************** (C) COPYRIGHT 2022 SONiX *******************************
* COMPANY:	SONiX
* DATE:		2022/01/03
* AUTHOR:		Roger
* IC:			SN8F5754
*____________________________________________________________________________
* REVISION		Date				User		Description
* 1.0					2022/01/03	Roger		1. First release
*____________________________________________________________________________
*****************************************************************************/



/*_____ I N C L U D E S _________________________________*/
#define __XRAM_SFR_H__
#include <SN8F5754.h>
#include "halfvddbias.h"
#include "timer.h"
#include "LED_Display.h"
/*_____ F U N C T I O N S _______________________________*/
void HVDD_Initialization(void);
void GPIO_Initialization(void);
void TIMER0_Initialization(void);
void LCD_Initialization(void);
void LCD_Clockstate(void);
void LCD_Clockmode(void);
void LCD_Clockupdated(void);
/*_____ D E F I N I T I O N S ___________________________*/
#define INTGND_TIME_20MS					100
#define INTGND_TIME_1S						50
/*_____ D E C L A R A T I O N S _________________________*/


bit f_bIntgnd1s=0;
idata uint8_t r_lcd_buf[2]= {0};	
code uint8_t r_table[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x27,0x7f,0x6f}; 				
idata uint8_t bLEDMartxi1_ScanCNT =0;
uint8_t DigScanCnt = 0;
uint8_t bAPP_Timer20msCNT = 0;
uint8_t bAPP_Timer1sCNT = 0;
LEDTypeDef Mix;
ClockTypeDef clock;

/*_____ M A C R O S _____________________________________*/
#define    IO_PORT0_OUTPUT_LOW        P0M = 0x00;    P0 &= 0x00;
#define    IO_PORT1_OUTPUT_LOW        P1M = 0x00;    P1 &= 0x00;
#define    IO_PORT2_OUTPUT_LOW        P2M = 0x00;    P2 &= 0x00;
#define    IO_PORT3_OUTPUT_LOW        P3M = 0x00;    P3 &= 0x00;

/**
 *	@Brief		main: 0.5VDDBIAS Test Demo Function
 *	@param		None
 *	@return		None
 */
void main(void)
{
	/*
   *	System initial:
	 *	(1) SWAT
	 *	(2) GPIO
	 *	(3) TIMER
	 *	(4) 0.5VDDBIAS
	 *	(5) LCD Clock	
	 */		
		DEGCMD	= mskDEGCMD_SWATOFF;									/*	(1)	*/
		GPIO_Initialization();												/*	(2)	*/
		TIMER0_Initialization(); 											/*	(3)	*/
		HVDD_Initialization();												/*	(4)	*/
		LCD_Initialization();													/*	(5)	*/
		while(1)
		{		
			LCD_Clockstate();														/*	Update LCD Function	*/
			WDTR = 0x5A;																/*	Clear Watchdog Timer	*/
		}
}


/**
 *	@Brief		GPIO_Initialization:	GPIO Initial Function
 *	@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		HVDD_Initialization:	0.5VDDBIAS Initial Function
 *	@param		None
 *	@return		None
 */
void HVDD_Initialization(void)
{
	/*	
	 *	(1)	HALFVDDBIAS Enable		 
	 */	
		BIASCTL	|=	mskBIASCTL_BIASEN;								/*	(1)	*/
}


/**
 *	@Brief		TIMER0_Initialization:	Timer0 Initial Function
 *	@param		None
 *	@return		None
 */
void TIMER0_Initialization(void)
{
	/*
	 *	(1) Set Timer0 Mode1 (16-bit Timer) And IHRC As Timer0 Source
	 *	(2) Set	TL0 Overflow Value
	 *	(3)	Set	TH0 Overflow Value
	 *	(4)	Set Timer0 Freq Division
	 *	(5)	Enable Timer0 ISR
	 *	(6)	Enable EAL
	 *	(7)	Timer0 Start
	 *	  
	 */	
		TMOD |= (mskTMOD_T0_MODE2|mskTMOD_T0CT);			/*	(1) */
		TH0 = TH0_VALUE;            									/*	(2)	*/
    TL0 = TH0_VALUE;            									/*	(3)	*/	
		TCON0 |= mskTCON0_T0_DIV128;  								/*	(4)	*/ 	
	  IEN0 |= mskIEN0_ET0;  												/*	(5)	*/
		EAL =	1;																			/*	(6)	*/
		TCON |=	mskTCON_TR0;													/*	(7)	*/

}


/**
 *	@Brief		LCD_Initialization:	LCD Clock Initial Function
 *	@param		None
 *	@return		None
 */
void LCD_Initialization()
{	
	/*	
	 *	(1)	LCD Second Initial	 
	 */	
		clock.sec =  0;																/*	(1)	*/
}


/**
 *	@Brief		LCD_Clockstate:	determine 1s Clock Function
 *	@param		None
 *	@return		None
 */
void LCD_Clockstate(void)
{
	/*	
	 *	(1)	1s	Determine	Flag
	 *	(2)	Clear Flag
	 *	(3)	Update LCD Clock	
	 */	
		if(f_bIntgnd1s)                              	/*	(1)	*/
		{		
			f_bIntgnd1s =	0;														/*	(2)	*/
			LCD_Clockmode();														/*	(3)	*/
		}	
}



/**
 *	@Brief		LCD_Clockmode:	Update LCD Clock Function
 *	@param		None
 *	@return		None
 */
void LCD_Clockmode(void)
{
	/*	
	 *	(1)	Update LCD Clock 
	 *	(2)	Calculte LCD Timer
	 *	(3)	Update LCD Buffer	
	 */		
		if(++clock.sec > 59)													/*	(1)	*/
		{
			clock.sec = 0;
		}	
		Mix.seg1 = clock.sec/10;                      /*	(2)	*/
		Mix.seg2 = clock.sec%10;
		LCD_Clockupdated();														/*	(3)	*/
}


/**
 *	@Brief		LCD_Clockupdated:	Update LCD Buffer Function
 *	@param		None
 *	@return		None
 */
void LCD_Clockupdated(void)
{
		r_lcd_buf[0] =r_table[Mix.seg1];
		r_lcd_buf[1] =r_table[Mix.seg2];
}


/**
 *	@Brief		LEDMatrixDriver:	LCD Matrix Function
 *	@param		None
 *	@return		None
 */
void LEDMatrixDriver(void)
{
		MATRIXLED1_IO_INPUT_SCHMITT_OFF;
		P0M=0xFF; P1M=0x00;
		P00=1;
		COMCTL |=(mskCOMCTL_COM0|mskCOMCTL_COM1|mskCOMCTL_COM2|mskCOMCTL_COM3|mskCOMCTL_COM4|mskCOMCTL_COM5|mskCOMCTL_COM6|mskCOMCTL_COM7);				
		switch(bLEDMartxi1_ScanCNT)
		{
			case 0:		LED_MATRIX1_SEG1_A_ON ;		break;
			case 1:		LED_MATRIX1_SEG1_A_OFF ;	break;
			case 2:		LED_MATRIX1_SEG1_B_ON ;	 	break;
			case 3:		LED_MATRIX1_SEG1_B_OFF ;  break;
			case 4: 	LED_MATRIX1_SEG1_C_ON ; 	break;
			case 5:		LED_MATRIX1_SEG1_C_OFF ;  break;
			case 6:		LED_MATRIX1_SEG1_D_ON ; 	break;
			case 7:		LED_MATRIX1_SEG1_D_OFF ;	break;
			case 8:		LED_MATRIX1_SEG1_E_ON ;		break;
			case 9:		LED_MATRIX1_SEG1_E_OFF ;  break;
			case 10:	LED_MATRIX1_SEG1_F_ON ; 	break;
			case 11: 	LED_MATRIX1_SEG1_F_OFF ;  break;
			case 12:	LED_MATRIX1_SEG1_G_ON ; 	break;
			case 13:	LED_MATRIX1_SEG1_G_OFF ;  break;

			case 14:	LED_MATRIX1_SEG2_A_ON ;		break;
			case 15:	LED_MATRIX1_SEG2_A_OFF ;	break;
			case 16:	LED_MATRIX1_SEG2_B_ON ;	 	break;
			case 17:	LED_MATRIX1_SEG2_B_OFF ;  break;
			case 18: 	LED_MATRIX1_SEG2_C_ON ; 	break;
			case 19:	LED_MATRIX1_SEG2_C_OFF ;  break;
			case 20:	LED_MATRIX1_SEG2_D_ON ; 	break;
			case 21:	LED_MATRIX1_SEG2_D_OFF ;	break;
			case 22:	LED_MATRIX1_SEG2_E_ON ;		break;
			case 23:	LED_MATRIX1_SEG2_E_OFF ;  break;
			case 24:	LED_MATRIX1_SEG2_F_ON ; 	break;
			case 25: 	LED_MATRIX1_SEG2_F_OFF ;  break;
			case 26:	LED_MATRIX1_SEG2_G_ON ; 	break;
			case 27:	LED_MATRIX1_SEG2_G_OFF ;  break;
								
								
			default:	bLEDMartxi1_ScanCNT =0; break;
		}

		bLEDMartxi1_ScanCNT++;
		if(bLEDMartxi1_ScanCNT>27)
		{
			bLEDMartxi1_ScanCNT=0;
		}
}



/**
 *	@Brief		ISR_Timer0:	Timer0 Interrupt Function
 *	@param		None
 *	@return		None
 */
void ISR_Timer0(void) interrupt ISRTimer0	using 3
{
		LEDMatrixDriver();
		if(++ bAPP_Timer20msCNT >= INTGND_TIME_20MS)
		{
			bAPP_Timer20msCNT = 0;
			if(++ bAPP_Timer1sCNT >= INTGND_TIME_1S)
			{
				bAPP_Timer1sCNT = 0;
				f_bIntgnd1s = 1;								//** 1s flag			
			}
		}
}
