/*********************************************************
*Copyright (C), 2024-2030
*文件名:	common.h
*版  本:	Ver1.00
*日  期:	2025/07/09
*描  述:	公共头文件,寄存器设置例化
*备  注:	
**********************************************************/
#ifndef   __COMMON_H__
#define   __COMMON_H__

/* 定义数据类型 */
typedef unsigned char  uchar;	//无符号8位整型变量
typedef unsigned int   uint;	//无符号16位整型变量 
typedef unsigned long  ulong; //无符号32位整型变量

typedef unsigned char  U8;	    //无符号8位整型变量
typedef unsigned int   U16;	    //无符号16位整型变量 
typedef unsigned long  U32;     //无符号32位整型变量

typedef unsigned char  INT8U;   //无符号8位整型变量
typedef signed   char  INT8S;   //有符号8位整型变量
typedef unsigned int   INT16U;  //无符号16位整型变量 
typedef signed   int   INT16S;  //有符号16位整型变量
typedef unsigned long  INT32U;  //无符号32位整型变量
typedef signed   long  INT32S;	//有符号32位整型变量
typedef float          FP32;    //单精度浮点型变量 
typedef double         FP64;    //双精度浮点型变量
/* PnxF 寄存器地址偏移, 相对于 P00F(0x8000)*/
typedef enum
{
	P00F_O = 0x00,
	P01F_O = 0x01,
	P02F_O = 0x02,      
	P03F_O = 0x03,  
	P04F_O = 0x04,      
	P05F_O = 0x05,      
	P06F_O = 0x06,      
	P07F_O = 0x07,      
	P10F_O = 0x08,      
	P11F_O = 0x09,      
	P12F_O = 0x0A,      
	P13F_O = 0x0B,      
	P14F_O = 0x0C,      
	P15F_O = 0x0D,      
	P16F_O = 0x0E,     
	P17F_O = 0x0F,      
	P20F_O = 0x10,      
	P21F_O = 0x11,     
}IOF_O;


#define BIT_SET(reg,n) 			((reg) |= (1l<<(n)))		
#define BIT_CLR(reg,n)			((reg) &=~(1l<<(n)))	
#define BIT_GET(reg,n)			(((reg)>>(n))&1l)		
#define BIT_XOR(reg,n,val)	(BIT_GET(reg,n)^(val))	
#define BIT_ALONE(reg,n)		((reg) = (1l<<(n)))	

/*时钟配置*/
#define SYS_HRC_27M				SCCON = 0;
#define SYS_LRC_32K				SCCON = 0x40;
#define SYS_LRC_16K				SCCON = 0x41;
#define SYS_LRC_8K				SCCON = 0x43;
#define SYS_LRC_4K				SCCON = 0x47;
#define SYS_LRC_2K				SCCON = 0x4F;
#define SYS_LOSC					XSCON = 0x10;while(!(XSCON&0x01));SCCON = 0x80
#define OPEN_LOSC					XSCON |= 0x10;while(!(XSCON&0x01))



/*GPIO配置*/
#define  P00_FUN_HR        		P00F = 0
#define  P00_FUN_UP        		P00F = 0x80
#define  P00_FUN_DOWN        	P00F = 0x40
#define  P00_FUN_UP_INPUT    	P00F = 0X81
#define  P00_FUN_DOWN_INPUT  	P00F = 0X41
#define  P00_FUN_FlOAT_INPUT	P00F = 0X01
#define  P00_FUN_OPR_OUTPUT  	P00F = 0X22
#define  P00_FUN_OUTPUT      	P00F = 0X02 
#define  P00_FUN_PWM0      		P00F = 0X03 
#define  P00_FUN_LED0      		P00F = 0X04 
#define  P00_FUN_I2C        	P00F = 0X05 
#define  P00_FUN_UART        	P00F = 0X05 
#define  P00_FUN_TK0      	 	P00F = 0X06 
#define  P00_FUN_AIN0      	  P00F = 0X07
#define  P00_FUN_LXI      	  P00F = 0X08 

#define  P01_FUN_HR        		P01F = 0
#define  P01_FUN_UP        		P01F = 0x80
#define  P01_FUN_DOWN        	P01F = 0x40
#define  P01_FUN_UP_INPUT    	P01F = 0X81
#define  P01_FUN_DOWN_INPUT  	P01F = 0X41
#define  P01_FUN_FlOAT_INPUT	P01F = 0X01
#define  P01_FUN_OPR_OUTPUT  	P01F = 0X22
#define  P01_FUN_OUTPUT      	P01F = 0X02 
#define  P01_FUN_PWM1      		P01F = 0X03 
#define  P01_FUN_LED1      		P01F = 0X04 
#define  P01_FUN_I2C        	P01F = 0X05 
#define  P01_FUN_UART        	P01F = 0X05 
#define  P01_FUN_TK1      	 	P01F = 0X06 
#define  P01_FUN_AIN1      	  P01F = 0X07
#define  P01_FUN_LXO      	  P01F = 0X08 

#define  P02_FUN_HR        		P02F = 0
#define  P02_FUN_UP        		P02F = 0x80
#define  P02_FUN_DOWN        	P02F = 0x40
#define  P02_FUN_UP_INPUT    	P02F = 0X81
#define  P02_FUN_DOWN_INPUT  	P02F = 0X41
#define  P02_FUN_FlOAT_INPUT	P02F = 0X01
#define  P02_FUN_OPR_OUTPUT  	P02F = 0X22
#define  P02_FUN_OUTPUT      	P02F = 0X02 
#define  P02_FUN_PWM2      		P02F = 0X03 
#define  P02_FUN_LED2      		P02F = 0X04 
#define  P02_FUN_I2C        	P02F = 0X05 
#define  P02_FUN_UART        	P02F = 0X05 
#define  P02_FUN_TK2      	 	P02F = 0X06 
#define  P02_FUN_AIN2      	  P02F = 0X07
#define  P02_FUN_RST      	  P02F = 0X08 

#define  P03_FUN_HR        		P03F = 0
#define  P03_FUN_UP        		P03F = 0x80
#define  P03_FUN_DOWN        	P03F = 0x40
#define  P03_FUN_UP_INPUT    	P03F = 0X81
#define  P03_FUN_DOWN_INPUT  	P03F = 0X41
#define  P03_FUN_FlOAT_INPUT	P03F = 0X01
#define  P03_FUN_OPR_OUTPUT  	P03F = 0X22
#define  P03_FUN_OUTPUT      	P03F = 0X02 
#define  P03_FUN_PWM3      		P03F = 0X03 
#define  P03_FUN_LED3      		P03F = 0X04 
#define  P03_FUN_I2C        	P03F = 0X05 
#define  P03_FUN_UART        	P03F = 0X05 
#define  P03_FUN_TK3      	 	P03F = 0X06 
#define  P03_FUN_AIN3      	  P03F = 0X07

#define  P04_FUN_HR        		P04F = 0
#define  P04_FUN_UP        		P04F = 0x80
#define  P04_FUN_DOWN        	P04F = 0x40
#define  P04_FUN_UP_INPUT    	P04F = 0X81
#define  P04_FUN_DOWN_INPUT  	P04F = 0X41
#define  P04_FUN_FlOAT_INPUT	P04F = 0X01
#define  P04_FUN_OPR_OUTPUT  	P04F = 0X22
#define  P04_FUN_OUTPUT      	P04F = 0X02 
#define  P04_FUN_PWM4      		P04F = 0X03 
#define  P04_FUN_LED4      		P04F = 0X04 
#define  P04_FUN_I2C        	P04F = 0X05 
#define  P04_FUN_UART        	P04F = 0X05 
#define  P04_FUN_TK4      	 	P04F = 0X06 
#define  P04_FUN_AIN4      	  P04F = 0X07

#define  P05_FUN_HR        		P05F = 0
#define  P05_FUN_UP        		P05F = 0x80
#define  P05_FUN_DOWN        	P05F = 0x40
#define  P05_FUN_UP_INPUT    	P05F = 0X81
#define  P05_FUN_DOWN_INPUT  	P05F = 0X41
#define  P05_FUN_FlOAT_INPUT	P05F = 0X01
#define  P05_FUN_OPR_OUTPUT  	P05F = 0X22
#define  P05_FUN_OUTPUT      	P05F = 0X02 
#define  P05_FUN_PWM5      		P05F = 0X03 
#define  P05_FUN_LED5      		P05F = 0X04 
#define  P05_FUN_I2C        	P05F = 0X05 
#define  P05_FUN_UART        	P05F = 0X05 
#define  P05_FUN_TK5      	 	P05F = 0X06 
#define  P05_FUN_AIN5      	  P05F = 0X07

#define  P06_FUN_HR        		P06F = 0
#define  P06_FUN_UP        		P06F = 0x80
#define  P06_FUN_DOWN        	P06F = 0x40
#define  P06_FUN_UP_INPUT    	P06F = 0X81
#define  P06_FUN_DOWN_INPUT  	P06F = 0X41
#define  P06_FUN_FlOAT_INPUT	P06F = 0X01
#define  P06_FUN_OPR_OUTPUT  	P06F = 0X22
#define  P06_FUN_OUTPUT      	P06F = 0X02 
#define  P06_FUN_PWM0      		P06F = 0X03 
#define  P06_FUN_LED6      		P06F = 0X04 
#define  P06_FUN_I2C        	P06F = 0X05 
#define  P06_FUN_UART        	P06F = 0X05 
#define  P06_FUN_TK6      	 	P06F = 0X06 
#define  P06_FUN_AIN6      	  P06F = 0X07

#define  P07_FUN_HR        		P07F = 0
#define  P07_FUN_UP        		P07F = 0x80
#define  P07_FUN_DOWN        	P07F = 0x40
#define  P07_FUN_UP_INPUT    	P07F = 0X81
#define  P07_FUN_DOWN_INPUT  	P07F = 0X41
#define  P07_FUN_FlOAT_INPUT	P07F = 0X01
#define  P07_FUN_OPR_OUTPUT  	P07F = 0X22
#define  P07_FUN_OUTPUT      	P07F = 0X02 
#define  P07_FUN_PWM1      		P07F = 0X03 
#define  P07_FUN_LED7      		P07F = 0X04 
#define  P07_FUN_I2C        	P07F = 0X05 
#define  P07_FUN_UART        	P07F = 0X05 
#define  P07_FUN_TK7      	 	P07F = 0X06 
#define  P07_FUN_AIN7      	  P07F = 0X07

#define  P10_FUN_HR        		P10F = 0
#define  P10_FUN_UP        		P10F = 0x80
#define  P10_FUN_DOWN        	P10F = 0x40
#define  P10_FUN_UP_INPUT    	P10F = 0X81
#define  P10_FUN_DOWN_INPUT  	P10F = 0X41
#define  P10_FUN_FlOAT_INPUT	P10F = 0X01
#define  P10_FUN_T2EX					P10F = 0X01
#define  P10_FUN_OPR_OUTPUT  	P10F = 0X22
#define  P10_FUN_OUTPUT      	P10F = 0X02 
#define  P10_FUN_PWM2      		P10F = 0X03 
#define  P10_FUN_LED8      		P10F = 0X04 
#define  P10_FUN_I2C        	P10F = 0X05 
#define  P10_FUN_UART        	P10F = 0X05 
#define  P10_FUN_TK8      	 	P10F = 0X06 
#define  P10_FUN_AIN8      	  P10F = 0X07

#define  P11_FUN_HR        		P11F = 0
#define  P11_FUN_UP        		P11F = 0x80
#define  P11_FUN_DOWN        	P11F = 0x40
#define  P11_FUN_UP_INPUT    	P11F = 0X81
#define  P11_FUN_DOWN_INPUT  	P11F = 0X41
#define  P11_FUN_FlOAT_INPUT	P11F = 0X01
#define  P11_FUN_T2EX					P11F = 0X01
#define  P11_FUN_OPR_OUTPUT  	P11F = 0X22
#define  P11_FUN_OUTPUT      	P11F = 0X02 
#define  P11_FUN_PWM5      		P11F = 0X03 
#define  P11_FUN_LED9      		P11F = 0X04 
#define  P11_FUN_I2C        	P11F = 0X05 
#define  P11_FUN_UART        	P11F = 0X05 
#define  P11_FUN_TK9      	 	P11F = 0X06 
#define  P11_FUN_AIN9      	  P11F = 0X07
#define  P11_FUN_SDO      	  P11F = 0X08 

#define  P12_FUN_HR        		P12F = 0
#define  P12_FUN_UP        		P12F = 0x80
#define  P12_FUN_DOWN        	P12F = 0x40
#define  P12_FUN_UP_INPUT    	P12F = 0X81
#define  P12_FUN_DOWN_INPUT  	P12F = 0X41
#define  P12_FUN_FlOAT_INPUT	P12F = 0X01
#define  P12_FUN_OPR_OUTPUT  	P12F = 0X22
#define  P12_FUN_OUTPUT      	P12F = 0X02 
#define  P12_FUN_PWM4      		P12F = 0X03 
#define  P12_FUN_LED10      	P12F = 0X04 
#define  P12_FUN_I2C        	P12F = 0X05 
#define  P12_FUN_UART        	P12F = 0X05 
#define  P12_FUN_TK10      	 	P12F = 0X06 
#define  P12_FUN_AIN10      	P12F = 0X07
#define  P12_FUN_SDI      	  P12F = 0X08 

#define  P13_FUN_HR        		P13F = 0
#define  P13_FUN_UP        		P13F = 0x80
#define  P13_FUN_DOWN        	P13F = 0x40
#define  P13_FUN_UP_INPUT    	P13F = 0X81
#define  P13_FUN_DOWN_INPUT  	P13F = 0X41
#define  P13_FUN_FlOAT_INPUT	P13F = 0X01
#define  P13_FUN_OPR_OUTPUT  	P13F = 0X22
#define  P13_FUN_OUTPUT      	P13F = 0X02 
#define  P13_FUN_PWM3      		P13F = 0X03 
#define  P13_FUN_LED11      	P13F = 0X04 
#define  P13_FUN_I2C        	P13F = 0X05 
#define  P13_FUN_UART        	P13F = 0X05 
#define  P13_FUN_TK11      	 	P13F = 0X06 
#define  P13_FUN_AIN11      	P13F = 0X07
#define  P13_FUN_SCK      	  P13F = 0X08 

#define  P14_FUN_HR        		P14F = 0
#define  P14_FUN_UP        		P14F = 0x80
#define  P14_FUN_DOWN        	P14F = 0x40
#define  P14_FUN_UP_INPUT    	P14F = 0X81
#define  P14_FUN_DOWN_INPUT  	P14F = 0X41
#define  P14_FUN_FlOAT_INPUT	P14F = 0X01
#define  P14_FUN_OPR_OUTPUT  	P14F = 0X22
#define  P14_FUN_OUTPUT      	P14F = 0X02 
#define  P14_FUN_PWM0      		P14F = 0X03 
#define  P14_FUN_LED12      	P14F = 0X04 
#define  P14_FUN_I2C        	P14F = 0X05 
#define  P14_FUN_UART        	P14F = 0X05 
#define  P14_FUN_TK12      	 	P14F = 0X06 
#define  P14_FUN_AIN12      	P14F = 0X07
#define  P14_FUN_SCS      	  P14F = 0X08 

#define  P15_FUN_HR        		P15F = 0
#define  P15_FUN_UP        		P15F = 0x80
#define  P15_FUN_DOWN        	P15F = 0x40
#define  P15_FUN_UP_INPUT    	P15F = 0X81
#define  P15_FUN_DOWN_INPUT  	P15F = 0X41
#define  P15_FUN_FlOAT_INPUT	P15F = 0X01
#define  P15_FUN_OPR_OUTPUT  	P15F = 0X22
#define  P15_FUN_OUTPUT      	P15F = 0X02 
#define  P15_FUN_PWM1      		P15F = 0X03 
#define  P15_FUN_LED13      	P15F = 0X04 
#define  P15_FUN_I2C        	P15F = 0X05 
#define  P15_FUN_UART        	P15F = 0X05 
#define  P15_FUN_TK13      	 	P15F = 0X06 
#define  P15_FUN_AIN13      	P15F = 0X07

#define  P16_FUN_HR        		P16F = 0
#define  P16_FUN_UP        		P16F = 0x80
#define  P16_FUN_DOWN        	P16F = 0x40
#define  P16_FUN_UP_INPUT    	P16F = 0X81
#define  P16_FUN_DOWN_INPUT  	P16F = 0X41
#define  P16_FUN_FlOAT_INPUT	P16F = 0X01
#define  P16_FUN_T2I					P16F = 0X01
#define  P16_FUN_OPR_OUTPUT  	P16F = 0X22
#define  P16_FUN_OUTPUT      	P16F = 0X02 
#define  P16_FUN_T2O	      	P16F = 0X02 
#define  P16_FUN_PWM2      		P16F = 0X03 
#define  P16_FUN_LED14      	P16F = 0X04 
#define  P16_FUN_I2C        	P16F = 0X05 
#define  P16_FUN_UART        	P16F = 0X05 
#define  P16_FUN_TK14      	 	P16F = 0X06 
#define  P16_FUN_AIN14      	P16F = 0X07

#define  P17_FUN_HR        		P17F = 0
#define  P17_FUN_UP        		P17F = 0x80
#define  P17_FUN_DOWN        	P17F = 0x40
#define  P17_FUN_UP_INPUT    	P17F = 0X81
#define  P17_FUN_DOWN_INPUT  	P17F = 0X41
#define  P17_FUN_FlOAT_INPUT	P17F = 0X01
#define  P17_FUN_OPR_OUTPUT  	P17F = 0X22
#define  P17_FUN_OUTPUT      	P17F = 0X02 
#define  P17_FUN_PWM3      		P17F = 0X03 
#define  P17_FUN_LED15      	P17F = 0X04 
#define  P17_FUN_I2C        	P17F = 0X05 
#define  P17_FUN_UART        	P17F = 0X05 
#define  P17_FUN_TK15      	 	P17F = 0X06 
#define  P17_FUN_AIN15      	P17F = 0X07

#define  P20_FUN_HR        		P20F = 0
#define  P20_FUN_UP        		P20F = 0x80
#define  P20_FUN_DOWN        	P20F = 0x40
#define  P20_FUN_UP_INPUT    	P20F = 0X81
#define  P20_FUN_DOWN_INPUT  	P20F = 0X41
#define  P20_FUN_FlOAT_INPUT	P20F = 0X01
#define  P20_FUN_OPR_OUTPUT  	P20F = 0X22
#define  P20_FUN_OUTPUT      	P20F = 0X02 
#define  P20_FUN_PWM4      		P20F = 0X03 
#define  P20_FUN_LED16      	P20F = 0X04 
#define  P20_FUN_I2C        	P20F = 0X05 
#define  P20_FUN_UART        	P20F = 0X05 
#define  P20_FUN_TK16      	 	P20F = 0X06 
#define  P20_FUN_AIN16      	P20F = 0X07
#define  P20_FUN_SCS      	  P20F = 0X08 

#define  P21_FUN_HR        		P21F = 0
#define  P21_FUN_UP        		P21F = 0x80
#define  P21_FUN_DOWN        	P21F = 0x40
#define  P21_FUN_UP_INPUT    	P21F = 0X81
#define  P21_FUN_DOWN_INPUT  	P21F = 0X41
#define  P21_FUN_FlOAT_INPUT	P21F = 0X01
#define  P21_FUN_OPR_OUTPUT  	P21F = 0X22
#define  P21_FUN_OUTPUT      	P21F = 0X02 
#define  P21_FUN_PWM5      		P21F = 0X03 
#define  P21_FUN_LED17      	P21F = 0X04 
#define  P21_FUN_I2C        	P21F = 0X05 
#define  P21_FUN_UART        	P21F = 0X05 
#define  P21_FUN_TK17      	 	P21F = 0X06 
#define  P21_FUN_AIN17      	P21F = 0X07




/*IO输出配置*/
#define  P00_REV				P00 = ~P00
#define  P01_REV				P01 = ~P01
#define  P02_REV				P02 = ~P02
#define  P03_REV				P03 = ~P03
#define  P04_REV				P04 = ~P04
#define  P05_REV				P05 = ~P05
#define  P06_REV				P06 = ~P06
#define  P07_REV				P07 = ~P07
#define  P10_REV				P10 = ~P10
#define  P11_REV				P11 = ~P11
#define  P12_REV				P12 = ~P12
#define  P13_REV				P13 = ~P13
#define  P14_REV				P14 = ~P14
#define  P15_REV				P15 = ~P15
#define  P16_REV				P16 = ~P16
#define  P17_REV				P17 = ~P17
#define  P20_REV				P20 = ~P20
#define  P21_REV				P21 = ~P21


#define  P00_HIGH				P00 = 1
#define  P01_HIGH				P01 = 1
#define  P02_HIGH				P02 = 1
#define  P03_HIGH				P03 = 1
#define  P04_HIGH				P04 = 1
#define  P05_HIGH				P05 = 1
#define  P06_HIGH				P06 = 1
#define  P07_HIGH				P07 = 1
#define  P10_HIGH				P10 = 1
#define  P11_HIGH				P11 = 1
#define  P12_HIGH				P12 = 1
#define  P13_HIGH				P13 = 1
#define  P14_HIGH				P14 = 1
#define  P15_HIGH				P15 = 1
#define  P16_HIGH				P16 = 1
#define  P17_HIGH				P17 = 1
#define  P20_HIGH				P20 = 1
#define  P21_HIGH				P21 = 1


#define  P00_LOW				P00 = 0
#define  P01_LOW				P01 = 0
#define  P02_LOW				P02 = 0
#define  P03_LOW				P03 = 0
#define  P04_LOW				P04 = 0
#define  P05_LOW				P05 = 0
#define  P06_LOW				P06 = 0
#define  P07_LOW				P07 = 0
#define  P10_LOW				P10 = 0
#define  P11_LOW				P11 = 0
#define  P12_LOW				P12 = 0
#define  P13_LOW				P13 = 0
#define  P14_LOW				P14 = 0
#define  P15_LOW				P15 = 0
#define  P16_LOW				P16 = 0
#define  P17_LOW				P17 = 0
#define  P20_LOW				P20 = 0
#define  P21_LOW				P21 = 0



/*WDT配置*/
#define  WDT_OPEN					WDTFLG = 0xA5
#define  WDT_CLOSE				WDTFLG = 0x5A
#define  WDT_RST_8MS			WDTCON = 0
#define  WDT_RST_16MS			WDTCON = 0x01
#define  WDT_RST_31MS			WDTCON = 0x02
#define  WDT_RST_62MS			WDTCON = 0x03
#define  WDT_RST_125MS		WDTCON = 0x04
#define  WDT_RST_250MS		WDTCON = 0x05
#define  WDT_RST_500MS		WDTCON = 0x06
#define  WDT_RST_1000MS		WDTCON = 0x07
#define  WDT_INT_8MS			WDTCON = 0x80
#define  WDT_INT_16MS			WDTCON = 0x81
#define  WDT_INT_31MS			WDTCON = 0x82
#define  WDT_INT_62MS			WDTCON = 0x83
#define  WDT_INT_125MS		WDTCON = 0x84
#define  WDT_INT_250MS		WDTCON = 0x85
#define  WDT_INT_500MS		WDTCON = 0x86
#define  WDT_INT_1000MS		WDTCON = 0x87


/*WKT配置*/
#define  WKT_LRC_OPEN				WKTCON |= 0x80
#define  WKT_LOSC_OPEN			WKTCON |= 0xC0
#define  INT_FlAG_WKT				0x10
#define  WKT_LRC_500MS			WKTCON = WKTCON&0xFC;WKTCON |= 0x03;WKTSS = (499)>>2
#define  WKT_LRC_1000MS			WKTCON = WKTCON&0xFC;WKTCON |= 0x03;WKTSS = (999)>>2
#define  WKT_LOSC_500MS			WKTCON = WKTCON&0xFC;WKTCON |= 0x03;WKTSS = (511)>>2
#define  WKT_LOSC_1000MS		WKTCON = WKTCON&0xFC;WKTCON |= 0x03;WKTSS = (1023)>>2


/*中断使能控制*/
#define	 INT_SPI				INT4 = 1;EA = 1
#define	 INT_ADC				INT4 = 1;EA = 1
#define	 INT_TK 				INT4 = 1;EA = 1
#define	 INT_TIMER2			INT3 = 1;EA = 1
#define	 INT_I2C				INT3 = 1;EA = 1
#define	 INT_LED				INT3 = 1;EA = 1
#define	 INT_WKT				INT3 = 1;EA = 1
#define	 INT_LVD				INT2 = 1;EA = 1
#define	 INT_WDT				INT2 = 1;EA = 1
#define	 INT_PWM				INT2 = 1;EA = 1
#define	 INT_UART				INT2 = 1;EA = 1
#define	 INT_TIME1			ET1 = 1;EA = 1
#define	 INT_INT1				INT1 = 1;EA = 1
#define	 INT_TIME0			ET0 = 1;EA = 1
#define	 INT_INT0				INT0 = 1;EA = 1


/*休眠模式控制*/
#define  STOP_0					PCON = 0x02;PWCON |= 0xC0
#define  STOP_1					PCON = 0x02;PWCON |= 0x80
#define  STOP_2					PCON = 0x02;PWCON |= 0x40
#define  STOP_3					PCON = 0x02;PWCON |= 0x00
#define  IDLE_0					PCON = 0x01;PWCON |= 0xC0
#define  IDLE_1					PCON = 0x01;PWCON |= 0x80
#define  IDLE_2					PCON = 0x01;PWCON |= 0x40
#define  IDLE_3					PCON = 0x01;PWCON |= 0x00


/*LVD配置*/	
#define  CLOSE_LVD 					LVDCON = 0x00
#define  FLAG_LVD 					0x10
#define  LVD_INT_2_0V				LVDCON = 0x40
#define  LVD_INT_2_4V				LVDCON = 0x41
#define  LVD_INT_2_7V				LVDCON = 0x42
#define  LVD_INT_3_0V				LVDCON = 0x43
#define  LVD_INT_3_3V				LVDCON = 0x44
#define  LVD_INT_3_7V				LVDCON = 0x45
#define  LVD_INT_4_0V				LVDCON = 0x46
#define  LVD_INT_4_3V				LVDCON = 0x47
#define  LVD_RST_2_0V				LVDCON = 0x80
#define  LVD_RST_2_4V				LVDCON = 0x81
#define  LVD_RST_2_7V				LVDCON = 0x82
#define  LVD_RST_3_0V				LVDCON = 0x83
#define  LVD_RST_3_3V				LVDCON = 0x84
#define  LVD_RST_3_7V				LVDCON = 0x85
#define  LVD_RST_4_0V				LVDCON = 0x86
#define  LVD_RST_4_3V				LVDCON = 0x87
#define  LVD_FLAG_2_0V			LVDCON = 0xC0
#define  LVD_FLAG_2_4V			LVDCON = 0xC1
#define  LVD_FLAG_2_7V			LVDCON = 0xC2
#define  LVD_FLAG_3_0V			LVDCON = 0xC3
#define  LVD_FLAG_3_3V			LVDCON = 0xC4
#define  LVD_FLAG_3_7V			LVDCON = 0xC5
#define  LVD_FLAG_4_0V			LVDCON = 0xC6
#define  LVD_FLAG_4_3V			LVDCON = 0xC7
#define  LVD_LFTS_OPEN			LVDCON |= 0x20
#define  LVD_LFTS_CLOSE			LVDCON &= 0xDF


/*SLED配置*/
#define  LED_INT_LRC_LIGHT		LSCON |= 0xC0
#define  LED_INT_LRC_EXT			LSCON |= 0xA0
#define  LED_INT_LOSC_LIGHT		LSCON |= 0xC8
#define  LED_INT_LOSC_EXT			LSCON |= 0xA8
#define  FLAG_LED_LIGHT				0x02
#define  FLAG_LED_EXT					0x01
#define	 LED_COM_HIGH_LOW			LSCFG = 0xA1// 亮：COM高，SEG低；灭：COM和SEG低 	
#define	 LED_ESG_HIGH_LOW			LSCFG = 0xA2// 亮：COM低，SEG高；灭：COM和SEG低 
#define	 LED_COM_HIGH_HIGH		LSCFG = 0x51// 亮：COM高，SEG低；灭：COM和SEG高 	
#define	 LED_ESG_HIGH_HIGH		LSCFG = 0x52// 亮：COM低，SEG高；灭：COM和SEG高

 	
/*PWM配置*/
#define  PWM0_OPEN					PWMEN |= 0x01
#define  PWM1_OPEN					PWMEN |= 0x02
#define  PWM2_OPEN					PWMEN |= 0x04
#define  PWM3_OPEN					PWMEN |= 0x08
#define  PWM4_OPEN					PWMEN |= 0x10
#define  PWM5_OPEN					PWMEN |= 0x20

#define  PWM0_INT_OPEN			PWMIE |= 0x01
#define  PWM1_INT_OPEN			PWMIE |= 0x02
#define  PWM2_INT_OPEN			PWMIE |= 0x04
#define  PWM3_INT_OPEN			PWMIE |= 0x08
#define  PWM4_INT_OPEN			PWMIE |= 0x10
#define  PWM5_INT_OPEN			PWMIE |= 0x20


/*TIME*/
#define	 TIME0_OPEN     TR0 = 1
#define	 TIME1_OPEN     TR1 = 1
#define	 TIME0_CLOSE    TR0 = 0
#define	 TIME1_CLOSE    TR1 = 0
#define	 INT0_OPEN      IE0 = 1
#define	 INT1_OPEN      IE1 = 1
#define	 INT0_LEVEL     IT0 = 0
#define	 INT1_LEVEL     IT1 = 0
#define	 INT0_EDGE      IT0 = 1
#define	 INT1_EDGE      IT1 = 1


/*UART*/
#define  UART1_MODE0		S1CON &= 0x7F
#define  UART1_MODE1		S1CON |= 0x80


/*outwith*/
#define  CLOSE_HRC			HRCON = 0x00
#define	 CLOSE_TK				TKCON = 0x00
#define	 CLOSE_IIC			I2CCON = 0x00
#define  FLASH_SLEEP    FSCON = 0x80
#define	 ADC_VDD			  ADCFG = 0x00
#define	 LDO_MODE0			PWCON |= 0xC0
#define	 LDO_MODE1			PWCON |= 0x80
#define	 LDO_MODE2			PWCON |= 0x40
#define	 LDO_MODE3			PWCON &= 0x3f
#define	 SLEEP_STOP			PCON = 0x02
#define	 SLEEP_IDLE			PCON = 0x01
#define  CLOSE_POR			RSTCON = 0x00


/*IIC配置IO口*/
#define	 SCL_P00			I2CCPS = 0x00
#define	 SCL_P01			I2CCPS = 0x01
#define	 SCL_P02			I2CCPS = 0x02
#define	 SCL_P03			I2CCPS = 0x03
#define	 SCL_P04			I2CCPS = 0x04
#define	 SCL_P05			I2CCPS = 0x05
#define	 SCL_P06			I2CCPS = 0x06
#define	 SCL_P07			I2CCPS = 0x07
#define	 SCL_P10			I2CCPS = 0x08
#define	 SCL_P11			I2CCPS = 0x09
#define	 SCL_P12			I2CCPS = 0x0A
#define	 SCL_P13			I2CCPS = 0x0B
#define	 SCL_P14			I2CCPS = 0x0C
#define	 SCL_P15			I2CCPS = 0x0D
#define	 SCL_P16			I2CCPS = 0x0E
#define	 SCL_P17			I2CCPS = 0x0F
#define	 SCL_P20			I2CCPS = 0x10
#define	 SCL_P21			I2CCPS = 0x11


#define	 SDA_P00			I2CDPS = 0x00
#define	 SDA_P01			I2CDPS = 0x01
#define	 SDA_P02			I2CDPS = 0x02
#define	 SDA_P03			I2CDPS = 0x03
#define	 SDA_P04			I2CDPS = 0x04
#define	 SDA_P05			I2CDPS = 0x05
#define	 SDA_P06			I2CDPS = 0x06
#define	 SDA_P07			I2CDPS = 0x07
#define	 SDA_P10			I2CDPS = 0x08
#define	 SDA_P11			I2CDPS = 0x09
#define	 SDA_P12			I2CDPS = 0x0A
#define	 SDA_P13			I2CDPS = 0x0B
#define	 SDA_P14			I2CDPS = 0x0C
#define	 SDA_P15			I2CDPS = 0x0D
#define	 SDA_P16			I2CDPS = 0x0E
#define	 SDA_P17			I2CDPS = 0x0F
#define	 SDA_P20			I2CDPS = 0x10
#define	 SDA_P21			I2CDPS = 0x11


/*NVR常用定义*/
#define SECTOR_SIZE 512					//字节长度

#define NVR1_START_ADDR 0X00		//NVR1扇区首地址
#define NVR2_START_ADDR 0X200		//NVR2扇区首地址
#define NVR3_START_ADDR 0X400		//NVR3扇区首地址
#define NVR4_START_ADDR 0X600		//NVR4扇区首地址
#define NVR5_START_ADDR 0X800		//NVR1扇区首地址

#define NVR_FLASH_MARK_ENABLE_ADDR 	0x10C
#define NVR_FLASH_MARK_ENABLE_CMD 	0xAA
#define NVR_FLASH_MARK_ADDR 				0x10D
#define NVR_FLASH_MARK_DATA 				0x80

#define NVR_UNLOCK 								0X55	//解锁NVR
#define NVR_WRITE 								0X46	//写NVR 
#define NVR_ERASE 								0X47	//擦NVR一个扇区
#define INNER_FLASH_ERASE_UN-MASK 0X04
#define NVR_LOCK 									0XAA	//锁住NVR
#define NVR_READ								 	0X45	//读NVR DATA区

/*FLASH常用定义*/

#define SECTOR_SIZE 					512		
#define FlASH_SIZE 						(32*1024)

#define FLASH_UNLOCK					0X55			//解锁FLASH区
#define FLASH_ERASE_UNMASK 		0X04

#define FLASH_WRITE_DATA 			0X02			//写FLASH DATA区
#define FLASH_WRITE_CODE 			0X06			//写FLASH CODE区

#define FLASH_READ_DATA 			0X01			//读FLASH DATA区
#define FLASH_READ_CODE 			0X05			//读FLASH CODE区

#define FLASH_ERASE_DATA 			0X03			//擦除FLASH DATA一个扇区
#define FLASH_ERASE_CODE 			0X07			//擦除FLASH CODE一个扇区
#define FLASH_ERASE_WHOLE			0X04			//FLASH整片擦除

#define FLASH_LOCK 						0XAA			//锁定FLASH

#define FLASH_CHECK_SUM_CODE 	0x08
#define FLASH_CHECK_SUM_DATA 	0x08


void xram_clear(void);
void delay_ms(int n);

#endif