//*********************************************************************** 
//Copyright (c) 2024   HuaXin Micro-electronics Co.,Ltd 
//
//HS16P2811 
//T3PWM
//configuration introduction
//芯片T3定时器5路独立10位PWM 输出，共用相同周期，时钟源选Fcpu
//designer:华芯微MCU组
//updata：20241105
//***********************************************************************  
//RAM: 176*8bytes
//ROM: 2K
//STACK: 8-level
//*********************************************************************** 
#include 	"main.h"		


//--------------------------------------------------------------------------------------------------
/*
	     ╔══════╗　 ╔══════╗
	VDD  ╬1 ●   ╚═══╝    16╬   GND
	PA7  ╬2              15╬   PA0
	PA6  ╬3              14╬   PA1
	PA5  ╬4              13╬   PA2
	PA4  ╬5              12╬   PA3
	PC5  ╬6              11╬   PC0
	PC4  ╬7              10╬   PC1
	PC3  ╬8              9 ╬   PC2
         ╚═════════════════╝
*/
//--------------------------------------------------------------------------------------------------
unsigned char T3_Cnt=0;
unsigned char T3_Mode=0;

//--------------------------------------------------------------------------------------------------
//Timer3-define
#define  iT3_1_rate		7
#define  iT3_2_rate		6
#define  iT3_4_rate		5
#define  iT3_8_rate		4
#define  iT3_16_rate	3
#define  iT3_32_rate	2
#define  iT3_64_rate	1
#define  iT3_128_rate	0

#define  iT3_val		500

/**
 * @ brief:	T3_Timer_Fcpu_Init-T3-定时器模式-Fcpu
 * @ note: 定时器模式，时钟源选Fcpu；T3 定时器溢出方式：0累加到 周期寄存器 T3PH:T3PL后溢出，从0再开始计时;
 * @ param:	rate-分频系数,timer_val-初值
 * @ retval:null
 */
void T3_Timer_Fcpu_Init(unsigned char rate, unsigned int timer_val)
{
	T3C2 = 0x00;	//00** *111
		//bit7:PDTM2
			//0 = T3PWM3 & T3PWM2 工作与独立输出模式
			//1 = T3PWM3 & T3PWM2 工作与互补输出模式
		//bit6:PDTM0
			//0 = T3PWM1 & T3PWM0 工作与独立输出模式
			//1 = T3PWM1 & T3PWM0 工作与互补输出模式
		//bit5:T3PWM4INV 	//0 = 不取反;			1 = 取反
		//bit4:T3PWM3INV  	//0 = 不取反;			1 = 取反
		//bit3:T3PWM2INV  	//0 = 不取反;			1 = 取反
		//bit2:T3PWM4EN		//0 = 禁止 T3PWM4 输出;	1 = 允许 T3PWM4 输出 
		//bit1:T3PWM3EN		//0 = 禁止 T3PWM3 输出;	1 = 允许 T3PWM3 输出 
		//bit0:T3PWM2EN		//0 = 禁止 T3PWM2 输出;	1 = 允许 T3PWM2 输出 
		
	T3C1 = 0x00;	//-0-0 -111
	T3C1|=(rate & 0x07);	//分频系数（不能大于7）
		//bit6:T3OUT0   	//0 = T3PWM 输出不受影响;	1 = T3PWM 输出全低电平 
		//bit4:T3CKS0 		//0 = Fcpu/2;	-√			1 = Fosc
		//bit2~0:T3CK<2:0>
			//111 = 1分频;		110 = 2分频;		101 = 4分频;	100 = 8分频
			//011 = 16分频;		010 = 32分频;		001 = 64分频;	000 = 128分频
		
	T3C0 = 0x80;	//1-** -11-
		//bit7:T3REN  定时器总使能;			//0 = 禁止;		1 = 使能-√
		//bit5:T3PWM1INV  T3PWM1输出极性;	//0 = 不取反;	1 = 取反
		//bit4:T3PWM0INV  T3PWM0输出极性;	//0 = 不取反;	1 = 取反
		//bit2:T3PWM1EN 输出使能;			//0 = 禁止;		1 = 启动
		//bit1:T3PWM0EN 输出使能;			//0 = 禁止;		1 = 启动
	
	//timer
	T3TH = (0xfff-timer_val)/256;
	T3TL = (0xfff-timer_val)%256;		
	
	T3IF = 0;
	T3IE = 1;
	PEIE = 1;
	GIE = 1;
}


/**
 * @ brief:	T3_Timer_Fosc_Init-T3-定时器模式-Fosc
 * @ note: 定时器模式，时钟源选-Fosc；T3 定时器溢出方式：0累加到 周期寄存器 T3PH:T3PL后溢出，从0再开始计时;
 * @ param:	rate-分频系数,timer_val-初值
 * @ retval:null
 */
void T3_Timer_Fosc_Init(unsigned char rate, unsigned int timer_val)
{
	T3C2 = 0x00;	//00** *111
		//bit7:PDTM2
			//0 = T3PWM3 & T3PWM2 工作与独立输出模式
			//1 = T3PWM3 & T3PWM2 工作与互补输出模式
		//bit6:PDTM0
			//0 = T3PWM1 & T3PWM0 工作与独立输出模式
			//1 = T3PWM1 & T3PWM0 工作与互补输出模式
		//bit5:T3PWM4INV 	//0 = 不取反;			1 = 取反
		//bit4:T3PWM3INV  	//0 = 不取反;			1 = 取反
		//bit3:T3PWM2INV  	//0 = 不取反;			1 = 取反
		//bit2:T3PWM4EN		//0 = 禁止 T3PWM4 输出;	1 = 允许 T3PWM4 输出 
		//bit1:T3PWM3EN		//0 = 禁止 T3PWM3 输出;	1 = 允许 T3PWM3 输出 
		//bit0:T3PWM2EN		//0 = 禁止 T3PWM2 输出;	1 = 允许 T3PWM2 输出 
		
	T3C1 = 0x00;	//-0-0 -111
	T3C1|=(1<<4);	//Fosc
	T3C1|=(rate & 0x07);	//分频系数（不能大于7）
		//bit6:T3OUT0   	//0 = T3PWM 输出不受影响;	1 = T3PWM 输出全低电平 
		//bit4:T3CKS0 		//0 = Fcpu/2;				1 = Fosc-√
		//bit2~0:T3CK<2:0>
			//111 = 1分频;		110 = 2分频;		101 = 4分频;	100 = 8分频
			//011 = 16分频;		010 = 32分频;		001 = 64分频;	000 = 128分频
		
	T3C0 = 0x80;	//1-** -11-
		//bit7:T3REN  定时器总使能;			//0 = 禁止;		1 = 使能-√
		//bit5:T3PWM1INV  T3PWM1输出极性;	//0 = 不取反;	1 = 取反
		//bit4:T3PWM0INV  T3PWM0输出极性;	//0 = 不取反;	1 = 取反
		//bit2:T3PWM1EN 输出使能;			//0 = 禁止;		1 = 启动
		//bit1:T3PWM0EN 输出使能;			//0 = 禁止;		1 = 启动
	
	//timer
	T3TH = (0xfff - timer_val)/256;
	T3TL = (0xfff - timer_val)%256;		
	
	T3IF = 0;
	T3IE = 1;
	PEIE = 1;
	GIE = 1;
}


/**
 * @ brief:	-T3-5*PWM模式-独立-Fcpu
 * @ note: T3 定时器溢出方式：0累加到 周期寄存器 T3PH:T3PL后溢出，从0再开始计时;
 * @ param:	rate-分频系数
 * @ retval:null
 */
void T3_5PWM_Indep_Fcpu(unsigned char rate, unsigned int timer_val)
{
	PWMSEL1= 0x01;	//---- **01
		//bit1~0:T3PWM4S<1:0>
			//00 = 无					01 = PC0 输出 T3PWM4
			//10 = PC5 输出T3PWM4		11 = PA4 输出 T3PWM4	
	PWMSEL = 0x55;	//0101 0101
		//bit7~6:T3PWM3S<1:0>
			//00 = 无					01 = PA3 输出 T3PWM3
			//10 = PC4 输出T3PWM3		11 = PA7 输出 T3PWM3
		//bit5~4:T3PWM2S<1:0>
			//00 = 无					01 = PA2 输出 T3PWM2
			//10 = PC3 输出T3PWM2		11 = PA6 输出 T3PWM2
		//bit3~2:T3PWM1S<1:0>
			//00 = 无					01 = PA1 输出 T3PWM1
			//10 = PC2 输出T3PWM1		11 = PA5 输出 T3PWM1
		//bit1~0:T3PWM0S<1:0>
			//00 = PC4 输出T3PWM0		01 = PA0 输出 T3PWM0
			//10 = PC1 输出T3PWM0		11 = PA4 输出 T3PWM0

	T3C2 = 0x07;	//0000 0111
		//bit7:PDTM2
			//0 = T3PWM3 & T3PWM2 工作与独立输出模式-√
			//1 = T3PWM3 & T3PWM2 工作与互补输出模式
		//bit6:PDTM0
			//0 = T3PWM1 & T3PWM0 工作与独立输出模式-√
			//1 = T3PWM1 & T3PWM0 工作与互补输出模式
		//bit5:T3PWM4INV 	//0 = 不取反;-√			1 = 取反
		//bit4:T3PWM3INV  	//0 = 不取反;-√			1 = 取反
		//bit3:T3PWM2INV  	//0 = 不取反;-√			1 = 取反
		//bit2:T3PWM4EN		//0 = 禁止 T3PWM4 输出;	1 = 允许 T3PWM4 输出 -√
		//bit1:T3PWM3EN		//0 = 禁止 T3PWM3 输出;	1 = 允许 T3PWM3 输出 -√
		//bit0:T3PWM2EN		//0 = 禁止 T3PWM2 输出;	1 = 允许 T3PWM2 输出 -√
	
	T3C1 = 0;	//-0-0 -111
	T3C1|= (rate&0x07);	//分频
		//bit6:T3OUT0   	//0 = T3PWM 输出不受影响;-√	1 = T3PWM 输出全低电平 
		//bit4:T3CKS0 		//0 = Fcpu/2;-√				1 = Fosc
		//bit2~0:T3CK<2:0>
			//111 = 1分频;		110 = 2分频;		101 = 4分频;	100 = 8分频
			//011 = 16分频;		010 = 32分频;		001 = 64分频;	000 = 128分频
		
	T3C0 = 0x86;	//1-00 -11-
		//bit7:T3REN  定时器总使能;			//0 = 禁止;		1 = 使能-√
		//bit5:T3PWM1INV  T3PWM1输出极性;	//0 = 不取反;-√	1 = 取反
		//bit4:T3PWM0INV  T3PWM0输出极性;	//0 = 不取反;-√	1 = 取反
		//bit2:T3PWM1EN 输出使能;			//0 = 禁止;		1 = 启动-√
		//bit1:T3PWM0EN 输出使能;			//0 = 禁止;		1 = 启动-√
	
	//PWM 周期
	T3PH = timer_val/256;
	T3PL = timer_val%256;
	
	//duty0-pa0
	T3D0H = 10/256;
	T3D0L = 10%256;

	//duty1-pa1
	T3D1H = 1/256;	
	T3D1L = 1%256;
	
	//duty2-pa2
	T3D2H = 300/256;
	T3D2L = 300%256;
	
	//duty3-pa3
	T3D3H = 500/256; //占空比100% 可以输出-全高-
	T3D3L = 500%256;
	
	//duty4-pc0
	T3D4H = 499/256;
	T3D4L = 499%256;
		
	T3IF = 0;
	T3IE = 1;//PWM模式可以不打开定时器中断
	PEIE = 1;
	GIE = 1;
}


/**
 * @ brief:	-T3-5*PWM模式-INV-Fosc
 * @ note: 5PWM独立输出模式，时钟源选-Fosc；T3 定时器溢出方式：0累加到 周期寄存器 T3PH:T3PL后溢出，从0再开始计时;
 * @ param:	rate-分频系数,timer_val-初值
 * @ retval:null
 */
void T3_5PWM_Indep_INV_Fosc(unsigned char rate, unsigned int timer_val)
{
	PWMSEL1= 0x01;	//---- **01
		//bit1~0:T3PWM4S<1:0>
			//00 = 无					01 = PC0 输出 T3PWM4
			//10 = PC5 输出T3PWM4		11 = PA4 输出 T3PWM4	
	PWMSEL = 0x55;	//0101 0101
		//bit7~6:T3PWM3S<1:0>
			//00 = 无					01 = PA3 输出 T3PWM3
			//10 = PC4 输出T3PWM3		11 = PA7 输出 T3PWM3
		//bit5~4:T3PWM2S<1:0>
			//00 = 无					01 = PA2 输出 T3PWM2
			//10 = PC3 输出T3PWM2		11 = PA6 输出 T3PWM2
		//bit3~2:T3PWM1S<1:0>
			//00 = 无					01 = PA1 输出 T3PWM1
			//10 = PC2 输出T3PWM1		11 = PA5 输出 T3PWM1
		//bit1~0:T3PWM0S<1:0>
			//00 = PC4 输出T3PWM0		01 = PA0 输出 T3PWM0
			//10 = PC1 输出T3PWM0		11 = PA4 输出 T3PWM0

	T3C2 = 0x3F;	//0011 1111
		//bit7:PDTM2
			//0 = T3PWM3 & T3PWM2 工作与独立输出模式-√
			//1 = T3PWM3 & T3PWM2 工作与互补输出模式
		//bit6:PDTM0
			//0 = T3PWM1 & T3PWM0 工作与独立输出模式-√
			//1 = T3PWM1 & T3PWM0 工作与互补输出模式
		//bit5:T3PWM4INV 	//0 = 不取反;			1 = 取反-√
		//bit4:T3PWM3INV  	//0 = 不取反;			1 = 取反-√
		//bit3:T3PWM2INV  	//0 = 不取反;			1 = 取反-√
		//bit2:T3PWM4EN		//0 = 禁止 T3PWM4 输出;	1 = 允许 T3PWM4 输出 -√
		//bit1:T3PWM3EN		//0 = 禁止 T3PWM3 输出;	1 = 允许 T3PWM3 输出 -√
		//bit0:T3PWM2EN		//0 = 禁止 T3PWM2 输出;	1 = 允许 T3PWM2 输出 -√
	
	T3C1 = 0;	//-0-0 -111
	T3C1|= (1<<4);	//bit4:=1-Fosc
	T3C1|= (rate&0x07);	//分频
		//bit6:T3OUT0   	//0 = T3PWM 输出不受影响;-√	1 = T3PWM 输出全低电平 
		//bit4:T3CKS0 		//0 = Fcpu/2;				1 = Fosc-√
		//bit2~0:T3CK<2:0>
			//111 = 1分频;		110 = 2分频;		101 = 4分频;	100 = 8分频
			//011 = 16分频;		010 = 32分频;		001 = 64分频;	000 = 128分频
		
	T3C0 = 0xB6;	//1-11 -11-
		//bit7:T3REN  定时器总使能;			//0 = 禁止;		1 = 使能-√
		//bit5:T3PWM1INV  T3PWM1输出极性;	//0 = 不取反;	1 = 取反-√
		//bit4:T3PWM0INV  T3PWM0输出极性;	//0 = 不取反;	1 = 取反-√
		//bit2:T3PWM1EN 输出使能;			//0 = 禁止;		1 = 启动-√
		//bit1:T3PWM0EN 输出使能;			//0 = 禁止;		1 = 启动-√
	
	//PWM 周期
	T3PH = timer_val/256;
	T3PL = timer_val%256;
	
	//duty0-pa0
	T3D0H = 10/256;
	T3D0L = 10%256;

	//duty1-pa1
	T3D1H = 1/256;
	T3D1L = 1%256;
	
	//duty2-pa2
	T3D2H = 300/256;
	T3D2L = 300%256;
	
	//duty3-pa3
	T3D3H = 500/256;
	T3D3L = 500%256;
	
	//duty4-pc0
	T3D4H = 499/256;
	T3D4L = 499%256;
		
	T3IF = 0;
	T3IE = 0;//PWM模式可以不打开定时器中断
	PEIE = 1;
	// GIE = 1;
}


/**
 * @ brief:	-T3-2*2PWM模式-互补-Fcpu
 * @ note: 2PWM互补模式，时钟源选-Fcpu；T3 定时器溢出方式：0累加到 周期寄存器 T3PH:T3PL后溢出，从0再开始计时;
 * @ param:	rate-分频系数,timer_val-初值
 * @ retval:null
 */
void T3_22PWM_Compl_Fcpu(unsigned char rate, unsigned int timer_val)
{
	T3C2 = 0x07;	//0000 0111
	T3C2 |= 0xc0;	//1100 0111
		//bit7:PDTM2
			//0 = T3PWM3 & T3PWM2 工作与独立输出模式
			//1 = T3PWM3 & T3PWM2 工作与互补输出模式 -√
		//bit6:PDTM0
			//0 = T3PWM1 & T3PWM0 工作与独立输出模式
			//1 = T3PWM1 & T3PWM0 工作与互补输出模式 -√
		//bit5:T3PWM4INV 	//0 = 不取反;	-√		1 = 取反
		//bit4:T3PWM3INV  	//0 = 不取反;	-√		1 = 取反
		//bit3:T3PWM2INV  	//0 = 不取反;	-√		1 = 取反
		//bit2:T3PWM4EN		//0 = 禁止 T3PWM4 输出;	1 = 允许 T3PWM4 输出 -√
		//bit1:T3PWM3EN		//0 = 禁止 T3PWM3 输出;	1 = 允许 T3PWM3 输出 -√
		//bit0:T3PWM2EN		//0 = 禁止 T3PWM2 输出;	1 = 允许 T3PWM2 输出 -√
	
	T3C1 = 0;	//-0-0 -000
	T3C1|=(rate&0x07);	//rate-分频（Fcpu/2）
		//bit6:T3OUT0   	//0 = T3PWM 输出不受影响;-√	1 = T3PWM 输出全低电平 
		//bit4:T3CKS0 		//0 = Fcpu/2;	-√			1 = Fosc
		//bit2~0:T3CK<2:0>
			//111 = 1分频;		110 = 2分频;		101 = 4分频;	100 = 8分频
			//011 = 16分频;		010 = 32分频;		001 = 64分频;	000 = 128分频
		
	T3C0 = 0x86;	//1-00 -11-
		//bit7:T3REN  定时器总使能;			//0 = 禁止;		1 = 使能-√
		//bit5:T3PWM1INV  T3PWM1输出极性;	//0 = 不取反;-√	1 = 取反
		//bit4:T3PWM0INV  T3PWM0输出极性;	//0 = 不取反;-√	1 = 取反
		//bit2:T3PWM1EN 输出使能;			//0 = 禁止;		1 = 启动-√
		//bit1:T3PWM0EN 输出使能;			//0 = 禁止;		1 = 启动-√
	
	//PWM 周期
	T3PH = timer_val/256;
	T3PL = timer_val%256;
	
	//duty0-pa0
	T3D0H = 50/256;
	T3D0L = 50%256;

	//duty1-pa1
	T3D1H = 20/256;
	T3D1L = 20%256;
	
	//duty2-pa2
	T3D2H = 300/256;
	T3D2L = 300%256;
	
	//duty3-pa3
	T3D3H = 50/256;
	T3D3L = 50%256;
	
	//duty4-pc0
	T3D4H = 100/256;
	T3D4L = 100%256;
		
	T3IF = 0;
	T3IE = 0;//PWM模式可以不打开定时器中断
	PEIE = 1;
	// GIE = 1;
}


/**
 * @ brief:	-T3-2*2PWM模式-互补-Fcpu-with-PWM0输出全0
 * @ note: 2组PWM互补模式，时钟源选-Fcpu；PWM0输出全0;T3 定时器溢出方式：0累加到 周期寄存器 T3PH:T3PL后溢出，从0再开始计时;
 * @ param:	rate-分频系数,timer_val-初值
 * @ retval:null
 */
void T3_22PWM_Compl_Fcpu_PWM0out0(unsigned char rate, unsigned int timer_val)
{
	T3C2 = 0x07;	//0000 0111
	T3C2 |= 0xc0;	//1100 0111
		//bit7:PDTM2
			//0 = T3PWM3 & T3PWM2 工作与独立输出模式
			//1 = T3PWM3 & T3PWM2 工作与互补输出模式 -√
		//bit6:PDTM0
			//0 = T3PWM1 & T3PWM0 工作与独立输出模式
			//1 = T3PWM1 & T3PWM0 工作与互补输出模式 -√
		//bit5:T3PWM4INV 	//0 = 不取反;	-√		1 = 取反
		//bit4:T3PWM3INV  	//0 = 不取反;	-√		1 = 取反
		//bit3:T3PWM2INV  	//0 = 不取反;	-√		1 = 取反
		//bit2:T3PWM4EN		//0 = 禁止 T3PWM4 输出;	1 = 允许 T3PWM4 输出 -√
		//bit1:T3PWM3EN		//0 = 禁止 T3PWM3 输出;	1 = 允许 T3PWM3 输出 -√
		//bit0:T3PWM2EN		//0 = 禁止 T3PWM2 输出;	1 = 允许 T3PWM2 输出 -√
	
	T3C1 = 0;	//-0-0 -000
	T3C1|=(1<<6);	//T3PWM0输出全0
	T3C1|=(rate&0x07);	//rate-分频（Fcpu/2）
		//bit6:T3OUT0   	//0 = T3PWM 输出不受影响;	1 = T3PWM 输出全低电平 -√
		//bit4:T3CKS0 		//0 = Fcpu/2;	-√			1 = Fosc
		//bit2~0:T3CK<2:0>
			//111 = 1分频;		110 = 2分频;		101 = 4分频;	100 = 8分频
			//011 = 16分频;		010 = 32分频;		001 = 64分频;	000 = 128分频
		
	T3C0 = 0x86;	//1-00 -11-
		//bit7:T3REN  定时器总使能;			//0 = 禁止;		1 = 使能-√
		//bit5:T3PWM1INV  T3PWM1输出极性;	//0 = 不取反;-√	1 = 取反
		//bit4:T3PWM0INV  T3PWM0输出极性;	//0 = 不取反;-√	1 = 取反
		//bit2:T3PWM1EN 输出使能;			//0 = 禁止;		1 = 启动-√
		//bit1:T3PWM0EN 输出使能;			//0 = 禁止;		1 = 启动-√
	
	//PWM 周期
	T3PH = timer_val/256;
	T3PL = timer_val%256;
	
	//duty0-pa0
	T3D0H = 50/256;
	T3D0L = 50%256;

	//duty1-pa1
	T3D1H = 20/256;
	T3D1L = 20%256;
	
	//duty2-pa2
	T3D2H = 300/256;
	T3D2L = 300%256;
	
	//duty3-pa3
	T3D3H = 50/256;
	T3D3L = 50%256;
	
	//duty4-pc0
	T3D4H = 100/256;
	T3D4L = 100%256;
		
	T3IF = 0;
	T3IE = 0;//PWM模式可以不打开定时器中断
	PEIE = 1;
	// GIE = 1;
}





/**
 * @ brief:	main
 * @ param:	null
 * @ retval:null
 */
void main(void) 
{
	System_Init();
	GPIO_Init();
	CLR_RAM_EveryBank();
	
	// T3 定时器溢出方式：0累加到 周期寄存器 T3PH:T3PL后溢出，从0再开始计时;
	T3_Mode = 2;
	switch(T3_Mode)	//T3PWM_Init(T3_PWM_5);
	{
		case 0: T3_Timer_Fcpu_Init(iT3_1_rate,iT3_val);				break;			
		case 1: T3_Timer_Fosc_Init(iT3_8_rate,iT3_val);				break;
		
		case 2: T3_5PWM_Indep_Fcpu(iT3_1_rate,iT3_val);				break;
		case 3: T3_5PWM_Indep_INV_Fosc(iT3_1_rate,iT3_val);			break;
		
		case 4: T3_22PWM_Compl_Fcpu(iT3_1_rate,iT3_val);			break;
		case 5:	T3_22PWM_Compl_Fcpu_PWM0out0(iT3_1_rate,iT3_val);	break;
		default: break;
	}	
	
	while(1) 
	{ 
		CLRWDT();  
		//PORTA = PORTA^0xff;	
		PORTC = PORTC^0x02;		
		DelayUs(100);
		switch(T3_Mode)	//T1_Init();
		{			
			case 5: PORTA ^= (1<<1);	break;
			default: break;
		}
	} 
} 


/**
 * @ brief:	GPIO Init
 * @ param:	null
 * @ retval:null
 */
void GPIO_Init(void) 
{
//;PORTA
	PORTA = 0b00000000;		//0000 0000	//端口输出赋值
	TRISA = 0b00000000;		//0000 0000	:0-OUT;1-IN 	//输入输出配置
	PAODCON = 0b00000000;	//0000 0000 :1-EN;0-DIS 	//开漏配置
	PAHCON = 0b11111111;	//1111 1111 :1-DIS;0-EN 	//内部上拉配置
	PADCON = 0b11111111;	//1111 1111 :1-DIS;0-EN 	//内部下拉配置
	WUACON = 0b00000000;	//0000 0000	:1-EN;0=DIS 	//端口唤醒配置
		
//;PORTC
	PORTC = 0b00000000;		//--00 0000	//端口输出赋值
	TRISC = 0b00000000;		//--00 0000	:0-OUT;1-IN 	//输入输出配置
	PCDCON = 0b11111111;	//--11 1111 :1-DIS;0-EN		//内部下拉配置
	PCHCON = 0b11111111;	//--11 1111 :1-DIS;0-EN 	//内部上拉配置
} 


/**
 * @ brief:	System_Init
 * @ param:	null
 * @ retval:null
 */ 
void System_Init(void)
{
	OPTION = 0b000000000;	 //-0-- ----
			//bit6:INTEDG：外部中断触发方式选择位（PC1）	;0-falling edge, 1-rising edge
			
	INTCON = 0b00000000;	//00-0 0-00
			//bit7:GIE 全局中断使能位
			//bit6:PEIE 外设总中断使能位
			//bit4:INTIE 外部中断使能位(PC1)
			//bit3:PAIE PORTA输入变化中断使能位
			//bit1:INTIF 外部中断标志位
			//bit0:PAIF PORTA输入变化中断标志位			
	
	PCON  = 0b10110000;		//1-1- ----	
			//Bit7：WDTE: WDT（看门狗定时器）使能位	;0-DIS, 1-EN
			//Bit5：LVREN: 低电压复位使能位			;0-DIS, 1-EN
				
	PIE1 = 0B00000000;		//-0-- ---0 
			//Bit6:ADCIE ADC中断使能位
			//Bit0:T1IE 定时器T1中断使能位 
			
	PIR1 = 0B00000000;		//-0-- ---0 
			//Bit6:ADCIE ADC转换完成标志位
			//Bit0:T1IE 定时器T1溢出标志位 
			
	PIE2 = 0B00000000;	//---- -0-0
			//Bit1:T3IE 定时器T3中断使能位
			//Bit2:T4IE 定时器T4中断使能位
			//Bit5:C1IE 比较器C1中断使能位
			
	PIR2 = 0B00000000;	//---- -0-0
			//Bit1:T3IE 定时器T3溢出标志位,软件清0
			//Bit2:T4IE 定时器T4溢出标志位,软件清0
			//Bit5:C1IE 比较器C1输出变化时被置位,软件清0
}


/**
 * @ brief:	CLR_RAM_EveryBank
 * @ note
 *  @对用户寄存器进行清0操作 
 *  @分BANK区清楚 
 *  @嵌入汇编操作 
 *  @IRP:FSR7:	间接寻址寻址空间（充分理解 间接寻址操作原理）
 *  @	0 0		BANK0
 *  @	0 1		BANK1
 *  @	1 0		BANK2
 *  @	1 1		BANK3
 * @ param:	null
 * @ retval:null
 */     
void CLR_RAM_EveryBank(void)
{
	#asm   
		MOVLW	0x5F  //个数
		MOVWF	0x20  //个数存放地址 
		MOVLW 	0x7F  //REG从后往前 清理 
		MOVWF	_FSR,F 
	CLR_RAM_LOOP:   
		CALL	CLR_RAM_Demo
		
		DECF	_FSR,F   
		DECFSZ	0X20,F   
		GOTO	CLR_RAM_LOOP
		
		CALL	CLR_RAM_Demo
		RETURN
		
	CLR_RAM_Demo:	
	//	0 1		BANK1
		//BCF	_STATUS,7	//IRP
		BSF		_FSR,7		//01:BANK1
		CLRF	_INDF  
		
	//	0 0		BANK0
		//BCF	_STATUS,7	//IRP
		BCF		_FSR,7		//00:BANK0
		CLRF	_INDF
		
		RETURN
	#endasm	
}   

/**
 * @ brief:	Delay
 * @ note
 *  @ Fcpu=1MHz;DelayUs = 11+7x
 *  @ 不同时钟Fcpu，注意时间差异
 * @ param:	null
 * @ retval:null
 */
//****delay function****// 
void DelayUs(unsigned char delayUs)
{
	while(delayUs--);
} 

//==============================================
//1.>中断函数
//;Bank0~3 编译系统会自动切换,用户不需要手动切换; 
//==============================================
void interrupt myIsr(void)    
{
	//----User Code ----/
	#if 1
	if(T3IF)	//T3
	{
		T3IF = 0;
		//timer
//		T3TH = (0xfff-iT3_val)/256;
//		T3TL = (0xfff-iT3_val)%256;		
		
		PORTC ^= 1<<2;
		
		T3_Cnt++;
		if(T3_Cnt > 4)
		{
			T3_Cnt = 0;
		}
	}
	#endif
}