/****************************************************************************************
 * 文件名:debug_touch.c
 * 版 本:   V1.0
 * 日 期:   2015-4-01
 * 作 者:  上海芯旺微电子技术有限公司
 * 说明：    电容触摸库调试输出函数文件实体
 *
 ****************************************************************************************/
#include "debug_touch.h"
#if 1
//数据上传允许位，至少需要保持1路，使能PC通道选取，默认开启第一通道，当需要省RAM时，可固定通道输出
unsigned char  TOUCH_CH_TRS_EN[16]={
									0,0,0,0,0,0,0,0,
									0,0,0,0,0,0,0,0,
									};
unsigned char T_buf,i_T_D;
volatile unsigned char sentbuf1,sentbuf2,sentbuf3,sentbuf4;

signed int 	Date_Baihualv_Get_V;	//  先获取，可以实现符号转变便于观察


void TOUCH_DEBUG_TRS_BAIHUALV()
{
	  unsigned char j,step;
	  // 判断当前通道允许上传时进行上传过程
	#if 0
	    TOUCH_CH_TRS_EN[_KF8_TOUCH_CH_EN[Touch_Channel_Protect]]=1;
	#else
	    __asm
	    MOV R1,#High(__KF8_TOUCH_CH_EN+0)
	    MOV R0,#(Touch_Channel_Protect)
	    ADD R0,#Low(__KF8_TOUCH_CH_EN+0)
	    JNB PSW,0
	    INC R1
	    PAGESEL __R2PCL
	    CALL __R2PCL
	    PAGESEL $
	    ADD R0,#(_TOUCH_CH_TRS_EN+0)
	    MOV R1,#0x01
	    BANKSEL _TOUCH_CH_TRS_EN
	    ST [R0],R1
	    __endasm;
	#endif
	    //;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
	#if 0
		if(TOUCH_CH_TRS_EN[_TOUCH_CH_EN[_KF8_LIBc_channel_]])
	#else
			__asm
			BANKSEL __KF8_LIBc_channel_
			MOV R0,__KF8_LIBc_channel_
			ADD R0,#Low(__KF8_TOUCH_CH_EN+0)
			MOV R1,#High(__KF8_TOUCH_CH_EN+0)
			JNB PSW,0
			INC R1
			PAGESEL __R2PCL
			CALL __R2PCL
			PAGESEL $
			ADD R0,#(_TOUCH_CH_TRS_EN+0)
			BANKSEL _TOUCH_CH_TRS_EN
			LD R1,[R0]
			XOR R1,#0x00
			JNB PSW,2
			JMP Label_x_y_z_a_1
			__endasm;
	#endif
		{
			CLK_SET_IN;
			DAT_SET_IN;
			Touch_Delay1;

		//获取上传权限,高clk有效允许传输

			if(CLK_GET_STATE)
			{
				Touch_Delay1;
				if(!CLK_GET_STATE)
					return;
			}
			else if(!CLK_GET_STATE)
	       {
	    		   return;
	       }
			else
				 return;
	       //发送启动信号，等待接收准备就绪
	       CLK_SET_OUT;

	       CLK_SET_LOW;
	       Touch_Delay1;

	       CLK_SET_HIGH;		//上升沿

	       Touch_Delay1;

	       if(!DAT_GET_STATE)
	       {
	        	   return;
	       }

	       CLK_SET_LOW;			//下降沿


	       //开始数据的上传，优先传输通道值，基线低位，基线高位，采样低位，采样高位
	       Touch_Delay1;

	       DAT_SET_OUT;
	       step=0;
	       j=5;
	//=============================================
	       for(step=0;step<5;step++)
	       {
	    	  // Date_Baihualv_Get_V=(_KF8_LIBi_Date_Change_[_KF8_LIBc_channel_])
	    		__asm
				   BANKSEL __KF8_LIBc_channel_
				   CLR PSW,0
				   RLCR __KF8_LIBc_channel_
				   BANKSEL __KF8_LIBi_Date_Change_
				   ADD R0,#(__KF8_LIBi_Date_Change_+0)
				   LD R1,[R0]
				   BANKSEL _Date_Baihualv_Get_V
				   MOV _Date_Baihualv_Get_V,R1

				   INC R0								// int 型具有
				   BANKSEL __KF8_LIBi_Date_Change_
				   LD R1,[R0]
				   BANKSEL _Date_Baihualv_Get_V
				   MOV _Date_Baihualv_Get_V+1,R1
	    		__endasm;

	    		   switch(step)
	    		   {
					   case 0:
					   {
						   j=5;
	#if 0
						   T_buf=_KF8_TOUCH_CH_EN[_KF8_LIBc_channel_];
	#else
						   __asm
						   BANKSEL __KF8_LIBc_channel_
						   MOV R0, __KF8_LIBc_channel_
						   ADD R0,#Low(__KF8_TOUCH_CH_EN+0)
						   MOV R1,#High(__KF8_TOUCH_CH_EN+0)
						   JNB PSW,0
						   INC R1
						   MOV PCH,R1
						   PAGESEL __R2PCL
						   CALL __R2PCL
						   PAGESEL $
						   BANKSEL _T_buf
						   MOV _T_buf,R0
						   __endasm;
	#endif
					   }break;
// 实现变化率的绝对值输出，如果值为负的话 2线偏离20变化量
					   case 1:
					   {
						   j=8;
						#if 0
						   T_buf=(unsigned char)(Date_Baihualv_Get_V);
						#else
						   __asm
						   BANKSEL _Date_Baihualv_Get_V
						   MOV R1,_Date_Baihualv_Get_V
						   JB _Date_Baihualv_Get_V+1,7
						   JMP $+2
						   CPL R1
						   INC R1
						   BANKSEL _T_buf
						   MOV _T_buf,R1
						   __endasm;
						#endif
					   }break;

					   case 2:
					   {
						   j=8;
						#if 0
						   T_buf=(unsigned char)(Date_Baihualv_Get_V>>8);
						#else
						   __asm
						   BANKSEL _Date_Baihualv_Get_V
						   MOV R1,_Date_Baihualv_Get_V+1
						   JB R1,7
						   JMP Show_temp_DS_000

						   CPL R1
						   MOV R0,_Date_Baihualv_Get_V
						   CPL R0
						   INC R0
						   JNB  PSW,2
						   INC R1

						Show_temp_DS_000:
						   BANKSEL _T_buf
						   MOV _T_buf,R1
						   __endasm;
						#endif

					   }break;

					   case 3:
					   {
						   j=8;
						#if 0
					   T_buf=(unsigned char)(Date_Baihualv_Get_V);
						#else
						   __asm
						   BANKSEL _Date_Baihualv_Get_V
						   MOV R1,_Date_Baihualv_Get_V
						   JB  _Date_Baihualv_Get_V+1,7
						   JMP $+2
						   CPL R1
						   INC R1
						   BANKSEL _T_buf
						   MOV _T_buf,R1
						   __endasm;
						#endif
					   }break;

					   case 4:
					   {
						   j=8;
						#if 0
					   T_buf=(unsigned char)(Date_Baihualv_Get_V>>8);
						#else
						   __asm
						   BANKSEL _Date_Baihualv_Get_V
						   MOV R1,_Date_Baihualv_Get_V+1
						   JB R1,7
						   JMP Show_temp_DS_001

						   CPL R1
						   MOV R0,_Date_Baihualv_Get_V
						   CPL R0
						   INC R0
						   JNB  PSW,2
						   INC R1

						   INC R1
						Show_temp_DS_001:
						   BANKSEL _T_buf
						   MOV _T_buf,R1
						   __endasm;
						#endif

					   }break;
	    		   }
	//-------------------------------------------
	       for(i_T_D=0;i_T_D<j;i_T_D++)
	       {
	          if(T_buf&0x01)
	        	  DAT_SET_HIGH;
	          else
	        	  DAT_SET_LOW;

	          CLK_SET_HIGH;
	          Touch_Delay1;

	          CLK_SET_LOW;
	          Touch_Delay1;

	          T_buf=T_buf>>1;
	       }
	//------------------------------------------
	       Touch_Delay2;
	       }
	 //===========================================
	       //获取有无通道设置下发需求
	       DAT_SET_IN;
	       Touch_Delay2;	//编程器判断需要时间

	       CLK_SET_HIGH;
	       Touch_Delay1;
	       Touch_Delay1;
	       Touch_Delay1;
	      //
	       //;;;;;
	       if(DAT_GET_STATE)
	       {
	    	    CLK_SET_LOW;
	    	    T_buf=0;
	    	   Touch_Delay1;
	           for(i_T_D=0;i_T_D<6;i_T_D++)
	           {
	              CLK_SET_HIGH;
	              Touch_Delay1;

	              if(i_T_D!=5)
	              {
	            	  T_buf=T_buf>>1;
	            	  if(DAT_GET_STATE)
	            		  T_buf=T_buf|0x10;
	              }
	              else
	              {
	            	  if(T_buf<16)   //传输错误的，防下非影响其他数据
	            	  {
	            		  if(DAT_GET_STATE)
	            		  {
	            			//   TOUCH_CH_TRS_EN[T_buf]=1;
	            			   __asm
	            			   BANKSEL _T_buf
	            			   MOV R0,_T_buf
	            			   ADD R0,#(_TOUCH_CH_TRS_EN+0)
	            			   MOV R1,#0x01
	            			   BANKSEL _TOUCH_CH_TRS_EN
	            			   ST  [R0],R1
	            			   __endasm;
	            		  }
	            	      else
	            	      {
	            	    	 //TOUCH_CH_TRS_EN[T_buf]=0;
							   __asm
							   BANKSEL _T_buf
							   MOV R0,_T_buf
							   ADD R0,#(_TOUCH_CH_TRS_EN+0)
							   MOV R1,#0x00
							   BANKSEL _TOUCH_CH_TRS_EN
							   ST  [R0],R1
							   __endasm;
	            	      }
	            	  }
	              }
	              CLK_SET_LOW;
	              Touch_Delay1;
	           }
	       }
	       else
	       {
	    	   CLK_SET_LOW;
	       	   Touch_Delay1;
	       }
	       //回到起始输入，接收端输出状态
	       CLK_SET_IN;
	       DAT_SET_IN;

	    //结束同时数据上传过程
		}
		//=================================
		__asm
		Label_x_y_z_a_1:
		__endasm;
		//////////////////////////////////
	   //结束判读是否需要上传过程

}
void TOUCH_DEBUG_TRS_DEAL()
{
  unsigned char j,step;
  // 判断当前通道允许上传时进行上传过程
#if 0
    TOUCH_CH_TRS_EN[_KF8_TOUCH_CH_EN[Touch_Channel_Protect]]=1;
#else
    __asm
    MOV R1,#High(__KF8_TOUCH_CH_EN+0)
    MOV R0,#(Touch_Channel_Protect)
    ADD R0,#Low(__KF8_TOUCH_CH_EN+0)
    JNB PSW,0
    INC R1
    PAGESEL __R2PCL
    CALL __R2PCL
    PAGESEL $
    ADD R0,#(_TOUCH_CH_TRS_EN+0)
    MOV R1,#0x01
    BANKSEL _TOUCH_CH_TRS_EN
    ST [R0],R1
    __endasm;
#endif
    //;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
#if 0
	if(TOUCH_CH_TRS_EN[_TOUCH_CH_EN[_KF8_LIBc_channel_]])
#else
		__asm
		BANKSEL __KF8_LIBc_channel_
		MOV R0,__KF8_LIBc_channel_
		ADD R0,#Low(__KF8_TOUCH_CH_EN+0)
		MOV R1,#High(__KF8_TOUCH_CH_EN+0)
		JNB PSW,0
		INC R1
		PAGESEL __R2PCL
		CALL __R2PCL
		PAGESEL $
		ADD R0,#(_TOUCH_CH_TRS_EN+0)
		BANKSEL _TOUCH_CH_TRS_EN
		LD R1,[R0]
		XOR R1,#0x00
		JNB PSW,2
		JMP Label_x_y_z_a
		__endasm;
#endif
	{
		CLK_SET_IN;
		DAT_SET_IN;
		Touch_Delay1;

	//获取上传权限,高clk有效允许传输

		if(CLK_GET_STATE)
		{
			Touch_Delay1;
			if(!CLK_GET_STATE)
				return;
		}
		else if(!CLK_GET_STATE)
       {
    		   return;
       }
		else
			 return;
       //发送启动信号，等待接收准备就绪
       CLK_SET_OUT;

       CLK_SET_LOW;
       Touch_Delay1;

       CLK_SET_HIGH;		//上升沿

       Touch_Delay1;

       if(!DAT_GET_STATE)
       {
        	   return;
       }

       CLK_SET_LOW;			//下降沿


       //开始数据的上传，优先传输通道值，基线低位，基线高位，采样低位，采样高位
       Touch_Delay1;

       DAT_SET_OUT;
       step=0;
       j=5;
//=============================================
       for(step=0;step<5;step++)
       {
    		   switch(step)
    		   {
				   case 0:
				   {
					   j=5;
#if 0
					   T_buf=_KF8_TOUCH_CH_EN[_KF8_LIBc_channel_];
#else
					   __asm
					   BANKSEL __KF8_LIBc_channel_
					   MOV R0, __KF8_LIBc_channel_
					   ADD R0,#Low(__KF8_TOUCH_CH_EN+0)
					   MOV R1,#High(__KF8_TOUCH_CH_EN+0)
					   JNB PSW,0
					   INC R1
					   MOV PCH,R1
					   PAGESEL __R2PCL
					   CALL __R2PCL
					   PAGESEL $
					   BANKSEL _T_buf
					   MOV _T_buf,R0
					   __endasm;
#endif
				   }break;

				   case 1:
				   {
					   j=8;
#if 0
					   T_buf=(unsigned char)(_KF8_LIBi_buff_baseline_[_KF8_LIBc_channel_]);
#else
					   __asm
					   BANKSEL __KF8_LIBc_channel_
					   CLR PSW,0
					   RLCR __KF8_LIBc_channel_
					   BANKSEL __KF8_LIBi_buff_baseline_
					   ADD R0,#(__KF8_LIBi_buff_baseline_+0)
					   LD R1,[R0]
					   BANKSEL _T_buf
					   MOV _T_buf,R1
					   __endasm;
#endif
				   }break;

				   case 2:
				   {
					   j=8;
#if 0
					   T_buf=(unsigned char)(_KF8_LIBi_buff_baseline_[_KF8_LIBc_channel_]>>8);
#else
					   __asm
					   BANKSEL __KF8_LIBc_channel_
					   CLR PSW,0
					   RLCR __KF8_LIBc_channel_
					   BANKSEL __KF8_LIBi_buff_baseline_
					   ADD R0,#(__KF8_LIBi_buff_baseline_+1)
					   LD R1,[R0]
					   BANKSEL _T_buf
					   MOV _T_buf,R1
					   __endasm;
#endif

				   }break;

				   case 3:
				   {
					   j=8;
#if 0
					   T_buf=(unsigned char)(_KF8_LIBi_buff_hit_[_KF8_LIBc_channel_]);
#else
					   __asm
					   BANKSEL __KF8_LIBc_channel_
					   CLR PSW,0
					   RLCR __KF8_LIBc_channel_
					   BANKSEL __KF8_LIBi_buff_hit_
					   ADD R0,#(__KF8_LIBi_buff_hit_+0)
					   LD R1,[R0]
					   BANKSEL _T_buf
					   MOV _T_buf,R1
					   __endasm;
#endif
				   }break;

				   case 4:
				   {
					   j=8;
#if 0
					   T_buf=(unsigned char)(_KF8_LIBi_buff_hit_[_KF8_LIBc_channel_]>>8);
#else
					   __asm
					   BANKSEL __KF8_LIBc_channel_
					   CLR PSW,0
					   RLCR __KF8_LIBc_channel_
					   BANKSEL __KF8_LIBi_buff_hit_
					   ADD R0,#(__KF8_LIBi_buff_hit_+1)
					   LD R1,[R0]
					   BANKSEL _T_buf
					   MOV _T_buf,R1
					   __endasm;
#endif
				   }break;
    		   }
//-------------------------------------------
       for(i_T_D=0;i_T_D<j;i_T_D++)
       {
          if(T_buf&0x01)
        	  DAT_SET_HIGH;
          else
        	  DAT_SET_LOW;

          CLK_SET_HIGH;
          Touch_Delay1;

          CLK_SET_LOW;
          Touch_Delay1;

          T_buf=T_buf>>1;
       }
//------------------------------------------
       Touch_Delay2;
       }
 //===========================================
       //获取有无通道设置下发需求
       DAT_SET_IN;
       Touch_Delay2;	//编程器判断需要时间

       CLK_SET_HIGH;
       Touch_Delay1;
       Touch_Delay1;
       Touch_Delay1;
      //
       //;;;;;
       if(DAT_GET_STATE)
       {
    	    CLK_SET_LOW;
    	    T_buf=0;
    	   Touch_Delay1;
           for(i_T_D=0;i_T_D<6;i_T_D++)
           {
              CLK_SET_HIGH;
              Touch_Delay1;

              if(i_T_D!=5)
              {
            	  T_buf=T_buf>>1;
            	  if(DAT_GET_STATE)
            		  T_buf=T_buf|0x10;
              }
              else
              {
            	  if(T_buf<16)   //传输错误的，防下非影响其他数据
            	  {
            		  if(DAT_GET_STATE)
            		  {
            			//   TOUCH_CH_TRS_EN[T_buf]=1;
            			   __asm
            			   BANKSEL _T_buf
            			   MOV R0,_T_buf
            			   ADD R0,#(_TOUCH_CH_TRS_EN+0)
            			   MOV R1,#0x01
            			   BANKSEL _TOUCH_CH_TRS_EN
            			   ST  [R0],R1
            			   __endasm;
            		  }
            	      else
            	      {
            	    	 //TOUCH_CH_TRS_EN[T_buf]=0;
						   __asm
						   BANKSEL _T_buf
						   MOV R0,_T_buf
						   ADD R0,#(_TOUCH_CH_TRS_EN+0)
						   MOV R1,#0x00
						   BANKSEL _TOUCH_CH_TRS_EN
						   ST  [R0],R1
						   __endasm;
            	      }
            	  }
              }
              CLK_SET_LOW;
              Touch_Delay1;
           }
       }
       else
       {
    	   CLK_SET_LOW;
       	   Touch_Delay1;
       }
       //回到起始输入，接收端输出状态
       CLK_SET_IN;
       DAT_SET_IN;

    //结束同时数据上传过程
	}
	//=================================
	__asm
	Label_x_y_z_a:
	__endasm;
	if(1);
	//////////////////////////////////
   //结束判读是否需要上传过程
}
//=================================================================
//-----------------------------------------------------------------
//=================================================================

void TOUCH_DEBUG_SEND_OUT()
{
	unsigned char j,step;
	    //;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
		{
			CLK_SET_IN;
			DAT_SET_IN;
			Touch_Delay1;
		//获取上传权限,高clk有效允许传输

			if(CLK_GET_STATE)
			{
				Touch_Delay1;
				if(!CLK_GET_STATE)
					return;
			}
			else if(!CLK_GET_STATE)
	       {
	    		   return;
	       }
			else
				 return;
	       //发送启动信号，等待接收准备就绪
	       CLK_SET_OUT;

	       CLK_SET_LOW;
	       Touch_Delay1;

	       CLK_SET_HIGH;		//上升沿
	       Touch_Delay1;

	       if(!DAT_GET_STATE)
	       {
	        	   return;
	       }

	       CLK_SET_LOW;			//下降沿

	       //开始数据的上传，优先传输通道值，基线低位，基线高位，采样低位，采样高位
	       Touch_Delay1;

	       DAT_SET_OUT;

	       step=0;
	       j=5;
	//=============================================
	       for(step=0;step<5;step++)
	       {
			   switch(step)
			   {
				   case 0:
				   {
					   j=5;   T_buf=0x04;
				   }break;
				   case 1:
				   {
					   j=8;   T_buf=sentbuf1;
				   }break;
				   case 2:
				   {
					   j=8;   T_buf=sentbuf2;
				   }break;
				   case 3:
				   {
					   j=8;   T_buf=sentbuf3;
				   }break;
				   case 4:
				   {
					   j=8;   T_buf=sentbuf4;
				   }break;
			   }
	//-------------------------------------------
			   for(i_T_D=0;i_T_D<j;i_T_D++)
			   {
				  if(T_buf&0x01)
					  DAT_SET_HIGH;
				  else
					  DAT_SET_LOW;

				  CLK_SET_HIGH;
				  Touch_Delay1;

				  CLK_SET_LOW;
				  Touch_Delay1;

				  T_buf=T_buf>>1;
			   }
	//------------------------------------------
	       Touch_Delay2;
	       }
	 //===========================================
	       //获取有无通道设置下发需求
	       DAT_SET_IN;
	       Touch_Delay2;	//编程器判断需要时间

	       CLK_SET_HIGH;
	       Touch_Delay1;
	       Touch_Delay1;
	       Touch_Delay1;
	      //
	       //;;;;;
	    	CLK_SET_LOW;
	        Touch_Delay1;
	       //回到起始输入，接收端输出状态
	       CLK_SET_IN;
	       DAT_SET_IN;

	    //结束同时数据上传过程
		}
		//=================================

		//////////////////////////////////
}
#endif
