/******************************************************************************
  * mc32p8141 TouchDemo_MC32P8141_8Key程序
  * 说明：
  * 1.可实现8键触控，针对于Demo开发
  * 2.连接引脚到TK焊盘上，通过手触摸，对应的LED会点亮
  * 3.模拟串口打印TK数据功能
  *	   	客户可通过外接串口工具，使能串口函数Uart_Process()，把TK数据打印出来分析
  ******************************************************************************
  * 几个特殊指令用法
  *	   	stop指令： Stop();
  *	   	清狗指令： Clrwdt();
  *	   	空指令：   Nop();
  ******************************************************************************
  * 仿真软件版本
  *	   	WinScopeIDE_Beta_v1.14.11_20200302
  *	
 ******************************************************************************/
#include "mc32p8141.h"
//引脚定义
#define    	   	   	   	RXD            	   	   	   	   	P03D
#define    	   	   	   	TXD        	       	   	   	   	P02D
#define    	   	   	   	Key        	       	   	   	   	P04D
#define    	   	   	   	BUZZUR         	   	   	   	   	P05D
/*
触控端口控制器
*/
#define    	   	   	   	TKN0_OE            	   	   	P16OE 
#define    	   	   	   	TKN1_OE            	   	   	P15OE
#define    	   	   	   	TKN2_OE            	   	   	P14OE
#define    	   	   	   	TKN3_OE            	   	   	P13OE
#define    	   	   	   	TKN4_OE            	   	   	P12OE
#define    	   	   	   	TKN5_OE            	   	   	P11OE
#define    	   	   	   	TKN6_OE            	   	   	P10OE
#define    	   	   	   	TKN7_OE            	   	   	P05OE

#define    	   	   	   	TKN0_ADCR          	   	   	P16DC 
#define    	   	   	   	TKN1_ADCR          	   	   	P15DC
#define    	   	   	   	TKN2_ADCR          	   	   	P14DC
#define    	   	   	   	TKN3_ADCR          	   	   	P13DC
#define    	   	   	   	TKN4_ADCR          	   	   	P12DC
#define    	   	   	   	TKN5_ADCR          	   	   	P11DC
#define    	   	   	   	TKN6_ADCR          	   	   	P10DC
#define    	   	   	   	TKN7_ADCR          	   	   	P05DC

/*
触控指示灯，分别对应Touch Board的S1-S5
*/
#define    	   	   	   	LED1               	   	   	P17D
#define    	   	   	   	LED2               	   	   	P00D
#define    	   	   	   	LED3               	   	   	P01D
#define    	   	   	   	LED4               	   	   	P06D
#define    	   	   	   	LED5               	   	   	P07D
/*设置用到的TK总数及选用的TK通道*/
#define	   	   	   	   	TK_NUM 	   	   	   	   	   	   	   	5
#define	   	   	   	   	TK_Channel0	   	   	   	   	   	1
#define	   	   	   	   	TK_Channel1	   	   	   	   	   	5
#define	   	   	   	   	TK_Channel2	   	   	   	   	   	4
#define	   	   	   	   	TK_Channel3	   	   	   	   	   	3
#define	   	   	   	   	TK_Channel4	   	   	   	   	   	2
#define	   	   	   	   	TK_Channel5	   	   	   	   	   	
#define	   	   	   	   	TK_Channel6	   	   	   	   	   	
#define	   	   	   	   	TK_Channel7	
/*
变量
*/
uchar  	   	ABuf,StatusBuf;                	
uint    TK_Value;  	//采样数据原始值
uint    TK_BaseSamples[TK_NUM];    	//触摸键基准值
uint    TK_BaseSamples_Buf[TK_NUM]; 
uint   	   	TK_BaseSamples_Temp[TK_NUM];   	
uchar   TK_BaseSamples_Time[TK_NUM];   	//触摸键基准采样次数

uchar  	   	TK_Threshold_Channel[8];//TK检测阈值
uchar   TK_Channel_NUM;	//触控按键号
uchar  	   	TK_Channel_NUM_Temp;//触控按键输入通道
uchar   TK_BaseSamples_Cnt;
uchar  	   	Flag_TK;
uint   	   	Buz_Cnt;
uchar 		len ;
uchar 		TK_State;
#define	   	   	Uart_Mode  	   	   	   	   	   	0
#define	   	   	Recive_Data_Max	   	   	2
#if	   	   	Uart_Mode == 1
uchar   DATA_Send,DATA_buf;
uchar  	   	Uart_Recive_Bit,Recive_Temp;
uchar  	   	TK_Value_H_Temp,TK_Value_L_Temp;
uchar  	   	Recive_Data_Table[Recive_Data_Max];
uchar  	   	Recive_Data_Number;
void   	UART_Recive_Data();
void   	UART_Send_Data(uchar DATA_buf);
#endif


typedef union{
unsigned char byte;
struct{
           	unsigned char bit0:1;
           	unsigned char bit1:1;
           	unsigned char bit2:1;
           	unsigned char bit3:1;
           	unsigned char bit4:1;
           	unsigned char bit5:1;
           	unsigned char bit6:1;
           	unsigned char bit7:1;
}bits;
}flag;


flag flag1;
#define        	Flag_TK_Read   	   	   	   	   	flag1.bits.bit0	   	   	   	   	
#define        	   	F_Recive_Over  	   	   	   	flag1.bits.bit1
#define	   	   	   	   	F_Recive   	   	   	   	   	   	   	flag1.bits.bit2
#define	   	   	   	   	Bit_Flag   	   	   	   	   	   	   	flag1.bits.bit3

#define    	   	   	   	off    	   	   	   	  0
#define    	   	   	   	on     	   	   	   	  1
   	   	   	   	   	
/*TK软件配置*/
#if    	TK_NUM == 1
const uchar Channel_table[TK_NUM]  = {TK_Channel0};
#elif TK_NUM == 2
const uchar Channel_table[TK_NUM]  = {TK_Channel0, TK_Channel1};
#elif TK_NUM == 3
const uchar Channel_table[TK_NUM]  = {TK_Channel0, TK_Channel1, TK_Channel2};
#elif TK_NUM == 4
const uchar Channel_table[TK_NUM]  = {TK_Channel0, TK_Channel1, TK_Channel2, TK_Channel3};
#elif TK_NUM == 5
const uchar Channel_table[TK_NUM]  = {TK_Channel0, TK_Channel1, TK_Channel2, TK_Channel3, TK_Channel4};
#elif TK_NUM == 6
const uchar Channel_table[TK_NUM]  = {TK_Channel0, TK_Channel1, TK_Channel2, TK_Channel3, TK_Channel4, TK_Channel5};
#elif TK_NUM == 7
const uchar Channel_table[TK_NUM]  = {TK_Channel0, TK_Channel1, TK_Channel2, TK_Channel3, TK_Channel4, TK_Channel5, TK_Channel6};
#elif TK_NUM == 8
const uchar Channel_table[TK_NUM]  = {TK_Channel0, TK_Channel1, TK_Channel2, TK_Channel3, TK_Channel4, TK_Channel5, TK_Channel6, TK_Channel7};
#endif
/*TK检测阈值选择*/

#define TK_BaseSamples_perscan 	   	   	   	   	   	4  	//基准值更新次数，推荐为2的N次方
#define	TK_Scan_Total  	   	   	   	   	   	   	   	   	   	   	TK_NUM*8  //TK采样总数   倍数可以设置，当按键数较多时可将倍数调小，
   	   	   	   	   	   	   	   	   	   	   	   	   	   	   	   	//若基准值更新速度太慢会影响到其他按键的判断。当按键数较少时可将倍数放大，提高抗干扰性。
#define	TK_Release_Threshold   	   	   	   	   	   	   	20
#define	TK_BaseSamples_Updata_Threshold	   	30

/************************************************
函数名:   	Init_Sys
说明:
************************************************/
void  Init_Sys(void)
{  	   	   	
//========= Io初始化 ==============
    OEP0=0xE7;
    OEP1=0xC1;
    
   	IOP0=0x00;
   	IOP1=0x00;
   	
   	PUP0=0x18;
   	PUP1=0x00;
   	PUP1=0;
   	PDP1=0;
   	P03KE=1;
   	KBIE=1;

   	KBIF=0;
   	TXD=1;
//========= 寄存器初始化 ==============
   	TK_Channel_NUM_Temp=Channel_table[0];
} 
/************************************************
函数名:   	Clr_Ram
说明:
************************************************/
void  Clr_Ram(void)
{
   	__asm
ClrRam:
       	movai  	0x7F
       	movra  	FSR0
ClrRam_Loop:
       	clrr   	INDF0
       	djzr    FSR0
   	   	goto   	ClrRam_Loop
   	   	clrr   	INDF0      	   	
    __endasm;
}   
/************************************************
函数名:   	Init_timer
说明:
************************************************/
void  Init_timer(void)
{
   	   	T0CR=0x44;
   	   	T1CR=0x03;
   	   	T2CR=0x01;
   	   	T1CNT=250;
   	   	T1LOAD=250;
   	   	T0CNTH=0Xf0;
   	   	T0CNTL=0X7F;
   	   	T0LOADH=0Xf0;
   	   	T0LOADL=0X7F;  	   	//16MS
   	   	TKCR0=0XD8;
   	   	TKCR1=0X80;                
   	   	TKCR1=TKCR1&0xf8;
   	   	TKCR1=TKCR1+TK_Channel_NUM_Temp;
   	   	T0IE=1;
   	   	T0EN=1;
   	   	T1IE=1;
   	   	T1EN=1;
   	   	T2IE=1;
   	   	T2EN=1;
   	   	GIE= 1;    	   	//开总中断     	   	       	    
}
#if	Uart_Mode == 1
/************************************************
函数名:   	UART_Deley
说明:	
************************************************/
void  UART_Deley(void)
{
   	   	while(!Bit_Flag);
   	   	Bit_Flag=0;
}

/************************************************
函数名:   	UART_Send_Data
说明:	CPU=2M 9600波特率
************************************************/
void  UART_Send_Data(uchar DATA_buf)
{
   	   	uchar  	i;
   	   	if(F_Recive==0)
   	   	{
   	   	   	   	T2CNT=101;
   	   	   	   	T2LOAD=101;
   	   	}
   	   	Bit_Flag=0;
   	TXD=0;
   	UART_Deley();//起始位
   	for(i=0;i<8;i++)
   	{
   	   	   	DATA_Send=DATA_buf&0x01;
   	   	   	if(DATA_Send)
   	   	   	{
   	   	   	   	TXD=1;
   	   	   	}
   	   	   	else 
   	   	   	{
   	   	   	   	TXD=0;
   	   	   	}
   	   	   	DATA_buf=DATA_buf>>1;
   	   	   	UART_Deley();
   	}
   	TXD=1;
   	UART_Deley();
}  	

/* 	***********************************************
函  	数名:    	UART_Recive_Data
说  	明: 	CPU=2M 9600波特率
** 	**********************************************/
void  UART_Recive_Data()
{  	
   	   	if(Uart_Recive_Bit==0)
   	   	{
   	   	   	   	T2CNT=102;
   	   	   	   	T2LOAD=102;//Start Bit
   	   	   	   	Uart_Recive_Bit++;
   	   	}
   	   	else   	if(Uart_Recive_Bit<9)
   	   	{
   	   	   	   	Recive_Temp>>=1;
   	   	   	   	if(RXD)
   	   	   	   	{
   	   	   	   	   	Recive_Temp|=0x80;
   	   	   	   	}
   	   	   	   	else
   	   	   	   	{
   	   	   	   	   	Recive_Temp&=0x7f;
   	   	   	   	}
   	   	   	   	Uart_Recive_Bit++;
   	   	}
   	   	else   	   	//停止位
   	   	{
   	   	   	KBIF=0;
   	   	   	   	KBIE=1;
   	   	   	   	Uart_Recive_Bit=0;
   	   	   	   	Recive_Data_Table[Recive_Data_Number]=Recive_Temp;
   	   	   	   	Recive_Data_Number++;
   	   	   	   	if(Recive_Data_Number>=Recive_Data_Max)
   	   	   	   	{
   	   	   	   	   	   	Recive_Data_Number=0;
   	   	   	   	   	   	F_Recive=0;
   	   	   	   	   	   	F_Recive_Over=1;
   	   	   	   	}
   	   	   	   	
   	   	} 
} 
/************************************************
函数名:   	Uart_Send_Process
说明: 
************************************************/ 
void   	Uart_Send_Process()
{

   	   	TK_Value_H_Temp=TK_Value>>8;
   	   	TK_Value_L_Temp=TK_Value&0xff;
   	   	UART_Send_Data(0x55);
   	   	UART_Send_Data(TK_Channel_NUM);
   	   	UART_Send_Data(TK_Value_H_Temp); 
   	   	UART_Send_Data(TK_Value_L_Temp);  
   	   	UART_Send_Data(TK_Threshold_Channel[TK_Channel_NUM_Temp]);
   	   	UART_Send_Data(0xAA);

}  	   	 
/************************************************
函数名:   	Uart_Recive_Process
说明: 
************************************************/
void   	Uart_Recive_Process()
{
   	   	if(F_Recive_Over)
   	   	{
   	   	   	   	F_Recive_Over=0;
   	   	   	   	switch(Recive_Data_Table[0])
   	   	   	   	{
   	   	   	   	case 0:
   	   	   	   	   	TK_Threshold_Channel[TK_Channel0]=Recive_Data_Table[1];
   	   	   	   	   	break;
   	   	   	   	case 1:
   	   	   	   	   	TK_Threshold_Channel[TK_Channel1]=Recive_Data_Table[1];
   	   	   	   	   	break;
   	   	   	   	case 2:
   	   	   	   	   	TK_Threshold_Channel[TK_Channel2]=Recive_Data_Table[1];
   	   	   	   	   	break;
   	   	   	   	case 3:
   	   	   	   	   	TK_Threshold_Channel[TK_Channel3]=Recive_Data_Table[1];
   	   	   	   	   	break;
   	   	   	   	case 4:
   	   	   	   	   	TK_Threshold_Channel[TK_Channel4]=Recive_Data_Table[1];
   	   	   	   	   	break;
   	   	   	   	default:
   	   	   	   	   	break;
   	   	   	   	}
   	   	}
}
#endif
/************************************************
函数名: Touch_Value_Deal
说明: 
************************************************/ 
void Touch_Value_Deal()
{
   	if(Flag_TK_Read)
   	{
   	   	Flag_TK_Read=0;
   	   	   	//基准值更新
   	   	TK_BaseSamples_Cnt++;
   	   	if(TK_BaseSamples_Cnt<=TK_NUM)
   	   	{      	   	   	   	
   	   	   	if(TK_BaseSamples_Temp[TK_Channel_NUM]>TK_Value)   	  
   	   	   	   	   	   	{
   	   	   	   	   	   	   	   	if((TK_BaseSamples_Temp[TK_Channel_NUM]-TK_Value)<TK_BaseSamples_Updata_Threshold)
   	   	   	   	   	   	   	   	{
   	   	   	   	   	   	   	   	   	   	TK_BaseSamples_Temp[TK_Channel_NUM]=TK_Value;
   	   	   	   	   	   	   	   	}
   	   	   	   	   	   	   	   	else
   	   	   	   	   	   	   	   	{
   	   	   	   	   	   	   	   	   	   	TK_BaseSamples_Temp[TK_Channel_NUM]=TK_BaseSamples_Temp[TK_Channel_NUM];
   	   	   	   	   	   	   	   	}
   	   	   	   	   	   	}
   	   	   	   	   	   	else
   	   	   	   	   	   	{
   	   	   	   	   	   	   	   	TK_BaseSamples_Temp[TK_Channel_NUM]=TK_Value;
   	   	   	   	   	   	}
   	   	   	   	   	   	TK_BaseSamples_Buf[TK_Channel_NUM]+=TK_BaseSamples_Temp[TK_Channel_NUM];
   	   	   	   	   	   	if(++TK_BaseSamples_Time[TK_Channel_NUM]>=TK_BaseSamples_perscan)
   	   	   	{
   	   	   	   	   	TK_BaseSamples_Time[TK_Channel_NUM]=0;
   	   	   	   	   	TK_BaseSamples[TK_Channel_NUM]=TK_BaseSamples_Buf[TK_Channel_NUM]/TK_BaseSamples_perscan;
   	   	   	   	   	TK_BaseSamples_Buf[TK_Channel_NUM]=0;      	   	   	   	
   	   	   	}
   	   	}
   	   	else if(TK_BaseSamples_Cnt>=TK_Scan_Total)
   	   	{
   	   	   	   	TK_BaseSamples_Cnt=0;
   	   	}  
   	   	
   	   	   	//采样值与基准值做比较   	   	
   	   	   	   	 
   	   	   	   	if(TK_Value<TK_BaseSamples[TK_Channel_NUM])
   	   	{
   	   	   	   	   	   	switch (TK_Channel_NUM)
   	   	   	   	   	   	{
   	   	   	   	   	   	   	#if TK_NUM >= 1
   	   	   	   	   	   	case 0:
   	   	   	   	   	   	   	if((TK_BaseSamples[TK_Channel_NUM]-TK_Value)>TK_Threshold_Channel[TK_Channel0])
   	   	   	   	   	   	   	{
   	   	   	   	   	   	   	   	   	Flag_TK|=1<<TK_Channel0;
   	   	   	   	   	   	   	}

   	   	   	   	   	   	   	else   	if((TK_BaseSamples[TK_Channel_NUM]-TK_Value)<TK_Release_Threshold)
   	   	   	   	   	   	   	{
   	   	   	   	   	   	   	   	Flag_TK&=~(1<<TK_Channel0);
   	   	   	   	   	   	   	}
   	   	   	   	   	   	   	break;
   	   	   	   	   	   	   	#endif

   	   	   	   	   	   	   	#if	TK_NUM >= 2
   	   	   	   	   	   	case 1:
   	   	   	   	   	   	   	if((TK_BaseSamples[TK_Channel_NUM]-TK_Value)>TK_Threshold_Channel[TK_Channel1])
   	   	   	   	   	   	   	{
   	   	   	   	   	   	   	   	   	Flag_TK|=1<<TK_Channel1;
   	   	   	   	   	   	   	}
   	
   	   	   	   	   	   	   	else   	if((TK_BaseSamples[TK_Channel_NUM]-TK_Value)<TK_Release_Threshold)
   	   	   	   	   	   	   	{
   	   	   	   	   	   	   	   	Flag_TK&=~(1<<TK_Channel1);
   	   	   	   	   	   	   	}
   	   	   	   	   	   	   	break;
   	   	   	   	   	   	   	#endif

   	   	   	   	   	   	   	#if	TK_NUM >= 3
   	   	   	   	   	   	case 2:
   	   	   	   	   	   	   	if((TK_BaseSamples[TK_Channel_NUM]-TK_Value)>TK_Threshold_Channel[TK_Channel2])
   	   	   	   	   	   	   	{
   	   	   	   	   	   	   	   	   	Flag_TK|=1<<TK_Channel2;
   	   	   	   	   	   	   	}
   	
   	   	   	   	   	   	   	else   	if((TK_BaseSamples[TK_Channel_NUM]-TK_Value)<TK_Release_Threshold)
   	   	   	   	   	   	   	{
   	   	   	   	   	   	   	   	Flag_TK&=~(1<<TK_Channel2);
   	   	   	   	   	   	   	}
   	   	   	   	   	   	   	break;
   	   	   	   	   	   	   	#endif

   	   	   	   	   	   	   	#if	TK_NUM >= 4
   	   	   	   	   	   	case 3:
   	   	   	   	   	   	   	if((TK_BaseSamples[TK_Channel_NUM]-TK_Value)>TK_Threshold_Channel[TK_Channel3])
   	   	   	   	   	   	   	{
   	   	   	   	   	   	   	   	   	Flag_TK|=1<<TK_Channel3;
   	   	   	   	   	   	   	}
   	
   	   	   	   	   	   	   	else   	if((TK_BaseSamples[TK_Channel_NUM]-TK_Value)<TK_Release_Threshold)
   	   	   	   	   	   	   	{
   	   	   	   	   	   	   	   	Flag_TK&=~(1<<TK_Channel3);
   	   	   	   	   	   	   	}
   	   	   	   	   	   	   	break;
   	   	   	   	   	   	   	#endif

   	   	   	   	   	   	   	#if	TK_NUM >= 5
   	   	   	   	   	   	case 4:
   	   	   	   	   	   	   	if((TK_BaseSamples[TK_Channel_NUM]-TK_Value)>TK_Threshold_Channel[TK_Channel4])
   	   	   	   	   	   	   	{
   	   	   	   	   	   	   	   	   	Flag_TK|=1<<TK_Channel4;
   	   	   	   	   	   	   	}
   	
   	   	   	   	   	   	   	else   	if((TK_BaseSamples[TK_Channel_NUM]-TK_Value)<TK_Release_Threshold)
   	   	   	   	   	   	   	{
   	   	   	   	   	   	   	   	Flag_TK&=~(1<<TK_Channel4);
   	   	   	   	   	   	   	}
   	   	   	   	   	   	   	break;
   	   	   	   	   	   	   	#endif

   	   	   	   	   	   	   	#if	TK_NUM >= 6
   	   	   	   	   	   	case 5:
   	   	   	   	   	   	   	if((TK_BaseSamples[TK_Channel_NUM]-TK_Value)>TK_Threshold_Channel[TK_Channel5])
   	   	   	   	   	   	   	{
   	   	   	   	   	   	   	   	   	Flag_TK|=1<<TK_Channel5;
   	   	   	   	   	   	   	}
   	
   	   	   	   	   	   	   	else   	if((TK_BaseSamples[TK_Channel_NUM]-TK_Value)<TK_Release_Threshold)
   	   	   	   	   	   	   	{
   	   	   	   	   	   	   	   	Flag_TK&=~(1<<TK_Channel5);
   	   	   	   	   	   	   	}
   	   	   	   	   	   	   	break;
   	   	   	   	   	   	   	#endif

   	   	   	   	   	   	   	#if	TK_NUM >= 7
   	   	   	   	   	   	case 6:
   	   	   	   	   	   	   	if((TK_BaseSamples[TK_Channel_NUM]-TK_Value)>TK_Threshold_Channel[TK_Channel6])
   	   	   	   	   	   	   	{
   	   	   	   	   	   	   	   	   	Flag_TK|=1<<TK_Channel6;
   	   	   	   	   	   	   	}
   	
   	   	   	   	   	   	   	else   	if((TK_BaseSamples[TK_Channel_NUM]-TK_Value)<TK_Release_Threshold)
   	   	   	   	   	   	   	{
   	   	   	   	   	   	   	   	Flag_TK&=~(1<<TK_Channel6);
   	   	   	   	   	   	   	}
   	   	   	   	   	   	   	break;
   	   	   	   	   	   	   	#endif

   	   	   	   	   	   	   	#if	TK_NUM >= 8
   	   	   	   	   	   	case 7:
   	   	   	   	   	   	   	if((TK_BaseSamples[TK_Channel_NUM]-TK_Value)>TK_Threshold_Channel[TK_Channel7])
   	   	   	   	   	   	   	{
   	   	   	   	   	   	   	   	   	Flag_TK|=1<<TK_Channel7;
   	   	   	   	   	   	   	}
   	
   	   	   	   	   	   	   	else   	if((TK_BaseSamples[TK_Channel_NUM]-TK_Value)<TK_Release_Threshold)
   	   	   	   	   	   	   	{
   	   	   	   	   	   	   	   	Flag_TK&=~(1<<TK_Channel7);
   	   	   	   	   	   	   	}
   	   	   	   	   	   	   	break;
   	   	   	   	   	   	   	#endif

   	   	   	   	   	   	default:
   	   	   	   	   	   	   	break;
   	   	   	   	   	   	}
   	   	   	   	   	   	 
   	   	   	   	}
   	   	   	   	else
   	   	   	   	{
   	   	   	   	   	   	switch (TK_Channel_NUM)
   	   	   	   	   	   	{
   	   	   	   	   	   	   	#if TK_NUM >= 1
   	   	   	   	   	   	case 0:
   	   	   	   	   	   	   	Flag_TK&=~(1<<TK_Channel0);
   	   	   	   	   	   	   	break;
   	   	   	   	   	   	   	#endif

   	   	   	   	   	   	   	#if	TK_NUM >= 2
   	   	   	   	   	   	case 1:
   	   	   	   	   	   	   	Flag_TK&=~(1<<TK_Channel1);
   	   	   	   	   	   	   	break;
   	   	   	   	   	   	   	#endif

   	   	   	   	   	   	   	#if	TK_NUM >= 3
   	   	   	   	   	   	case 2:
   	   	   	   	   	   	   	Flag_TK&=~(1<<TK_Channel2);
   	   	   	   	   	   	   	break;
   	   	   	   	   	   	   	#endif

   	   	   	   	   	   	   	#if	TK_NUM >= 4
   	   	   	   	   	   	case 3:
   	   	   	   	   	   	   	Flag_TK&=~(1<<TK_Channel3);
   	   	   	   	   	   	   	break;
   	   	   	   	   	   	   	#endif

   	   	   	   	   	   	   	#if	TK_NUM >= 5
   	   	   	   	   	   	case 4:
   	   	   	   	   	   	   	Flag_TK&=~(1<<TK_Channel4);
   	   	   	   	   	   	   	break;
   	   	   	   	   	   	   	#endif

   	   	   	   	   	   	   	#if	TK_NUM >= 6
   	   	   	   	   	   	case 5:
   	   	   	   	   	   	   	Flag_TK&=~(1<<TK_Channel5);
   	   	   	   	   	   	   	break;
   	   	   	   	   	   	   	#endif

   	   	   	   	   	   	   	#if	TK_NUM >= 7
   	   	   	   	   	   	case 6:
   	   	   	   	   	   	   	Flag_TK&=~(1<<TK_Channel6);
   	   	   	   	   	   	   	break;
   	   	   	   	   	   	   	#endif

   	   	   	   	   	   	   	#if	TK_NUM >= 8
   	   	   	   	   	   	case 7:
   	   	   	   	   	   	   	Flag_TK&=~(1<<TK_Channel7);
   	   	   	   	   	   	   	break;
   	   	   	   	   	   	#endif

   	   	   	   	   	   	default:
   	   	   	   	   	   	   	break;
   	   	   	   	   	   	}
   	   	   	   	}
   	   	   	   	#if	Uart_Mode == 1
   	   	   	   	Uart_Send_Process();
   	   	   	   	#endif
   	   	//切换TK通道，配置下一个TK采集                               
   	   	P1ADCR=0X00;
   	   	OEP1=OEP1|0X3E;
   	   	IOP1=IOP1&0XC1;
   	   	TK_Channel_NUM++;
   	   	if(TK_Channel_NUM>=TK_NUM)
   	   	{
   	   	   	   	TK_Channel_NUM=0;
   	   	}
   	   	   	   	TK_Channel_NUM_Temp=Channel_table[TK_Channel_NUM];
   	   	   	   	switch (TK_Channel_NUM_Temp)
   	   	   	   	{
   	   	   	   	case 0:
   	   	   	   	   	TKN0_OE=0;
   	   	   	   	   	TKN0_ADCR=1;
   	   	   	   	   	break;
   	   	   	   	case 1:
   	   	   	   	   	TKN1_OE=0;
   	   	   	   	   	TKN1_ADCR=1;
   	   	   	   	   	break;
   	   	   	   	case 2:
   	   	   	   	   	TKN2_OE=0;
   	   	   	   	   	TKN2_ADCR=1;
   	   	   	   	   	break;
   	   	   	   	case 3:
   	   	   	   	   	TKN3_OE=0;
   	   	   	   	   	TKN3_ADCR=1;
   	   	   	   	   	break;
   	   	   	   	case 4:
   	   	   	   	   	TKN4_OE=0;
   	   	   	   	   	TKN4_ADCR=1;
   	   	   	   	   	break;
   	   	   	   	case 5:
   	   	   	   	   	TKN5_OE=0;
   	   	   	   	   	TKN5_ADCR=1;
   	   	   	   	   	break;
   	   	   	   	case 6:
   	   	   	   	   	TKN6_OE=0;
   	   	   	   	   	TKN6_ADCR=1;
   	   	   	   	   	break;
   	   	   	   	case 7:
   	   	   	   	   	TKN7_OE=0;
   	   	   	   	   	TKN7_ADCR=1;
   	   	   	   	   	break;
   	   	   	   	default:
   	   	   	   	   	break;
   	   	   	   	}
   	   	
   	   	TKCR1=TKCR1&0xf8;
   	   	TKCR1=TKCR1+TK_Channel_NUM_Temp;
       	
   	   	TKCON=0;
   	   	TKCNTH=0;
   	   	TKCNTL=0;  	//注意一定要手动清掉TK值，软件不会自动清
   	   	TKCON=1; 
   	   	T0EN=1;  //重新开启新的TK采集

   	}  	   	
 
   	
}  
/************************************************
函数名: LED_Process
说明: 
************************************************/ 
void LED_Process()
{
   	TK_State = Flag_TK;
   	for (len = 0; len < TK_NUM; len++)
   	{
   	   	switch (len)
   	   	{
   	   	case 0:
   	   	  if (TK_State & (1 << TK_Channel0)) 
   	   	  {// 点亮第一个LED灯
   	   	    LED1 = on;
   	   	  }
   	   	  else
   	   	  {
   	   	    LED1 = off;
   	   	  }
   	   	  break;
   	   	case 1:
   	   	  if (TK_State & (1 << TK_Channel1)) 
   	   	  {// 点亮第二个LED灯
   	   	    LED5 = on;
   	   	  }
   	   	  else
   	   	  {
   	   	    LED5 = off;
   	   	  }
   	   	  break;
   	   	case 2:
   	   	  if (TK_State & (1 << TK_Channel2)) 
   	   	  {// 点亮第三个LED灯
   	   	    LED4 = on;
   	   	  }
   	   	  else
   	   	  {
   	   	    LED4 = off;
   	   	  }
   	   	  break;
   	   	case 3:
   	   	  if (TK_State & (1 << TK_Channel3)) 
   	   	  {// 点亮第四个LED灯
   	   	    LED3 = on;
   	   	  }
   	   	  else
   	   	  {
   	   	    LED3 = off;
   	   	  }
   	   	  break;
   	   	case 4:
   	   	  if (TK_State & (1 << TK_Channel4))  
   	   	  {// 点亮第五个LED灯
   	   	    LED2 = on;
   	   	  }
   	   	  else
   	   	  {
   	   	    LED2 = off;
   	   	  }
   	   	  break;
   	   	
   	   	default:
   	   	  break;
   	   	}
   	}  	

}
   	   	   	           	
/************************************************
函数名:主函数
说明:
************************************************/
void main()
{      	                 
    Init_Sys();
   	Clr_Ram();
   	Init_timer();
   	TK_Threshold_Channel[TK_Channel0]=0x41;
   	TK_Threshold_Channel[TK_Channel1]=0x4A;
   	TK_Threshold_Channel[TK_Channel2]=0x3C;
   	TK_Threshold_Channel[TK_Channel3]=0x49;
   	TK_Threshold_Channel[TK_Channel4]=0x46;
   	   	Buz_Cnt=200;
    while(1) 
    {   
       	   	Touch_Value_Deal();
   	   	LED_Process();
   	   	   	   	#if	Uart_Mode == 1
   	   	   	   	Uart_Recive_Process();
   	   	   	   	#endif
   	}
}
   	       	       	       	       	       	


/************************************************
函数名:中断程序
说明:
************************************************/
void int_isr(void) __interrupt
{
       	__asm
       	movra   _ABuf
       	swapar  PFLAG
       	movra   _StatusBuf
       	__endasm;
//========= T0/T1中断 ==============     	
       	if(T0IF&&T0IE)
       	{      	       	
       	   	T0IF   = 0; 
       	   	TK_Value=TKCNTH;
       	   	TK_Value=TK_Value<<4;
       	   	TK_Value=TK_Value&0xFF0;           	   	
   	   	   	TK_Value=TK_Value+((TKCNTL>>4)&0x0f); 
   	   	   	
   	   	   	Flag_TK_Read=1;
   	   	   	   	       	
       	}
       	if(T1IF&&T1IE)
       	{
       	   	   	T1IF=0;
       	   	   	if(Buz_Cnt!=0)
       	   	   	{
       	   	   	   	   	Buz_Cnt--;
       	   	   	   	   	BUZZUR=1;
   	
       	   	   	}
       	   	   	else
       	   	   	{
       	   	   	   	   	BUZZUR=0;
       	   	   	}
       	}
   	   	   	   	
       	if(T2IF&&T2IE)
       	{
       	   	   	T2IF=0;
   	   	   	   	#if	Uart_Mode == 1
       	   	   	Bit_Flag=1;
   	   	   	   	   	   	if(F_Recive)
   	   	   	   	   	   	{
   	   	   	   	   	   	   	   	UART_Recive_Data();
   	   	   	   	   	   	}
       	   	 
   	   	   	   	#endif
       	}
       	if(KBIE&&KBIF)
       	{
       	   	   	KBIF=0;
   	   	   	   	 #if   	Uart_Mode == 1 	
       	   	   	   	if(RXD==0) 	   	   	   	   	//下降沿
       	   	   	   	{  	   	   	   	   	   	   	   	
       	   	   	   	   	   	KBIE=0;
       	   	   	   	   	   	KBIF=0;
   	   	   	   	   	   	   	   	T2CNT=50;
       	   	   	   	   	   	T2LOAD=50;
       	   	   	   	   	   	F_Recive=1;
       	   	   	   	}
   	   	   	   	   	#endif
       	   	}
   	   	   	   	
   	   	if(TKIF)
   	   	{
   	   	   	TKIF=0;
   	   	}
       	if(ADIF)
       	{      	       	
       	   	ADIF  = 0;             	       	
       	}          	
//========= 外部中断 ==============

       	__asm
       	swapar  _StatusBuf
       	movra   PFLAG
       	swapr   _ABuf
       	swapar  _ABuf
       	__endasm;
}