#include "Pwm_Capture_value.h"
INT8U   OLD_PULSE1F;		//旧的转速表信号输入口状态 
INT16U  CSB_T01_HL;
INT16U  CSB_T10_HL;
INT16U  CSB_SJTime_HL;	//转速表事件标定时间(单位60US) 
INT8U   T1IMEB_CUNT;    //定时器(用转速表输入脉冲超长监测)      
INT16U  CSB_TS_BUFF;    //车速表 时间缓冲 
INT8U   CSB_Freq_FF;		//=1表示测试到频率值
INT8U   CSB_TB_FF;			//=0表示无TB值
INT16U  CSB_TS_CC;
INT8U   CSB_PULS_BUFF;
INT8U   CSB_PUZUO_FF;		//=0扑捉上沿,=1扑捉下沿
INT8U   CSB_TB_FF;			//=0表示无TB值
INT8U   CSB_T01_FF;		  //=0表示无T01值
INT16U  CSB_TB_HL; 
INT8U   CSB_PULS_N;
INT16U  CSB_SS_FrHL;    //车速表实时输入频率
/**********************************************************************************
 * 函数名称：BL_PWM_Duty_Cycle_Capture_CSB_ISR
 * 函数说明：车速频率获取
 *           车速频率
 *
 * *******************************************************************************/
void BL_PWM_Duty_Cycle_Capture_CSB_ISR(void) 
{ 
	INT16U mTIME;
	CSB_SJTime_HL++;

	if( LINE_SPEED_IN )  //车速频率输入
	{        
		if(!OLD_PULSE1F) //旧的 旧的转速表信号输入口状态 
		{         
			//0--->1   低电平到高电平
			OLD_PULSE1F=1;   

			CSB_T01_HL = CSB_SJTime_HL;  // CSB_SJTime_HL 转速表事件标定时间(单位60US) 
			CSB_T01_FF = 1;				       //表示T01处有值

			if(CSB_PUZUO_FF)			       //扑捉上沿?,no 不捕捉上升沿  //=0扑捉上沿,=1扑捉下沿
			{    	
				if( (CSB_T01_HL-CSB_T10_HL) < 8 )
					goto  INT_CSB_END;

				T1IMEB_CUNT  = 0;			//清超时计时器
				CSB_PUZUO_FF = 0;			//置扑捉上沿标志 

				//-------------------------
				mTIME=CSB_T01_HL-CSB_TB_HL;

				if( mTIME>=1875 )		//0.12s            1515     11Hz处 (1/11)*1000000/50=1818
				{
					CSB_TS_BUFF    = mTIME; 
					CSB_PULS_BUFF  = 1;

					CSB_Freq_FF=1;				//置测试到频率标志

					CSB_T01_HL=0;
					CSB_SJTime_HL=0;

					CSB_TB_FF=0;		//清有TB标志

					CSB_TS_CC = CSB_PULS_N=0;
				}
			}
		}        
	}
	else
	{
		if(OLD_PULSE1F)
		{
			//1--->0
			OLD_PULSE1F=0;  
			CSB_T10_HL = CSB_SJTime_HL;

			if(CSB_PUZUO_FF)			//扑捉上沿?,no
				goto  INT_CSB_END;

			if(CSB_T01_FF)				//T01处有值?,YES
			{
				if( (CSB_T10_HL-CSB_T01_HL) < 8 )
					goto  INT_CSB_END;	

				T1IMEB_CUNT = 0;		//清超时计时器

				CSB_PUZUO_FF = 1;		//置扑捉下沿标志 

				if(CSB_TB_FF)			//有TB值?,YES
				{
					//有TB值
					CSB_PULS_N++;

					CSB_TS_CC=CSB_TS_CC+CSB_T01_HL-CSB_TB_HL;

					CSB_TB_HL=CSB_T01_HL;

					CSB_SJTime_HL=CSB_SJTime_HL-CSB_TB_HL;

					CSB_T10_HL=CSB_SJTime_HL;

					CSB_TB_HL=0; 

					//---------------------------------
					if((((INT8U)CSB_PULS_N)&((4)-1))==0)//CSB_PULS_NN是4或8的整数倍?,YES
					{
						CSB_TS_BUFF=CSB_TS_CC;
						CSB_PULS_BUFF=CSB_PULS_N;

						if(CSB_TS_CC>=2344) //2500(0.15s)    1667(0.1S)       2000*0.05MS=0.1S
						{
							CSB_TS_CC=CSB_PULS_N=0;
							CSB_Freq_FF=1;				//置测试到频率标志
						}
					}
					else
					{
						if(CSB_TS_CC>=2344) //2500(0.15s)    1667(0.1S)       2000*0.05MS=0.1S
						{
							if(CSB_PULS_N< 4)
							{
								CSB_TS_BUFF=CSB_TS_CC;
								CSB_PULS_BUFF=CSB_PULS_N;

								CSB_TS_CC=CSB_PULS_N=0;

								CSB_Freq_FF=1;				//置测试到频率标志
							}
							else
							{
								CSB_TS_CC=CSB_PULS_N=0;
								CSB_Freq_FF=1;				//置测试到频率标志
							}
						}
					}
				}
				else
				{ 
					//无TB值
					CSB_TB_FF=1;
					CSB_TB_HL=CSB_T01_HL;
				}
			}
		}
	}
INT_CSB_END :	
	  mTIME = 0 ; 
}

INT16U  Hyq_mPULS ;
INT16U  Hyq_mTIME ;

INT16U GJ_T_JSCCSBF(INT8U mTYPE)
{              
	INT16U mPULS;
	INT16U mTIME;

	if(mTYPE)
	{
		cli();                      //禁止全局中断
		mPULS=CSB_PULS_BUFF;
		mTIME=CSB_TS_BUFF;
		sei();                      //开全局中断      

	}
	//else
	//{
	//	cli();                      //禁止全局中断
	//	mPULS=ZSB_PULS_BUFF;
	//	mTIME=ZSB_TS_BUFF;
	//	sei();                      //开全局中断      
	//      
	//}
  Hyq_mPULS =  mPULS;
  Hyq_mTIME =  mTIME;
  
	return  (((INT32U)199897)*mPULS)/(mTIME);       //64us-156170    50us-199897       
} 
//==================================================
//读并计算车速表的中断中的脉冲频率的10倍值     2015-12-30
//
//出口:返回1表示读到有效数据,且数据在CSB_SS_FrHL中
//==================================================                
INT8U ReadCSBPULESP(void)
{       
	T1IMEB_CUNT++;
	if(T1IMEB_CUNT==202)	//300MS?    152*1.98=300.96MS
	{ 
		T1IMEB_CUNT=0;

		cli();                      //禁止全局中断
		CSB_PUZUO_FF=0;
		CSB_T01_FF=0;
		CSB_SJTime_HL=0;
		sei();                      //开全局中断      

		CSB_TB_FF=0;
		CSB_TS_CC=CSB_PULS_N=0;
		CSB_Freq_FF=0;				

		CSB_SS_FrHL=0;  	//置频率为0

		return 1;
	}

	if(CSB_Freq_FF)                 //测量到CAR_N_CCC个车速表输入脉冲?
	{
		CSB_Freq_FF=0;
		CSB_SS_FrHL = GJ_T_JSCCSBF(1);   //根据车转速表在中断中的数据计算10倍频率
		if( CSB_SS_FrHL<=15 )      //30
			CSB_SS_FrHL=0;
		return 1;
	}
	else
	{
		return 0;	
	}	
} 